/packages/pasjpeg/examples/rdtarga.pas

https://github.com/slibre/freepascal · Pascal · 559 lines · 387 code · 80 blank · 92 comment · 27 complexity · 62fa236ed644c100dad19916c259f715 MD5 · raw file

  1. Unit RdTarga;
  2. { rdtarga.c ; Copyright (C) 1991-1996, Thomas G. Lane.
  3. These routines may need modification for non-Unix environments or
  4. specialized applications. As they stand, they assume input from
  5. an ordinary stdio stream. They further assume that reading begins
  6. at the start of the file; start_input may need work if the
  7. user interface has already read some data (e.g., to determine that
  8. the file is indeed Targa format).
  9. Based on code contributed by Lee Daniel Crocker. }
  10. interface
  11. {$I jconfig.inc}
  12. uses
  13. jmorecfg,
  14. jpeglib,
  15. jinclude,
  16. jdeferr,
  17. jerror,
  18. cdjpeg; { Common decls for cjpeg/djpeg applications }
  19. { The module selection routine for Targa format input. }
  20. {GLOBAL}
  21. function jinit_read_targa (cinfo : j_compress_ptr) : cjpeg_source_ptr;
  22. implementation
  23. { Macros to deal with unsigned chars as efficiently as compiler allows }
  24. type
  25. U_CHAR = byte;
  26. UCH = int;
  27. (*type
  28. {$ifdef CHAR_IS_UNSIGNED}
  29. UCH = int;
  30. {$else}
  31. UCH = int (x and $FF);
  32. {$endif}
  33. *)
  34. { Private version of data source object }
  35. type
  36. tga_source_ptr = ^tga_source_struct;
  37. tga_source_struct = record
  38. pub : cjpeg_source_struct; { public fields }
  39. cinfo : j_compress_ptr; { back link saves passing separate parm }
  40. colormap : JSAMPARRAY; { Targa colormap (converted to my format) }
  41. whole_image : jvirt_sarray_ptr; { Needed if funny input row order }
  42. current_row : JDIMENSION; { Current logical row number to read }
  43. { Pointer to routine to extract next Targa pixel from input file }
  44. read_pixel : procedure (sinfo : tga_source_ptr);
  45. { Result of read_pixel is delivered here: }
  46. tga_pixel : array[0..4-1] of U_CHAR;
  47. pixel_size : int; { Bytes per Targa pixel (1 to 4) }
  48. { State info for reading RLE-coded pixels; both counts must be init to 0 }
  49. block_count : int; { # of pixels remaining in RLE block }
  50. dup_pixel_count : int; { # of times to duplicate previous pixel }
  51. { This saves the correct pixel-row-expansion method for preload_image }
  52. get_pixel_rows : function(cinfo : j_compress_ptr;
  53. sinfo : cjpeg_source_ptr) : JDIMENSION;
  54. end;
  55. { For expanding 5-bit pixel values to 8-bit with best rounding }
  56. const
  57. c5to8bits : array[0..32-1] of UINT8 =
  58. ( 0, 8, 16, 25, 33, 41, 49, 58,
  59. 66, 74, 82, 90, 99, 107, 115, 123,
  60. 132, 140, 148, 156, 165, 173, 181, 189,
  61. 197, 206, 214, 222, 230, 239, 247, 255);
  62. function getc(f : fileptr) : byte;
  63. begin
  64. getc := 0;
  65. end;
  66. const
  67. EOF = byte(26); { ^Z }
  68. {LOCAL}
  69. function read_byte (sinfo : tga_source_ptr) : int;
  70. { Read next byte from Targa file }
  71. var
  72. {register} infile : FILEptr;
  73. {register} c : int;
  74. begin
  75. infile := sinfo^.pub.input_file;
  76. c := getc(infile);
  77. if (c = EOF) then
  78. ERREXIT(j_common_ptr(sinfo^.cinfo), JERR_INPUT_EOF);
  79. read_byte := c;
  80. end;
  81. {LOCAL}
  82. procedure read_colormap (sinfo : tga_source_ptr;
  83. cmaplen : int;
  84. mapentrysize : int);
  85. { Read the colormap from a Targa file }
  86. var
  87. i : int;
  88. begin
  89. { Presently only handles 24-bit BGR format }
  90. if (mapentrysize <> 24) then
  91. ERREXIT(j_common_ptr(sinfo^.cinfo), JERR_TGA_BADCMAP);
  92. for i := 0 to pred(cmaplen) do
  93. begin
  94. sinfo^.colormap^[2]^[i] := JSAMPLE (read_byte(sinfo));
  95. sinfo^.colormap^[1]^[i] := JSAMPLE (read_byte(sinfo));
  96. sinfo^.colormap^[0]^[i] := JSAMPLE (read_byte(sinfo));
  97. end;
  98. end;
  99. { read_pixel methods: get a single pixel from Targa file into tga_pixel[] }
  100. {METHODDEF}
  101. procedure read_non_rle_pixel (sinfo : tga_source_ptr); far;
  102. { Read one Targa pixel from the input file; no RLE expansion }
  103. var
  104. {register} infile : FILEptr;
  105. {register} i : int;
  106. begin
  107. infile := sinfo^.pub.input_file;
  108. for i := 0 to pred(sinfo^.pixel_size) do
  109. begin
  110. sinfo^.tga_pixel[i] := U_CHAR (getc(infile));
  111. end;
  112. end;
  113. {METHODDEF}
  114. procedure read_rle_pixel (sinfo : tga_source_ptr); far;
  115. { Read one Targa pixel from the input file, expanding RLE data as needed }
  116. var
  117. {register} infile : FILEptr;
  118. {register} i : int;
  119. begin
  120. infile := sinfo^.pub.input_file;
  121. { Duplicate previously read pixel? }
  122. if (sinfo^.dup_pixel_count > 0) then
  123. begin
  124. Dec(sinfo^.dup_pixel_count);
  125. exit;
  126. end;
  127. { Time to read RLE block header? }
  128. Dec(sinfo^.block_count);
  129. if (sinfo^.block_count < 0) then
  130. begin { decrement pixels remaining in block }
  131. i := read_byte(sinfo);
  132. if (i and $80) <> 0 then
  133. begin { Start of duplicate-pixel block? }
  134. sinfo^.dup_pixel_count := i and $7F; { number of dups after this one }
  135. sinfo^.block_count := 0; { then read new block header }
  136. end
  137. else
  138. begin
  139. sinfo^.block_count := i and $7F; { number of pixels after this one }
  140. end;
  141. end;
  142. { Read next pixel }
  143. for i := 0 to pred(sinfo^.pixel_size) do
  144. begin
  145. sinfo^.tga_pixel[i] := U_CHAR (getc(infile));
  146. end;
  147. end;
  148. { Read one row of pixels.
  149. We provide several different versions depending on input file format. }
  150. {METHODDEF}
  151. function get_8bit_gray_row (cinfo : j_compress_ptr;
  152. sinfo : cjpeg_source_ptr) : JDIMENSION; far;
  153. { This version is for reading 8-bit grayscale pixels }
  154. var
  155. source : tga_source_ptr;
  156. {register} ptr : JSAMPLE_PTR;
  157. {register} col : JDIMENSION;
  158. begin
  159. source := tga_source_ptr (sinfo);
  160. ptr := JSAMPLE_PTR(source^.pub.buffer^[0]);
  161. for col := pred(cinfo^.image_width) downto 0 do
  162. begin
  163. source^.read_pixel (source); { Load next pixel into tga_pixel }
  164. ptr^ := JSAMPLE (UCH(source^.tga_pixel[0]));
  165. Inc(ptr);
  166. end;
  167. get_8bit_gray_row := 1;
  168. end;
  169. {METHODDEF}
  170. function get_8bit_row (cinfo : j_compress_ptr;
  171. sinfo : cjpeg_source_ptr) : JDIMENSION; far;
  172. { This version is for reading 8-bit colormap indexes }
  173. var
  174. source : tga_source_ptr;
  175. {register} t : int;
  176. {register} ptr : JSAMPLE_PTR;
  177. {register} col : JDIMENSION;
  178. {register} colormap : JSAMPARRAY;
  179. begin
  180. source := tga_source_ptr (sinfo);
  181. colormap := source^.colormap;
  182. ptr := JSAMPLE_PTR(source^.pub.buffer^[0]);
  183. for col := pred(cinfo^.image_width) downto 0 do
  184. begin
  185. source^.read_pixel (source); { Load next pixel into tga_pixel }
  186. t := UCH(source^.tga_pixel[0]);
  187. ptr^ := colormap^[0]^[t];
  188. Inc(ptr);
  189. ptr^ := colormap^[1]^[t];
  190. Inc(ptr);
  191. ptr^ := colormap^[2]^[t];
  192. Inc(ptr);
  193. end;
  194. get_8bit_row := 1;
  195. end;
  196. {METHODDEF}
  197. function get_16bit_row (cinfo : j_compress_ptr;
  198. sinfo : cjpeg_source_ptr) : JDIMENSION; far;
  199. { This version is for reading 16-bit pixels }
  200. var
  201. source : tga_source_ptr;
  202. {register} t : int;
  203. {register} ptr : JSAMPROW;
  204. {register} col : JDIMENSION;
  205. begin
  206. source := tga_source_ptr (sinfo);
  207. ptr := source^.pub.buffer^[0];
  208. for col := pred(cinfo^.image_width) downto 0 do
  209. begin
  210. source^.read_pixel (source); { Load next pixel into tga_pixel }
  211. t := UCH(source^.tga_pixel[0]);
  212. Inc(t, UCH(source^.tga_pixel[1]) shr 8);
  213. { We expand 5 bit data to 8 bit sample width.
  214. The format of the 16-bit (LSB first) input word is
  215. xRRRRRGGGGGBBBBB
  216. }
  217. ptr^[2] := JSAMPLE (c5to8bits[t and $1F]);
  218. t := t shr 5;
  219. ptr^[1] := JSAMPLE (c5to8bits[t and $1F]);
  220. t := t shr 5;
  221. ptr^[0] := JSAMPLE (c5to8bits[t and $1F]);
  222. Inc(JSAMPLE_PTR(ptr), 3);
  223. end;
  224. get_16bit_row :=1;
  225. end;
  226. {METHODDEF}
  227. function get_24bit_row (cinfo : j_compress_ptr;
  228. sinfo : cjpeg_source_ptr) : JDIMENSION; far;
  229. { This version is for reading 24-bit pixels }
  230. var
  231. source : tga_source_ptr;
  232. {register} ptr : JSAMPLE_PTR;
  233. {register} col : JDIMENSION;
  234. begin
  235. source := tga_source_ptr (sinfo);
  236. ptr := JSAMPLE_PTR(source^.pub.buffer^[0]);
  237. for col := pred(cinfo^.image_width) downto 0 do
  238. begin
  239. source^.read_pixel (source); { Load next pixel into tga_pixel }
  240. ptr^ := JSAMPLE (UCH(source^.tga_pixel[2])); { change BGR to RGB order }
  241. Inc(ptr);
  242. ptr^ := JSAMPLE (UCH(source^.tga_pixel[1]));
  243. Inc(ptr);
  244. ptr^ := JSAMPLE (UCH(source^.tga_pixel[0]));
  245. Inc(ptr);
  246. end;
  247. get_24bit_row := 1;
  248. end;
  249. { Targa also defines a 32-bit pixel format with order B,G,R,A.
  250. We presently ignore the attribute byte, so the code for reading
  251. these pixels is identical to the 24-bit routine above.
  252. This works because the actual pixel length is only known to read_pixel. }
  253. const
  254. get_32bit_row : function (cinfo : j_compress_ptr;
  255. sinfo : cjpeg_source_ptr) : JDIMENSION
  256. = get_24bit_row;
  257. { This method is for re-reading the input data in standard top-down
  258. row order. The entire image has already been read into whole_image
  259. with proper conversion of pixel format, but it's in a funny row order. }
  260. {METHODDEF}
  261. function get_memory_row (cinfo : j_compress_ptr;
  262. sinfo : cjpeg_source_ptr) : JDIMENSION; far;
  263. var
  264. source : tga_source_ptr;
  265. source_row : JDIMENSION;
  266. begin
  267. source := tga_source_ptr (sinfo);
  268. { Compute row of source that maps to current_row of normal order }
  269. { For now, assume image is bottom-up and not interlaced. }
  270. { NEEDS WORK to support interlaced images! }
  271. source_row := cinfo^.image_height - source^.current_row - 1;
  272. { Fetch that row from virtual array }
  273. source^.pub.buffer := cinfo^.mem^.access_virt_sarray
  274. (j_common_ptr (cinfo), source^.whole_image,
  275. source_row, JDIMENSION (1), FALSE);
  276. Inc(source^.current_row);
  277. get_memory_row := 1;
  278. end;
  279. { This method loads the image into whole_image during the first call on
  280. get_pixel_rows. The get_pixel_rows pointer is then adjusted to call
  281. get_memory_row on subsequent calls. }
  282. {METHODDEF}
  283. function preload_image (cinfo : j_compress_ptr;
  284. sinfo : cjpeg_source_ptr) : JDIMENSION; far;
  285. var
  286. source : tga_source_ptr;
  287. row : JDIMENSION;
  288. progress : cd_progress_ptr;
  289. begin
  290. source := tga_source_ptr (sinfo);
  291. progress := cd_progress_ptr (cinfo^.progress);
  292. { Read the data into a virtual array in input-file row order. }
  293. for row := 0 to pred(cinfo^.image_height) do
  294. begin
  295. if (progress <> NIL) then
  296. begin
  297. progress^.pub.pass_counter := long (row);
  298. progress^.pub.pass_limit := long (cinfo^.image_height);
  299. progress^.pub.progress_monitor (j_common_ptr (cinfo));
  300. end;
  301. source^.pub.buffer := cinfo^.mem^.access_virt_sarray
  302. (j_common_ptr(cinfo), source^.whole_image, row, JDIMENSION(1), TRUE);
  303. source^.get_pixel_rows (cinfo, sinfo);
  304. end;
  305. if (progress <> NIL) then
  306. Inc(progress^.completed_extra_passes);
  307. { Set up to read from the virtual array in unscrambled order }
  308. source^.pub.get_pixel_rows := get_memory_row;
  309. source^.current_row := 0;
  310. { And read the first row }
  311. preload_image := get_memory_row(cinfo, sinfo);
  312. end;
  313. { Read the file header; return image size and component count. }
  314. {METHODDEF}
  315. procedure start_input_tga (cinfo : j_compress_ptr;
  316. sinfo : cjpeg_source_ptr); far;
  317. var
  318. source : tga_source_ptr;
  319. targaheader : array[0..18-1] of U_CHAR;
  320. idlen, cmaptype, subtype, flags, interlace_type, components : int;
  321. width, height, maplen : uInt;
  322. is_bottom_up : boolean;
  323. var
  324. progress : cd_progress_ptr;
  325. begin
  326. source := tga_source_ptr (sinfo);
  327. if JFREAD(source^.pub.input_file, @targaheader, 18) <> size_t(18) then
  328. ERREXIT(j_common_ptr(cinfo), JERR_INPUT_EOF);
  329. { Pretend "15-bit" pixels are 16-bit --- we ignore attribute bit anyway }
  330. if (targaheader[16] = 15) then
  331. targaheader[16] := 16;
  332. idlen := UCH(targaheader[0]);
  333. cmaptype := UCH(targaheader[1]);
  334. subtype := UCH(targaheader[2]);
  335. maplen := {GET_2B(5);}
  336. uInt (UCH(targaheader[5])) +
  337. ( uInt (UCH(targaheader[5+1])) ) shl 8;
  338. width := {GET_2B(12);}
  339. ( uInt(UCH(targaheader[12])) +
  340. ( uInt(UCH(targaheader[12+1])) ) shl 8);
  341. height := {GET_2B(14);}
  342. ( uInt(UCH(targaheader[14])) +
  343. ( uInt(UCH(targaheader[14+1])) ) shl 8);
  344. source^.pixel_size := UCH(targaheader[16]) shl 3;
  345. flags := UCH(targaheader[17]); { Image Descriptor byte }
  346. is_bottom_up := (flags and $20) = 0; { bit 5 set => top-down }
  347. interlace_type := flags shl 6; { bits 6/7 are interlace code }
  348. if (cmaptype > 1) or { cmaptype must be 0 or 1 }
  349. (source^.pixel_size < 1) or (source^.pixel_size > 4) or
  350. ((UCH(targaheader[16]) and 7) <> 0) or { bits/pixel must be multiple of 8 }
  351. (interlace_type <> 0) then { currently don't allow interlaced image }
  352. ERREXIT(j_common_ptr(cinfo), JERR_TGA_BADPARMS);
  353. if (subtype > 8) then
  354. begin
  355. { It's an RLE-coded file }
  356. source^.read_pixel := read_rle_pixel;
  357. source^.block_count := 0;
  358. source^.dup_pixel_count := 0;
  359. Dec(subtype, 8);
  360. end
  361. else
  362. begin
  363. { Non-RLE file }
  364. source^.read_pixel := read_non_rle_pixel;
  365. end;
  366. { Now should have subtype 1, 2, or 3 }
  367. components := 3; { until proven different }
  368. cinfo^.in_color_space := JCS_RGB;
  369. case (subtype) of
  370. 1:begin { Colormapped image }
  371. if (source^.pixel_size = 1) and (cmaptype = 1) then
  372. source^.get_pixel_rows := get_8bit_row
  373. else
  374. ERREXIT(j_common_ptr(cinfo), JERR_TGA_BADPARMS);
  375. TRACEMS2(j_common_ptr(cinfo), 1, JTRC_TGA_MAPPED, width, height);
  376. end;
  377. 2:begin { RGB image }
  378. case (source^.pixel_size) of
  379. 2: source^.get_pixel_rows := get_16bit_row;
  380. 3: source^.get_pixel_rows := get_24bit_row;
  381. 4: source^.get_pixel_rows := get_32bit_row;
  382. else
  383. ERREXIT(j_common_ptr(cinfo), JERR_TGA_BADPARMS);
  384. end;
  385. TRACEMS2(j_common_ptr(cinfo), 1, JTRC_TGA, width, height);
  386. end;
  387. 3:begin { Grayscale image }
  388. components := 1;
  389. cinfo^.in_color_space := JCS_GRAYSCALE;
  390. if (source^.pixel_size = 1) then
  391. source^.get_pixel_rows := get_8bit_gray_row
  392. else
  393. ERREXIT(j_common_ptr(cinfo), JERR_TGA_BADPARMS);
  394. TRACEMS2(j_common_ptr(cinfo), 1, JTRC_TGA_GRAY, width, height);
  395. end;
  396. else
  397. ERREXIT(j_common_ptr(cinfo), JERR_TGA_BADPARMS);
  398. end;
  399. if (is_bottom_up) then
  400. begin
  401. { Create a virtual array to buffer the upside-down image. }
  402. source^.whole_image := cinfo^.mem^.request_virt_sarray
  403. (j_common_ptr (cinfo), JPOOL_IMAGE, FALSE,
  404. JDIMENSION(width * components), JDIMENSION (height), JDIMENSION (1));
  405. if (cinfo^.progress <> NIL) then
  406. begin
  407. progress := cd_progress_ptr (cinfo^.progress);
  408. Inc(progress^.total_extra_passes); { count file input as separate pass }
  409. end;
  410. { source^.pub.buffer will point to the virtual array. }
  411. source^.pub.buffer_height := 1; { in case anyone looks at it }
  412. source^.pub.get_pixel_rows := preload_image;
  413. end
  414. else
  415. begin
  416. { Don't need a virtual array, but do need a one-row input buffer. }
  417. source^.whole_image := NIL;
  418. source^.pub.buffer := cinfo^.mem^.alloc_sarray (
  419. j_common_ptr (cinfo), JPOOL_IMAGE,
  420. JDIMENSION (width * components), JDIMENSION (1)) ;
  421. source^.pub.buffer_height := 1;
  422. source^.pub.get_pixel_rows := source^.get_pixel_rows;
  423. end;
  424. while (idlen > 0) do { Throw away ID field }
  425. begin
  426. Dec(idlen);
  427. {void} read_byte(source);
  428. end;
  429. if (maplen > 0) then
  430. begin
  431. if (maplen > 256) or {GET_2B(3) <> 0}
  432. ( (uInt (UCH(targaheader[3])) +
  433. (uInt (UCH(targaheader[3+1])) ) shl 8) <> 0) then
  434. ERREXIT(j_common_ptr(cinfo), JERR_TGA_BADCMAP);
  435. { Allocate space to store the colormap }
  436. source^.colormap := cinfo^.mem^.alloc_sarray (
  437. j_common_ptr (cinfo), JPOOL_IMAGE,
  438. JDIMENSION (maplen), JDIMENSION (3));
  439. { and read it from the file }
  440. read_colormap(source, int (maplen), UCH(targaheader[7]));
  441. end
  442. else
  443. begin
  444. if (cmaptype <> 0) then { but you promised a cmap! }
  445. ERREXIT(j_common_ptr(cinfo), JERR_TGA_BADPARMS);
  446. source^.colormap := NIL;
  447. end;
  448. cinfo^.input_components := components;
  449. cinfo^.data_precision := 8;
  450. cinfo^.image_width := width;
  451. cinfo^.image_height := height;
  452. end;
  453. { Finish up at the end of the file. }
  454. {METHODDEF}
  455. procedure finish_input_tga (cinfo : j_compress_ptr;
  456. sinfo : cjpeg_source_ptr); far;
  457. begin
  458. { no work }
  459. end;
  460. { The module selection routine for Targa format input. }
  461. {GLOBAL}
  462. function jinit_read_targa (cinfo : j_compress_ptr) : cjpeg_source_ptr;
  463. var
  464. source : tga_source_ptr;
  465. begin
  466. { Create module interface object }
  467. source := tga_source_ptr (
  468. cinfo^.mem^.alloc_small (j_common_ptr (cinfo), JPOOL_IMAGE,
  469. SIZEOF(tga_source_struct)) );
  470. source^.cinfo := cinfo; { make back link for subroutines }
  471. { Fill in method ptrs, except get_pixel_rows which start_input sets }
  472. source^.pub.start_input := start_input_tga;
  473. source^.pub.finish_input := finish_input_tga;
  474. jinit_read_targa := cjpeg_source_ptr (source);
  475. end;
  476. end. { TARGA_SUPPORTED }