PageRenderTime 57ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/src/FreeImage/Source/LibJPEG/transupp.c

https://bitbucket.org/cabalistic/ogredeps/
C | 1597 lines | 1153 code | 105 blank | 339 comment | 333 complexity | 84258ade6bb2fab4c60d5f426edcf963 MD5 | raw file
Possible License(s): LGPL-3.0, BSD-3-Clause, CPL-1.0, Unlicense, GPL-2.0, GPL-3.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, BSD-2-Clause, LGPL-2.1
  1. /*
  2. * transupp.c
  3. *
  4. * Copyright (C) 1997-2011, Thomas G. Lane, Guido Vollbeding.
  5. * This file is part of the Independent JPEG Group's software.
  6. * For conditions of distribution and use, see the accompanying README file.
  7. *
  8. * This file contains image transformation routines and other utility code
  9. * used by the jpegtran sample application. These are NOT part of the core
  10. * JPEG library. But we keep these routines separate from jpegtran.c to
  11. * ease the task of maintaining jpegtran-like programs that have other user
  12. * interfaces.
  13. */
  14. /* Although this file really shouldn't have access to the library internals,
  15. * it's helpful to let it call jround_up() and jcopy_block_row().
  16. */
  17. #define JPEG_INTERNALS
  18. #include "jinclude.h"
  19. #include "jpeglib.h"
  20. #include "transupp.h" /* My own external interface */
  21. #include <ctype.h> /* to declare isdigit() */
  22. #if TRANSFORMS_SUPPORTED
  23. /*
  24. * Lossless image transformation routines. These routines work on DCT
  25. * coefficient arrays and thus do not require any lossy decompression
  26. * or recompression of the image.
  27. * Thanks to Guido Vollbeding for the initial design and code of this feature,
  28. * and to Ben Jackson for introducing the cropping feature.
  29. *
  30. * Horizontal flipping is done in-place, using a single top-to-bottom
  31. * pass through the virtual source array. It will thus be much the
  32. * fastest option for images larger than main memory.
  33. *
  34. * The other routines require a set of destination virtual arrays, so they
  35. * need twice as much memory as jpegtran normally does. The destination
  36. * arrays are always written in normal scan order (top to bottom) because
  37. * the virtual array manager expects this. The source arrays will be scanned
  38. * in the corresponding order, which means multiple passes through the source
  39. * arrays for most of the transforms. That could result in much thrashing
  40. * if the image is larger than main memory.
  41. *
  42. * If cropping or trimming is involved, the destination arrays may be smaller
  43. * than the source arrays. Note it is not possible to do horizontal flip
  44. * in-place when a nonzero Y crop offset is specified, since we'd have to move
  45. * data from one block row to another but the virtual array manager doesn't
  46. * guarantee we can touch more than one row at a time. So in that case,
  47. * we have to use a separate destination array.
  48. *
  49. * Some notes about the operating environment of the individual transform
  50. * routines:
  51. * 1. Both the source and destination virtual arrays are allocated from the
  52. * source JPEG object, and therefore should be manipulated by calling the
  53. * source's memory manager.
  54. * 2. The destination's component count should be used. It may be smaller
  55. * than the source's when forcing to grayscale.
  56. * 3. Likewise the destination's sampling factors should be used. When
  57. * forcing to grayscale the destination's sampling factors will be all 1,
  58. * and we may as well take that as the effective iMCU size.
  59. * 4. When "trim" is in effect, the destination's dimensions will be the
  60. * trimmed values but the source's will be untrimmed.
  61. * 5. When "crop" is in effect, the destination's dimensions will be the
  62. * cropped values but the source's will be uncropped. Each transform
  63. * routine is responsible for picking up source data starting at the
  64. * correct X and Y offset for the crop region. (The X and Y offsets
  65. * passed to the transform routines are measured in iMCU blocks of the
  66. * destination.)
  67. * 6. All the routines assume that the source and destination buffers are
  68. * padded out to a full iMCU boundary. This is true, although for the
  69. * source buffer it is an undocumented property of jdcoefct.c.
  70. */
  71. LOCAL(void)
  72. do_crop (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
  73. JDIMENSION x_crop_offset, JDIMENSION y_crop_offset,
  74. jvirt_barray_ptr *src_coef_arrays,
  75. jvirt_barray_ptr *dst_coef_arrays)
  76. /* Crop. This is only used when no rotate/flip is requested with the crop. */
  77. {
  78. JDIMENSION dst_blk_y, x_crop_blocks, y_crop_blocks;
  79. int ci, offset_y;
  80. JBLOCKARRAY src_buffer, dst_buffer;
  81. jpeg_component_info *compptr;
  82. /* We simply have to copy the right amount of data (the destination's
  83. * image size) starting at the given X and Y offsets in the source.
  84. */
  85. for (ci = 0; ci < dstinfo->num_components; ci++) {
  86. compptr = dstinfo->comp_info + ci;
  87. x_crop_blocks = x_crop_offset * compptr->h_samp_factor;
  88. y_crop_blocks = y_crop_offset * compptr->v_samp_factor;
  89. for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks;
  90. dst_blk_y += compptr->v_samp_factor) {
  91. dst_buffer = (*srcinfo->mem->access_virt_barray)
  92. ((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y,
  93. (JDIMENSION) compptr->v_samp_factor, TRUE);
  94. src_buffer = (*srcinfo->mem->access_virt_barray)
  95. ((j_common_ptr) srcinfo, src_coef_arrays[ci],
  96. dst_blk_y + y_crop_blocks,
  97. (JDIMENSION) compptr->v_samp_factor, FALSE);
  98. for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
  99. jcopy_block_row(src_buffer[offset_y] + x_crop_blocks,
  100. dst_buffer[offset_y],
  101. compptr->width_in_blocks);
  102. }
  103. }
  104. }
  105. }
  106. LOCAL(void)
  107. do_flip_h_no_crop (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
  108. JDIMENSION x_crop_offset,
  109. jvirt_barray_ptr *src_coef_arrays)
  110. /* Horizontal flip; done in-place, so no separate dest array is required.
  111. * NB: this only works when y_crop_offset is zero.
  112. */
  113. {
  114. JDIMENSION MCU_cols, comp_width, blk_x, blk_y, x_crop_blocks;
  115. int ci, k, offset_y;
  116. JBLOCKARRAY buffer;
  117. JCOEFPTR ptr1, ptr2;
  118. JCOEF temp1, temp2;
  119. jpeg_component_info *compptr;
  120. /* Horizontal mirroring of DCT blocks is accomplished by swapping
  121. * pairs of blocks in-place. Within a DCT block, we perform horizontal
  122. * mirroring by changing the signs of odd-numbered columns.
  123. * Partial iMCUs at the right edge are left untouched.
  124. */
  125. MCU_cols = srcinfo->output_width /
  126. (dstinfo->max_h_samp_factor * dstinfo->min_DCT_h_scaled_size);
  127. for (ci = 0; ci < dstinfo->num_components; ci++) {
  128. compptr = dstinfo->comp_info + ci;
  129. comp_width = MCU_cols * compptr->h_samp_factor;
  130. x_crop_blocks = x_crop_offset * compptr->h_samp_factor;
  131. for (blk_y = 0; blk_y < compptr->height_in_blocks;
  132. blk_y += compptr->v_samp_factor) {
  133. buffer = (*srcinfo->mem->access_virt_barray)
  134. ((j_common_ptr) srcinfo, src_coef_arrays[ci], blk_y,
  135. (JDIMENSION) compptr->v_samp_factor, TRUE);
  136. for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
  137. /* Do the mirroring */
  138. for (blk_x = 0; blk_x * 2 < comp_width; blk_x++) {
  139. ptr1 = buffer[offset_y][blk_x];
  140. ptr2 = buffer[offset_y][comp_width - blk_x - 1];
  141. /* this unrolled loop doesn't need to know which row it's on... */
  142. for (k = 0; k < DCTSIZE2; k += 2) {
  143. temp1 = *ptr1; /* swap even column */
  144. temp2 = *ptr2;
  145. *ptr1++ = temp2;
  146. *ptr2++ = temp1;
  147. temp1 = *ptr1; /* swap odd column with sign change */
  148. temp2 = *ptr2;
  149. *ptr1++ = -temp2;
  150. *ptr2++ = -temp1;
  151. }
  152. }
  153. if (x_crop_blocks > 0) {
  154. /* Now left-justify the portion of the data to be kept.
  155. * We can't use a single jcopy_block_row() call because that routine
  156. * depends on memcpy(), whose behavior is unspecified for overlapping
  157. * source and destination areas. Sigh.
  158. */
  159. for (blk_x = 0; blk_x < compptr->width_in_blocks; blk_x++) {
  160. jcopy_block_row(buffer[offset_y] + blk_x + x_crop_blocks,
  161. buffer[offset_y] + blk_x,
  162. (JDIMENSION) 1);
  163. }
  164. }
  165. }
  166. }
  167. }
  168. }
  169. LOCAL(void)
  170. do_flip_h (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
  171. JDIMENSION x_crop_offset, JDIMENSION y_crop_offset,
  172. jvirt_barray_ptr *src_coef_arrays,
  173. jvirt_barray_ptr *dst_coef_arrays)
  174. /* Horizontal flip in general cropping case */
  175. {
  176. JDIMENSION MCU_cols, comp_width, dst_blk_x, dst_blk_y;
  177. JDIMENSION x_crop_blocks, y_crop_blocks;
  178. int ci, k, offset_y;
  179. JBLOCKARRAY src_buffer, dst_buffer;
  180. JBLOCKROW src_row_ptr, dst_row_ptr;
  181. JCOEFPTR src_ptr, dst_ptr;
  182. jpeg_component_info *compptr;
  183. /* Here we must output into a separate array because we can't touch
  184. * different rows of a single virtual array simultaneously. Otherwise,
  185. * this is essentially the same as the routine above.
  186. */
  187. MCU_cols = srcinfo->output_width /
  188. (dstinfo->max_h_samp_factor * dstinfo->min_DCT_h_scaled_size);
  189. for (ci = 0; ci < dstinfo->num_components; ci++) {
  190. compptr = dstinfo->comp_info + ci;
  191. comp_width = MCU_cols * compptr->h_samp_factor;
  192. x_crop_blocks = x_crop_offset * compptr->h_samp_factor;
  193. y_crop_blocks = y_crop_offset * compptr->v_samp_factor;
  194. for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks;
  195. dst_blk_y += compptr->v_samp_factor) {
  196. dst_buffer = (*srcinfo->mem->access_virt_barray)
  197. ((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y,
  198. (JDIMENSION) compptr->v_samp_factor, TRUE);
  199. src_buffer = (*srcinfo->mem->access_virt_barray)
  200. ((j_common_ptr) srcinfo, src_coef_arrays[ci],
  201. dst_blk_y + y_crop_blocks,
  202. (JDIMENSION) compptr->v_samp_factor, FALSE);
  203. for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
  204. dst_row_ptr = dst_buffer[offset_y];
  205. src_row_ptr = src_buffer[offset_y];
  206. for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks; dst_blk_x++) {
  207. if (x_crop_blocks + dst_blk_x < comp_width) {
  208. /* Do the mirrorable blocks */
  209. dst_ptr = dst_row_ptr[dst_blk_x];
  210. src_ptr = src_row_ptr[comp_width - x_crop_blocks - dst_blk_x - 1];
  211. /* this unrolled loop doesn't need to know which row it's on... */
  212. for (k = 0; k < DCTSIZE2; k += 2) {
  213. *dst_ptr++ = *src_ptr++; /* copy even column */
  214. *dst_ptr++ = - *src_ptr++; /* copy odd column with sign change */
  215. }
  216. } else {
  217. /* Copy last partial block(s) verbatim */
  218. jcopy_block_row(src_row_ptr + dst_blk_x + x_crop_blocks,
  219. dst_row_ptr + dst_blk_x,
  220. (JDIMENSION) 1);
  221. }
  222. }
  223. }
  224. }
  225. }
  226. }
  227. LOCAL(void)
  228. do_flip_v (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
  229. JDIMENSION x_crop_offset, JDIMENSION y_crop_offset,
  230. jvirt_barray_ptr *src_coef_arrays,
  231. jvirt_barray_ptr *dst_coef_arrays)
  232. /* Vertical flip */
  233. {
  234. JDIMENSION MCU_rows, comp_height, dst_blk_x, dst_blk_y;
  235. JDIMENSION x_crop_blocks, y_crop_blocks;
  236. int ci, i, j, offset_y;
  237. JBLOCKARRAY src_buffer, dst_buffer;
  238. JBLOCKROW src_row_ptr, dst_row_ptr;
  239. JCOEFPTR src_ptr, dst_ptr;
  240. jpeg_component_info *compptr;
  241. /* We output into a separate array because we can't touch different
  242. * rows of the source virtual array simultaneously. Otherwise, this
  243. * is a pretty straightforward analog of horizontal flip.
  244. * Within a DCT block, vertical mirroring is done by changing the signs
  245. * of odd-numbered rows.
  246. * Partial iMCUs at the bottom edge are copied verbatim.
  247. */
  248. MCU_rows = srcinfo->output_height /
  249. (dstinfo->max_v_samp_factor * dstinfo->min_DCT_v_scaled_size);
  250. for (ci = 0; ci < dstinfo->num_components; ci++) {
  251. compptr = dstinfo->comp_info + ci;
  252. comp_height = MCU_rows * compptr->v_samp_factor;
  253. x_crop_blocks = x_crop_offset * compptr->h_samp_factor;
  254. y_crop_blocks = y_crop_offset * compptr->v_samp_factor;
  255. for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks;
  256. dst_blk_y += compptr->v_samp_factor) {
  257. dst_buffer = (*srcinfo->mem->access_virt_barray)
  258. ((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y,
  259. (JDIMENSION) compptr->v_samp_factor, TRUE);
  260. if (y_crop_blocks + dst_blk_y < comp_height) {
  261. /* Row is within the mirrorable area. */
  262. src_buffer = (*srcinfo->mem->access_virt_barray)
  263. ((j_common_ptr) srcinfo, src_coef_arrays[ci],
  264. comp_height - y_crop_blocks - dst_blk_y -
  265. (JDIMENSION) compptr->v_samp_factor,
  266. (JDIMENSION) compptr->v_samp_factor, FALSE);
  267. } else {
  268. /* Bottom-edge blocks will be copied verbatim. */
  269. src_buffer = (*srcinfo->mem->access_virt_barray)
  270. ((j_common_ptr) srcinfo, src_coef_arrays[ci],
  271. dst_blk_y + y_crop_blocks,
  272. (JDIMENSION) compptr->v_samp_factor, FALSE);
  273. }
  274. for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
  275. if (y_crop_blocks + dst_blk_y < comp_height) {
  276. /* Row is within the mirrorable area. */
  277. dst_row_ptr = dst_buffer[offset_y];
  278. src_row_ptr = src_buffer[compptr->v_samp_factor - offset_y - 1];
  279. src_row_ptr += x_crop_blocks;
  280. for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks;
  281. dst_blk_x++) {
  282. dst_ptr = dst_row_ptr[dst_blk_x];
  283. src_ptr = src_row_ptr[dst_blk_x];
  284. for (i = 0; i < DCTSIZE; i += 2) {
  285. /* copy even row */
  286. for (j = 0; j < DCTSIZE; j++)
  287. *dst_ptr++ = *src_ptr++;
  288. /* copy odd row with sign change */
  289. for (j = 0; j < DCTSIZE; j++)
  290. *dst_ptr++ = - *src_ptr++;
  291. }
  292. }
  293. } else {
  294. /* Just copy row verbatim. */
  295. jcopy_block_row(src_buffer[offset_y] + x_crop_blocks,
  296. dst_buffer[offset_y],
  297. compptr->width_in_blocks);
  298. }
  299. }
  300. }
  301. }
  302. }
  303. LOCAL(void)
  304. do_transpose (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
  305. JDIMENSION x_crop_offset, JDIMENSION y_crop_offset,
  306. jvirt_barray_ptr *src_coef_arrays,
  307. jvirt_barray_ptr *dst_coef_arrays)
  308. /* Transpose source into destination */
  309. {
  310. JDIMENSION dst_blk_x, dst_blk_y, x_crop_blocks, y_crop_blocks;
  311. int ci, i, j, offset_x, offset_y;
  312. JBLOCKARRAY src_buffer, dst_buffer;
  313. JCOEFPTR src_ptr, dst_ptr;
  314. jpeg_component_info *compptr;
  315. /* Transposing pixels within a block just requires transposing the
  316. * DCT coefficients.
  317. * Partial iMCUs at the edges require no special treatment; we simply
  318. * process all the available DCT blocks for every component.
  319. */
  320. for (ci = 0; ci < dstinfo->num_components; ci++) {
  321. compptr = dstinfo->comp_info + ci;
  322. x_crop_blocks = x_crop_offset * compptr->h_samp_factor;
  323. y_crop_blocks = y_crop_offset * compptr->v_samp_factor;
  324. for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks;
  325. dst_blk_y += compptr->v_samp_factor) {
  326. dst_buffer = (*srcinfo->mem->access_virt_barray)
  327. ((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y,
  328. (JDIMENSION) compptr->v_samp_factor, TRUE);
  329. for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
  330. for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks;
  331. dst_blk_x += compptr->h_samp_factor) {
  332. src_buffer = (*srcinfo->mem->access_virt_barray)
  333. ((j_common_ptr) srcinfo, src_coef_arrays[ci],
  334. dst_blk_x + x_crop_blocks,
  335. (JDIMENSION) compptr->h_samp_factor, FALSE);
  336. for (offset_x = 0; offset_x < compptr->h_samp_factor; offset_x++) {
  337. dst_ptr = dst_buffer[offset_y][dst_blk_x + offset_x];
  338. src_ptr = src_buffer[offset_x][dst_blk_y + offset_y + y_crop_blocks];
  339. for (i = 0; i < DCTSIZE; i++)
  340. for (j = 0; j < DCTSIZE; j++)
  341. dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j];
  342. }
  343. }
  344. }
  345. }
  346. }
  347. }
  348. LOCAL(void)
  349. do_rot_90 (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
  350. JDIMENSION x_crop_offset, JDIMENSION y_crop_offset,
  351. jvirt_barray_ptr *src_coef_arrays,
  352. jvirt_barray_ptr *dst_coef_arrays)
  353. /* 90 degree rotation is equivalent to
  354. * 1. Transposing the image;
  355. * 2. Horizontal mirroring.
  356. * These two steps are merged into a single processing routine.
  357. */
  358. {
  359. JDIMENSION MCU_cols, comp_width, dst_blk_x, dst_blk_y;
  360. JDIMENSION x_crop_blocks, y_crop_blocks;
  361. int ci, i, j, offset_x, offset_y;
  362. JBLOCKARRAY src_buffer, dst_buffer;
  363. JCOEFPTR src_ptr, dst_ptr;
  364. jpeg_component_info *compptr;
  365. /* Because of the horizontal mirror step, we can't process partial iMCUs
  366. * at the (output) right edge properly. They just get transposed and
  367. * not mirrored.
  368. */
  369. MCU_cols = srcinfo->output_height /
  370. (dstinfo->max_h_samp_factor * dstinfo->min_DCT_h_scaled_size);
  371. for (ci = 0; ci < dstinfo->num_components; ci++) {
  372. compptr = dstinfo->comp_info + ci;
  373. comp_width = MCU_cols * compptr->h_samp_factor;
  374. x_crop_blocks = x_crop_offset * compptr->h_samp_factor;
  375. y_crop_blocks = y_crop_offset * compptr->v_samp_factor;
  376. for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks;
  377. dst_blk_y += compptr->v_samp_factor) {
  378. dst_buffer = (*srcinfo->mem->access_virt_barray)
  379. ((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y,
  380. (JDIMENSION) compptr->v_samp_factor, TRUE);
  381. for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
  382. for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks;
  383. dst_blk_x += compptr->h_samp_factor) {
  384. if (x_crop_blocks + dst_blk_x < comp_width) {
  385. /* Block is within the mirrorable area. */
  386. src_buffer = (*srcinfo->mem->access_virt_barray)
  387. ((j_common_ptr) srcinfo, src_coef_arrays[ci],
  388. comp_width - x_crop_blocks - dst_blk_x -
  389. (JDIMENSION) compptr->h_samp_factor,
  390. (JDIMENSION) compptr->h_samp_factor, FALSE);
  391. } else {
  392. /* Edge blocks are transposed but not mirrored. */
  393. src_buffer = (*srcinfo->mem->access_virt_barray)
  394. ((j_common_ptr) srcinfo, src_coef_arrays[ci],
  395. dst_blk_x + x_crop_blocks,
  396. (JDIMENSION) compptr->h_samp_factor, FALSE);
  397. }
  398. for (offset_x = 0; offset_x < compptr->h_samp_factor; offset_x++) {
  399. dst_ptr = dst_buffer[offset_y][dst_blk_x + offset_x];
  400. if (x_crop_blocks + dst_blk_x < comp_width) {
  401. /* Block is within the mirrorable area. */
  402. src_ptr = src_buffer[compptr->h_samp_factor - offset_x - 1]
  403. [dst_blk_y + offset_y + y_crop_blocks];
  404. for (i = 0; i < DCTSIZE; i++) {
  405. for (j = 0; j < DCTSIZE; j++)
  406. dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j];
  407. i++;
  408. for (j = 0; j < DCTSIZE; j++)
  409. dst_ptr[j*DCTSIZE+i] = -src_ptr[i*DCTSIZE+j];
  410. }
  411. } else {
  412. /* Edge blocks are transposed but not mirrored. */
  413. src_ptr = src_buffer[offset_x]
  414. [dst_blk_y + offset_y + y_crop_blocks];
  415. for (i = 0; i < DCTSIZE; i++)
  416. for (j = 0; j < DCTSIZE; j++)
  417. dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j];
  418. }
  419. }
  420. }
  421. }
  422. }
  423. }
  424. }
  425. LOCAL(void)
  426. do_rot_270 (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
  427. JDIMENSION x_crop_offset, JDIMENSION y_crop_offset,
  428. jvirt_barray_ptr *src_coef_arrays,
  429. jvirt_barray_ptr *dst_coef_arrays)
  430. /* 270 degree rotation is equivalent to
  431. * 1. Horizontal mirroring;
  432. * 2. Transposing the image.
  433. * These two steps are merged into a single processing routine.
  434. */
  435. {
  436. JDIMENSION MCU_rows, comp_height, dst_blk_x, dst_blk_y;
  437. JDIMENSION x_crop_blocks, y_crop_blocks;
  438. int ci, i, j, offset_x, offset_y;
  439. JBLOCKARRAY src_buffer, dst_buffer;
  440. JCOEFPTR src_ptr, dst_ptr;
  441. jpeg_component_info *compptr;
  442. /* Because of the horizontal mirror step, we can't process partial iMCUs
  443. * at the (output) bottom edge properly. They just get transposed and
  444. * not mirrored.
  445. */
  446. MCU_rows = srcinfo->output_width /
  447. (dstinfo->max_v_samp_factor * dstinfo->min_DCT_v_scaled_size);
  448. for (ci = 0; ci < dstinfo->num_components; ci++) {
  449. compptr = dstinfo->comp_info + ci;
  450. comp_height = MCU_rows * compptr->v_samp_factor;
  451. x_crop_blocks = x_crop_offset * compptr->h_samp_factor;
  452. y_crop_blocks = y_crop_offset * compptr->v_samp_factor;
  453. for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks;
  454. dst_blk_y += compptr->v_samp_factor) {
  455. dst_buffer = (*srcinfo->mem->access_virt_barray)
  456. ((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y,
  457. (JDIMENSION) compptr->v_samp_factor, TRUE);
  458. for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
  459. for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks;
  460. dst_blk_x += compptr->h_samp_factor) {
  461. src_buffer = (*srcinfo->mem->access_virt_barray)
  462. ((j_common_ptr) srcinfo, src_coef_arrays[ci],
  463. dst_blk_x + x_crop_blocks,
  464. (JDIMENSION) compptr->h_samp_factor, FALSE);
  465. for (offset_x = 0; offset_x < compptr->h_samp_factor; offset_x++) {
  466. dst_ptr = dst_buffer[offset_y][dst_blk_x + offset_x];
  467. if (y_crop_blocks + dst_blk_y < comp_height) {
  468. /* Block is within the mirrorable area. */
  469. src_ptr = src_buffer[offset_x]
  470. [comp_height - y_crop_blocks - dst_blk_y - offset_y - 1];
  471. for (i = 0; i < DCTSIZE; i++) {
  472. for (j = 0; j < DCTSIZE; j++) {
  473. dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j];
  474. j++;
  475. dst_ptr[j*DCTSIZE+i] = -src_ptr[i*DCTSIZE+j];
  476. }
  477. }
  478. } else {
  479. /* Edge blocks are transposed but not mirrored. */
  480. src_ptr = src_buffer[offset_x]
  481. [dst_blk_y + offset_y + y_crop_blocks];
  482. for (i = 0; i < DCTSIZE; i++)
  483. for (j = 0; j < DCTSIZE; j++)
  484. dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j];
  485. }
  486. }
  487. }
  488. }
  489. }
  490. }
  491. }
  492. LOCAL(void)
  493. do_rot_180 (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
  494. JDIMENSION x_crop_offset, JDIMENSION y_crop_offset,
  495. jvirt_barray_ptr *src_coef_arrays,
  496. jvirt_barray_ptr *dst_coef_arrays)
  497. /* 180 degree rotation is equivalent to
  498. * 1. Vertical mirroring;
  499. * 2. Horizontal mirroring.
  500. * These two steps are merged into a single processing routine.
  501. */
  502. {
  503. JDIMENSION MCU_cols, MCU_rows, comp_width, comp_height, dst_blk_x, dst_blk_y;
  504. JDIMENSION x_crop_blocks, y_crop_blocks;
  505. int ci, i, j, offset_y;
  506. JBLOCKARRAY src_buffer, dst_buffer;
  507. JBLOCKROW src_row_ptr, dst_row_ptr;
  508. JCOEFPTR src_ptr, dst_ptr;
  509. jpeg_component_info *compptr;
  510. MCU_cols = srcinfo->output_width /
  511. (dstinfo->max_h_samp_factor * dstinfo->min_DCT_h_scaled_size);
  512. MCU_rows = srcinfo->output_height /
  513. (dstinfo->max_v_samp_factor * dstinfo->min_DCT_v_scaled_size);
  514. for (ci = 0; ci < dstinfo->num_components; ci++) {
  515. compptr = dstinfo->comp_info + ci;
  516. comp_width = MCU_cols * compptr->h_samp_factor;
  517. comp_height = MCU_rows * compptr->v_samp_factor;
  518. x_crop_blocks = x_crop_offset * compptr->h_samp_factor;
  519. y_crop_blocks = y_crop_offset * compptr->v_samp_factor;
  520. for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks;
  521. dst_blk_y += compptr->v_samp_factor) {
  522. dst_buffer = (*srcinfo->mem->access_virt_barray)
  523. ((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y,
  524. (JDIMENSION) compptr->v_samp_factor, TRUE);
  525. if (y_crop_blocks + dst_blk_y < comp_height) {
  526. /* Row is within the vertically mirrorable area. */
  527. src_buffer = (*srcinfo->mem->access_virt_barray)
  528. ((j_common_ptr) srcinfo, src_coef_arrays[ci],
  529. comp_height - y_crop_blocks - dst_blk_y -
  530. (JDIMENSION) compptr->v_samp_factor,
  531. (JDIMENSION) compptr->v_samp_factor, FALSE);
  532. } else {
  533. /* Bottom-edge rows are only mirrored horizontally. */
  534. src_buffer = (*srcinfo->mem->access_virt_barray)
  535. ((j_common_ptr) srcinfo, src_coef_arrays[ci],
  536. dst_blk_y + y_crop_blocks,
  537. (JDIMENSION) compptr->v_samp_factor, FALSE);
  538. }
  539. for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
  540. dst_row_ptr = dst_buffer[offset_y];
  541. if (y_crop_blocks + dst_blk_y < comp_height) {
  542. /* Row is within the mirrorable area. */
  543. src_row_ptr = src_buffer[compptr->v_samp_factor - offset_y - 1];
  544. for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks; dst_blk_x++) {
  545. dst_ptr = dst_row_ptr[dst_blk_x];
  546. if (x_crop_blocks + dst_blk_x < comp_width) {
  547. /* Process the blocks that can be mirrored both ways. */
  548. src_ptr = src_row_ptr[comp_width - x_crop_blocks - dst_blk_x - 1];
  549. for (i = 0; i < DCTSIZE; i += 2) {
  550. /* For even row, negate every odd column. */
  551. for (j = 0; j < DCTSIZE; j += 2) {
  552. *dst_ptr++ = *src_ptr++;
  553. *dst_ptr++ = - *src_ptr++;
  554. }
  555. /* For odd row, negate every even column. */
  556. for (j = 0; j < DCTSIZE; j += 2) {
  557. *dst_ptr++ = - *src_ptr++;
  558. *dst_ptr++ = *src_ptr++;
  559. }
  560. }
  561. } else {
  562. /* Any remaining right-edge blocks are only mirrored vertically. */
  563. src_ptr = src_row_ptr[x_crop_blocks + dst_blk_x];
  564. for (i = 0; i < DCTSIZE; i += 2) {
  565. for (j = 0; j < DCTSIZE; j++)
  566. *dst_ptr++ = *src_ptr++;
  567. for (j = 0; j < DCTSIZE; j++)
  568. *dst_ptr++ = - *src_ptr++;
  569. }
  570. }
  571. }
  572. } else {
  573. /* Remaining rows are just mirrored horizontally. */
  574. src_row_ptr = src_buffer[offset_y];
  575. for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks; dst_blk_x++) {
  576. if (x_crop_blocks + dst_blk_x < comp_width) {
  577. /* Process the blocks that can be mirrored. */
  578. dst_ptr = dst_row_ptr[dst_blk_x];
  579. src_ptr = src_row_ptr[comp_width - x_crop_blocks - dst_blk_x - 1];
  580. for (i = 0; i < DCTSIZE2; i += 2) {
  581. *dst_ptr++ = *src_ptr++;
  582. *dst_ptr++ = - *src_ptr++;
  583. }
  584. } else {
  585. /* Any remaining right-edge blocks are only copied. */
  586. jcopy_block_row(src_row_ptr + dst_blk_x + x_crop_blocks,
  587. dst_row_ptr + dst_blk_x,
  588. (JDIMENSION) 1);
  589. }
  590. }
  591. }
  592. }
  593. }
  594. }
  595. }
  596. LOCAL(void)
  597. do_transverse (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
  598. JDIMENSION x_crop_offset, JDIMENSION y_crop_offset,
  599. jvirt_barray_ptr *src_coef_arrays,
  600. jvirt_barray_ptr *dst_coef_arrays)
  601. /* Transverse transpose is equivalent to
  602. * 1. 180 degree rotation;
  603. * 2. Transposition;
  604. * or
  605. * 1. Horizontal mirroring;
  606. * 2. Transposition;
  607. * 3. Horizontal mirroring.
  608. * These steps are merged into a single processing routine.
  609. */
  610. {
  611. JDIMENSION MCU_cols, MCU_rows, comp_width, comp_height, dst_blk_x, dst_blk_y;
  612. JDIMENSION x_crop_blocks, y_crop_blocks;
  613. int ci, i, j, offset_x, offset_y;
  614. JBLOCKARRAY src_buffer, dst_buffer;
  615. JCOEFPTR src_ptr, dst_ptr;
  616. jpeg_component_info *compptr;
  617. MCU_cols = srcinfo->output_height /
  618. (dstinfo->max_h_samp_factor * dstinfo->min_DCT_h_scaled_size);
  619. MCU_rows = srcinfo->output_width /
  620. (dstinfo->max_v_samp_factor * dstinfo->min_DCT_v_scaled_size);
  621. for (ci = 0; ci < dstinfo->num_components; ci++) {
  622. compptr = dstinfo->comp_info + ci;
  623. comp_width = MCU_cols * compptr->h_samp_factor;
  624. comp_height = MCU_rows * compptr->v_samp_factor;
  625. x_crop_blocks = x_crop_offset * compptr->h_samp_factor;
  626. y_crop_blocks = y_crop_offset * compptr->v_samp_factor;
  627. for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks;
  628. dst_blk_y += compptr->v_samp_factor) {
  629. dst_buffer = (*srcinfo->mem->access_virt_barray)
  630. ((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y,
  631. (JDIMENSION) compptr->v_samp_factor, TRUE);
  632. for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
  633. for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks;
  634. dst_blk_x += compptr->h_samp_factor) {
  635. if (x_crop_blocks + dst_blk_x < comp_width) {
  636. /* Block is within the mirrorable area. */
  637. src_buffer = (*srcinfo->mem->access_virt_barray)
  638. ((j_common_ptr) srcinfo, src_coef_arrays[ci],
  639. comp_width - x_crop_blocks - dst_blk_x -
  640. (JDIMENSION) compptr->h_samp_factor,
  641. (JDIMENSION) compptr->h_samp_factor, FALSE);
  642. } else {
  643. src_buffer = (*srcinfo->mem->access_virt_barray)
  644. ((j_common_ptr) srcinfo, src_coef_arrays[ci],
  645. dst_blk_x + x_crop_blocks,
  646. (JDIMENSION) compptr->h_samp_factor, FALSE);
  647. }
  648. for (offset_x = 0; offset_x < compptr->h_samp_factor; offset_x++) {
  649. dst_ptr = dst_buffer[offset_y][dst_blk_x + offset_x];
  650. if (y_crop_blocks + dst_blk_y < comp_height) {
  651. if (x_crop_blocks + dst_blk_x < comp_width) {
  652. /* Block is within the mirrorable area. */
  653. src_ptr = src_buffer[compptr->h_samp_factor - offset_x - 1]
  654. [comp_height - y_crop_blocks - dst_blk_y - offset_y - 1];
  655. for (i = 0; i < DCTSIZE; i++) {
  656. for (j = 0; j < DCTSIZE; j++) {
  657. dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j];
  658. j++;
  659. dst_ptr[j*DCTSIZE+i] = -src_ptr[i*DCTSIZE+j];
  660. }
  661. i++;
  662. for (j = 0; j < DCTSIZE; j++) {
  663. dst_ptr[j*DCTSIZE+i] = -src_ptr[i*DCTSIZE+j];
  664. j++;
  665. dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j];
  666. }
  667. }
  668. } else {
  669. /* Right-edge blocks are mirrored in y only */
  670. src_ptr = src_buffer[offset_x]
  671. [comp_height - y_crop_blocks - dst_blk_y - offset_y - 1];
  672. for (i = 0; i < DCTSIZE; i++) {
  673. for (j = 0; j < DCTSIZE; j++) {
  674. dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j];
  675. j++;
  676. dst_ptr[j*DCTSIZE+i] = -src_ptr[i*DCTSIZE+j];
  677. }
  678. }
  679. }
  680. } else {
  681. if (x_crop_blocks + dst_blk_x < comp_width) {
  682. /* Bottom-edge blocks are mirrored in x only */
  683. src_ptr = src_buffer[compptr->h_samp_factor - offset_x - 1]
  684. [dst_blk_y + offset_y + y_crop_blocks];
  685. for (i = 0; i < DCTSIZE; i++) {
  686. for (j = 0; j < DCTSIZE; j++)
  687. dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j];
  688. i++;
  689. for (j = 0; j < DCTSIZE; j++)
  690. dst_ptr[j*DCTSIZE+i] = -src_ptr[i*DCTSIZE+j];
  691. }
  692. } else {
  693. /* At lower right corner, just transpose, no mirroring */
  694. src_ptr = src_buffer[offset_x]
  695. [dst_blk_y + offset_y + y_crop_blocks];
  696. for (i = 0; i < DCTSIZE; i++)
  697. for (j = 0; j < DCTSIZE; j++)
  698. dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j];
  699. }
  700. }
  701. }
  702. }
  703. }
  704. }
  705. }
  706. }
  707. /* Parse an unsigned integer: subroutine for jtransform_parse_crop_spec.
  708. * Returns TRUE if valid integer found, FALSE if not.
  709. * *strptr is advanced over the digit string, and *result is set to its value.
  710. */
  711. LOCAL(boolean)
  712. jt_read_integer (const char ** strptr, JDIMENSION * result)
  713. {
  714. const char * ptr = *strptr;
  715. JDIMENSION val = 0;
  716. for (; isdigit(*ptr); ptr++) {
  717. val = val * 10 + (JDIMENSION) (*ptr - '0');
  718. }
  719. *result = val;
  720. if (ptr == *strptr)
  721. return FALSE; /* oops, no digits */
  722. *strptr = ptr;
  723. return TRUE;
  724. }
  725. /* Parse a crop specification (written in X11 geometry style).
  726. * The routine returns TRUE if the spec string is valid, FALSE if not.
  727. *
  728. * The crop spec string should have the format
  729. * <width>[f]x<height>[f]{+-}<xoffset>{+-}<yoffset>
  730. * where width, height, xoffset, and yoffset are unsigned integers.
  731. * Each of the elements can be omitted to indicate a default value.
  732. * (A weakness of this style is that it is not possible to omit xoffset
  733. * while specifying yoffset, since they look alike.)
  734. *
  735. * This code is loosely based on XParseGeometry from the X11 distribution.
  736. */
  737. GLOBAL(boolean)
  738. jtransform_parse_crop_spec (jpeg_transform_info *info, const char *spec)
  739. {
  740. info->crop = FALSE;
  741. info->crop_width_set = JCROP_UNSET;
  742. info->crop_height_set = JCROP_UNSET;
  743. info->crop_xoffset_set = JCROP_UNSET;
  744. info->crop_yoffset_set = JCROP_UNSET;
  745. if (isdigit(*spec)) {
  746. /* fetch width */
  747. if (! jt_read_integer(&spec, &info->crop_width))
  748. return FALSE;
  749. if (*spec == 'f' || *spec == 'F') {
  750. spec++;
  751. info->crop_width_set = JCROP_FORCE;
  752. } else
  753. info->crop_width_set = JCROP_POS;
  754. }
  755. if (*spec == 'x' || *spec == 'X') {
  756. /* fetch height */
  757. spec++;
  758. if (! jt_read_integer(&spec, &info->crop_height))
  759. return FALSE;
  760. if (*spec == 'f' || *spec == 'F') {
  761. spec++;
  762. info->crop_height_set = JCROP_FORCE;
  763. } else
  764. info->crop_height_set = JCROP_POS;
  765. }
  766. if (*spec == '+' || *spec == '-') {
  767. /* fetch xoffset */
  768. info->crop_xoffset_set = (*spec == '-') ? JCROP_NEG : JCROP_POS;
  769. spec++;
  770. if (! jt_read_integer(&spec, &info->crop_xoffset))
  771. return FALSE;
  772. }
  773. if (*spec == '+' || *spec == '-') {
  774. /* fetch yoffset */
  775. info->crop_yoffset_set = (*spec == '-') ? JCROP_NEG : JCROP_POS;
  776. spec++;
  777. if (! jt_read_integer(&spec, &info->crop_yoffset))
  778. return FALSE;
  779. }
  780. /* We had better have gotten to the end of the string. */
  781. if (*spec != '\0')
  782. return FALSE;
  783. info->crop = TRUE;
  784. return TRUE;
  785. }
  786. /* Trim off any partial iMCUs on the indicated destination edge */
  787. LOCAL(void)
  788. trim_right_edge (jpeg_transform_info *info, JDIMENSION full_width)
  789. {
  790. JDIMENSION MCU_cols;
  791. MCU_cols = info->output_width / info->iMCU_sample_width;
  792. if (MCU_cols > 0 && info->x_crop_offset + MCU_cols ==
  793. full_width / info->iMCU_sample_width)
  794. info->output_width = MCU_cols * info->iMCU_sample_width;
  795. }
  796. LOCAL(void)
  797. trim_bottom_edge (jpeg_transform_info *info, JDIMENSION full_height)
  798. {
  799. JDIMENSION MCU_rows;
  800. MCU_rows = info->output_height / info->iMCU_sample_height;
  801. if (MCU_rows > 0 && info->y_crop_offset + MCU_rows ==
  802. full_height / info->iMCU_sample_height)
  803. info->output_height = MCU_rows * info->iMCU_sample_height;
  804. }
  805. /* Request any required workspace.
  806. *
  807. * This routine figures out the size that the output image will be
  808. * (which implies that all the transform parameters must be set before
  809. * it is called).
  810. *
  811. * We allocate the workspace virtual arrays from the source decompression
  812. * object, so that all the arrays (both the original data and the workspace)
  813. * will be taken into account while making memory management decisions.
  814. * Hence, this routine must be called after jpeg_read_header (which reads
  815. * the image dimensions) and before jpeg_read_coefficients (which realizes
  816. * the source's virtual arrays).
  817. *
  818. * This function returns FALSE right away if -perfect is given
  819. * and transformation is not perfect. Otherwise returns TRUE.
  820. */
  821. GLOBAL(boolean)
  822. jtransform_request_workspace (j_decompress_ptr srcinfo,
  823. jpeg_transform_info *info)
  824. {
  825. jvirt_barray_ptr *coef_arrays;
  826. boolean need_workspace, transpose_it;
  827. jpeg_component_info *compptr;
  828. JDIMENSION xoffset, yoffset;
  829. JDIMENSION width_in_iMCUs, height_in_iMCUs;
  830. JDIMENSION width_in_blocks, height_in_blocks;
  831. int ci, h_samp_factor, v_samp_factor;
  832. /* Determine number of components in output image */
  833. if (info->force_grayscale &&
  834. srcinfo->jpeg_color_space == JCS_YCbCr &&
  835. srcinfo->num_components == 3)
  836. /* We'll only process the first component */
  837. info->num_components = 1;
  838. else
  839. /* Process all the components */
  840. info->num_components = srcinfo->num_components;
  841. /* Compute output image dimensions and related values. */
  842. jpeg_core_output_dimensions(srcinfo);
  843. /* Return right away if -perfect is given and transformation is not perfect.
  844. */
  845. if (info->perfect) {
  846. if (info->num_components == 1) {
  847. if (!jtransform_perfect_transform(srcinfo->output_width,
  848. srcinfo->output_height,
  849. srcinfo->min_DCT_h_scaled_size,
  850. srcinfo->min_DCT_v_scaled_size,
  851. info->transform))
  852. return FALSE;
  853. } else {
  854. if (!jtransform_perfect_transform(srcinfo->output_width,
  855. srcinfo->output_height,
  856. srcinfo->max_h_samp_factor * srcinfo->min_DCT_h_scaled_size,
  857. srcinfo->max_v_samp_factor * srcinfo->min_DCT_v_scaled_size,
  858. info->transform))
  859. return FALSE;
  860. }
  861. }
  862. /* If there is only one output component, force the iMCU size to be 1;
  863. * else use the source iMCU size. (This allows us to do the right thing
  864. * when reducing color to grayscale, and also provides a handy way of
  865. * cleaning up "funny" grayscale images whose sampling factors are not 1x1.)
  866. */
  867. switch (info->transform) {
  868. case JXFORM_TRANSPOSE:
  869. case JXFORM_TRANSVERSE:
  870. case JXFORM_ROT_90:
  871. case JXFORM_ROT_270:
  872. info->output_width = srcinfo->output_height;
  873. info->output_height = srcinfo->output_width;
  874. if (info->num_components == 1) {
  875. info->iMCU_sample_width = srcinfo->min_DCT_v_scaled_size;
  876. info->iMCU_sample_height = srcinfo->min_DCT_h_scaled_size;
  877. } else {
  878. info->iMCU_sample_width =
  879. srcinfo->max_v_samp_factor * srcinfo->min_DCT_v_scaled_size;
  880. info->iMCU_sample_height =
  881. srcinfo->max_h_samp_factor * srcinfo->min_DCT_h_scaled_size;
  882. }
  883. break;
  884. default:
  885. info->output_width = srcinfo->output_width;
  886. info->output_height = srcinfo->output_height;
  887. if (info->num_components == 1) {
  888. info->iMCU_sample_width = srcinfo->min_DCT_h_scaled_size;
  889. info->iMCU_sample_height = srcinfo->min_DCT_v_scaled_size;
  890. } else {
  891. info->iMCU_sample_width =
  892. srcinfo->max_h_samp_factor * srcinfo->min_DCT_h_scaled_size;
  893. info->iMCU_sample_height =
  894. srcinfo->max_v_samp_factor * srcinfo->min_DCT_v_scaled_size;
  895. }
  896. break;
  897. }
  898. /* If cropping has been requested, compute the crop area's position and
  899. * dimensions, ensuring that its upper left corner falls at an iMCU boundary.
  900. */
  901. if (info->crop) {
  902. /* Insert default values for unset crop parameters */
  903. if (info->crop_xoffset_set == JCROP_UNSET)
  904. info->crop_xoffset = 0; /* default to +0 */
  905. if (info->crop_yoffset_set == JCROP_UNSET)
  906. info->crop_yoffset = 0; /* default to +0 */
  907. if (info->crop_xoffset >= info->output_width ||
  908. info->crop_yoffset >= info->output_height)
  909. ERREXIT(srcinfo, JERR_BAD_CROP_SPEC);
  910. if (info->crop_width_set == JCROP_UNSET)
  911. info->crop_width = info->output_width - info->crop_xoffset;
  912. if (info->crop_height_set == JCROP_UNSET)
  913. info->crop_height = info->output_height - info->crop_yoffset;
  914. /* Ensure parameters are valid */
  915. if (info->crop_width <= 0 || info->crop_width > info->output_width ||
  916. info->crop_height <= 0 || info->crop_height > info->output_height ||
  917. info->crop_xoffset > info->output_width - info->crop_width ||
  918. info->crop_yoffset > info->output_height - info->crop_height)
  919. ERREXIT(srcinfo, JERR_BAD_CROP_SPEC);
  920. /* Convert negative crop offsets into regular offsets */
  921. if (info->crop_xoffset_set == JCROP_NEG)
  922. xoffset = info->output_width - info->crop_width - info->crop_xoffset;
  923. else
  924. xoffset = info->crop_xoffset;
  925. if (info->crop_yoffset_set == JCROP_NEG)
  926. yoffset = info->output_height - info->crop_height - info->crop_yoffset;
  927. else
  928. yoffset = info->crop_yoffset;
  929. /* Now adjust so that upper left corner falls at an iMCU boundary */
  930. if (info->crop_width_set == JCROP_FORCE)
  931. info->output_width = info->crop_width;
  932. else
  933. info->output_width =
  934. info->crop_width + (xoffset % info->iMCU_sample_width);
  935. if (info->crop_height_set == JCROP_FORCE)
  936. info->output_height = info->crop_height;
  937. else
  938. info->output_height =
  939. info->crop_height + (yoffset % info->iMCU_sample_height);
  940. /* Save x/y offsets measured in iMCUs */
  941. info->x_crop_offset = xoffset / info->iMCU_sample_width;
  942. info->y_crop_offset = yoffset / info->iMCU_sample_height;
  943. } else {
  944. info->x_crop_offset = 0;
  945. info->y_crop_offset = 0;
  946. }
  947. /* Figure out whether we need workspace arrays,
  948. * and if so whether they are transposed relative to the source.
  949. */
  950. need_workspace = FALSE;
  951. transpose_it = FALSE;
  952. switch (info->transform) {
  953. case JXFORM_NONE:
  954. if (info->x_crop_offset != 0 || info->y_crop_offset != 0)
  955. need_workspace = TRUE;
  956. /* No workspace needed if neither cropping nor transforming */
  957. break;
  958. case JXFORM_FLIP_H:
  959. if (info->trim)
  960. trim_right_edge(info, srcinfo->output_width);
  961. if (info->y_crop_offset != 0)
  962. need_workspace = TRUE;
  963. /* do_flip_h_no_crop doesn't need a workspace array */
  964. break;
  965. case JXFORM_FLIP_V:
  966. if (info->trim)
  967. trim_bottom_edge(info, srcinfo->output_height);
  968. /* Need workspace arrays having same dimensions as source image. */
  969. need_workspace = TRUE;
  970. break;
  971. case JXFORM_TRANSPOSE:
  972. /* transpose does NOT have to trim anything */
  973. /* Need workspace arrays having transposed dimensions. */
  974. need_workspace = TRUE;
  975. transpose_it = TRUE;
  976. break;
  977. case JXFORM_TRANSVERSE:
  978. if (info->trim) {
  979. trim_right_edge(info, srcinfo->output_height);
  980. trim_bottom_edge(info, srcinfo->output_width);
  981. }
  982. /* Need workspace arrays having transposed dimensions. */
  983. need_workspace = TRUE;
  984. transpose_it = TRUE;
  985. break;
  986. case JXFORM_ROT_90:
  987. if (info->trim)
  988. trim_right_edge(info, srcinfo->output_height);
  989. /* Need workspace arrays having transposed dimensions. */
  990. need_workspace = TRUE;
  991. transpose_it = TRUE;
  992. break;
  993. case JXFORM_ROT_180:
  994. if (info->trim) {
  995. trim_right_edge(info, srcinfo->output_width);
  996. trim_bottom_edge(info, srcinfo->output_height);
  997. }
  998. /* Need workspace arrays having same dimensions as source image. */
  999. need_workspace = TRUE;
  1000. break;
  1001. case JXFORM_ROT_270:
  1002. if (info->trim)
  1003. trim_bottom_edge(info, srcinfo->output_width);
  1004. /* Need workspace arrays having transposed dimensions. */
  1005. need_workspace = TRUE;
  1006. transpose_it = TRUE;
  1007. break;
  1008. }
  1009. /* Allocate workspace if needed.
  1010. * Note that we allocate arrays padded out to the next iMCU boundary,
  1011. * so that transform routines need not worry about missing edge blocks.
  1012. */
  1013. if (need_workspace) {
  1014. coef_arrays = (jvirt_barray_ptr *)
  1015. (*srcinfo->mem->alloc_small) ((j_common_ptr) srcinfo, JPOOL_IMAGE,
  1016. SIZEOF(jvirt_barray_ptr) * info->num_components);
  1017. width_in_iMCUs = (JDIMENSION)
  1018. jdiv_round_up((long) info->output_width,
  1019. (long) info->iMCU_sample_width);
  1020. height_in_iMCUs = (JDIMENSION)
  1021. jdiv_round_up((long) info->output_height,
  1022. (long) info->iMCU_sample_height);
  1023. for (ci = 0; ci < info->num_components; ci++) {
  1024. compptr = srcinfo->comp_info + ci;
  1025. if (info->num_components == 1) {
  1026. /* we're going to force samp factors to 1x1 in this case */
  1027. h_samp_factor = v_samp_factor = 1;
  1028. } else if (transpose_it) {
  1029. h_samp_factor = compptr->v_samp_factor;
  1030. v_samp_factor = compptr->h_samp_factor;
  1031. } else {
  1032. h_samp_factor = compptr->h_samp_factor;
  1033. v_samp_factor = compptr->v_samp_factor;
  1034. }
  1035. width_in_blocks = width_in_iMCUs * h_samp_factor;
  1036. height_in_blocks = height_in_iMCUs * v_samp_factor;
  1037. coef_arrays[ci] = (*srcinfo->mem->request_virt_barray)
  1038. ((j_common_ptr) srcinfo, JPOOL_IMAGE, FALSE,
  1039. width_in_blocks, height_in_blocks, (JDIMENSION) v_samp_factor);
  1040. }
  1041. info->workspace_coef_arrays = coef_arrays;
  1042. } else
  1043. info->workspace_coef_arrays = NULL;
  1044. return TRUE;
  1045. }
  1046. /* Transpose destination image parameters */
  1047. LOCAL(void)
  1048. transpose_critical_parameters (j_compress_ptr dstinfo)
  1049. {
  1050. int tblno, i, j, ci, itemp;
  1051. jpeg_component_info *compptr;
  1052. JQUANT_TBL *qtblptr;
  1053. JDIMENSION jtemp;
  1054. UINT16 qtemp;
  1055. /* Transpose image dimensions */
  1056. jtemp = dstinfo->image_width;
  1057. dstinfo->image_width = dstinfo->image_height;
  1058. dstinfo->image_height = jtemp;
  1059. itemp = dstinfo->min_DCT_h_scaled_size;
  1060. dstinfo->min_DCT_h_scaled_size = dstinfo->min_DCT_v_scaled_size;
  1061. dstinfo->min_DCT_v_scaled_size = itemp;
  1062. /* Transpose sampling factors */
  1063. for (ci = 0; ci < dstinfo->num_components; ci++) {
  1064. compptr = dstinfo->comp_info + ci;
  1065. itemp = compptr->h_samp_factor;
  1066. compptr->h_samp_factor = compptr->v_samp_factor;
  1067. compptr->v_samp_factor = itemp;
  1068. }
  1069. /* Transpose quantization tables */
  1070. for (tblno = 0; tblno < NUM_QUANT_TBLS; tblno++) {
  1071. qtblptr = dstinfo->quant_tbl_ptrs[tblno];
  1072. if (qtblptr != NULL) {
  1073. for (i = 0; i < DCTSIZE; i++) {
  1074. for (j = 0; j < i; j++) {
  1075. qtemp = qtblptr->quantval[i*DCTSIZE+j];
  1076. qtblptr->quantval[i*DCTSIZE+j] = qtblptr->quantval[j*DCTSIZE+i];
  1077. qtblptr->quantval[j*DCTSIZE+i] = qtemp;
  1078. }
  1079. }
  1080. }
  1081. }
  1082. }
  1083. /* Adjust Exif image parameters.
  1084. *
  1085. * We try to adjust the Tags ExifImageWidth and ExifImageHeight if possible.
  1086. */
  1087. LOCAL(void)
  1088. adjust_exif_parameters (JOCTET FAR * data, unsigned int length,
  1089. JDIMENSION new_width, JDIMENSION new_height)
  1090. {
  1091. boolean is_motorola; /* Flag for byte order */
  1092. unsigned int number_of_tags, tagnum;
  1093. unsigned int firstoffset, offset;
  1094. JDIMENSION new_value;
  1095. if (length < 12) return; /* Length of an IFD entry */
  1096. /* Discover byte order */
  1097. if (GETJOCTET(data[0]) == 0x49 && GETJOCTET(data[1]) == 0x49)
  1098. is_motorola = FALSE;
  1099. else if (GETJOCTET(data[0]) == 0x4D && GETJOCTET(data[1]) == 0x4D)
  1100. is_motorola = TRUE;
  1101. else
  1102. return;
  1103. /* Check Tag Mark */
  1104. if (is_motorola) {
  1105. if (GETJOCTET(data[2]) != 0) return;
  1106. if (GETJOCTET(data[3]) != 0x2A) return;
  1107. } else {
  1108. if (GETJOCTET(data[3]) != 0) return;
  1109. if (GETJOCTET(data[2]) != 0x2A) return;
  1110. }
  1111. /* Get first IFD offset (offset to IFD0) */
  1112. if (is_motorola) {
  1113. if (GETJOCTET(data[4]) != 0) return;
  1114. if (GETJOCTET(data[5]) != 0) return;
  1115. firstoffset = GETJOCTET(data[6]);
  1116. firstoffset <<= 8;
  1117. firstoffset += GETJOCTET(data[7]);
  1118. } else {
  1119. if (GETJOCTET(data[7]) != 0) return;
  1120. if (GETJOCTET(data[6]) != 0) return;
  1121. firstoffset = GETJOCTET(data[5]);
  1122. firstoffset <<= 8;
  1123. firstoffset += GETJOCTET(data[4]);
  1124. }
  1125. if (firstoffset > length - 2) return; /* check end of data segment */
  1126. /* Get the number of directory entries contained in this IFD */
  1127. if (is_motorola) {
  1128. number_of_tags = GETJOCTET(data[firstoffset]);
  1129. number_of_tags <<= 8;
  1130. number_of_tags += GETJOCTET(data[firstoffset+1]);
  1131. } else {
  1132. number_of_tags = GETJOCTET(data[firstoffset+1]);
  1133. number_of_tags <<= 8;
  1134. number_of_tags += GETJOCTET(data[firstoffset]);
  1135. }
  1136. if (number_of_tags == 0) return;
  1137. firstoffset += 2;
  1138. /* Search for ExifSubIFD offset Tag in IFD0 */
  1139. for (;;) {
  1140. if (firstoffset > length - 12) return; /* check end of data segment */
  1141. /* Get Tag number */
  1142. if (is_motorola) {
  1143. tagnum = GETJOCTET(data[firstoffset]);
  1144. tagnum <<= 8;
  1145. tagnum += GETJOCTET(data[firstoffset+1]);
  1146. } else {
  1147. tagnum = GETJOCTET(data[firstoffset+1]);
  1148. tagnum <<= 8;
  1149. tagnum += GETJOCTET(data[firstoffset]);
  1150. }
  1151. if (tagnum == 0x8769) break; /* found ExifSubIFD offset Tag */
  1152. if (--number_of_tags == 0) return;
  1153. firstoffset += 12;
  1154. }
  1155. /* Get the ExifSubIFD offset */
  1156. if (is_motorola) {
  1157. if (GETJOCTET(data[firstoffset+8]) != 0) return;
  1158. if (GETJOCTET(data[firstoffset+9]) != 0) return;
  1159. offset = GETJOCTET(data[firstoffset+10]);
  1160. offset <<= 8;
  1161. offset += GETJOCTET(data[firstoffset+11]);
  1162. } else {
  1163. if (GETJOCTET(data[firstoffset+11]) != 0) return;
  1164. if (GETJOCTET(data[firstoffset+10]) != 0) return;
  1165. offset = GETJOCTET(data[firstoffset+9]);
  1166. offset <<= 8;
  1167. offset += GETJOCTET(data[firstoffset+8]);
  1168. }
  1169. if (offset > length - 2) return; /* check end of data segment */
  1170. /* Get the number of directory entries contained in this SubIFD */
  1171. if (is_motorola) {
  1172. number_of_tags = GETJOCTET(data[offset]);
  1173. number_of_tags <<= 8;
  1174. number_of_tags += GETJOCTET(data[offset+1]);
  1175. } else {
  1176. number_of_tags = GETJOCTET(data[offset+1]);
  1177. number_of_tags <<= 8;
  1178. number_of_tags += GETJOCTET(data[offset]);
  1179. }
  1180. if (number_of_tags < 2) return;
  1181. offset += 2;
  1182. /* Search for ExifImageWidth and ExifImageHeight Tags in this SubIFD */
  1183. do {
  1184. if (offset > length - 12) return; /* check end of data segment */
  1185. /* Get Tag number */
  1186. if (is_motorola) {
  1187. tagnum = GETJOCTET(data[offset]);
  1188. tagnum <<= 8;
  1189. tagnum += GETJOCTET(data[offset+1]);
  1190. } else {
  1191. tagnum = GETJOCTET(data[offset+1]);
  1192. tagnum <<= 8;
  1193. tagnum += GETJOCTET(data[offset]);
  1194. }
  1195. if (tagnum == 0xA002 || tagnum == 0xA003) {
  1196. if (tagnum == 0xA002)
  1197. new_value = new_width; /* ExifImageWidth Tag */
  1198. else
  1199. new_value = new_height; /* ExifImageHeight Tag */
  1200. if (is_motorola) {
  1201. data[offset+2] = 0; /* Format = unsigned long (4 octets) */
  1202. data[offset+3] = 4;
  1203. data[offset+4] = 0; /* Number Of Components = 1 */
  1204. data[offset+5] = 0;
  1205. data[offset+6] = 0;
  1206. data[offset+7] = 1;
  1207. data[offset+8] = 0;
  1208. data[offset+9] = 0;
  1209. data[offset+10] = (JOCTET)((new_value >> 8) & 0xFF);
  1210. data[offset+11] = (JOCTET)(new_value & 0xFF);
  1211. } else {
  1212. data[offset+2] = 4; /* Format = unsigned long (4 octets) */
  1213. data[offset+3] = 0;
  1214. data[offset+4] = 1; /* Number Of Components = 1 */
  1215. data[offset+5] = 0;
  1216. data[offset+6] = 0;
  1217. data[offset+7] = 0;
  1218. data[offset+8] = (JOCTET)(new_value & 0xFF);
  1219. data[offset+9] = (JOCTET)((new_value >> 8) & 0xFF);
  1220. data[offset+10] = 0;
  1221. data[offset+11] = 0;
  1222. }
  1223. }
  1224. offset += 12;
  1225. } while (--number_of_tags);
  1226. }
  1227. /* Adjust output image parameters as needed.
  1228. *
  1229. * This must be called after jpeg_copy_critical_parameters()
  1230. * and before jpeg_write_coefficients().
  1231. *
  1232. * The return value is the set of virtual coefficient arrays to be written
  1233. * (either the ones allocated by jtransform_request_workspace, or the
  1234. * original source data arrays). The caller will need to pass this value
  1235. * to jpeg_write_coefficients().
  1236. */
  1237. GLOBAL(jvirt_barray_ptr *)
  1238. jtransform_adjust_parameters (j_decompress_ptr srcinfo,
  1239. j_compress_ptr dstinfo,
  1240. jvirt_barray_ptr *src_coef_arrays,
  1241. jpeg_transform_info *info)
  1242. {
  1243. /* If force-to-grayscale is requested, adjust destination parameters */
  1244. if (info->force_grayscale) {
  1245. /* First, ensure we have YCbCr or grayscale data, and that the source's
  1246. * Y channel is full resolution. (No reasonable person would make Y
  1247. * be less than full resolution, so actually coping with that case
  1248. * isn't worth extra code space. But we check it to avoid crashing.)
  1249. */
  1250. if (((dstinfo->jpeg_color_space == JCS_YCbCr &&
  1251. dstinfo->num_components == 3) ||
  1252. (dstinfo->jpeg_color_space == JCS_GRAYSCALE &&
  1253. dstinfo->num_components == 1)) &&
  1254. srcinfo->comp_info[0].h_samp_factor == srcinfo->max_h_samp_factor &&
  1255. srcinfo->comp_info[0].v_samp_factor == srcinfo->max_v_samp_factor) {
  1256. /* We use jpeg_set_colorspace to make sure subsidiary settings get fixed
  1257. * properly. Among other things, it sets the target h_samp_factor &
  1258. * v_samp_factor to 1, which typically won't match the source.
  1259. * We have to preserve the source's quantization table number, however.
  1260. */
  1261. int sv_quant_tbl_no = dstinfo->comp_info[0].quant_tbl_no;
  1262. jpeg_set_colorspace(dstinfo, JCS_GRAYSCALE);
  1263. dstinfo->comp_info[0].quant_tbl_no = sv_quant_tbl_no;
  1264. } else {
  1265. /* Sorry, can't do it */
  1266. ERREXIT(dstinfo, JERR_CONVERSION_NOTIMPL);
  1267. }
  1268. } else if (info->num_components == 1) {
  1269. /* For a single-component source, we force the destination sampling factors
  1270. * to 1x1, with or without force_grayscale. This is useful because some
  1271. * decoders choke on grayscale images with other sampling factors.
  1272. */
  1273. dstinfo->comp_info[0].h_samp_factor = 1;
  1274. dstinfo->comp_info[0].v_samp_factor = 1;
  1275. }
  1276. /* Correct the destination's image dimensions as necessary
  1277. * for rotate/flip, resize, and crop operations.
  1278. */
  1279. dstinfo->jpeg_width = info->output_width;
  1280. dstinfo->jpeg_height = info->output_height;
  1281. /* Transpose destination image parameters */
  1282. switch (info->transform) {
  1283. case JXFORM_TRANSPOSE:
  1284. case JXFORM_TRANSVERSE:
  1285. case JXFORM_ROT_90:
  1286. case JXFORM_ROT_270:
  1287. transpose_critical_parameters(dstinfo);
  1288. break;
  1289. default:
  1290. break;
  1291. }
  1292. /* Adjust Exif properties */
  1293. if (srcinfo->marker_list != NULL &&
  1294. srcinfo->marker_list->marker == JPEG_APP0+1 &&
  1295. srcinfo->marker_list->data_length >= 6 &&
  1296. GETJOCTET(srcinfo->marker_list->data[0]) == 0x45 &&
  1297. GETJOCTET(srcinfo->marker_list->data[1]) == 0x78 &&
  1298. GETJOCTET(srcinfo->marker_list->data[2]) == 0x69 &&
  1299. GETJOCTET(srcinfo->marker_list->data[3]) == 0x66 &&
  1300. GETJOCTET(srcinfo->marker_list->data[4]) == 0 &&
  1301. GETJOCTET(srcinfo->marker_list->data[5]) == 0) {
  1302. /* Suppress output of JFIF marker */
  1303. dstinfo->write_JFIF_header = FALSE;
  1304. /* Adjust Exif image parameters */
  1305. if (dstinfo->jpeg_width != srcinfo->image_width ||
  1306. dstinfo->jpeg_height != srcinfo->image_height)
  1307. /* Align data segment to start of TIFF structure for parsing */
  1308. adjust_exif_parameters(srcinfo->marker_list->data + 6,
  1309. srcinfo->marker_list->data_length - 6,
  1310. dstinfo->jpeg_width, dstinfo->jpeg_height);
  1311. }
  1312. /* Return the appropriate output data set */
  1313. if (info->workspace_coef_arrays != NULL)
  1314. return info->workspace_coef_arrays;
  1315. return src_coef_arrays;
  1316. }
  1317. /* Execute the actual transformation, if any.
  1318. *
  1319. * This must be called *after* jpeg_write_coefficients, because it depends
  1320. * on jpeg_write_coefficients to have computed subsidiary values such as
  1321. * the per-component width and height fields in the destination object.
  1322. *
  1323. * Note that some transformations will modify the source data arrays!
  1324. */
  1325. GLOBAL(void)
  1326. jtransform_execute_transform (j_decompress_ptr srcinfo,
  1327. j_compress_ptr dstinfo,
  1328. jvirt_barray_ptr *src_coef_arrays,
  1329. jpeg_transform_info *info)
  1330. {
  1331. jvirt_barray_ptr *dst_coef_arrays = info->workspace_coef_arrays;
  1332. /* Note: conditions tested here should match those in switch statement
  1333. * in jtransform_request_workspace()
  1334. */
  1335. switch (info->transform) {
  1336. case JXFORM_NONE:
  1337. if (info->x_crop_offset != 0 || info->y_crop_offset != 0)
  1338. do_crop(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset,
  1339. src_coef_arrays, dst_coef_arrays);
  1340. break;
  1341. case JXFORM_FLIP_H:
  1342. if (info->y_crop_offset != 0)
  1343. do_flip_h(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset,
  1344. src_coef_arrays, dst_coef_arrays);
  1345. else
  1346. do_flip_h_no_crop(srcinfo, dstinfo, info->x_crop_offset,
  1347. src_coef_arrays);
  1348. break;
  1349. case JXFORM_FLIP_V:
  1350. do_flip_v(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset,
  1351. src_coef_arrays, dst_coef_arrays);
  1352. break;
  1353. case JXFORM_TRANSPOSE:
  1354. do_transpose(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset,
  1355. src_coef_arrays, dst_coef_arrays);
  1356. break;
  1357. case JXFORM_TRANSVERSE:
  1358. do_transverse(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset,
  1359. src_coef_arrays, dst_coef_arrays);
  1360. break;
  1361. case JXFORM_ROT_90:
  1362. do_rot_90(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset,
  1363. src_coef_arrays, dst_coef_arrays);
  1364. break;
  1365. case JXFORM_ROT_180:
  1366. do_rot_180(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset,
  1367. src_coef_arrays, dst_coef_arrays);
  1368. break;
  1369. case JXFORM_ROT_270:
  1370. do_rot_270(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset,
  1371. src_coef_arrays, dst_coef_arrays);
  1372. break;
  1373. }
  1374. }
  1375. /* jtransform_perfect_transform
  1376. *
  1377. * Determine whether lossless transformation is perfectly
  1378. * possible for a specified image and transformation.
  1379. *
  1380. * Inputs:
  1381. * image_width, image_height: source image dimensions.
  1382. * MCU_width, MCU_height: pixel dimensions of MCU.
  1383. * transform: transformation identifier.
  1384. * Parameter sources from initialized jpeg_struct
  1385. * (after reading source header):
  1386. * image_width = cinfo.image_width
  1387. * image_height = cinfo.image_height
  1388. * MCU_width = cinfo.max_h_samp_factor * cinfo.block_size
  1389. * MCU_height = cinfo.max_v_samp_factor * cinfo.block_size
  1390. * Result:
  1391. * TRUE = perfect transformation possible
  1392. * FALSE = perfect transformation not possible
  1393. * (may use custom action then)
  1394. */
  1395. GLOBAL(boolean)
  1396. jtransform_perfect_transform(JDIMENSION image_width, JDIMENSION image_height,
  1397. int MCU_width, int MCU_height,
  1398. JXFORM_CODE transform)
  1399. {
  1400. boolean result = TRUE; /* initialize TRUE */
  1401. switch (transform) {
  1402. case JXFORM_FLIP_H:
  1403. case JXFORM_ROT_270:
  1404. if (image_width % (JDIMENSION) MCU_width)
  1405. result = FALSE;
  1406. break;
  1407. case JXFORM_FLIP_V:
  1408. case JXFORM_ROT_90:
  1409. if (image_height % (JDIMENSION) MCU_height)
  1410. result = FALSE;
  1411. break;
  1412. case JXFORM_TRANSVERSE:
  1413. case JXFORM_ROT_180:
  1414. if (image_width % (JDIMENSION) MCU_width)
  1415. result = FALSE;
  1416. if (image_height % (JDIMENSION) MCU_height)
  1417. result = FALSE;
  1418. break;
  1419. default:
  1420. break;
  1421. }
  1422. return result;
  1423. }
  1424. #endif /* TRANSFORMS_SUPPORTED */
  1425. /* Setup decompression object to save desired markers in memory.
  1426. * This must be called before jpeg_read_header() to have the desired effect.
  1427. */
  1428. GLOBAL(void)
  1429. jcopy_markers_setup (j_decompress_ptr srcinfo, JCOPY_OPTION option)
  1430. {
  1431. #ifdef SAVE_MARKERS_SUPPORTED
  1432. int m;
  1433. /* Save comments except under NONE option */
  1434. if (option != JCOPYOPT_NONE) {
  1435. jpeg_save_markers(srcinfo, JPEG_COM, 0xFFFF);
  1436. }
  1437. /* Save all types of APPn markers iff ALL option */
  1438. if (option == JCOPYOPT_ALL) {
  1439. for (m = 0; m < 16; m++)
  1440. jpeg_save_markers(srcinfo, JPEG_APP0 + m, 0xFFFF);
  1441. }
  1442. #endif /* SAVE_MARKERS_SUPPORTED */
  1443. }
  1444. /* Copy markers saved in the given source object to the destination object.
  1445. * This should be called just after jpeg_start_compress() or
  1446. * jpeg_write_coefficients().
  1447. * Note that those routines will have written the SOI, and also the
  1448. * JFIF APP0 or Adobe APP14 markers if selected.
  1449. */
  1450. GLOBAL(void)
  1451. jcopy_markers_execute (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
  1452. JCOPY_OPTION option)
  1453. {
  1454. jpeg_saved_marker_ptr marker;
  1455. /* In the current implementation, we don't actually need to examine the
  1456. * option flag here; we just copy everything that got saved.
  1457. * But to avoid confusion, we do not output JFIF and Adobe APP14 markers
  1458. * if the encoder library already wrote one.
  1459. */
  1460. for (marker = srcinfo->marker_list; marker != NULL; marker = marker->next) {
  1461. if (dstinfo->write_JFIF_header &&
  1462. marker->marker == JPEG_APP0 &&
  1463. marker->data_length >= 5 &&
  1464. GETJOCTET(marker->data[0]) == 0x4A &&
  1465. GETJOCTET(marker->data[1]) == 0x46 &&
  1466. GETJOCTET(marker->data[2]) == 0x49 &&
  1467. GETJOCTET(marker->data[3]) == 0x46 &&
  1468. GETJOCTET(marker->data[4]) == 0)
  1469. continue; /* reject duplicate JFIF */
  1470. if (dstinfo->write_Adobe_marker &&
  1471. marker->marker == JPEG_APP0+14 &&
  1472. marker->data_length >= 5 &&
  1473. GETJOCTET(marker->data[0]) == 0x41 &&
  1474. GETJOCTET(marker->data[1]) == 0x64 &&
  1475. GETJOCTET(marker->data[2]) == 0x6F &&
  1476. GETJOCTET(marker->data[3]) == 0x62 &&
  1477. GETJOCTET(marker->data[4]) == 0x65)
  1478. continue; /* reject duplicate Adobe */
  1479. #ifdef NEED_FAR_POINTERS
  1480. /* We could use jpeg_write_marker if the data weren't FAR... */
  1481. {
  1482. unsigned int i;
  1483. jpeg_write_m_header(dstinfo, marker->marker, marker->data_length);
  1484. for (i = 0; i < marker->data_length; i++)
  1485. jpeg_write_m_byte(dstinfo, marker->data[i]);
  1486. }
  1487. #else
  1488. jpeg_write_marker(dstinfo, marker->marker,
  1489. marker->data, marker->data_length);
  1490. #endif
  1491. }
  1492. }