summaryrefslogtreecommitdiff
path: root/devtools/bin/srgb_colorspace.pl
blob: 3c5404ce7e9bba6c401fc1e483e844c2170ea6a8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#
#   Arguments parsing
#

local $script = $0;

while ( 1 )
{
	local $arg = shift;
	if ( $arg =~ /-catsrgb1/i )
		{	goto RUN_CAT_SRGB_1;	}
	if ( $arg =~ /-safecat/i )
		{	goto RUN_SAFE_CAT;	}
	if ( $arg =~ /-delaywrite/i )
		{	goto RUN_DELAY_WRITE;	}
	if ( $arg =~ /-processvmts/i )
		{	goto RUN_PROCESS_VMTS;	}

	# default mode - forward to processvmts:
	# exit system( "dir" );
	exit system( "dir /s /b *.vmt | perl $script -processvmts" );
}




RUN_CAT_SRGB_1:
#
#   -----------  Adding "srgb 1" to options list --------
#

local $bAlreadyHasSrgb1 = 0;
while ( <> )
{
	local $line = $_;
	print $line;
	$bAlreadyHasSrgb1 = 1 if $line =~ /srgb(.*)1/i;
}
print "\nsrgb 1\n" if !$bAlreadyHasSrgb1;
exit 0;



RUN_DELAY_WRITE:
#
#   -----------  Delay write to a file (essentially delayed shell redirection) ----
#
local *fileptr;
local $filename = shift;

local @lines;
while ( <> ) { push( @lines, $_ ); }

open( fileptr, ">$filename" );
foreach ( @lines ) { print fileptr $_ ; }
close( fileptr );

exit 0;



RUN_SAFE_CAT:
#
#   -----------  Cat of a non-existing file will succeed  ----
#
local *fileptr;
local $filename = shift;

exit 1 if !open( fileptr, "<$filename" );
while ( <fileptr> ) { print $_ ; }
close( fileptr );

exit 0;




RUN_PROCESS_VMTS:
#
#   -----------  Processing the list of vmts ------------
#


#
#   Lookup array of directories under which to search for
#   textures when resolving texture paths from vmt.
#
local @textureslookup = (
#	"u:/data/content/tf/",
#	"u:/data/content/portal/",

#	"u:/data/content/ep2/",
#	"u:/data/content/episodic/",
	"u:/data/content/hl2/",
);

#
#   Mapping of shaders that require srgb conversion for
#   basetextures. Entries listed lowercase, mapped to 1.
#
local %srgbshaders = (
	vertexlitgeneric            => 1,
	lightmappedgeneric          => 1,
	unlitgeneric                => 1,
	eyerefract                  => 1,
	portalrefract               => 1,
	aftershock                  => 1,
	sky                         => 1,
	lightmappedreflective       => 1,
	portalstaticoverlay         => 1,
	particlesphere              => 1,
	shatteredglass              => 1,
	sprite                      => 1,
	spritecard                  => 1,
	teeth                       => 1,
	treeleaf                    => 1,
	vortwarp                    => 1,
	water                       => 1,
	windowimposter              => 1,
	worldtwotextureblend        => 1,
	worldvertexalpha            => 1,
);

#
#   Mapping of variables that we will be looking for.
#
local %shadervarscheck = (
	basetexture					=> 1,
	basetexture2                => 1,
	basetexture3                => 1,
	basetexture4                => 1,
	envmap                      => 1,
	iris                        => 1,
	ambientoccltexture          => 1,
	refracttinttexture          => 1,
	albedo                      => 1,
	compress                    => 1,
	stretch                     => 1,
	emissiveblendbasetexture    => 1,
	emissiveblendtexture        => 1,
	fleshinteriortexture        => 1,
	fleshbordertexture1d        => 1,
	fleshsubsurfacetexture      => 1,
	fleshcubetexture            => 1,
	texture2                    => 1,
	hdrcompressedtexture        => 1,
	hdrcompressedtexture0       => 1,
	hdrcompressedtexture1       => 1,
	hdrcompressedtexture2       => 1,
	portalcolortexture          => 1,
	monitorscreen               => 1,
	staticblendtexture          => 1,
	refracttexture              => 1,
	reflecttexture              => 1,
);

#
#   After parsing the list of vmt files contains a map of
#   texture names as keys to the list of two elements:
#   [0]
#     0: texture is non-srgb
#     1: texture is srgb and needs conversion
#    -1: texture is ambiguous
#   [1]
#     list of vmts referring to the texture
#
local %textures2convert;


#
#   Parse our list of vmt files
#   Generate the list with "dir /s /b *.vmt"
#
while ( <> )
{

	local $fname = $_;
	$fname =~ s/^\s*(.*)\s*$/$1/;
	$fname =~ s/\\/\//g;
#	print "-" . $fname . "-\n";

	#	Open the file
	local *vmtfileptr;
	open( vmtfileptr, "<$fname" ) || die;

	local $line;
	local $shadername;
	local %basetexture;
	local $basetexture_real;
	local $basetexture_blacklisted = 0;

	#   Find shadername and textures
	while ( $line = <vmtfileptr> )
	{
		$line =~ s/^\s*(.*)\s*$/$1/;
		$line = lc( $line );

		if ( $. == 1 )
		{
			$shadername = $line;
			$shadername =~ s/\"//g;
		}

		while ( ( $varname, undef ) = each %shadervarscheck )
		{
			next if $line !~ /^\s*["]?\$$varname["]?\s+(.*)$/i;

			$line = $1;
			if ( $line =~ /^\"([^\"])*\"(.*)$/ ) {
				$line =~ s/^\"([^\"]*)\"(.*)$/$1/;
			} else {
				$line =~ s/^(\S*)(.*)$/$1/;
			}
			$line =~ s/^\s*(.*)\s*$/$1/;
			$line =~ s/\\/\//g;

			next if $line =~ /^\_rt/i;

			$basetexture{ $line } = 1;
			$basetexture_real = $line if $varname =~ /^basetexture$/i;
			last;
		}

		# blacklist stuff
		$basetexture_blacklisted = 1 if $line =~ /^\s*["]?\$extractgreenalpha["]?\s*(.*)$/i;
		print ">>> $fname blacklisted by >>> $line >>>\n" if $line =~ /^\s*["]?\$extractgreenalpha["]?\s*(.*)$/i;
	}

	delete $basetexture{ $basetexture_real } if $basetexture_blacklisted;
	print ">>> blacklisted texture: $basetexture_real .\n" if $basetexture_blacklisted;
	delete $basetexture{ "env_cubemap" };

	close vmtfileptr;

	#   Print the shader and the base texture
#	print "\tshader  = " . $shadername . "\n";
#	foreach ( keys %basetexture ) { print "\ttexture = " . $_ . "\n" };

	#   If the texture needs a conversion stick it into the map
	local $isSrgb = 1; #--shadercheck--( exists $srgbshaders{ $shadername } ? 1 : 0 );

	foreach ( keys %basetexture )
	{
		local $basetexture = $_;
		if ( exists $textures2convert{ $basetexture } )
		{
			#   Check for validity
			$isSrgb = -1 if $textures2convert{ $basetexture }->[0] != $isSrgb;
		}
		else
		{
			$textures2convert{ $basetexture } = [ $isSrgb, [] ];
		}
	
		$textures2convert{ $basetexture }->[0] = $isSrgb;
		push( @{ $textures2convert{ $basetexture }->[1] }, $fname );
	}

}


#
#   Now walk over the accumulated textures and establish the srgb mapping
#
local @failedtextures;
while ( ( $basetexture, $value ) = each %textures2convert )
{

	local $isSrgb = $value->[0];

	if ( -1 == $isSrgb )
	{
		print "Warning: Ambiguity for ";
		print "$basetexture from ";
		foreach ( @{ $value->[1] } ) { print " $_"; };
		print "!\n";
	}

	next if 1 != $isSrgb;

	#
	# Need to convert this texture
	#
	local $res = ConvertTexture( $basetexture );
	push( @failedtextures, $basetexture ) if !$res;

}

#   Dump failed textures
if ( $#failedtextures > 0 )
{
	print "\n---- errors ----\n";
	foreach ( @failedtextures )
	{
		local $basetexture = $_;
		print "ERROR: $basetexture  in  ";
		foreach ( @{ $textures2convert{ $basetexture }->[1] } ) { print " $_"; }
		print ";\n";
	}
	print "----  end   ----\n";
}


#
#   Converts a texture.
#   Returns 1 for txt, 2 for psd, 3 for newly created txt.
#   Returns 0 when nothing found.
#
sub ConvertTexture
{

	local $basetexture = shift;
	local $result = 0;
	print "-- $basetexture ... ";

	foreach ( @textureslookup )
	{
		# Try to open the txt file
		local $txpath = $_ . "materialsrc/" . $basetexture;
		if ( -f "$txpath.txt" || -f "$txpath.tga" )
		{
			# Check it out
			print "txt, p4 ... ";
			`p4 edit "$txpath.txt" >nul 2>&1`;
			`p4 lock "$txpath.txt" >nul 2>&1`;

			`perl $script -safecat "$txpath.txt" | perl $script -catsrgb1 | perl $script -delaywrite "$txpath.txt"`;

			`p4 add "$txpath.txt" >nul 2>&1` if -f "$txpath.txt";

			# Also patch the VTF
			$txpath =~ s,/content/,/game/,;
			$txpath =~ s,/materialsrc/,/materials/,;
			#PatchVtfFile( "$txpath.vtf" );

			print "done ... ";
			++ $result;
		}
		if ( -f "$txpath.psd" )
		{
			# Check it out
			print "psd, p4 ... ";
			`p4 edit "$txpath.psd" >nul 2>&1`;
			`p4 lock "$txpath.psd" >nul 2>&1`;

			`psdinfo -read "$txpath.psd" | perl $script -catsrgb1 | psdinfo -write "$txpath.psd"`;

			`p4 add "$txpath.psd" >nul 2>&1`;

			# Also patch the VTF
			$txpath =~ s,/content/,/game/,;
			$txpath =~ s,/materialsrc/,/materials/,;
			#PatchVtfFile( "$txpath.vtf" );

			print "done ... ";
			++ $result;
		}
	}

	foreach ( @textureslookup )
	{
		# Try to find the vtf file
		local $txpath = $_ . "materials/" . $basetexture;
		$txpath =~ s,/content/,/game/,i;
		if ( -f "$txpath.vtf" )
		{
			if ( !$result )
			{
				# Create the text file then
				print "src missing ... ";
				$txpath =~ s,/game/,/content/,,i;
				$txpath =~ s,/materials/,/materialsrc/,;
			
				`p4 edit "$txpath.txt" >nul 2>&1`;
				`p4 lock "$txpath.txt" >nul 2>&1`;

				`perl $script -safecat "$txpath.txt" | perl $script -catsrgb1 | perl $script -delaywrite "$txpath.txt"`;

				`p4 add "$txpath.txt" >nul 2>&1` if -f "$txpath.txt";

				# Also patch the VTF
				$txpath =~ s,/content/,/game/,;
				$txpath =~ s,/materialsrc/,/materials/,;
			}
			PatchVtfFile( "$txpath.vtf" );

			print "done ... ";
			++ $result;
		}
	}

	if ( !$result )
	{
		print "ERROR: not found.\n";
	}
	else
	{
		print "ok.\n";
	}
	return $result;

}

exit 0;


RUN_PATCH_VTF:
#
#   -----------  Patch a VTF file --------
#

local $filename = shift;
exit PatchVtfFile( $filename );

sub PatchVtfFile
{

	local *fileptr;
	local $filename = shift;
	local $vtfFlags;

	print "vtf, p4 ... ";
	`p4 edit "$filename" >nul 2>&1`;
	`p4 lock "$filename" >nul 2>&1`;

	if ( !open( fileptr, "+<$filename" ) )
	{
		print "VTF missing ... ";
		return 1;
	}
	binmode( fileptr );

	seek( fileptr, 5 * 4, 0 );
	read( fileptr, $vtfFlags, 4 );
	$vtfFlags = unpack( "I", $vtfFlags );

	# adding SRGB flag
	$vtfFlags = $vtfFlags | 0x00000040; # mask or

	seek( fileptr, 5 * 4, 0 );
	print fileptr pack( "I", $vtfFlags );

	close( fileptr );

	print "vtf ok ... ";
	return 0;

}