Team Fortress 2 Source Code as on 22/4/2020
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.

1583 lines
56 KiB

  1. /*
  2. * transupp.c
  3. *
  4. * Copyright (C) 1997-2009, 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>x<height>{+-}<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. info->crop_width_set = JCROP_POS;
  750. }
  751. if (*spec == 'x' || *spec == 'X') {
  752. /* fetch height */
  753. spec++;
  754. if (! jt_read_integer(&spec, &info->crop_height))
  755. return FALSE;
  756. info->crop_height_set = JCROP_POS;
  757. }
  758. if (*spec == '+' || *spec == '-') {
  759. /* fetch xoffset */
  760. info->crop_xoffset_set = (*spec == '-') ? JCROP_NEG : JCROP_POS;
  761. spec++;
  762. if (! jt_read_integer(&spec, &info->crop_xoffset))
  763. return FALSE;
  764. }
  765. if (*spec == '+' || *spec == '-') {
  766. /* fetch yoffset */
  767. info->crop_yoffset_set = (*spec == '-') ? JCROP_NEG : JCROP_POS;
  768. spec++;
  769. if (! jt_read_integer(&spec, &info->crop_yoffset))
  770. return FALSE;
  771. }
  772. /* We had better have gotten to the end of the string. */
  773. if (*spec != '\0')
  774. return FALSE;
  775. info->crop = TRUE;
  776. return TRUE;
  777. }
  778. /* Trim off any partial iMCUs on the indicated destination edge */
  779. LOCAL(void)
  780. trim_right_edge (jpeg_transform_info *info, JDIMENSION full_width)
  781. {
  782. JDIMENSION MCU_cols;
  783. MCU_cols = info->output_width / info->iMCU_sample_width;
  784. if (MCU_cols > 0 && info->x_crop_offset + MCU_cols ==
  785. full_width / info->iMCU_sample_width)
  786. info->output_width = MCU_cols * info->iMCU_sample_width;
  787. }
  788. LOCAL(void)
  789. trim_bottom_edge (jpeg_transform_info *info, JDIMENSION full_height)
  790. {
  791. JDIMENSION MCU_rows;
  792. MCU_rows = info->output_height / info->iMCU_sample_height;
  793. if (MCU_rows > 0 && info->y_crop_offset + MCU_rows ==
  794. full_height / info->iMCU_sample_height)
  795. info->output_height = MCU_rows * info->iMCU_sample_height;
  796. }
  797. /* Request any required workspace.
  798. *
  799. * This routine figures out the size that the output image will be
  800. * (which implies that all the transform parameters must be set before
  801. * it is called).
  802. *
  803. * We allocate the workspace virtual arrays from the source decompression
  804. * object, so that all the arrays (both the original data and the workspace)
  805. * will be taken into account while making memory management decisions.
  806. * Hence, this routine must be called after jpeg_read_header (which reads
  807. * the image dimensions) and before jpeg_read_coefficients (which realizes
  808. * the source's virtual arrays).
  809. *
  810. * This function returns FALSE right away if -perfect is given
  811. * and transformation is not perfect. Otherwise returns TRUE.
  812. */
  813. GLOBAL(boolean)
  814. jtransform_request_workspace (j_decompress_ptr srcinfo,
  815. jpeg_transform_info *info)
  816. {
  817. jvirt_barray_ptr *coef_arrays;
  818. boolean need_workspace, transpose_it;
  819. jpeg_component_info *compptr;
  820. JDIMENSION xoffset, yoffset;
  821. JDIMENSION width_in_iMCUs, height_in_iMCUs;
  822. JDIMENSION width_in_blocks, height_in_blocks;
  823. int ci, h_samp_factor, v_samp_factor;
  824. /* Determine number of components in output image */
  825. if (info->force_grayscale &&
  826. srcinfo->jpeg_color_space == JCS_YCbCr &&
  827. srcinfo->num_components == 3)
  828. /* We'll only process the first component */
  829. info->num_components = 1;
  830. else
  831. /* Process all the components */
  832. info->num_components = srcinfo->num_components;
  833. /* Compute output image dimensions and related values. */
  834. jpeg_core_output_dimensions(srcinfo);
  835. /* Return right away if -perfect is given and transformation is not perfect.
  836. */
  837. if (info->perfect) {
  838. if (info->num_components == 1) {
  839. if (!jtransform_perfect_transform(srcinfo->output_width,
  840. srcinfo->output_height,
  841. srcinfo->min_DCT_h_scaled_size,
  842. srcinfo->min_DCT_v_scaled_size,
  843. info->transform))
  844. return FALSE;
  845. } else {
  846. if (!jtransform_perfect_transform(srcinfo->output_width,
  847. srcinfo->output_height,
  848. srcinfo->max_h_samp_factor * srcinfo->min_DCT_h_scaled_size,
  849. srcinfo->max_v_samp_factor * srcinfo->min_DCT_v_scaled_size,
  850. info->transform))
  851. return FALSE;
  852. }
  853. }
  854. /* If there is only one output component, force the iMCU size to be 1;
  855. * else use the source iMCU size. (This allows us to do the right thing
  856. * when reducing color to grayscale, and also provides a handy way of
  857. * cleaning up "funny" grayscale images whose sampling factors are not 1x1.)
  858. */
  859. switch (info->transform) {
  860. case JXFORM_TRANSPOSE:
  861. case JXFORM_TRANSVERSE:
  862. case JXFORM_ROT_90:
  863. case JXFORM_ROT_270:
  864. info->output_width = srcinfo->output_height;
  865. info->output_height = srcinfo->output_width;
  866. if (info->num_components == 1) {
  867. info->iMCU_sample_width = srcinfo->min_DCT_v_scaled_size;
  868. info->iMCU_sample_height = srcinfo->min_DCT_h_scaled_size;
  869. } else {
  870. info->iMCU_sample_width =
  871. srcinfo->max_v_samp_factor * srcinfo->min_DCT_v_scaled_size;
  872. info->iMCU_sample_height =
  873. srcinfo->max_h_samp_factor * srcinfo->min_DCT_h_scaled_size;
  874. }
  875. break;
  876. default:
  877. info->output_width = srcinfo->output_width;
  878. info->output_height = srcinfo->output_height;
  879. if (info->num_components == 1) {
  880. info->iMCU_sample_width = srcinfo->min_DCT_h_scaled_size;
  881. info->iMCU_sample_height = srcinfo->min_DCT_v_scaled_size;
  882. } else {
  883. info->iMCU_sample_width =
  884. srcinfo->max_h_samp_factor * srcinfo->min_DCT_h_scaled_size;
  885. info->iMCU_sample_height =
  886. srcinfo->max_v_samp_factor * srcinfo->min_DCT_v_scaled_size;
  887. }
  888. break;
  889. }
  890. /* If cropping has been requested, compute the crop area's position and
  891. * dimensions, ensuring that its upper left corner falls at an iMCU boundary.
  892. */
  893. if (info->crop) {
  894. /* Insert default values for unset crop parameters */
  895. if (info->crop_xoffset_set == JCROP_UNSET)
  896. info->crop_xoffset = 0; /* default to +0 */
  897. if (info->crop_yoffset_set == JCROP_UNSET)
  898. info->crop_yoffset = 0; /* default to +0 */
  899. if (info->crop_xoffset >= info->output_width ||
  900. info->crop_yoffset >= info->output_height)
  901. ERREXIT(srcinfo, JERR_BAD_CROP_SPEC);
  902. if (info->crop_width_set == JCROP_UNSET)
  903. info->crop_width = info->output_width - info->crop_xoffset;
  904. if (info->crop_height_set == JCROP_UNSET)
  905. info->crop_height = info->output_height - info->crop_yoffset;
  906. /* Ensure parameters are valid */
  907. if (info->crop_width <= 0 || info->crop_width > info->output_width ||
  908. info->crop_height <= 0 || info->crop_height > info->output_height ||
  909. info->crop_xoffset > info->output_width - info->crop_width ||
  910. info->crop_yoffset > info->output_height - info->crop_height)
  911. ERREXIT(srcinfo, JERR_BAD_CROP_SPEC);
  912. /* Convert negative crop offsets into regular offsets */
  913. if (info->crop_xoffset_set == JCROP_NEG)
  914. xoffset = info->output_width - info->crop_width - info->crop_xoffset;
  915. else
  916. xoffset = info->crop_xoffset;
  917. if (info->crop_yoffset_set == JCROP_NEG)
  918. yoffset = info->output_height - info->crop_height - info->crop_yoffset;
  919. else
  920. yoffset = info->crop_yoffset;
  921. /* Now adjust so that upper left corner falls at an iMCU boundary */
  922. info->output_width =
  923. info->crop_width + (xoffset % info->iMCU_sample_width);
  924. info->output_height =
  925. info->crop_height + (yoffset % info->iMCU_sample_height);
  926. /* Save x/y offsets measured in iMCUs */
  927. info->x_crop_offset = xoffset / info->iMCU_sample_width;
  928. info->y_crop_offset = yoffset / info->iMCU_sample_height;
  929. } else {
  930. info->x_crop_offset = 0;
  931. info->y_crop_offset = 0;
  932. }
  933. /* Figure out whether we need workspace arrays,
  934. * and if so whether they are transposed relative to the source.
  935. */
  936. need_workspace = FALSE;
  937. transpose_it = FALSE;
  938. switch (info->transform) {
  939. case JXFORM_NONE:
  940. if (info->x_crop_offset != 0 || info->y_crop_offset != 0)
  941. need_workspace = TRUE;
  942. /* No workspace needed if neither cropping nor transforming */
  943. break;
  944. case JXFORM_FLIP_H:
  945. if (info->trim)
  946. trim_right_edge(info, srcinfo->output_width);
  947. if (info->y_crop_offset != 0)
  948. need_workspace = TRUE;
  949. /* do_flip_h_no_crop doesn't need a workspace array */
  950. break;
  951. case JXFORM_FLIP_V:
  952. if (info->trim)
  953. trim_bottom_edge(info, srcinfo->output_height);
  954. /* Need workspace arrays having same dimensions as source image. */
  955. need_workspace = TRUE;
  956. break;
  957. case JXFORM_TRANSPOSE:
  958. /* transpose does NOT have to trim anything */
  959. /* Need workspace arrays having transposed dimensions. */
  960. need_workspace = TRUE;
  961. transpose_it = TRUE;
  962. break;
  963. case JXFORM_TRANSVERSE:
  964. if (info->trim) {
  965. trim_right_edge(info, srcinfo->output_height);
  966. trim_bottom_edge(info, srcinfo->output_width);
  967. }
  968. /* Need workspace arrays having transposed dimensions. */
  969. need_workspace = TRUE;
  970. transpose_it = TRUE;
  971. break;
  972. case JXFORM_ROT_90:
  973. if (info->trim)
  974. trim_right_edge(info, srcinfo->output_height);
  975. /* Need workspace arrays having transposed dimensions. */
  976. need_workspace = TRUE;
  977. transpose_it = TRUE;
  978. break;
  979. case JXFORM_ROT_180:
  980. if (info->trim) {
  981. trim_right_edge(info, srcinfo->output_width);
  982. trim_bottom_edge(info, srcinfo->output_height);
  983. }
  984. /* Need workspace arrays having same dimensions as source image. */
  985. need_workspace = TRUE;
  986. break;
  987. case JXFORM_ROT_270:
  988. if (info->trim)
  989. trim_bottom_edge(info, srcinfo->output_width);
  990. /* Need workspace arrays having transposed dimensions. */
  991. need_workspace = TRUE;
  992. transpose_it = TRUE;
  993. break;
  994. }
  995. /* Allocate workspace if needed.
  996. * Note that we allocate arrays padded out to the next iMCU boundary,
  997. * so that transform routines need not worry about missing edge blocks.
  998. */
  999. if (need_workspace) {
  1000. coef_arrays = (jvirt_barray_ptr *)
  1001. (*srcinfo->mem->alloc_small) ((j_common_ptr) srcinfo, JPOOL_IMAGE,
  1002. SIZEOF(jvirt_barray_ptr) * info->num_components);
  1003. width_in_iMCUs = (JDIMENSION)
  1004. jdiv_round_up((long) info->output_width,
  1005. (long) info->iMCU_sample_width);
  1006. height_in_iMCUs = (JDIMENSION)
  1007. jdiv_round_up((long) info->output_height,
  1008. (long) info->iMCU_sample_height);
  1009. for (ci = 0; ci < info->num_components; ci++) {
  1010. compptr = srcinfo->comp_info + ci;
  1011. if (info->num_components == 1) {
  1012. /* we're going to force samp factors to 1x1 in this case */
  1013. h_samp_factor = v_samp_factor = 1;
  1014. } else if (transpose_it) {
  1015. h_samp_factor = compptr->v_samp_factor;
  1016. v_samp_factor = compptr->h_samp_factor;
  1017. } else {
  1018. h_samp_factor = compptr->h_samp_factor;
  1019. v_samp_factor = compptr->v_samp_factor;
  1020. }
  1021. width_in_blocks = width_in_iMCUs * h_samp_factor;
  1022. height_in_blocks = height_in_iMCUs * v_samp_factor;
  1023. coef_arrays[ci] = (*srcinfo->mem->request_virt_barray)
  1024. ((j_common_ptr) srcinfo, JPOOL_IMAGE, FALSE,
  1025. width_in_blocks, height_in_blocks, (JDIMENSION) v_samp_factor);
  1026. }
  1027. info->workspace_coef_arrays = coef_arrays;
  1028. } else
  1029. info->workspace_coef_arrays = NULL;
  1030. return TRUE;
  1031. }
  1032. /* Transpose destination image parameters */
  1033. LOCAL(void)
  1034. transpose_critical_parameters (j_compress_ptr dstinfo)
  1035. {
  1036. int tblno, i, j, ci, itemp;
  1037. jpeg_component_info *compptr;
  1038. JQUANT_TBL *qtblptr;
  1039. JDIMENSION jtemp;
  1040. UINT16 qtemp;
  1041. /* Transpose image dimensions */
  1042. jtemp = dstinfo->image_width;
  1043. dstinfo->image_width = dstinfo->image_height;
  1044. dstinfo->image_height = jtemp;
  1045. itemp = dstinfo->min_DCT_h_scaled_size;
  1046. dstinfo->min_DCT_h_scaled_size = dstinfo->min_DCT_v_scaled_size;
  1047. dstinfo->min_DCT_v_scaled_size = itemp;
  1048. /* Transpose sampling factors */
  1049. for (ci = 0; ci < dstinfo->num_components; ci++) {
  1050. compptr = dstinfo->comp_info + ci;
  1051. itemp = compptr->h_samp_factor;
  1052. compptr->h_samp_factor = compptr->v_samp_factor;
  1053. compptr->v_samp_factor = itemp;
  1054. }
  1055. /* Transpose quantization tables */
  1056. for (tblno = 0; tblno < NUM_QUANT_TBLS; tblno++) {
  1057. qtblptr = dstinfo->quant_tbl_ptrs[tblno];
  1058. if (qtblptr != NULL) {
  1059. for (i = 0; i < DCTSIZE; i++) {
  1060. for (j = 0; j < i; j++) {
  1061. qtemp = qtblptr->quantval[i*DCTSIZE+j];
  1062. qtblptr->quantval[i*DCTSIZE+j] = qtblptr->quantval[j*DCTSIZE+i];
  1063. qtblptr->quantval[j*DCTSIZE+i] = qtemp;
  1064. }
  1065. }
  1066. }
  1067. }
  1068. }
  1069. /* Adjust Exif image parameters.
  1070. *
  1071. * We try to adjust the Tags ExifImageWidth and ExifImageHeight if possible.
  1072. */
  1073. LOCAL(void)
  1074. adjust_exif_parameters (JOCTET FAR * data, unsigned int length,
  1075. JDIMENSION new_width, JDIMENSION new_height)
  1076. {
  1077. boolean is_motorola; /* Flag for byte order */
  1078. unsigned int number_of_tags, tagnum;
  1079. unsigned int firstoffset, offset;
  1080. JDIMENSION new_value;
  1081. if (length < 12) return; /* Length of an IFD entry */
  1082. /* Discover byte order */
  1083. if (GETJOCTET(data[0]) == 0x49 && GETJOCTET(data[1]) == 0x49)
  1084. is_motorola = FALSE;
  1085. else if (GETJOCTET(data[0]) == 0x4D && GETJOCTET(data[1]) == 0x4D)
  1086. is_motorola = TRUE;
  1087. else
  1088. return;
  1089. /* Check Tag Mark */
  1090. if (is_motorola) {
  1091. if (GETJOCTET(data[2]) != 0) return;
  1092. if (GETJOCTET(data[3]) != 0x2A) return;
  1093. } else {
  1094. if (GETJOCTET(data[3]) != 0) return;
  1095. if (GETJOCTET(data[2]) != 0x2A) return;
  1096. }
  1097. /* Get first IFD offset (offset to IFD0) */
  1098. if (is_motorola) {
  1099. if (GETJOCTET(data[4]) != 0) return;
  1100. if (GETJOCTET(data[5]) != 0) return;
  1101. firstoffset = GETJOCTET(data[6]);
  1102. firstoffset <<= 8;
  1103. firstoffset += GETJOCTET(data[7]);
  1104. } else {
  1105. if (GETJOCTET(data[7]) != 0) return;
  1106. if (GETJOCTET(data[6]) != 0) return;
  1107. firstoffset = GETJOCTET(data[5]);
  1108. firstoffset <<= 8;
  1109. firstoffset += GETJOCTET(data[4]);
  1110. }
  1111. if (firstoffset > length - 2) return; /* check end of data segment */
  1112. /* Get the number of directory entries contained in this IFD */
  1113. if (is_motorola) {
  1114. number_of_tags = GETJOCTET(data[firstoffset]);
  1115. number_of_tags <<= 8;
  1116. number_of_tags += GETJOCTET(data[firstoffset+1]);
  1117. } else {
  1118. number_of_tags = GETJOCTET(data[firstoffset+1]);
  1119. number_of_tags <<= 8;
  1120. number_of_tags += GETJOCTET(data[firstoffset]);
  1121. }
  1122. if (number_of_tags == 0) return;
  1123. firstoffset += 2;
  1124. /* Search for ExifSubIFD offset Tag in IFD0 */
  1125. for (;;) {
  1126. if (firstoffset > length - 12) return; /* check end of data segment */
  1127. /* Get Tag number */
  1128. if (is_motorola) {
  1129. tagnum = GETJOCTET(data[firstoffset]);
  1130. tagnum <<= 8;
  1131. tagnum += GETJOCTET(data[firstoffset+1]);
  1132. } else {
  1133. tagnum = GETJOCTET(data[firstoffset+1]);
  1134. tagnum <<= 8;
  1135. tagnum += GETJOCTET(data[firstoffset]);
  1136. }
  1137. if (tagnum == 0x8769) break; /* found ExifSubIFD offset Tag */
  1138. if (--number_of_tags == 0) return;
  1139. firstoffset += 12;
  1140. }
  1141. /* Get the ExifSubIFD offset */
  1142. if (is_motorola) {
  1143. if (GETJOCTET(data[firstoffset+8]) != 0) return;
  1144. if (GETJOCTET(data[firstoffset+9]) != 0) return;
  1145. offset = GETJOCTET(data[firstoffset+10]);
  1146. offset <<= 8;
  1147. offset += GETJOCTET(data[firstoffset+11]);
  1148. } else {
  1149. if (GETJOCTET(data[firstoffset+11]) != 0) return;
  1150. if (GETJOCTET(data[firstoffset+10]) != 0) return;
  1151. offset = GETJOCTET(data[firstoffset+9]);
  1152. offset <<= 8;
  1153. offset += GETJOCTET(data[firstoffset+8]);
  1154. }
  1155. if (offset > length - 2) return; /* check end of data segment */
  1156. /* Get the number of directory entries contained in this SubIFD */
  1157. if (is_motorola) {
  1158. number_of_tags = GETJOCTET(data[offset]);
  1159. number_of_tags <<= 8;
  1160. number_of_tags += GETJOCTET(data[offset+1]);
  1161. } else {
  1162. number_of_tags = GETJOCTET(data[offset+1]);
  1163. number_of_tags <<= 8;
  1164. number_of_tags += GETJOCTET(data[offset]);
  1165. }
  1166. if (number_of_tags < 2) return;
  1167. offset += 2;
  1168. /* Search for ExifImageWidth and ExifImageHeight Tags in this SubIFD */
  1169. do {
  1170. if (offset > length - 12) return; /* check end of data segment */
  1171. /* Get Tag number */
  1172. if (is_motorola) {
  1173. tagnum = GETJOCTET(data[offset]);
  1174. tagnum <<= 8;
  1175. tagnum += GETJOCTET(data[offset+1]);
  1176. } else {
  1177. tagnum = GETJOCTET(data[offset+1]);
  1178. tagnum <<= 8;
  1179. tagnum += GETJOCTET(data[offset]);
  1180. }
  1181. if (tagnum == 0xA002 || tagnum == 0xA003) {
  1182. if (tagnum == 0xA002)
  1183. new_value = new_width; /* ExifImageWidth Tag */
  1184. else
  1185. new_value = new_height; /* ExifImageHeight Tag */
  1186. if (is_motorola) {
  1187. data[offset+2] = 0; /* Format = unsigned long (4 octets) */
  1188. data[offset+3] = 4;
  1189. data[offset+4] = 0; /* Number Of Components = 1 */
  1190. data[offset+5] = 0;
  1191. data[offset+6] = 0;
  1192. data[offset+7] = 1;
  1193. data[offset+8] = 0;
  1194. data[offset+9] = 0;
  1195. data[offset+10] = (JOCTET)((new_value >> 8) & 0xFF);
  1196. data[offset+11] = (JOCTET)(new_value & 0xFF);
  1197. } else {
  1198. data[offset+2] = 4; /* Format = unsigned long (4 octets) */
  1199. data[offset+3] = 0;
  1200. data[offset+4] = 1; /* Number Of Components = 1 */
  1201. data[offset+5] = 0;
  1202. data[offset+6] = 0;
  1203. data[offset+7] = 0;
  1204. data[offset+8] = (JOCTET)(new_value & 0xFF);
  1205. data[offset+9] = (JOCTET)((new_value >> 8) & 0xFF);
  1206. data[offset+10] = 0;
  1207. data[offset+11] = 0;
  1208. }
  1209. }
  1210. offset += 12;
  1211. } while (--number_of_tags);
  1212. }
  1213. /* Adjust output image parameters as needed.
  1214. *
  1215. * This must be called after jpeg_copy_critical_parameters()
  1216. * and before jpeg_write_coefficients().
  1217. *
  1218. * The return value is the set of virtual coefficient arrays to be written
  1219. * (either the ones allocated by jtransform_request_workspace, or the
  1220. * original source data arrays). The caller will need to pass this value
  1221. * to jpeg_write_coefficients().
  1222. */
  1223. GLOBAL(jvirt_barray_ptr *)
  1224. jtransform_adjust_parameters (j_decompress_ptr srcinfo,
  1225. j_compress_ptr dstinfo,
  1226. jvirt_barray_ptr *src_coef_arrays,
  1227. jpeg_transform_info *info)
  1228. {
  1229. /* If force-to-grayscale is requested, adjust destination parameters */
  1230. if (info->force_grayscale) {
  1231. /* First, ensure we have YCbCr or grayscale data, and that the source's
  1232. * Y channel is full resolution. (No reasonable person would make Y
  1233. * be less than full resolution, so actually coping with that case
  1234. * isn't worth extra code space. But we check it to avoid crashing.)
  1235. */
  1236. if (((dstinfo->jpeg_color_space == JCS_YCbCr &&
  1237. dstinfo->num_components == 3) ||
  1238. (dstinfo->jpeg_color_space == JCS_GRAYSCALE &&
  1239. dstinfo->num_components == 1)) &&
  1240. srcinfo->comp_info[0].h_samp_factor == srcinfo->max_h_samp_factor &&
  1241. srcinfo->comp_info[0].v_samp_factor == srcinfo->max_v_samp_factor) {
  1242. /* We use jpeg_set_colorspace to make sure subsidiary settings get fixed
  1243. * properly. Among other things, it sets the target h_samp_factor &
  1244. * v_samp_factor to 1, which typically won't match the source.
  1245. * We have to preserve the source's quantization table number, however.
  1246. */
  1247. int sv_quant_tbl_no = dstinfo->comp_info[0].quant_tbl_no;
  1248. jpeg_set_colorspace(dstinfo, JCS_GRAYSCALE);
  1249. dstinfo->comp_info[0].quant_tbl_no = sv_quant_tbl_no;
  1250. } else {
  1251. /* Sorry, can't do it */
  1252. ERREXIT(dstinfo, JERR_CONVERSION_NOTIMPL);
  1253. }
  1254. } else if (info->num_components == 1) {
  1255. /* For a single-component source, we force the destination sampling factors
  1256. * to 1x1, with or without force_grayscale. This is useful because some
  1257. * decoders choke on grayscale images with other sampling factors.
  1258. */
  1259. dstinfo->comp_info[0].h_samp_factor = 1;
  1260. dstinfo->comp_info[0].v_samp_factor = 1;
  1261. }
  1262. /* Correct the destination's image dimensions as necessary
  1263. * for rotate/flip, resize, and crop operations.
  1264. */
  1265. dstinfo->jpeg_width = info->output_width;
  1266. dstinfo->jpeg_height = info->output_height;
  1267. /* Transpose destination image parameters */
  1268. switch (info->transform) {
  1269. case JXFORM_TRANSPOSE:
  1270. case JXFORM_TRANSVERSE:
  1271. case JXFORM_ROT_90:
  1272. case JXFORM_ROT_270:
  1273. transpose_critical_parameters(dstinfo);
  1274. break;
  1275. default:
  1276. break;
  1277. }
  1278. /* Adjust Exif properties */
  1279. if (srcinfo->marker_list != NULL &&
  1280. srcinfo->marker_list->marker == JPEG_APP0+1 &&
  1281. srcinfo->marker_list->data_length >= 6 &&
  1282. GETJOCTET(srcinfo->marker_list->data[0]) == 0x45 &&
  1283. GETJOCTET(srcinfo->marker_list->data[1]) == 0x78 &&
  1284. GETJOCTET(srcinfo->marker_list->data[2]) == 0x69 &&
  1285. GETJOCTET(srcinfo->marker_list->data[3]) == 0x66 &&
  1286. GETJOCTET(srcinfo->marker_list->data[4]) == 0 &&
  1287. GETJOCTET(srcinfo->marker_list->data[5]) == 0) {
  1288. /* Suppress output of JFIF marker */
  1289. dstinfo->write_JFIF_header = FALSE;
  1290. /* Adjust Exif image parameters */
  1291. if (dstinfo->jpeg_width != srcinfo->image_width ||
  1292. dstinfo->jpeg_height != srcinfo->image_height)
  1293. /* Align data segment to start of TIFF structure for parsing */
  1294. adjust_exif_parameters(srcinfo->marker_list->data + 6,
  1295. srcinfo->marker_list->data_length - 6,
  1296. dstinfo->jpeg_width, dstinfo->jpeg_height);
  1297. }
  1298. /* Return the appropriate output data set */
  1299. if (info->workspace_coef_arrays != NULL)
  1300. return info->workspace_coef_arrays;
  1301. return src_coef_arrays;
  1302. }
  1303. /* Execute the actual transformation, if any.
  1304. *
  1305. * This must be called *after* jpeg_write_coefficients, because it depends
  1306. * on jpeg_write_coefficients to have computed subsidiary values such as
  1307. * the per-component width and height fields in the destination object.
  1308. *
  1309. * Note that some transformations will modify the source data arrays!
  1310. */
  1311. GLOBAL(void)
  1312. jtransform_execute_transform (j_decompress_ptr srcinfo,
  1313. j_compress_ptr dstinfo,
  1314. jvirt_barray_ptr *src_coef_arrays,
  1315. jpeg_transform_info *info)
  1316. {
  1317. jvirt_barray_ptr *dst_coef_arrays = info->workspace_coef_arrays;
  1318. /* Note: conditions tested here should match those in switch statement
  1319. * in jtransform_request_workspace()
  1320. */
  1321. switch (info->transform) {
  1322. case JXFORM_NONE:
  1323. if (info->x_crop_offset != 0 || info->y_crop_offset != 0)
  1324. do_crop(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset,
  1325. src_coef_arrays, dst_coef_arrays);
  1326. break;
  1327. case JXFORM_FLIP_H:
  1328. if (info->y_crop_offset != 0)
  1329. do_flip_h(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset,
  1330. src_coef_arrays, dst_coef_arrays);
  1331. else
  1332. do_flip_h_no_crop(srcinfo, dstinfo, info->x_crop_offset,
  1333. src_coef_arrays);
  1334. break;
  1335. case JXFORM_FLIP_V:
  1336. do_flip_v(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset,
  1337. src_coef_arrays, dst_coef_arrays);
  1338. break;
  1339. case JXFORM_TRANSPOSE:
  1340. do_transpose(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset,
  1341. src_coef_arrays, dst_coef_arrays);
  1342. break;
  1343. case JXFORM_TRANSVERSE:
  1344. do_transverse(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset,
  1345. src_coef_arrays, dst_coef_arrays);
  1346. break;
  1347. case JXFORM_ROT_90:
  1348. do_rot_90(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset,
  1349. src_coef_arrays, dst_coef_arrays);
  1350. break;
  1351. case JXFORM_ROT_180:
  1352. do_rot_180(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset,
  1353. src_coef_arrays, dst_coef_arrays);
  1354. break;
  1355. case JXFORM_ROT_270:
  1356. do_rot_270(srcinfo, dstinfo, info->x_crop_offset, info->y_crop_offset,
  1357. src_coef_arrays, dst_coef_arrays);
  1358. break;
  1359. }
  1360. }
  1361. /* jtransform_perfect_transform
  1362. *
  1363. * Determine whether lossless transformation is perfectly
  1364. * possible for a specified image and transformation.
  1365. *
  1366. * Inputs:
  1367. * image_width, image_height: source image dimensions.
  1368. * MCU_width, MCU_height: pixel dimensions of MCU.
  1369. * transform: transformation identifier.
  1370. * Parameter sources from initialized jpeg_struct
  1371. * (after reading source header):
  1372. * image_width = cinfo.image_width
  1373. * image_height = cinfo.image_height
  1374. * MCU_width = cinfo.max_h_samp_factor * cinfo.block_size
  1375. * MCU_height = cinfo.max_v_samp_factor * cinfo.block_size
  1376. * Result:
  1377. * TRUE = perfect transformation possible
  1378. * FALSE = perfect transformation not possible
  1379. * (may use custom action then)
  1380. */
  1381. GLOBAL(boolean)
  1382. jtransform_perfect_transform(JDIMENSION image_width, JDIMENSION image_height,
  1383. int MCU_width, int MCU_height,
  1384. JXFORM_CODE transform)
  1385. {
  1386. boolean result = TRUE; /* initialize TRUE */
  1387. switch (transform) {
  1388. case JXFORM_FLIP_H:
  1389. case JXFORM_ROT_270:
  1390. if (image_width % (JDIMENSION) MCU_width)
  1391. result = FALSE;
  1392. break;
  1393. case JXFORM_FLIP_V:
  1394. case JXFORM_ROT_90:
  1395. if (image_height % (JDIMENSION) MCU_height)
  1396. result = FALSE;
  1397. break;
  1398. case JXFORM_TRANSVERSE:
  1399. case JXFORM_ROT_180:
  1400. if (image_width % (JDIMENSION) MCU_width)
  1401. result = FALSE;
  1402. if (image_height % (JDIMENSION) MCU_height)
  1403. result = FALSE;
  1404. break;
  1405. default:
  1406. break;
  1407. }
  1408. return result;
  1409. }
  1410. #endif /* TRANSFORMS_SUPPORTED */
  1411. /* Setup decompression object to save desired markers in memory.
  1412. * This must be called before jpeg_read_header() to have the desired effect.
  1413. */
  1414. GLOBAL(void)
  1415. jcopy_markers_setup (j_decompress_ptr srcinfo, JCOPY_OPTION option)
  1416. {
  1417. #ifdef SAVE_MARKERS_SUPPORTED
  1418. int m;
  1419. /* Save comments except under NONE option */
  1420. if (option != JCOPYOPT_NONE) {
  1421. jpeg_save_markers(srcinfo, JPEG_COM, 0xFFFF);
  1422. }
  1423. /* Save all types of APPn markers iff ALL option */
  1424. if (option == JCOPYOPT_ALL) {
  1425. for (m = 0; m < 16; m++)
  1426. jpeg_save_markers(srcinfo, JPEG_APP0 + m, 0xFFFF);
  1427. }
  1428. #endif /* SAVE_MARKERS_SUPPORTED */
  1429. }
  1430. /* Copy markers saved in the given source object to the destination object.
  1431. * This should be called just after jpeg_start_compress() or
  1432. * jpeg_write_coefficients().
  1433. * Note that those routines will have written the SOI, and also the
  1434. * JFIF APP0 or Adobe APP14 markers if selected.
  1435. */
  1436. GLOBAL(void)
  1437. jcopy_markers_execute (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
  1438. JCOPY_OPTION option)
  1439. {
  1440. jpeg_saved_marker_ptr marker;
  1441. /* In the current implementation, we don't actually need to examine the
  1442. * option flag here; we just copy everything that got saved.
  1443. * But to avoid confusion, we do not output JFIF and Adobe APP14 markers
  1444. * if the encoder library already wrote one.
  1445. */
  1446. for (marker = srcinfo->marker_list; marker != NULL; marker = marker->next) {
  1447. if (dstinfo->write_JFIF_header &&
  1448. marker->marker == JPEG_APP0 &&
  1449. marker->data_length >= 5 &&
  1450. GETJOCTET(marker->data[0]) == 0x4A &&
  1451. GETJOCTET(marker->data[1]) == 0x46 &&
  1452. GETJOCTET(marker->data[2]) == 0x49 &&
  1453. GETJOCTET(marker->data[3]) == 0x46 &&
  1454. GETJOCTET(marker->data[4]) == 0)
  1455. continue; /* reject duplicate JFIF */
  1456. if (dstinfo->write_Adobe_marker &&
  1457. marker->marker == JPEG_APP0+14 &&
  1458. marker->data_length >= 5 &&
  1459. GETJOCTET(marker->data[0]) == 0x41 &&
  1460. GETJOCTET(marker->data[1]) == 0x64 &&
  1461. GETJOCTET(marker->data[2]) == 0x6F &&
  1462. GETJOCTET(marker->data[3]) == 0x62 &&
  1463. GETJOCTET(marker->data[4]) == 0x65)
  1464. continue; /* reject duplicate Adobe */
  1465. #ifdef NEED_FAR_POINTERS
  1466. /* We could use jpeg_write_marker if the data weren't FAR... */
  1467. {
  1468. unsigned int i;
  1469. jpeg_write_m_header(dstinfo, marker->marker, marker->data_length);
  1470. for (i = 0; i < marker->data_length; i++)
  1471. jpeg_write_m_byte(dstinfo, marker->data[i]);
  1472. }
  1473. #else
  1474. jpeg_write_marker(dstinfo, marker->marker,
  1475. marker->data, marker->data_length);
  1476. #endif
  1477. }
  1478. }