transupp.c 61 KB

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