transupp.c 57 KB

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