]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake3/q3map2/image.c
uncrustify! now the code is only ugly on the *inside*
[xonotic/netradiant.git] / tools / quake3 / q3map2 / image.c
1 /* -------------------------------------------------------------------------------
2
3    Copyright (C) 1999-2007 id Software, Inc. and contributors.
4    For a list of contributors, see the accompanying CONTRIBUTORS file.
5
6    This file is part of GtkRadiant.
7
8    GtkRadiant is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    GtkRadiant is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with GtkRadiant; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21
22    ----------------------------------------------------------------------------------
23
24    This code has been altered significantly from its original form, to support
25    several games based on the Quake III Arena engine, in the form of "Q3Map2."
26
27    ------------------------------------------------------------------------------- */
28
29
30
31 /* marker */
32 #define IMAGE_C
33
34
35
36 /* dependencies */
37 #include "q3map2.h"
38
39
40
41 /* -------------------------------------------------------------------------------
42
43    this file contains image pool management with reference counting. note: it isn't
44    reentrant, so only call it from init/shutdown code or wrap calls in a mutex
45
46    ------------------------------------------------------------------------------- */
47
48 /*
49    LoadDDSBuffer()
50    loads a dxtc (1, 3, 5) dds buffer into a valid rgba image
51  */
52
53 static void LoadDDSBuffer( byte *buffer, int size, byte **pixels, int *width, int *height ){
54         int w, h;
55         ddsPF_t pf;
56
57
58         /* dummy check */
59         if ( buffer == NULL || size <= 0 || pixels == NULL || width == NULL || height == NULL ) {
60                 return;
61         }
62
63         /* null out */
64         *pixels = 0;
65         *width = 0;
66         *height = 0;
67
68         /* get dds info */
69         if ( DDSGetInfo( (ddsBuffer_t*) buffer, &w, &h, &pf ) ) {
70                 Sys_Printf( "WARNING: Invalid DDS texture\n" );
71                 return;
72         }
73
74         /* only certain types of dds textures are supported */
75         if ( pf != DDS_PF_ARGB8888 && pf != DDS_PF_DXT1 && pf != DDS_PF_DXT3 && pf != DDS_PF_DXT5 ) {
76                 Sys_Printf( "WARNING: Only DDS texture formats ARGB8888, DXT1, DXT3, and DXT5 are supported (%d)\n", pf );
77                 return;
78         }
79
80         /* create image pixel buffer */
81         *width = w;
82         *height = h;
83         *pixels = safe_malloc( w * h * 4 );
84
85         /* decompress the dds texture */
86         DDSDecompress( (ddsBuffer_t*) buffer, *pixels );
87 }
88
89
90
91 /*
92    PNGReadData()
93    callback function for libpng to read from a memory buffer
94    note: this function is a total hack, as it reads/writes the png struct directly!
95  */
96
97 typedef struct pngBuffer_s
98 {
99         byte    *buffer;
100         int size, offset;
101 }
102 pngBuffer_t;
103
104 void PNGReadData( png_struct *png, png_byte *buffer, png_size_t size ){
105         pngBuffer_t     *pb = (pngBuffer_t*) png_get_io_ptr( png );
106
107
108         if ( ( pb->offset + size ) > pb->size ) {
109                 size = ( pb->size - pb->offset );
110         }
111         memcpy( buffer, &pb->buffer[ pb->offset ], size );
112         pb->offset += size;
113         //%     Sys_Printf( "Copying %d bytes from 0x%08X to 0x%08X (offset: %d of %d)\n", size, &pb->buffer[ pb->offset ], buffer, pb->offset, pb->size );
114 }
115
116
117
118 /*
119    LoadPNGBuffer()
120    loads a png file buffer into a valid rgba image
121  */
122
123 static void LoadPNGBuffer( byte *buffer, int size, byte **pixels, int *width, int *height ){
124         png_struct  *png;
125         png_info    *info, *end;
126         pngBuffer_t pb;
127         int i, bitDepth, colorType, channels;
128         png_uint_32 w, h;
129         byte        **rowPointers;
130
131
132         /* dummy check */
133         if ( buffer == NULL || size <= 0 || pixels == NULL || width == NULL || height == NULL ) {
134                 return;
135         }
136
137         /* null out */
138         *pixels = 0;
139         *width = 0;
140         *height = 0;
141
142         /* determine if this is a png file */
143         if ( png_sig_cmp( buffer, 0, 8 ) != 0 ) {
144                 Sys_Printf( "WARNING: Invalid PNG file\n" );
145                 return;
146         }
147
148         /* create png structs */
149         png = png_create_read_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL );
150         if ( png == NULL ) {
151                 Sys_Printf( "WARNING: Unable to create PNG read struct\n" );
152                 return;
153         }
154
155         info = png_create_info_struct( png );
156         if ( info == NULL ) {
157                 Sys_Printf( "WARNING: Unable to create PNG info struct\n" );
158                 png_destroy_read_struct( &png, NULL, NULL );
159                 return;
160         }
161
162         end = png_create_info_struct( png );
163         if ( end == NULL ) {
164                 Sys_Printf( "WARNING: Unable to create PNG end info struct\n" );
165                 png_destroy_read_struct( &png, &info, NULL );
166                 return;
167         }
168
169         /* set read callback */
170         pb.buffer = buffer;
171         pb.size = size;
172         pb.offset = 0;
173         png_set_read_fn( png, &pb, PNGReadData );
174         png->io_ptr = &pb; /* hack! */
175
176         /* set error longjmp */
177         if ( setjmp( png->jmpbuf ) ) {
178                 Sys_Printf( "WARNING: An error occurred reading PNG image\n" );
179                 png_destroy_read_struct( &png, &info, &end );
180                 return;
181         }
182
183         /* fixme: add proper i/o stuff here */
184
185         /* read png info */
186         png_read_info( png, info );
187
188         /* read image header chunk */
189         png_get_IHDR( png, info,
190                                   &w, &h, &bitDepth, &colorType, NULL, NULL, NULL );
191
192         /* read number of channels */
193         channels = png_get_channels( png, info );
194
195         /* the following will probably bork on certain types of png images, but hey... */
196
197         /* force indexed/gray/trans chunk to rgb */
198         if ( ( colorType == PNG_COLOR_TYPE_PALETTE && bitDepth <= 8 ) ||
199                  ( colorType == PNG_COLOR_TYPE_GRAY && bitDepth <= 8 ) ||
200                  png_get_valid( png, info, PNG_INFO_tRNS ) ) {
201                 png_set_expand( png );
202         }
203
204         /* strip 16bpc -> 8bpc */
205         if ( bitDepth == 16 ) {
206                 png_set_strip_16( png );
207         }
208
209         /* pad rgb to rgba */
210         if ( bitDepth == 8 && colorType == PNG_COLOR_TYPE_RGB ) {
211                 png_set_filler( png, 255, PNG_FILLER_AFTER );
212         }
213
214         /* create image pixel buffer */
215         *width = w;
216         *height = h;
217         *pixels = safe_malloc( w * h * 4 );
218
219         /* create row pointers */
220         rowPointers = safe_malloc( h * sizeof( byte* ) );
221         for ( i = 0; i < h; i++ )
222                 rowPointers[ i ] = *pixels + ( i * w * 4 );
223
224         /* read the png */
225         png_read_image( png, rowPointers );
226
227         /* clean up */
228         free( rowPointers );
229         png_destroy_read_struct( &png, &info, &end );
230
231 }
232
233
234
235 /*
236    ImageInit()
237    implicitly called by every function to set up image list
238  */
239
240 static void ImageInit( void ){
241         int i;
242
243
244         if ( numImages <= 0 ) {
245                 /* clear images (fixme: this could theoretically leak) */
246                 memset( images, 0, sizeof( images ) );
247
248                 /* generate *bogus image */
249                 images[ 0 ].name = safe_malloc( strlen( DEFAULT_IMAGE ) + 1 );
250                 strcpy( images[ 0 ].name, DEFAULT_IMAGE );
251                 images[ 0 ].filename = safe_malloc( strlen( DEFAULT_IMAGE ) + 1 );
252                 strcpy( images[ 0 ].filename, DEFAULT_IMAGE );
253                 images[ 0 ].width = 64;
254                 images[ 0 ].height = 64;
255                 images[ 0 ].refCount = 1;
256                 images[ 0 ].pixels = safe_malloc( 64 * 64 * 4 );
257                 for ( i = 0; i < ( 64 * 64 * 4 ); i++ )
258                         images[ 0 ].pixels[ i ] = 255;
259         }
260 }
261
262
263
264 /*
265    ImageFree()
266    frees an rgba image
267  */
268
269 void ImageFree( image_t *image ){
270         /* dummy check */
271         if ( image == NULL ) {
272                 return;
273         }
274
275         /* decrement refcount */
276         image->refCount--;
277
278         /* free? */
279         if ( image->refCount <= 0 ) {
280                 if ( image->name != NULL ) {
281                         free( image->name );
282                 }
283                 image->name = NULL;
284                 if ( image->filename != NULL ) {
285                         free( image->filename );
286                 }
287                 image->filename = NULL;
288                 free( image->pixels );
289                 image->width = 0;
290                 image->height = 0;
291                 numImages--;
292         }
293 }
294
295
296
297 /*
298    ImageFind()
299    finds an existing rgba image and returns a pointer to the image_t struct or NULL if not found
300  */
301
302 image_t *ImageFind( const char *filename ){
303         int i;
304         char name[ 1024 ];
305
306
307         /* init */
308         ImageInit();
309
310         /* dummy check */
311         if ( filename == NULL || filename[ 0 ] == '\0' ) {
312                 return NULL;
313         }
314
315         /* strip file extension off name */
316         strcpy( name, filename );
317         StripExtension( name );
318
319         /* search list */
320         for ( i = 0; i < MAX_IMAGES; i++ )
321         {
322                 if ( images[ i ].name != NULL && !strcmp( name, images[ i ].name ) ) {
323                         return &images[ i ];
324                 }
325         }
326
327         /* no matching image found */
328         return NULL;
329 }
330
331
332
333 /*
334    ImageLoad()
335    loads an rgba image and returns a pointer to the image_t struct or NULL if not found
336  */
337
338 image_t *ImageLoad( const char *filename ){
339         int i;
340         image_t     *image;
341         char name[ 1024 ];
342         int size;
343         byte        *buffer = NULL;
344
345
346         /* init */
347         ImageInit();
348
349         /* dummy check */
350         if ( filename == NULL || filename[ 0 ] == '\0' ) {
351                 return NULL;
352         }
353
354         /* strip file extension off name */
355         strcpy( name, filename );
356         StripExtension( name );
357
358         /* try to find existing image */
359         image = ImageFind( name );
360         if ( image != NULL ) {
361                 image->refCount++;
362                 return image;
363         }
364
365         /* none found, so find first non-null image */
366         image = NULL;
367         for ( i = 0; i < MAX_IMAGES; i++ )
368         {
369                 if ( images[ i ].name == NULL ) {
370                         image = &images[ i ];
371                         break;
372                 }
373         }
374
375         /* too many images? */
376         if ( image == NULL ) {
377                 Error( "MAX_IMAGES (%d) exceeded, there are too many image files referenced by the map.", MAX_IMAGES );
378         }
379
380         /* set it up */
381         image->name = safe_malloc( strlen( name ) + 1 );
382         strcpy( image->name, name );
383
384         /* attempt to load tga */
385         StripExtension( name );
386         strcat( name, ".tga" );
387         size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
388         if ( size > 0 ) {
389                 LoadTGABuffer( buffer, buffer + size, &image->pixels, &image->width, &image->height );
390         }
391         else
392         {
393                 /* attempt to load png */
394                 StripExtension( name );
395                 strcat( name, ".png" );
396                 size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
397                 if ( size > 0 ) {
398                         LoadPNGBuffer( buffer, size, &image->pixels, &image->width, &image->height );
399                 }
400                 else
401                 {
402                         /* attempt to load jpg */
403                         StripExtension( name );
404                         strcat( name, ".jpg" );
405                         size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
406                         if ( size > 0 ) {
407                                 if ( LoadJPGBuff( buffer, size, &image->pixels, &image->width, &image->height ) == -1 && image->pixels != NULL ) {
408                                         Sys_Printf( "WARNING: LoadJPGBuff: %s\n", (unsigned char*) image->pixels );
409                                 }
410                         }
411                         else
412                         {
413                                 /* attempt to load dds */
414                                 StripExtension( name );
415                                 strcat( name, ".dds" );
416                                 size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
417                                 if ( size > 0 ) {
418                                         LoadDDSBuffer( buffer, size, &image->pixels, &image->width, &image->height );
419
420                                         /* debug code */
421                                         #if 1
422                                         {
423                                                 ddsPF_t pf;
424                                                 DDSGetInfo( (ddsBuffer_t*) buffer, NULL, NULL, &pf );
425                                                 Sys_Printf( "pf = %d\n", pf );
426                                                 if ( image->width > 0 ) {
427                                                         StripExtension( name );
428                                                         strcat( name, "_converted.tga" );
429                                                         WriteTGA( "C:\\games\\quake3\\baseq3\\textures\\rad\\dds_converted.tga", image->pixels, image->width, image->height );
430                                                 }
431                                         }
432                                         #endif
433                                 }
434                         }
435                 }
436         }
437
438         /* free file buffer */
439         free( buffer );
440
441         /* make sure everything's kosher */
442         if ( size <= 0 || image->width <= 0 || image->height <= 0 || image->pixels == NULL ) {
443                 //%     Sys_Printf( "size = %d  width = %d  height = %d  pixels = 0x%08x (%s)\n",
444                 //%             size, image->width, image->height, image->pixels, name );
445                 free( image->name );
446                 image->name = NULL;
447                 return NULL;
448         }
449
450         /* set filename */
451         image->filename = safe_malloc( strlen( name ) + 1 );
452         strcpy( image->filename, name );
453
454         /* set count */
455         image->refCount = 1;
456         numImages++;
457
458         /* return the image */
459         return image;
460 }