Debian Codegraph @

tools/quake2/extra/common/lbmlib.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 1997-2006 Id Software, Inc.
4
5 This file is part of Quake 2 Tools source code.
6
7 Quake 2 Tools source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake 2 Tools source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Quake 2 Tools source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
21 */
22
23 // lbmlib.c
24
25 #include "cmdlib.h"
26 #include "lbmlib.h"
27
28
29
30 /*
31 ============================================================================
32
33 LBM STUFF
34
35 ============================================================================
36 */
37
38
39 typedef unsigned char UBYTE;
40 //conflicts with windows typedef short WORD;
41 typedef unsigned short UWORD;
42 typedef long LONG;
43
44 typedef enum
45 {
46 ms_none,
47 ms_mask,
48 ms_transcolor,
49 ms_lasso
50 } mask_t;
51
52 typedef enum
53 {
54 cm_none,
55 cm_rle1
56 } compress_t;
57
58 typedef struct
59 {
60 UWORD w,h;
61 short x,y;
62 UBYTE nPlanes;
63 UBYTE masking;
64 UBYTE compression;
65 UBYTE pad1;
66 UWORD transparentColor;
67 UBYTE xAspect,yAspect;
68 short pageWidth,pageHeight;
69 } bmhd_t;
70
71 extern bmhd_t bmhd; // will be in native byte order
72
73
74
75 #define FORMID ('F'+('O'<<8)+((int)'R'<<16)+((int)'M'<<24))
76 #define ILBMID ('I'+('L'<<8)+((int)'B'<<16)+((int)'M'<<24))
77 #define PBMID ('P'+('B'<<8)+((int)'M'<<16)+((int)' '<<24))
78 #define BMHDID ('B'+('M'<<8)+((int)'H'<<16)+((int)'D'<<24))
79 #define BODYID ('B'+('O'<<8)+((int)'D'<<16)+((int)'Y'<<24))
80 #define CMAPID ('C'+('M'<<8)+((int)'A'<<16)+((int)'P'<<24))
81
82
83 bmhd_t bmhd;
84
85 int Align (int l)
86 {
87 if (l&1)
88 return l+1;
89 return l;
90 }
91
92
93
94 /*
95 ================
96 LBMRLEdecompress
97
98 Source must be evenly aligned!
99 ================
100 */
101 byte *LBMRLEDecompress (byte *source,byte *unpacked, int bpwidth)
102 {
103 int count;
104 byte b,rept;
105
106 count = 0;
107
108 do
109 {
110 rept = *source++;
111
112 if (rept > 0x80)
113 {
114 rept = (rept^0xff)+2;
115 b = *source++;
116 memset(unpacked,b,rept);
117 unpacked += rept;
118 }
119 else if (rept < 0x80)
120 {
121 rept++;
122 memcpy(unpacked,source,rept);
123 unpacked += rept;
124 source += rept;
125 }
126 else
127 rept = 0; // rept of 0x80 is NOP
128
129 count += rept;
130
131 } while (count<bpwidth);
132
133 if (count>bpwidth)
134 Error ("Decompression exceeded width!\n");
135
136
137 return source;
138 }
139
140
141 /*
142 =================
143 LoadLBM
144 =================
145 */
146 void LoadLBM (char *filename, byte **picture, byte **palette)
147 {
148 byte *LBMbuffer, *picbuffer, *cmapbuffer;
149 int y;
150 byte *LBM_P, *LBMEND_P;
151 byte *pic_p;
152 byte *body_p;
153
154 int formtype,formlength;
155 int chunktype,chunklength;
156
157 // qiet compiler warnings
158 picbuffer = NULL;
159 cmapbuffer = NULL;
160
161 //
162 // load the LBM
163 //
164 LoadFile (filename, (void **)&LBMbuffer);
165
166 //
167 // parse the LBM header
168 //
169 LBM_P = LBMbuffer;
170 if ( *(int *)LBMbuffer != LittleLong(FORMID) )
171 Error ("No FORM ID at start of file!\n");
172
173 LBM_P += 4;
174 formlength = BigLong( *(int *)LBM_P );
175 LBM_P += 4;
176 LBMEND_P = LBM_P + Align(formlength);
177
178 formtype = LittleLong(*(int *)LBM_P);
179
180 if (formtype != ILBMID && formtype != PBMID)
181 Error ("Unrecognized form type: %c%c%c%c\n", formtype&0xff
182 ,(formtype>>8)&0xff,(formtype>>16)&0xff,(formtype>>24)&0xff);
183
184 LBM_P += 4;
185
186 //
187 // parse chunks
188 //
189
190 while (LBM_P < LBMEND_P)
191 {
192 chunktype = LBM_P[0] + (LBM_P[1]<<8) + (LBM_P[2]<<16) + (LBM_P[3]<<24);
193 LBM_P += 4;
194 chunklength = LBM_P[3] + (LBM_P[2]<<8) + (LBM_P[1]<<16) + (LBM_P[0]<<24);
195 LBM_P += 4;
196
197 switch ( chunktype )
198 {
199 case BMHDID:
200 memcpy (&bmhd,LBM_P,sizeof(bmhd));
201 bmhd.w = BigShort(bmhd.w);
202 bmhd.h = BigShort(bmhd.h);
203 bmhd.x = BigShort(bmhd.x);
204 bmhd.y = BigShort(bmhd.y);
205 bmhd.pageWidth = BigShort(bmhd.pageWidth);
206 bmhd.pageHeight = BigShort(bmhd.pageHeight);
207 break;
208
209 case CMAPID:
210 cmapbuffer = malloc (768);
211 memset (cmapbuffer, 0, 768);
212 memcpy (cmapbuffer, LBM_P, chunklength);
213 break;
214
215 case BODYID:
216 body_p = LBM_P;
217
218 pic_p = picbuffer = malloc (bmhd.w*bmhd.h);
219 if (formtype == PBMID)
220 {
221 //
222 // unpack PBM
223 //
224 for (y=0 ; y<bmhd.h ; y++, pic_p += bmhd.w)
225 {
226 if (bmhd.compression == cm_rle1)
227 body_p = LBMRLEDecompress ((byte *)body_p
228 , pic_p , bmhd.w);
229 else if (bmhd.compression == cm_none)
230 {
231 memcpy (pic_p,body_p,bmhd.w);
232 body_p += Align(bmhd.w);
233 }
234 }
235
236 }
237 else
238 {
239 //
240 // unpack ILBM
241 //
242 Error ("%s is an interlaced LBM, not packed", filename);
243 }
244 break;
245 }
246
247 LBM_P += Align(chunklength);
248 }
249
250 free (LBMbuffer);
251
252 *picture = picbuffer;
253
254 if (palette)
255 *palette = cmapbuffer;
256 }
257
258
259 /*
260 ============================================================================
261
262 WRITE LBM
263
264 ============================================================================
265 */
266
267 /*
268 ==============
269 WriteLBMfile
270 ==============
271 */
272 void WriteLBMfile (char *filename, byte *data,
273 int width, int height, byte *palette)
274 {
275 byte *lbm, *lbmptr;
276 int *formlength, *bmhdlength, *cmaplength, *bodylength;
277 int length;
278 bmhd_t basebmhd;
279
280 lbm = lbmptr = malloc (width*height+1000);
281
282 //
283 // start FORM
284 //
285 *lbmptr++ = 'F';
286 *lbmptr++ = 'O';
287 *lbmptr++ = 'R';
288 *lbmptr++ = 'M';
289
290 formlength = (int*)lbmptr;
291 lbmptr+=4; // leave space for length
292
293 *lbmptr++ = 'P';
294 *lbmptr++ = 'B';
295 *lbmptr++ = 'M';
296 *lbmptr++ = ' ';
297
298 //
299 // write BMHD
300 //
301 *lbmptr++ = 'B';
302 *lbmptr++ = 'M';
303 *lbmptr++ = 'H';
304 *lbmptr++ = 'D';
305
306 bmhdlength = (int *)lbmptr;
307 lbmptr+=4; // leave space for length
308
309 memset (&basebmhd,0,sizeof(basebmhd));
310 basebmhd.w = BigShort((short)width);
311 basebmhd.h = BigShort((short)height);
312 basebmhd.nPlanes = BigShort(8);
313 basebmhd.xAspect = BigShort(5);
314 basebmhd.yAspect = BigShort(6);
315 basebmhd.pageWidth = BigShort((short)width);
316 basebmhd.pageHeight = BigShort((short)height);
317
318 memcpy (lbmptr,&basebmhd,sizeof(basebmhd));
319 lbmptr += sizeof(basebmhd);
320
321 length = lbmptr-(byte *)bmhdlength-4;
322 *bmhdlength = BigLong(length);
323 if (length&1)
324 *lbmptr++ = 0; // pad chunk to even offset
325
326 //
327 // write CMAP
328 //
329 *lbmptr++ = 'C';
330 *lbmptr++ = 'M';
331 *lbmptr++ = 'A';
332 *lbmptr++ = 'P';
333
334 cmaplength = (int *)lbmptr;
335 lbmptr+=4; // leave space for length
336
337 memcpy (lbmptr,palette,768);
338 lbmptr += 768;
339
340 length = lbmptr-(byte *)cmaplength-4;
341 *cmaplength = BigLong(length);
342 if (length&1)
343 *lbmptr++ = 0; // pad chunk to even offset
344
345 //
346 // write BODY
347 //
348 *lbmptr++ = 'B';
349 *lbmptr++ = 'O';
350 *lbmptr++ = 'D';
351 *lbmptr++ = 'Y';
352
353 bodylength = (int *)lbmptr;
354 lbmptr+=4; // leave space for length
355
356 memcpy (lbmptr,data,width*height);
357 lbmptr += width*height;
358
359 length = lbmptr-(byte *)bodylength-4;
360 *bodylength = BigLong(length);
361 if (length&1)
362 *lbmptr++ = 0; // pad chunk to even offset
363
364 //
365 // done
366 //
367 length = lbmptr-(byte *)formlength-4;
368 *formlength = BigLong(length);
369 if (length&1)
370 *lbmptr++ = 0; // pad chunk to even offset
371
372 //
373 // write output file
374 //
375 SaveFile (filename, lbm, lbmptr-lbm);
376 free (lbm);
377 }
378
379
380 /*
381 ============================================================================
382
383 LOAD PCX
384
385 ============================================================================
386 */
387
388 typedef struct
389 {
390 char manufacturer;
391 char version;
392 char encoding;
393 char bits_per_pixel;
394 unsigned short xmin,ymin,xmax,ymax;
395 unsigned short hres,vres;
396 unsigned char palette[48];
397 char reserved;
398 char color_planes;
399 unsigned short bytes_per_line;
400 unsigned short palette_type;
401 char filler[58];
402 unsigned char data; // unbounded
403 } pcx_t;
404
405
406 /*
407 ==============
408 LoadPCX
409 ==============
410 */
411 void LoadPCX (char *filename, byte **pic, byte **palette, int *width, int *height)
412 {
413 byte *raw;
414 pcx_t *pcx;
415 int x, y;
416 int len;
417 int dataByte, runLength;
418 byte *out, *pix;
419
420 //
421 // load the file
422 //
423 len = LoadFile (filename, (void **)&raw);
424
425 //
426 // parse the PCX file
427 //
428 pcx = (pcx_t *)raw;
429 raw = &pcx->data;
430
431 pcx->xmin = LittleShort(pcx->xmin);
432 pcx->ymin = LittleShort(pcx->ymin);
433 pcx->xmax = LittleShort(pcx->xmax);
434 pcx->ymax = LittleShort(pcx->ymax);
435 pcx->hres = LittleShort(pcx->hres);
436 pcx->vres = LittleShort(pcx->vres);
437 pcx->bytes_per_line = LittleShort(pcx->bytes_per_line);
438 pcx->palette_type = LittleShort(pcx->palette_type);
439
440 if (pcx->manufacturer != 0x0a
441 || pcx->version != 5
442 || pcx->encoding != 1
443 || pcx->bits_per_pixel != 8
444 || pcx->xmax >= 640
445 || pcx->ymax >= 480)
446 Error ("Bad pcx file %s", filename);
447
448 if (palette)
449 {
450 *palette = malloc(768);
451 memcpy (*palette, (byte *)pcx + len - 768, 768);
452 }
453
454 if (width)
455 *width = pcx->xmax+1;
456 if (height)
457 *height = pcx->ymax+1;
458
459 if (!pic)
460 return;
461
462 out = malloc ( (pcx->ymax+1) * (pcx->xmax+1) );
463 if (!out)
464 Error ("Skin_Cache: couldn't allocate");
465
466 *pic = out;
467
468 pix = out;
469
470 for (y=0 ; y<=pcx->ymax ; y++, pix += pcx->xmax+1)
471 {
472 for (x=0 ; x<=pcx->xmax ; )
473 {
474 dataByte = *raw++;
475
476 if((dataByte & 0xC0) == 0xC0)
477 {
478 runLength = dataByte & 0x3F;
479 dataByte = *raw++;
480 }
481 else
482 runLength = 1;
483
484 while(runLength-- > 0)
485 pix[x++] = dataByte;
486 }
487
488 }
489
490 if ( raw - (byte *)pcx > len)
491 Error ("PCX file %s was malformed", filename);
492
493 free (pcx);
494 }
495
496 /*
497 ==============
498 WritePCXfile
499 ==============
500 */
501 void WritePCXfile (char *filename, byte *data,
502 int width, int height, byte *palette)
503 {
504 int i, j, length;
505 pcx_t *pcx;
506 byte *pack;
507
508 pcx = malloc (width*height*2+1000);
509 memset (pcx, 0, sizeof(*pcx));
510
511 pcx->manufacturer = 0x0a; // PCX id
512 pcx->version = 5; // 256 color
513 pcx->encoding = 1; // uncompressed
514 pcx->bits_per_pixel = 8; // 256 color
515 pcx->xmin = 0;
516 pcx->ymin = 0;
517 pcx->xmax = LittleShort((short)(width-1));
518 pcx->ymax = LittleShort((short)(height-1));
519 pcx->hres = LittleShort((short)width);
520 pcx->vres = LittleShort((short)height);
521 pcx->color_planes = 1; // chunky image
522 pcx->bytes_per_line = LittleShort((short)width);
523 pcx->palette_type = LittleShort(2); // not a grey scale
524
525 // pack the image
526 pack = &pcx->data;
527
528 for (i=0 ; i<height ; i++)
529 {
530 for (j=0 ; j<width ; j++)
531 {
532 if ( (*data & 0xc0) != 0xc0)
533 *pack++ = *data++;
534 else
535 {
536 *pack++ = 0xc1;
537 *pack++ = *data++;
538 }
539 }
540 }
541
542 // write the palette
543 *pack++ = 0x0c; // palette ID byte
544 for (i=0 ; i<768 ; i++)
545 *pack++ = *palette++;
546
547 // write output file
548 length = pack - (byte *)pcx;
549 SaveFile (filename, pcx, length);
550
551 free (pcx);
552 }
553
554
555 /*
556 ============================================================================
557
558 LOAD IMAGE
559
560 ============================================================================
561 */
562
563 /*
564 ==============
565 Load256Image
566
567 Will load either an lbm or pcx, depending on extension.
568 Any of the return pointers can be NULL if you don't want them.
569 ==============
570 */
571 void Load256Image (char *name, byte **pixels, byte **palette,
572 int *width, int *height)
573 {
574 char ext[128];
575
576 ExtractFileExtension (name, ext);
577 if (!Q_strcasecmp (ext, "lbm"))
578 {
579 LoadLBM (name, pixels, palette);
580 if (width)
581 *width = bmhd.w;
582 if (height)
583 *height = bmhd.h;
584 }
585 else if (!Q_strcasecmp (ext, "pcx"))
586 {
587 LoadPCX (name, pixels, palette, width, height);
588 }
589 else
590 Error ("%s doesn't have a known image extension", name);
591 }
592
593
594 /*
595 ==============
596 Save256Image
597
598 Will save either an lbm or pcx, depending on extension.
599 ==============
600 */
601 void Save256Image (char *name, byte *pixels, byte *palette,
602 int width, int height)
603 {
604 char ext[128];
605
606 ExtractFileExtension (name, ext);
607 if (!Q_strcasecmp (ext, "lbm"))
608 {
609 WriteLBMfile (name, pixels, width, height, palette);
610 }
611 else if (!Q_strcasecmp (ext, "pcx"))
612 {
613 WritePCXfile (name, pixels, width, height, palette);
614 }
615 else
616 Error ("%s doesn't have a known image extension", name);
617 }
618
619
620
621
622 /*
623 ============================================================================
624
625 TARGA IMAGE
626
627 ============================================================================
628 */
629
630 typedef struct _TargaHeader {
631 unsigned char id_length, colormap_type, image_type;
632 unsigned short colormap_index, colormap_length;
633 unsigned char colormap_size;
634 unsigned short x_origin, y_origin, width, height;
635 unsigned char pixel_size, attributes;
636 } TargaHeader;
637
638 int fgetLittleShort (FILE *f)
639 {
640 byte b1, b2;
641
642 b1 = fgetc(f);
643 b2 = fgetc(f);
644
645 return (short)(b1 + b2*256);
646 }
647
648 int fgetLittleLong (FILE *f)
649 {
650 byte b1, b2, b3, b4;
651
652 b1 = fgetc(f);
653 b2 = fgetc(f);
654 b3 = fgetc(f);
655 b4 = fgetc(f);
656
657 return b1 + (b2<<8) + (b3<<16) + (b4<<24);
658 }
659
660
661 /*
662 =============
663 LoadTGA
664 =============
665 */
666 void LoadTGA (char *name, byte **pixels, int *width, int *height)
667 {
668 int columns, rows, numPixels;
669 byte *pixbuf;
670 int row, column;
671 FILE *fin;
672 byte *targa_rgba;
673 TargaHeader targa_header;
674
675 fin = fopen (name, "rb");
676 if (!fin)
677 Error ("Couldn't read %s", name);
678
679 targa_header.id_length = fgetc(fin);
680 targa_header.colormap_type = fgetc(fin);
681 targa_header.image_type = fgetc(fin);
682
683 targa_header.colormap_index = fgetLittleShort(fin);
684 targa_header.colormap_length = fgetLittleShort(fin);
685 targa_header.colormap_size = fgetc(fin);
686 targa_header.x_origin = fgetLittleShort(fin);
687 targa_header.y_origin = fgetLittleShort(fin);
688 targa_header.width = fgetLittleShort(fin);
689 targa_header.height = fgetLittleShort(fin);
690 targa_header.pixel_size = fgetc(fin);
691 targa_header.attributes = fgetc(fin);
692
693 if (targa_header.image_type!=2
694 && targa_header.image_type!=10)
695 Error ("LoadTGA: Only type 2 and 10 targa RGB images supported\n");
696
697 if (targa_header.colormap_type !=0
698 || (targa_header.pixel_size!=32 && targa_header.pixel_size!=24))
699 Error ("Texture_LoadTGA: Only 32 or 24 bit images supported (no colormaps)\n");
700
701 columns = targa_header.width;
702 rows = targa_header.height;
703 numPixels = columns * rows;
704
705 if (width)
706 *width = columns;
707 if (height)
708 *height = rows;
709 targa_rgba = malloc(numPixels*4);
710 *pixels = targa_rgba;
711
712 if (targa_header.id_length != 0)
713 fseek(fin, targa_header.id_length, SEEK_CUR); // skip TARGA image comment
714
715 if (targa_header.image_type==2) { // Uncompressed, RGB images
716 for(row=rows-1; row>=0; row--) {
717 pixbuf = targa_rgba + row*columns*4;
718 for(column=0; column<columns; column++) {
719 unsigned char red,green,blue,alphabyte;
720 switch (targa_header.pixel_size) {
721 case 24:
722
723 blue = getc(fin);
724 green = getc(fin);
725 red = getc(fin);
726 *pixbuf++ = red;
727 *pixbuf++ = green;
728 *pixbuf++ = blue;
729 *pixbuf++ = 255;
730 break;
731 case 32:
732 blue = getc(fin);
733 green = getc(fin);
734 red = getc(fin);
735 alphabyte = getc(fin);
736 *pixbuf++ = red;
737 *pixbuf++ = green;
738 *pixbuf++ = blue;
739 *pixbuf++ = alphabyte;
740 break;
741 }
742 }
743 }
744 }
745 else if (targa_header.image_type==10) { // Runlength encoded RGB images
746 unsigned char red,green,blue,alphabyte,packetHeader,packetSize,j;
747 for(row=rows-1; row>=0; row--) {
748 pixbuf = targa_rgba + row*columns*4;
749 for(column=0; column<columns; ) {
750 packetHeader=getc(fin);
751 packetSize = 1 + (packetHeader & 0x7f);
752 if (packetHeader & 0x80) { // run-length packet
753 switch (targa_header.pixel_size) {
754 case 24:
755 blue = getc(fin);
756 green = getc(fin);
757 red = getc(fin);
758 alphabyte = 255;
759 break;
760 case 32:
761 blue = getc(fin);
762 green = getc(fin);
763 red = getc(fin);
764 alphabyte = getc(fin);
765 break;
766 }
767
768 for(j=0;j<packetSize;j++) {
769 *pixbuf++=red;
770 *pixbuf++=green;
771 *pixbuf++=blue;
772 *pixbuf++=alphabyte;
773 column++;
774 if (column==columns) { // run spans across rows
775 column=0;
776 if (row>0)
777 row--;
778 else
779 goto breakOut;
780 pixbuf = targa_rgba + row*columns*4;
781 }
782 }
783 }
784 else { // non run-length packet
785 for(j=0;j<packetSize;j++) {
786 switch (targa_header.pixel_size) {
787 case 24:
788 blue = getc(fin);
789 green = getc(fin);
790 red = getc(fin);
791 *pixbuf++ = red;
792 *pixbuf++ = green;
793 *pixbuf++ = blue;
794 *pixbuf++ = 255;
795 break;
796 case 32:
797 blue = getc(fin);
798 green = getc(fin);
799 red = getc(fin);
800 alphabyte = getc(fin);
801 *pixbuf++ = red;
802 *pixbuf++ = green;
803 *pixbuf++ = blue;
804 *pixbuf++ = alphabyte;
805 break;
806 }
807 column++;
808 if (column==columns) { // pixel packet run spans across rows
809 column=0;
810 if (row>0)
811 row--;
812 else
813 goto breakOut;
814 pixbuf = targa_rgba + row*columns*4;
815 }
816 }
817 }
818 }
819 breakOut:;
820 }
821 }
822
823 fclose(fin);
824 }