Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

381 lines
12 KiB

  1. #include "stdafx.h"
  2. #pragma hdrstop
  3. /*
  4. * jcapimin.c
  5. *
  6. * Copyright (C) 1994-1995, Thomas G. Lane.
  7. * This file is part of the Independent JPEG Group's software.
  8. * For conditions of distribution and use, see the accompanying README file.
  9. *
  10. * This file contains application interface code for the compression half
  11. * of the JPEG library. These are the "minimum" API routines that may be
  12. * needed in either the normal full-compression case or the transcoding-only
  13. * case.
  14. *
  15. * Most of the routines intended to be called directly by an application
  16. * are in this file or in jcapistd.c. But also see jcparam.c for
  17. * parameter-setup helper routines, jcomapi.c for routines shared by
  18. * compression and decompression, and jctrans.c for the transcoding case.
  19. */
  20. #define JPEG_INTERNALS
  21. #include "jinclude.h"
  22. #include "jpeglib.h"
  23. /*
  24. * Compression initialization.
  25. * Before calling this, all parameters and a data destination must be set up.
  26. *
  27. * We require a write_all_tables parameter as a failsafe check when writing
  28. * multiple datastreams from the same compression object. Since prior runs
  29. * will have left all the tables marked sent_table=TRUE, a subsequent run
  30. * would emit an abbreviated stream (no tables) by default. This may be what
  31. * is wanted, but for safety's sake it should not be the default behavior:
  32. * programmers should have to make a deliberate choice to emit abbreviated
  33. * images. Therefore the documentation and examples should encourage people
  34. * to pass write_all_tables=TRUE; then it will take active thought to do the
  35. * wrong thing.
  36. */
  37. GLOBAL (void)
  38. jpeg_start_compress (j_compress_ptr cinfo, boolean write_all_tables)
  39. {
  40. if (cinfo->global_state != CSTATE_START)
  41. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  42. if (write_all_tables)
  43. jpeg_suppress_tables(cinfo, FALSE); /* mark all tables to be written */
  44. /* (Re)initialize error mgr and destination modules */
  45. (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
  46. (*cinfo->dest->init_destination) (cinfo);
  47. /* Perform master selection of active modules */
  48. jinit_compress_master(cinfo);
  49. /* Set up for the first pass */
  50. (*cinfo->master->prepare_for_pass) (cinfo);
  51. /* Ready for application to drive first pass through jpeg_write_scanlines
  52. * or jpeg_write_raw_data.
  53. */
  54. cinfo->next_scanline = 0;
  55. cinfo->global_state = (cinfo->raw_data_in ? CSTATE_RAW_OK : CSTATE_SCANNING);
  56. }
  57. /*
  58. * Initialization of a JPEG compression object.
  59. * The error manager must already be set up (in case memory manager fails).
  60. */
  61. GLOBAL(void)
  62. jpeg_CreateCompress (j_compress_ptr cinfo, int version, size_t structsize)
  63. {
  64. int i;
  65. /* Guard against version mismatches between library and caller. */
  66. cinfo->mem = NULL; /* so jpeg_destroy knows mem mgr not called */
  67. if (version != JPEG_LIB_VERSION)
  68. ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
  69. if (structsize != SIZEOF(struct jpeg_compress_struct))
  70. ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE,
  71. (int) SIZEOF(struct jpeg_compress_struct), (int) structsize);
  72. /* For debugging purposes, zero the whole master structure.
  73. * But error manager pointer is already there, so save and restore it.
  74. */
  75. {
  76. struct jpeg_error_mgr * err = cinfo->err;
  77. MEMZERO(cinfo, SIZEOF(struct jpeg_compress_struct));
  78. cinfo->err = err;
  79. }
  80. cinfo->is_decompressor = FALSE;
  81. /* Initialize a memory manager instance for this object */
  82. jinit_memory_mgr((j_common_ptr) cinfo);
  83. /* Zero out pointers to permanent structures. */
  84. cinfo->progress = NULL;
  85. cinfo->dest = NULL;
  86. cinfo->comp_info = NULL;
  87. for (i = 0; i < NUM_QUANT_TBLS; i++)
  88. cinfo->quant_tbl_ptrs[i] = NULL;
  89. for (i = 0; i < NUM_HUFF_TBLS; i++) {
  90. cinfo->dc_huff_tbl_ptrs[i] = NULL;
  91. cinfo->ac_huff_tbl_ptrs[i] = NULL;
  92. }
  93. cinfo->input_gamma = 1.0; /* in case application forgets */
  94. /* OK, I'm ready */
  95. cinfo->global_state = CSTATE_START;
  96. }
  97. /*
  98. * Destruction of a JPEG compression object
  99. */
  100. GLOBAL (void)
  101. jpeg_destroy_compress (j_compress_ptr cinfo)
  102. {
  103. jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
  104. }
  105. /*
  106. * Abort processing of a JPEG compression operation,
  107. * but don't destroy the object itself.
  108. */
  109. GLOBAL (void)
  110. jpeg_abort_compress (j_compress_ptr cinfo)
  111. {
  112. jpeg_abort((j_common_ptr) cinfo); /* use common routine */
  113. }
  114. /*
  115. * Forcibly suppress or un-suppress all quantization and Huffman tables.
  116. * Marks all currently defined tables as already written (if suppress)
  117. * or not written (if !suppress). This will control whether they get emitted
  118. * by a subsequent jpeg_start_compress call.
  119. *
  120. * This routine is exported for use by applications that want to produce
  121. * abbreviated JPEG datastreams. It logically belongs in jcparam.c, but
  122. * since it is called by jpeg_start_compress, we put it here --- otherwise
  123. * jcparam.o would be linked whether the application used it or not.
  124. */
  125. GLOBAL (void)
  126. jpeg_suppress_tables (j_compress_ptr cinfo, boolean suppress)
  127. {
  128. int i;
  129. JQUANT_TBL * qtbl;
  130. JHUFF_TBL * htbl;
  131. for (i = 0; i < NUM_QUANT_TBLS; i++) {
  132. if ((qtbl = cinfo->quant_tbl_ptrs[i]) != NULL)
  133. qtbl->sent_table = suppress;
  134. }
  135. for (i = 0; i < NUM_HUFF_TBLS; i++) {
  136. if ((htbl = cinfo->dc_huff_tbl_ptrs[i]) != NULL)
  137. htbl->sent_table = suppress;
  138. if ((htbl = cinfo->ac_huff_tbl_ptrs[i]) != NULL)
  139. htbl->sent_table = suppress;
  140. }
  141. }
  142. /*
  143. * Write some scanlines of data to the JPEG compressor.
  144. *
  145. * The return value will be the number of lines actually written.
  146. * This should be less than the supplied num_lines only in case that
  147. * the data destination module has requested suspension of the compressor,
  148. * or if more than image_height scanlines are passed in.
  149. *
  150. * Note: we warn about excess calls to jpeg_write_scanlines() since
  151. * this likely signals an application programmer error. However,
  152. * excess scanlines passed in the last valid call are *silently* ignored,
  153. * so that the application need not adjust num_lines for end-of-image
  154. * when using a multiple-scanline buffer.
  155. */
  156. GLOBAL (JDIMENSION)
  157. jpeg_write_scanlines (j_compress_ptr cinfo, JSAMPARRAY scanlines,
  158. JDIMENSION num_lines)
  159. {
  160. JDIMENSION row_ctr, rows_left;
  161. if (cinfo->global_state != CSTATE_SCANNING)
  162. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  163. if (cinfo->next_scanline >= cinfo->image_height)
  164. WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
  165. /* Call progress monitor hook if present */
  166. if (cinfo->progress != NULL) {
  167. cinfo->progress->pass_counter = (long) cinfo->next_scanline;
  168. cinfo->progress->pass_limit = (long) cinfo->image_height;
  169. (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
  170. }
  171. /* Give master control module another chance if this is first call to
  172. * jpeg_write_scanlines. This lets output of the frame/scan headers be
  173. * delayed so that application can write COM, etc, markers between
  174. * jpeg_start_compress and jpeg_write_scanlines.
  175. */
  176. if (cinfo->master->call_pass_startup)
  177. (*cinfo->master->pass_startup) (cinfo);
  178. /* Ignore any extra scanlines at bottom of image. */
  179. rows_left = cinfo->image_height - cinfo->next_scanline;
  180. if (num_lines > rows_left)
  181. num_lines = rows_left;
  182. row_ctr = 0;
  183. (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, num_lines);
  184. cinfo->next_scanline += row_ctr;
  185. return row_ctr;
  186. }
  187. /*
  188. * Alternate entry point to write raw data.
  189. * Processes exactly one iMCU row per call, unless suspended.
  190. */
  191. GLOBAL (JDIMENSION)
  192. jpeg_write_raw_data (j_compress_ptr cinfo, JSAMPIMAGE data,
  193. JDIMENSION num_lines)
  194. {
  195. JDIMENSION lines_per_iMCU_row;
  196. if (cinfo->global_state != CSTATE_RAW_OK)
  197. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  198. if (cinfo->next_scanline >= cinfo->image_height) {
  199. WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
  200. return 0;
  201. }
  202. /* Call progress monitor hook if present */
  203. if (cinfo->progress != NULL) {
  204. cinfo->progress->pass_counter = (long) cinfo->next_scanline;
  205. cinfo->progress->pass_limit = (long) cinfo->image_height;
  206. (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
  207. }
  208. /* Give master control module another chance if this is first call to
  209. * jpeg_write_raw_data. This lets output of the frame/scan headers be
  210. * delayed so that application can write COM, etc, markers between
  211. * jpeg_start_compress and jpeg_write_raw_data.
  212. */
  213. if (cinfo->master->call_pass_startup)
  214. (*cinfo->master->pass_startup) (cinfo);
  215. /* Verify that at least one iMCU row has been passed. */
  216. lines_per_iMCU_row = cinfo->max_v_samp_factor * DCTSIZE;
  217. if (num_lines < lines_per_iMCU_row)
  218. ERREXIT(cinfo, JERR_BUFFER_SIZE);
  219. /* Directly compress the row. */
  220. if (! (*cinfo->coef->compress_data) (cinfo, data)) {
  221. /* If compressor did not consume the whole row, suspend processing. */
  222. return 0;
  223. }
  224. /* OK, we processed one iMCU row. */
  225. cinfo->next_scanline += lines_per_iMCU_row;
  226. return lines_per_iMCU_row;
  227. }
  228. /*
  229. * Finish JPEG compression.
  230. *
  231. * If a multipass operating mode was selected, this may do a great deal of
  232. * work including most of the actual output.
  233. */
  234. GLOBAL (void)
  235. jpeg_finish_compress (j_compress_ptr cinfo)
  236. {
  237. JDIMENSION iMCU_row;
  238. if (cinfo->global_state == CSTATE_SCANNING ||
  239. cinfo->global_state == CSTATE_RAW_OK) {
  240. /* Terminate first pass */
  241. if (cinfo->next_scanline < cinfo->image_height)
  242. ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
  243. (*cinfo->master->finish_pass) (cinfo);
  244. } else if (cinfo->global_state != CSTATE_WRCOEFS)
  245. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  246. /* Perform any remaining passes */
  247. while (! cinfo->master->is_last_pass) {
  248. (*cinfo->master->prepare_for_pass) (cinfo);
  249. for (iMCU_row = 0; iMCU_row < cinfo->total_iMCU_rows; iMCU_row++) {
  250. if (cinfo->progress != NULL) {
  251. cinfo->progress->pass_counter = (long) iMCU_row;
  252. cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows;
  253. (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
  254. }
  255. /* We bypass the main controller and invoke coef controller directly;
  256. * all work is being done from the coefficient buffer.
  257. */
  258. if (! (*cinfo->coef->compress_data) (cinfo, (JSAMPIMAGE) NULL))
  259. ERREXIT(cinfo, JERR_CANT_SUSPEND);
  260. }
  261. (*cinfo->master->finish_pass) (cinfo);
  262. }
  263. /* Write EOI, do final cleanup */
  264. (*cinfo->marker->write_file_trailer) (cinfo);
  265. (*cinfo->dest->term_destination) (cinfo);
  266. /* We can use jpeg_abort to release memory and reset global_state */
  267. jpeg_abort((j_common_ptr) cinfo);
  268. }
  269. /*
  270. * Write a special marker.
  271. * This is only recommended for writing COM or APPn markers.
  272. * Must be called after jpeg_start_compress() and before
  273. * first call to jpeg_write_scanlines() or jpeg_write_raw_data().
  274. */
  275. GLOBAL (void)
  276. jpeg_write_marker (j_compress_ptr cinfo, int marker,
  277. const JOCTET *dataptr, unsigned int datalen)
  278. {
  279. if (cinfo->next_scanline != 0 ||
  280. (cinfo->global_state != CSTATE_SCANNING &&
  281. cinfo->global_state != CSTATE_RAW_OK &&
  282. cinfo->global_state != CSTATE_WRCOEFS))
  283. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  284. (*cinfo->marker->write_any_marker) (cinfo, marker, dataptr, datalen);
  285. }
  286. /*
  287. * Alternate compression function: just write an abbreviated table file.
  288. * Before calling this, all parameters and a data destination must be set up.
  289. *
  290. * To produce a pair of files containing abbreviated tables and abbreviated
  291. * image data, one would proceed as follows:
  292. *
  293. * initialize JPEG object
  294. * set JPEG parameters
  295. * set destination to table file
  296. * jpeg_write_tables(cinfo);
  297. * set destination to image file
  298. * jpeg_start_compress(cinfo, FALSE);
  299. * write data...
  300. * jpeg_finish_compress(cinfo);
  301. *
  302. * jpeg_write_tables has the side effect of marking all tables written
  303. * (same as jpeg_suppress_tables(..., TRUE)). Thus a subsequent start_compress
  304. * will not re-emit the tables unless it is passed write_all_tables=TRUE.
  305. */
  306. GLOBAL (void)
  307. jpeg_write_tables (j_compress_ptr cinfo)
  308. {
  309. if (cinfo->global_state != CSTATE_START)
  310. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  311. /* (Re)initialize error mgr and destination modules */
  312. (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
  313. (*cinfo->dest->init_destination) (cinfo);
  314. /* Initialize the marker writer ... bit of a crock to do it here. */
  315. jinit_marker_writer(cinfo);
  316. /* Write them tables! */
  317. (*cinfo->marker->write_tables_only) (cinfo);
  318. /* And clean up. */
  319. (*cinfo->dest->term_destination) (cinfo);
  320. /* We can use jpeg_abort to release memory. */
  321. #ifndef NIFTY
  322. jpeg_abort((j_common_ptr) cinfo);
  323. #endif
  324. }