jdapimin.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. Unit JdAPImin;
  2. {$N+} { Nomssi: cinfo^.output_gamma }
  3. { This file contains application interface code for the decompression half
  4. of the JPEG library. These are the "minimum" API routines that may be
  5. needed in either the normal full-decompression case or the
  6. transcoding-only case.
  7. Most of the routines intended to be called directly by an application
  8. are in this file or in jdapistd.c. But also see jcomapi.c for routines
  9. shared by compression and decompression, and jdtrans.c for the transcoding
  10. case. }
  11. { Original : jdapimin.c ; Copyright (C) 1994-1998, Thomas G. Lane. }
  12. interface
  13. {$I jconfig.inc}
  14. uses
  15. jmorecfg,
  16. jinclude,
  17. jdeferr,
  18. jerror,
  19. jpeglib,
  20. jmemmgr, jdmarker, jdinput, jcomapi;
  21. { Nomssi }
  22. procedure jpeg_create_decompress(cinfo : j_decompress_ptr);
  23. { Initialization of a JPEG decompression object.
  24. The error manager must already be set up (in case memory manager fails). }
  25. {GLOBAL}
  26. procedure jpeg_CreateDecompress (cinfo : j_decompress_ptr;
  27. version : int;
  28. structsize : size_t);
  29. { Destruction of a JPEG decompression object }
  30. {GLOBAL}
  31. procedure jpeg_destroy_decompress (cinfo : j_decompress_ptr);
  32. { Decompression startup: read start of JPEG datastream to see what's there.
  33. Need only initialize JPEG object and supply a data source before calling.
  34. This routine will read as far as the first SOS marker (ie, actual start of
  35. compressed data), and will save all tables and parameters in the JPEG
  36. object. It will also initialize the decompression parameters to default
  37. values, and finally return JPEG_HEADER_OK. On return, the application may
  38. adjust the decompression parameters and then call jpeg_start_decompress.
  39. (Or, if the application only wanted to determine the image parameters,
  40. the data need not be decompressed. In that case, call jpeg_abort or
  41. jpeg_destroy to release any temporary space.)
  42. If an abbreviated (tables only) datastream is presented, the routine will
  43. return JPEG_HEADER_TABLES_ONLY upon reaching EOI. The application may then
  44. re-use the JPEG object to read the abbreviated image datastream(s).
  45. It is unnecessary (but OK) to call jpeg_abort in this case.
  46. The JPEG_SUSPENDED return code only occurs if the data source module
  47. requests suspension of the decompressor. In this case the application
  48. should load more source data and then re-call jpeg_read_header to resume
  49. processing.
  50. If a non-suspending data source is used and require_image is TRUE, then the
  51. return code need not be inspected since only JPEG_HEADER_OK is possible.
  52. This routine is now just a front end to jpeg_consume_input, with some
  53. extra error checking. }
  54. {GLOBAL}
  55. function jpeg_read_header (cinfo : j_decompress_ptr;
  56. require_image : boolean) : int;
  57. { Consume data in advance of what the decompressor requires.
  58. This can be called at any time once the decompressor object has
  59. been created and a data source has been set up.
  60. This routine is essentially a state machine that handles a couple
  61. of critical state-transition actions, namely initial setup and
  62. transition from header scanning to ready-for-start_decompress.
  63. All the actual input is done via the input controller's consume_input
  64. method. }
  65. {GLOBAL}
  66. function jpeg_consume_input (cinfo : j_decompress_ptr) : int;
  67. { Have we finished reading the input file? }
  68. {GLOBAL}
  69. function jpeg_input_complete (cinfo : j_decompress_ptr) : boolean;
  70. { Is there more than one scan? }
  71. {GLOBAL}
  72. function jpeg_has_multiple_scans (cinfo : j_decompress_ptr) : boolean;
  73. { Finish JPEG decompression.
  74. This will normally just verify the file trailer and release temp storage.
  75. Returns FALSE if suspended. The return value need be inspected only if
  76. a suspending data source is used. }
  77. {GLOBAL}
  78. function jpeg_finish_decompress (cinfo : j_decompress_ptr) : boolean;
  79. implementation
  80. procedure jpeg_create_decompress(cinfo : j_decompress_ptr);
  81. begin
  82. jpeg_CreateDecompress(cinfo, JPEG_LIB_VERSION,
  83. size_t(sizeof(jpeg_decompress_struct)));
  84. end;
  85. { Initialization of a JPEG decompression object.
  86. The error manager must already be set up (in case memory manager fails). }
  87. {GLOBAL}
  88. procedure jpeg_CreateDecompress (cinfo : j_decompress_ptr;
  89. version : int;
  90. structsize : size_t);
  91. var
  92. i : int;
  93. var
  94. err : jpeg_error_mgr_ptr;
  95. client_data : voidp;
  96. begin
  97. { Guard against version mismatches between library and caller. }
  98. cinfo^.mem := NIL; { so jpeg_destroy knows mem mgr not called }
  99. if (version <> JPEG_LIB_VERSION) then
  100. ERREXIT2(j_common_ptr(cinfo), JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
  101. if (structsize <> SIZEOF(jpeg_decompress_struct)) then
  102. ERREXIT2(j_common_ptr(cinfo), JERR_BAD_STRUCT_SIZE,
  103. int(SIZEOF(jpeg_decompress_struct)), int(structsize));
  104. { For debugging purposes, we zero the whole master structure.
  105. But the application has already set the err pointer, and may have set
  106. client_data, so we have to save and restore those fields.
  107. Note: if application hasn't set client_data, tools like Purify may
  108. complain here. }
  109. begin
  110. err := cinfo^.err;
  111. client_data := cinfo^.client_data; { ignore Purify complaint here }
  112. MEMZERO(j_common_ptr(cinfo), SIZEOF(jpeg_decompress_struct));
  113. cinfo^.err := err;
  114. cinfo^.client_data := client_data;
  115. end;
  116. cinfo^.is_decompressor := TRUE;
  117. { Initialize a memory manager instance for this object }
  118. jinit_memory_mgr(j_common_ptr(cinfo));
  119. { Zero out pointers to permanent structures. }
  120. cinfo^.progress := NIL;
  121. cinfo^.src := NIL;
  122. for i := 0 to pred(NUM_QUANT_TBLS) do
  123. cinfo^.quant_tbl_ptrs[i] := NIL;
  124. for i := 0 to pred(NUM_HUFF_TBLS) do
  125. begin
  126. cinfo^.dc_huff_tbl_ptrs[i] := NIL;
  127. cinfo^.ac_huff_tbl_ptrs[i] := NIL;
  128. end;
  129. { Initialize marker processor so application can override methods
  130. for COM, APPn markers before calling jpeg_read_header. }
  131. cinfo^.marker_list := NIL;
  132. jinit_marker_reader(cinfo);
  133. { And initialize the overall input controller. }
  134. jinit_input_controller(cinfo);
  135. { OK, I'm ready }
  136. cinfo^.global_state := DSTATE_START;
  137. end;
  138. { Destruction of a JPEG decompression object }
  139. {GLOBAL}
  140. procedure jpeg_destroy_decompress (cinfo : j_decompress_ptr);
  141. begin
  142. jpeg_destroy(j_common_ptr(cinfo)); { use common routine }
  143. end;
  144. { Abort processing of a JPEG decompression operation,
  145. but don't destroy the object itself. }
  146. {GLOBAL}
  147. procedure jpeg_abort_decompress (cinfo : j_decompress_ptr);
  148. begin
  149. jpeg_abort(j_common_ptr(cinfo)); { use common routine }
  150. end;
  151. { Set default decompression parameters. }
  152. {LOCAL}
  153. procedure default_decompress_parms (cinfo : j_decompress_ptr);
  154. var
  155. cid0 : int;
  156. cid1 : int;
  157. cid2 : int;
  158. begin
  159. { Guess the input colorspace, and set output colorspace accordingly. }
  160. { (Wish JPEG committee had provided a real way to specify this...) }
  161. { Note application may override our guesses. }
  162. case (cinfo^.num_components) of
  163. 1: begin
  164. cinfo^.jpeg_color_space := JCS_GRAYSCALE;
  165. cinfo^.out_color_space := JCS_GRAYSCALE;
  166. end;
  167. 3: begin
  168. if (cinfo^.saw_JFIF_marker) then
  169. begin
  170. cinfo^.jpeg_color_space := JCS_YCbCr; { JFIF implies YCbCr }
  171. end
  172. else
  173. if (cinfo^.saw_Adobe_marker) then
  174. begin
  175. case (cinfo^.Adobe_transform) of
  176. 0: cinfo^.jpeg_color_space := JCS_RGB;
  177. 1: cinfo^.jpeg_color_space := JCS_YCbCr;
  178. else
  179. begin
  180. WARNMS1(j_common_ptr(cinfo), JWRN_ADOBE_XFORM, cinfo^.Adobe_transform);
  181. cinfo^.jpeg_color_space := JCS_YCbCr; { assume it's YCbCr }
  182. end;
  183. end;
  184. end
  185. else
  186. begin
  187. { Saw no special markers, try to guess from the component IDs }
  188. cid0 := cinfo^.comp_info^[0].component_id;
  189. cid1 := cinfo^.comp_info^[1].component_id;
  190. cid2 := cinfo^.comp_info^[2].component_id;
  191. if (cid0 = 1) and (cid1 = 2) and (cid2 = 3) then
  192. cinfo^.jpeg_color_space := JCS_YCbCr { assume JFIF w/out marker }
  193. else
  194. if (cid0 = 82) and (cid1 = 71) and (cid2 = 66) then
  195. cinfo^.jpeg_color_space := JCS_RGB { ASCII 'R', 'G', 'B' }
  196. else
  197. begin
  198. {$IFDEF DEBUG}
  199. TRACEMS3(j_common_ptr(cinfo), 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);
  200. {$ENDIF}
  201. cinfo^.jpeg_color_space := JCS_YCbCr; { assume it's YCbCr }
  202. end;
  203. end;
  204. { Always guess RGB is proper output colorspace. }
  205. cinfo^.out_color_space := JCS_RGB;
  206. end;
  207. 4: begin
  208. if (cinfo^.saw_Adobe_marker) then
  209. begin
  210. case (cinfo^.Adobe_transform) of
  211. 0: cinfo^.jpeg_color_space := JCS_CMYK;
  212. 2: cinfo^.jpeg_color_space := JCS_YCCK;
  213. else
  214. begin
  215. WARNMS1(j_common_ptr(cinfo), JWRN_ADOBE_XFORM, cinfo^.Adobe_transform);
  216. cinfo^.jpeg_color_space := JCS_YCCK; { assume it's YCCK }
  217. end;
  218. end;
  219. end
  220. else
  221. begin
  222. { No special markers, assume straight CMYK. }
  223. cinfo^.jpeg_color_space := JCS_CMYK;
  224. end;
  225. cinfo^.out_color_space := JCS_CMYK;
  226. end;
  227. else
  228. begin
  229. cinfo^.jpeg_color_space := JCS_UNKNOWN;
  230. cinfo^.out_color_space := JCS_UNKNOWN;
  231. end;
  232. end;
  233. { Set defaults for other decompression parameters. }
  234. cinfo^.scale_num := 1; { 1:1 scaling }
  235. cinfo^.scale_denom := 1;
  236. cinfo^.output_gamma := 1.0;
  237. cinfo^.buffered_image := FALSE;
  238. cinfo^.raw_data_out := FALSE;
  239. cinfo^.dct_method := JDCT_DEFAULT;
  240. cinfo^.do_fancy_upsampling := TRUE;
  241. cinfo^.do_block_smoothing := TRUE;
  242. cinfo^.quantize_colors := FALSE;
  243. { We set these in case application only sets quantize_colors. }
  244. cinfo^.dither_mode := JDITHER_FS;
  245. {$ifdef QUANT_2PASS_SUPPORTED}
  246. cinfo^.two_pass_quantize := TRUE;
  247. {$else}
  248. cinfo^.two_pass_quantize := FALSE;
  249. {$endif}
  250. cinfo^.desired_number_of_colors := 256;
  251. cinfo^.colormap := NIL;
  252. { Initialize for no mode change in buffered-image mode. }
  253. cinfo^.enable_1pass_quant := FALSE;
  254. cinfo^.enable_external_quant := FALSE;
  255. cinfo^.enable_2pass_quant := FALSE;
  256. end;
  257. { Decompression startup: read start of JPEG datastream to see what's there.
  258. Need only initialize JPEG object and supply a data source before calling.
  259. This routine will read as far as the first SOS marker (ie, actual start of
  260. compressed data), and will save all tables and parameters in the JPEG
  261. object. It will also initialize the decompression parameters to default
  262. values, and finally return JPEG_HEADER_OK. On return, the application may
  263. adjust the decompression parameters and then call jpeg_start_decompress.
  264. (Or, if the application only wanted to determine the image parameters,
  265. the data need not be decompressed. In that case, call jpeg_abort or
  266. jpeg_destroy to release any temporary space.)
  267. If an abbreviated (tables only) datastream is presented, the routine will
  268. return JPEG_HEADER_TABLES_ONLY upon reaching EOI. The application may then
  269. re-use the JPEG object to read the abbreviated image datastream(s).
  270. It is unnecessary (but OK) to call jpeg_abort in this case.
  271. The JPEG_SUSPENDED return code only occurs if the data source module
  272. requests suspension of the decompressor. In this case the application
  273. should load more source data and then re-call jpeg_read_header to resume
  274. processing.
  275. If a non-suspending data source is used and require_image is TRUE, then the
  276. return code need not be inspected since only JPEG_HEADER_OK is possible.
  277. This routine is now just a front end to jpeg_consume_input, with some
  278. extra error checking. }
  279. {GLOBAL}
  280. function jpeg_read_header (cinfo : j_decompress_ptr;
  281. require_image : boolean) : int;
  282. var
  283. retcode : int;
  284. begin
  285. if (cinfo^.global_state <> DSTATE_START) and
  286. (cinfo^.global_state <> DSTATE_INHEADER) then
  287. ERREXIT1(j_common_ptr(cinfo), JERR_BAD_STATE, cinfo^.global_state);
  288. retcode := jpeg_consume_input(cinfo);
  289. case (retcode) of
  290. JPEG_REACHED_SOS:
  291. retcode := JPEG_HEADER_OK;
  292. JPEG_REACHED_EOI:
  293. begin
  294. if (require_image) then { Complain if application wanted an image }
  295. ERREXIT(j_common_ptr(cinfo), JERR_NO_IMAGE);
  296. { Reset to start state; it would be safer to require the application to
  297. call jpeg_abort, but we can't change it now for compatibility reasons.
  298. A side effect is to free any temporary memory (there shouldn't be any). }
  299. jpeg_abort(j_common_ptr(cinfo)); { sets state := DSTATE_START }
  300. retcode := JPEG_HEADER_TABLES_ONLY;
  301. end;
  302. JPEG_SUSPENDED: ; { no work }
  303. end;
  304. jpeg_read_header := retcode;
  305. end;
  306. { Consume data in advance of what the decompressor requires.
  307. This can be called at any time once the decompressor object has
  308. been created and a data source has been set up.
  309. This routine is essentially a state machine that handles a couple
  310. of critical state-transition actions, namely initial setup and
  311. transition from header scanning to ready-for-start_decompress.
  312. All the actual input is done via the input controller's consume_input
  313. method. }
  314. {GLOBAL}
  315. function jpeg_consume_input (cinfo : j_decompress_ptr) : int;
  316. var
  317. retcode : int;
  318. begin
  319. retcode := JPEG_SUSPENDED;
  320. { NB: every possible DSTATE value should be listed in this switch }
  321. if (cinfo^.global_state) = DSTATE_START then
  322. begin {work around the FALLTHROUGH}
  323. { Start-of-datastream actions: reset appropriate modules }
  324. cinfo^.inputctl^.reset_input_controller (cinfo);
  325. { Initialize application's data source module }
  326. cinfo^.src^.init_source (cinfo);
  327. cinfo^.global_state := DSTATE_INHEADER;
  328. end;
  329. case (cinfo^.global_state) of
  330. DSTATE_START,
  331. DSTATE_INHEADER:
  332. begin
  333. retcode := cinfo^.inputctl^.consume_input (cinfo);
  334. if (retcode = JPEG_REACHED_SOS) then
  335. begin { Found SOS, prepare to decompress }
  336. { Set up default parameters based on header data }
  337. default_decompress_parms(cinfo);
  338. { Set global state: ready for start_decompress }
  339. cinfo^.global_state := DSTATE_READY;
  340. end;
  341. end;
  342. DSTATE_READY:
  343. { Can't advance past first SOS until start_decompress is called }
  344. retcode := JPEG_REACHED_SOS;
  345. DSTATE_PRELOAD,
  346. DSTATE_PRESCAN,
  347. DSTATE_SCANNING,
  348. DSTATE_RAW_OK,
  349. DSTATE_BUFIMAGE,
  350. DSTATE_BUFPOST,
  351. DSTATE_STOPPING:
  352. retcode := cinfo^.inputctl^.consume_input (cinfo);
  353. else
  354. ERREXIT1(j_common_ptr(cinfo), JERR_BAD_STATE, cinfo^.global_state);
  355. end;
  356. jpeg_consume_input := retcode;
  357. end;
  358. { Have we finished reading the input file? }
  359. {GLOBAL}
  360. function jpeg_input_complete (cinfo : j_decompress_ptr) : boolean;
  361. begin
  362. { Check for valid jpeg object }
  363. if (cinfo^.global_state < DSTATE_START) or
  364. (cinfo^.global_state > DSTATE_STOPPING) then
  365. ERREXIT1(j_common_ptr(cinfo), JERR_BAD_STATE, cinfo^.global_state);
  366. jpeg_input_complete := cinfo^.inputctl^.eoi_reached;
  367. end;
  368. { Is there more than one scan? }
  369. {GLOBAL}
  370. function jpeg_has_multiple_scans (cinfo : j_decompress_ptr) : boolean;
  371. begin
  372. { Only valid after jpeg_read_header completes }
  373. if (cinfo^.global_state < DSTATE_READY) or
  374. (cinfo^.global_state > DSTATE_STOPPING) then
  375. ERREXIT1(j_common_ptr(cinfo), JERR_BAD_STATE, cinfo^.global_state);
  376. jpeg_has_multiple_scans := cinfo^.inputctl^.has_multiple_scans;
  377. end;
  378. { Finish JPEG decompression.
  379. This will normally just verify the file trailer and release temp storage.
  380. Returns FALSE if suspended. The return value need be inspected only if
  381. a suspending data source is used. }
  382. {GLOBAL}
  383. function jpeg_finish_decompress (cinfo : j_decompress_ptr) : boolean;
  384. begin
  385. if ((cinfo^.global_state = DSTATE_SCANNING) or
  386. (cinfo^.global_state = DSTATE_RAW_OK) and (not cinfo^.buffered_image)) then
  387. begin
  388. { Terminate final pass of non-buffered mode }
  389. if (cinfo^.output_scanline < cinfo^.output_height) then
  390. ERREXIT(j_common_ptr(cinfo), JERR_TOO_LITTLE_DATA);
  391. cinfo^.master^.finish_output_pass (cinfo);
  392. cinfo^.global_state := DSTATE_STOPPING;
  393. end
  394. else
  395. if (cinfo^.global_state = DSTATE_BUFIMAGE) then
  396. begin
  397. { Finishing after a buffered-image operation }
  398. cinfo^.global_state := DSTATE_STOPPING;
  399. end
  400. else
  401. if (cinfo^.global_state <> DSTATE_STOPPING) then
  402. begin
  403. { STOPPING := repeat call after a suspension, anything else is error }
  404. ERREXIT1(j_common_ptr(cinfo), JERR_BAD_STATE, cinfo^.global_state);
  405. end;
  406. { Read until EOI }
  407. while (not cinfo^.inputctl^.eoi_reached) do
  408. begin
  409. if (cinfo^.inputctl^.consume_input (cinfo) = JPEG_SUSPENDED) then
  410. begin
  411. jpeg_finish_decompress := FALSE; { Suspend, come back later }
  412. exit;
  413. end;
  414. end;
  415. { Do final cleanup }
  416. cinfo^.src^.term_source (cinfo);
  417. { We can use jpeg_abort to release memory and reset global_state }
  418. jpeg_abort(j_common_ptr(cinfo));
  419. jpeg_finish_decompress := TRUE;
  420. end;
  421. end.