lbmlib.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  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 = 8;
  245. basebmhd.xAspect = 5;
  246. basebmhd.yAspect = 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. if (pic)
  333. *pic = NULL;
  334. if (palette)
  335. *palette = NULL;
  336. if (width)
  337. *width = 0;
  338. if (height)
  339. *height = 0;
  340. //
  341. // load the file
  342. //
  343. len = LoadFile (filename, (void **)&raw);
  344. if (len == -1)
  345. return;
  346. //
  347. // parse the PCX file
  348. //
  349. pcx = (pcx_t *)raw;
  350. raw = &pcx->data;
  351. pcx->xmin = LittleShort(pcx->xmin);
  352. pcx->ymin = LittleShort(pcx->ymin);
  353. pcx->xmax = LittleShort(pcx->xmax);
  354. pcx->ymax = LittleShort(pcx->ymax);
  355. pcx->hres = LittleShort(pcx->hres);
  356. pcx->vres = LittleShort(pcx->vres);
  357. pcx->bytes_per_line = LittleShort(pcx->bytes_per_line);
  358. pcx->palette_type = LittleShort(pcx->palette_type);
  359. if (pcx->manufacturer != 0x0a
  360. || pcx->version != 5
  361. || pcx->encoding != 1
  362. || pcx->bits_per_pixel != 8
  363. || pcx->xmax >= 640
  364. || pcx->ymax >= 480)
  365. Error ("Bad pcx file %s", filename);
  366. if (palette)
  367. {
  368. *palette = malloc(768);
  369. memcpy (*palette, (byte *)pcx + len - 768, 768);
  370. }
  371. if (width)
  372. *width = pcx->xmax+1;
  373. if (height)
  374. *height = pcx->ymax+1;
  375. if (!pic)
  376. return;
  377. out = malloc ( (pcx->ymax+1) * (pcx->xmax+1) );
  378. if (!out)
  379. Error ("Skin_Cache: couldn't allocate");
  380. *pic = out;
  381. pix = out;
  382. for (y=0 ; y<=pcx->ymax ; y++, pix += pcx->xmax+1)
  383. {
  384. for (x=0 ; x<=pcx->xmax ; )
  385. {
  386. dataByte = *raw++;
  387. if((dataByte & 0xC0) == 0xC0)
  388. {
  389. runLength = dataByte & 0x3F;
  390. dataByte = *raw++;
  391. }
  392. else
  393. runLength = 1;
  394. while(runLength-- > 0)
  395. pix[x++] = dataByte;
  396. }
  397. }
  398. if ( raw - (byte *)pcx > len)
  399. Error ("PCX file %s was malformed", filename);
  400. free (pcx);
  401. }
  402. /*
  403. ==============
  404. WritePCXfile
  405. ==============
  406. */
  407. void WritePCXfile (char *filename, byte *data,
  408. int width, int height, byte *palette)
  409. {
  410. int i, j, length;
  411. pcx_t *pcx;
  412. byte *pack;
  413. pcx = malloc (width*height*2+1000);
  414. memset (pcx, 0, sizeof(*pcx));
  415. pcx->manufacturer = 0x0a; // PCX id
  416. pcx->version = 5; // 256 color
  417. pcx->encoding = 1; // uncompressed
  418. pcx->bits_per_pixel = 8; // 256 color
  419. pcx->xmin = 0;
  420. pcx->ymin = 0;
  421. pcx->xmax = LittleShort((short)(width-1));
  422. pcx->ymax = LittleShort((short)(height-1));
  423. pcx->hres = LittleShort((short)width);
  424. pcx->vres = LittleShort((short)height);
  425. pcx->color_planes = 1; // chunky image
  426. pcx->bytes_per_line = LittleShort((short)width);
  427. pcx->palette_type = LittleShort(2); // not a grey scale
  428. // pack the image
  429. pack = &pcx->data;
  430. for (i=0 ; i<height ; i++)
  431. {
  432. for (j=0 ; j<width ; j++)
  433. {
  434. if ( (*data & 0xc0) != 0xc0)
  435. *pack++ = *data++;
  436. else
  437. {
  438. *pack++ = 0xc1;
  439. *pack++ = *data++;
  440. }
  441. }
  442. }
  443. // write the palette
  444. *pack++ = 0x0c; // palette ID byte
  445. for (i=0 ; i<768 ; i++)
  446. *pack++ = *palette++;
  447. // write output file
  448. length = pack - (byte *)pcx;
  449. SaveFile (filename, pcx, length);
  450. free (pcx);
  451. }
  452. /*
  453. ============================================================================
  454. LOAD IMAGE
  455. ============================================================================
  456. */
  457. /*
  458. ==============
  459. Load256Image
  460. Will load either an lbm or pcx, depending on extension.
  461. Any of the return pointers can be NULL if you don't want them.
  462. ==============
  463. */
  464. void Load256Image (char *name, byte **pixels, byte **palette,
  465. int *width, int *height)
  466. {
  467. char ext[128];
  468. ExtractFileExtension (name, ext);
  469. if (!Q_strcasecmp (ext, "lbm"))
  470. {
  471. LoadLBM (name, pixels, palette);
  472. if (width)
  473. *width = bmhd.w;
  474. if (height)
  475. *height = bmhd.h;
  476. }
  477. else if (!Q_strcasecmp (ext, "pcx"))
  478. {
  479. LoadPCX (name, pixels, palette, width, height);
  480. }
  481. else
  482. Error ("%s doesn't have a known image extension", name);
  483. }
  484. /*
  485. ==============
  486. Save256Image
  487. Will save either an lbm or pcx, depending on extension.
  488. ==============
  489. */
  490. void Save256Image (char *name, byte *pixels, byte *palette,
  491. int width, int height)
  492. {
  493. char ext[128];
  494. ExtractFileExtension (name, ext);
  495. if (!Q_strcasecmp (ext, "lbm"))
  496. {
  497. WriteLBMfile (name, pixels, width, height, palette);
  498. }
  499. else if (!Q_strcasecmp (ext, "pcx"))
  500. {
  501. WritePCXfile (name, pixels, width, height, palette);
  502. }
  503. else
  504. Error ("%s doesn't have a known image extension", name);
  505. }
  506. /*
  507. ============================================================================
  508. TARGA IMAGE
  509. ============================================================================
  510. */
  511. typedef struct _TargaHeader {
  512. unsigned char id_length, colormap_type, image_type;
  513. unsigned short colormap_index, colormap_length;
  514. unsigned char colormap_size;
  515. unsigned short x_origin, y_origin, width, height;
  516. unsigned char pixel_size, attributes;
  517. } TargaHeader;
  518. int fgetLittleShort (FILE *f)
  519. {
  520. byte b1, b2;
  521. b1 = fgetc(f);
  522. b2 = fgetc(f);
  523. return (short)(b1 + b2*256);
  524. }
  525. int fgetLittleLong (FILE *f)
  526. {
  527. byte b1, b2, b3, b4;
  528. b1 = fgetc(f);
  529. b2 = fgetc(f);
  530. b3 = fgetc(f);
  531. b4 = fgetc(f);
  532. return b1 + (b2<<8) + (b3<<16) + (b4<<24);
  533. }
  534. /*
  535. =============
  536. LoadTGA
  537. =============
  538. */
  539. void LoadTGA (char *name, byte **pixels, int *width, int *height)
  540. {
  541. int columns, rows, numPixels;
  542. byte *pixbuf;
  543. int row, column;
  544. FILE *fin;
  545. byte *targa_rgba;
  546. TargaHeader targa_header;
  547. fin = fopen (name, "rb");
  548. if (!fin)
  549. Error ("Couldn't read %s", name);
  550. targa_header.id_length = fgetc(fin);
  551. targa_header.colormap_type = fgetc(fin);
  552. targa_header.image_type = fgetc(fin);
  553. targa_header.colormap_index = fgetLittleShort(fin);
  554. targa_header.colormap_length = fgetLittleShort(fin);
  555. targa_header.colormap_size = fgetc(fin);
  556. targa_header.x_origin = fgetLittleShort(fin);
  557. targa_header.y_origin = fgetLittleShort(fin);
  558. targa_header.width = fgetLittleShort(fin);
  559. targa_header.height = fgetLittleShort(fin);
  560. targa_header.pixel_size = fgetc(fin);
  561. targa_header.attributes = fgetc(fin);
  562. if (targa_header.image_type!=2
  563. && targa_header.image_type!=10)
  564. Error ("LoadTGA: Only type 2 and 10 targa RGB images supported\n");
  565. if (targa_header.colormap_type !=0
  566. || (targa_header.pixel_size!=32 && targa_header.pixel_size!=24))
  567. Error ("Texture_LoadTGA: Only 32 or 24 bit images supported (no colormaps)\n");
  568. columns = targa_header.width;
  569. rows = targa_header.height;
  570. numPixels = columns * rows;
  571. if (width)
  572. *width = columns;
  573. if (height)
  574. *height = rows;
  575. targa_rgba = malloc(numPixels*4);
  576. *pixels = targa_rgba;
  577. if (targa_header.id_length != 0)
  578. fseek(fin, targa_header.id_length, SEEK_CUR); // skip TARGA image comment
  579. if (targa_header.image_type==2) { // Uncompressed, RGB images
  580. for(row=rows-1; row>=0; row--) {
  581. pixbuf = targa_rgba + row*columns*4;
  582. for(column=0; column<columns; column++) {
  583. unsigned char red,green,blue,alphabyte;
  584. switch (targa_header.pixel_size) {
  585. case 24:
  586. blue = getc(fin);
  587. green = getc(fin);
  588. red = getc(fin);
  589. *pixbuf++ = red;
  590. *pixbuf++ = green;
  591. *pixbuf++ = blue;
  592. *pixbuf++ = 255;
  593. break;
  594. case 32:
  595. blue = getc(fin);
  596. green = getc(fin);
  597. red = getc(fin);
  598. alphabyte = getc(fin);
  599. *pixbuf++ = red;
  600. *pixbuf++ = green;
  601. *pixbuf++ = blue;
  602. *pixbuf++ = alphabyte;
  603. break;
  604. }
  605. }
  606. }
  607. }
  608. else if (targa_header.image_type==10) { // Runlength encoded RGB images
  609. unsigned char red,green,blue,alphabyte,packetHeader,packetSize,j;
  610. for(row=rows-1; row>=0; row--) {
  611. pixbuf = targa_rgba + row*columns*4;
  612. for(column=0; column<columns; ) {
  613. packetHeader=getc(fin);
  614. packetSize = 1 + (packetHeader & 0x7f);
  615. if (packetHeader & 0x80) { // run-length packet
  616. switch (targa_header.pixel_size) {
  617. case 24:
  618. blue = getc(fin);
  619. green = getc(fin);
  620. red = getc(fin);
  621. alphabyte = 255;
  622. break;
  623. case 32:
  624. blue = getc(fin);
  625. green = getc(fin);
  626. red = getc(fin);
  627. alphabyte = getc(fin);
  628. break;
  629. }
  630. for(j=0;j<packetSize;j++) {
  631. *pixbuf++=red;
  632. *pixbuf++=green;
  633. *pixbuf++=blue;
  634. *pixbuf++=alphabyte;
  635. column++;
  636. if (column==columns) { // run spans across rows
  637. column=0;
  638. if (row>0)
  639. row--;
  640. else
  641. goto breakOut;
  642. pixbuf = targa_rgba + row*columns*4;
  643. }
  644. }
  645. }
  646. else { // non run-length packet
  647. for(j=0;j<packetSize;j++) {
  648. switch (targa_header.pixel_size) {
  649. case 24:
  650. blue = getc(fin);
  651. green = getc(fin);
  652. red = getc(fin);
  653. *pixbuf++ = red;
  654. *pixbuf++ = green;
  655. *pixbuf++ = blue;
  656. *pixbuf++ = 255;
  657. break;
  658. case 32:
  659. blue = getc(fin);
  660. green = getc(fin);
  661. red = getc(fin);
  662. alphabyte = getc(fin);
  663. *pixbuf++ = red;
  664. *pixbuf++ = green;
  665. *pixbuf++ = blue;
  666. *pixbuf++ = alphabyte;
  667. break;
  668. }
  669. column++;
  670. if (column==columns) { // pixel packet run spans across rows
  671. column=0;
  672. if (row>0)
  673. row--;
  674. else
  675. goto breakOut;
  676. pixbuf = targa_rgba + row*columns*4;
  677. }
  678. }
  679. }
  680. }
  681. breakOut:;
  682. }
  683. }
  684. fclose(fin);
  685. }