lbmlib.c 17 KB

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