Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

transupp.c 33KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  1. /*
  2. * transupp.c
  3. *
  4. * Copyright (C) 1997, Thomas G. Lane.
  5. * This file is part of the Independent JPEG Group's software.
  6. * For conditions of distribution and use, see the accompanying README file.
  7. *
  8. * This file contains image transformation routines and other utility code
  9. * used by the jpegtran sample application. These are NOT part of the core
  10. * JPEG library. But we keep these routines separate from jpegtran.c to
  11. * ease the task of maintaining jpegtran-like programs that have other user
  12. * interfaces.
  13. */
  14. /* Although this file really shouldn't have access to the library internals,
  15. * it's helpful to let it call jround_up() and jcopy_block_row().
  16. */
  17. #define JPEG_INTERNALS
  18. #include "jinclude.h"
  19. #include "jpeglib.h"
  20. #include "transupp.h" /* My own external interface */
  21. #if TRANSFORMS_SUPPORTED
  22. /*
  23. * Lossless image transformation routines. These routines work on DCT
  24. * coefficient arrays and thus do not require any lossy decompression
  25. * or recompression of the image.
  26. * Thanks to Guido Vollbeding for the initial design and code of this feature.
  27. *
  28. * Horizontal flipping is done in-place, using a single top-to-bottom
  29. * pass through the virtual source array. It will thus be much the
  30. * fastest option for images larger than main memory.
  31. *
  32. * The other routines require a set of destination virtual arrays, so they
  33. * need twice as much memory as jpegtran normally does. The destination
  34. * arrays are always written in normal scan order (top to bottom) because
  35. * the virtual array manager expects this. The source arrays will be scanned
  36. * in the corresponding order, which means multiple passes through the source
  37. * arrays for most of the transforms. That could result in much thrashing
  38. * if the image is larger than main memory.
  39. *
  40. * Some notes about the operating environment of the individual transform
  41. * routines:
  42. * 1. Both the source and destination virtual arrays are allocated from the
  43. * source JPEG object, and therefore should be manipulated by calling the
  44. * source's memory manager.
  45. * 2. The destination's component count should be used. It may be smaller
  46. * than the source's when forcing to grayscale.
  47. * 3. Likewise the destination's sampling factors should be used. When
  48. * forcing to grayscale the destination's sampling factors will be all 1,
  49. * and we may as well take that as the effective iMCU size.
  50. * 4. When "trim" is in effect, the destination's dimensions will be the
  51. * trimmed values but the source's will be untrimmed.
  52. * 5. All the routines assume that the source and destination buffers are
  53. * padded out to a full iMCU boundary. This is true, although for the
  54. * source buffer it is an undocumented property of jdcoefct.c.
  55. * Notes 2,3,4 boil down to this: generally we should use the destination's
  56. * dimensions and ignore the source's.
  57. */
  58. LOCAL(void)
  59. do_flip_h (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
  60. jvirt_barray_ptr *src_coef_arrays)
  61. /* Horizontal flip; done in-place, so no separate dest array is required */
  62. {
  63. JDIMENSION MCU_cols, comp_width, blk_x, blk_y;
  64. int ci, k, offset_y;
  65. JBLOCKARRAY buffer;
  66. JCOEFPTR ptr1, ptr2;
  67. JCOEF temp1, temp2;
  68. jpeg_component_info *compptr;
  69. /* Horizontal mirroring of DCT blocks is accomplished by swapping
  70. * pairs of blocks in-place. Within a DCT block, we perform horizontal
  71. * mirroring by changing the signs of odd-numbered columns.
  72. * Partial iMCUs at the right edge are left untouched.
  73. */
  74. MCU_cols = dstinfo->image_width / (dstinfo->max_h_samp_factor * DCTSIZE);
  75. for (ci = 0; ci < dstinfo->num_components; ci++) {
  76. compptr = dstinfo->comp_info + ci;
  77. comp_width = MCU_cols * compptr->h_samp_factor;
  78. for (blk_y = 0; blk_y < compptr->height_in_blocks;
  79. blk_y += compptr->v_samp_factor) {
  80. buffer = (*srcinfo->mem->access_virt_barray)
  81. ((j_common_ptr) srcinfo, src_coef_arrays[ci], blk_y,
  82. (JDIMENSION) compptr->v_samp_factor, TRUE);
  83. for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
  84. for (blk_x = 0; blk_x * 2 < comp_width; blk_x++) {
  85. ptr1 = buffer[offset_y][blk_x];
  86. ptr2 = buffer[offset_y][comp_width - blk_x - 1];
  87. /* this unrolled loop doesn't need to know which row it's on... */
  88. for (k = 0; k < DCTSIZE2; k += 2) {
  89. temp1 = *ptr1; /* swap even column */
  90. temp2 = *ptr2;
  91. *ptr1++ = temp2;
  92. *ptr2++ = temp1;
  93. temp1 = *ptr1; /* swap odd column with sign change */
  94. temp2 = *ptr2;
  95. *ptr1++ = -temp2;
  96. *ptr2++ = -temp1;
  97. }
  98. }
  99. }
  100. }
  101. }
  102. }
  103. LOCAL(void)
  104. do_flip_v (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
  105. jvirt_barray_ptr *src_coef_arrays,
  106. jvirt_barray_ptr *dst_coef_arrays)
  107. /* Vertical flip */
  108. {
  109. JDIMENSION MCU_rows, comp_height, dst_blk_x, dst_blk_y;
  110. int ci, i, j, offset_y;
  111. JBLOCKARRAY src_buffer, dst_buffer;
  112. JBLOCKROW src_row_ptr, dst_row_ptr;
  113. JCOEFPTR src_ptr, dst_ptr;
  114. jpeg_component_info *compptr;
  115. /* We output into a separate array because we can't touch different
  116. * rows of the source virtual array simultaneously. Otherwise, this
  117. * is a pretty straightforward analog of horizontal flip.
  118. * Within a DCT block, vertical mirroring is done by changing the signs
  119. * of odd-numbered rows.
  120. * Partial iMCUs at the bottom edge are copied verbatim.
  121. */
  122. MCU_rows = dstinfo->image_height / (dstinfo->max_v_samp_factor * DCTSIZE);
  123. for (ci = 0; ci < dstinfo->num_components; ci++) {
  124. compptr = dstinfo->comp_info + ci;
  125. comp_height = MCU_rows * compptr->v_samp_factor;
  126. for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks;
  127. dst_blk_y += compptr->v_samp_factor) {
  128. dst_buffer = (*srcinfo->mem->access_virt_barray)
  129. ((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y,
  130. (JDIMENSION) compptr->v_samp_factor, TRUE);
  131. if (dst_blk_y < comp_height) {
  132. /* Row is within the mirrorable area. */
  133. src_buffer = (*srcinfo->mem->access_virt_barray)
  134. ((j_common_ptr) srcinfo, src_coef_arrays[ci],
  135. comp_height - dst_blk_y - (JDIMENSION) compptr->v_samp_factor,
  136. (JDIMENSION) compptr->v_samp_factor, FALSE);
  137. } else {
  138. /* Bottom-edge blocks will be copied verbatim. */
  139. src_buffer = (*srcinfo->mem->access_virt_barray)
  140. ((j_common_ptr) srcinfo, src_coef_arrays[ci], dst_blk_y,
  141. (JDIMENSION) compptr->v_samp_factor, FALSE);
  142. }
  143. for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
  144. if (dst_blk_y < comp_height) {
  145. /* Row is within the mirrorable area. */
  146. dst_row_ptr = dst_buffer[offset_y];
  147. src_row_ptr = src_buffer[compptr->v_samp_factor - offset_y - 1];
  148. for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks;
  149. dst_blk_x++) {
  150. dst_ptr = dst_row_ptr[dst_blk_x];
  151. src_ptr = src_row_ptr[dst_blk_x];
  152. for (i = 0; i < DCTSIZE; i += 2) {
  153. /* copy even row */
  154. for (j = 0; j < DCTSIZE; j++)
  155. *dst_ptr++ = *src_ptr++;
  156. /* copy odd row with sign change */
  157. for (j = 0; j < DCTSIZE; j++)
  158. *dst_ptr++ = - *src_ptr++;
  159. }
  160. }
  161. } else {
  162. /* Just copy row verbatim. */
  163. jcopy_block_row(src_buffer[offset_y], dst_buffer[offset_y],
  164. compptr->width_in_blocks);
  165. }
  166. }
  167. }
  168. }
  169. }
  170. LOCAL(void)
  171. do_transpose (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
  172. jvirt_barray_ptr *src_coef_arrays,
  173. jvirt_barray_ptr *dst_coef_arrays)
  174. /* Transpose source into destination */
  175. {
  176. JDIMENSION dst_blk_x, dst_blk_y;
  177. int ci, i, j, offset_x, offset_y;
  178. JBLOCKARRAY src_buffer, dst_buffer;
  179. JCOEFPTR src_ptr, dst_ptr;
  180. jpeg_component_info *compptr;
  181. /* Transposing pixels within a block just requires transposing the
  182. * DCT coefficients.
  183. * Partial iMCUs at the edges require no special treatment; we simply
  184. * process all the available DCT blocks for every component.
  185. */
  186. for (ci = 0; ci < dstinfo->num_components; ci++) {
  187. compptr = dstinfo->comp_info + ci;
  188. for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks;
  189. dst_blk_y += compptr->v_samp_factor) {
  190. dst_buffer = (*srcinfo->mem->access_virt_barray)
  191. ((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y,
  192. (JDIMENSION) compptr->v_samp_factor, TRUE);
  193. for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
  194. for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks;
  195. dst_blk_x += compptr->h_samp_factor) {
  196. src_buffer = (*srcinfo->mem->access_virt_barray)
  197. ((j_common_ptr) srcinfo, src_coef_arrays[ci], dst_blk_x,
  198. (JDIMENSION) compptr->h_samp_factor, FALSE);
  199. for (offset_x = 0; offset_x < compptr->h_samp_factor; offset_x++) {
  200. src_ptr = src_buffer[offset_x][dst_blk_y + offset_y];
  201. dst_ptr = dst_buffer[offset_y][dst_blk_x + offset_x];
  202. for (i = 0; i < DCTSIZE; i++)
  203. for (j = 0; j < DCTSIZE; j++)
  204. dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j];
  205. }
  206. }
  207. }
  208. }
  209. }
  210. }
  211. LOCAL(void)
  212. do_rot_90 (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
  213. jvirt_barray_ptr *src_coef_arrays,
  214. jvirt_barray_ptr *dst_coef_arrays)
  215. /* 90 degree rotation is equivalent to
  216. * 1. Transposing the image;
  217. * 2. Horizontal mirroring.
  218. * These two steps are merged into a single processing routine.
  219. */
  220. {
  221. JDIMENSION MCU_cols, comp_width, dst_blk_x, dst_blk_y;
  222. int ci, i, j, offset_x, offset_y;
  223. JBLOCKARRAY src_buffer, dst_buffer;
  224. JCOEFPTR src_ptr, dst_ptr;
  225. jpeg_component_info *compptr;
  226. /* Because of the horizontal mirror step, we can't process partial iMCUs
  227. * at the (output) right edge properly. They just get transposed and
  228. * not mirrored.
  229. */
  230. MCU_cols = dstinfo->image_width / (dstinfo->max_h_samp_factor * DCTSIZE);
  231. for (ci = 0; ci < dstinfo->num_components; ci++) {
  232. compptr = dstinfo->comp_info + ci;
  233. comp_width = MCU_cols * compptr->h_samp_factor;
  234. for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks;
  235. dst_blk_y += compptr->v_samp_factor) {
  236. dst_buffer = (*srcinfo->mem->access_virt_barray)
  237. ((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y,
  238. (JDIMENSION) compptr->v_samp_factor, TRUE);
  239. for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
  240. for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks;
  241. dst_blk_x += compptr->h_samp_factor) {
  242. src_buffer = (*srcinfo->mem->access_virt_barray)
  243. ((j_common_ptr) srcinfo, src_coef_arrays[ci], dst_blk_x,
  244. (JDIMENSION) compptr->h_samp_factor, FALSE);
  245. for (offset_x = 0; offset_x < compptr->h_samp_factor; offset_x++) {
  246. src_ptr = src_buffer[offset_x][dst_blk_y + offset_y];
  247. if (dst_blk_x < comp_width) {
  248. /* Block is within the mirrorable area. */
  249. dst_ptr = dst_buffer[offset_y]
  250. [comp_width - dst_blk_x - offset_x - 1];
  251. for (i = 0; i < DCTSIZE; i++) {
  252. for (j = 0; j < DCTSIZE; j++)
  253. dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j];
  254. i++;
  255. for (j = 0; j < DCTSIZE; j++)
  256. dst_ptr[j*DCTSIZE+i] = -src_ptr[i*DCTSIZE+j];
  257. }
  258. } else {
  259. /* Edge blocks are transposed but not mirrored. */
  260. dst_ptr = dst_buffer[offset_y][dst_blk_x + offset_x];
  261. for (i = 0; i < DCTSIZE; i++)
  262. for (j = 0; j < DCTSIZE; j++)
  263. dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j];
  264. }
  265. }
  266. }
  267. }
  268. }
  269. }
  270. }
  271. LOCAL(void)
  272. do_rot_270 (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
  273. jvirt_barray_ptr *src_coef_arrays,
  274. jvirt_barray_ptr *dst_coef_arrays)
  275. /* 270 degree rotation is equivalent to
  276. * 1. Horizontal mirroring;
  277. * 2. Transposing the image.
  278. * These two steps are merged into a single processing routine.
  279. */
  280. {
  281. JDIMENSION MCU_rows, comp_height, dst_blk_x, dst_blk_y;
  282. int ci, i, j, offset_x, offset_y;
  283. JBLOCKARRAY src_buffer, dst_buffer;
  284. JCOEFPTR src_ptr, dst_ptr;
  285. jpeg_component_info *compptr;
  286. /* Because of the horizontal mirror step, we can't process partial iMCUs
  287. * at the (output) bottom edge properly. They just get transposed and
  288. * not mirrored.
  289. */
  290. MCU_rows = dstinfo->image_height / (dstinfo->max_v_samp_factor * DCTSIZE);
  291. for (ci = 0; ci < dstinfo->num_components; ci++) {
  292. compptr = dstinfo->comp_info + ci;
  293. comp_height = MCU_rows * compptr->v_samp_factor;
  294. for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks;
  295. dst_blk_y += compptr->v_samp_factor) {
  296. dst_buffer = (*srcinfo->mem->access_virt_barray)
  297. ((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y,
  298. (JDIMENSION) compptr->v_samp_factor, TRUE);
  299. for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
  300. for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks;
  301. dst_blk_x += compptr->h_samp_factor) {
  302. src_buffer = (*srcinfo->mem->access_virt_barray)
  303. ((j_common_ptr) srcinfo, src_coef_arrays[ci], dst_blk_x,
  304. (JDIMENSION) compptr->h_samp_factor, FALSE);
  305. for (offset_x = 0; offset_x < compptr->h_samp_factor; offset_x++) {
  306. dst_ptr = dst_buffer[offset_y][dst_blk_x + offset_x];
  307. if (dst_blk_y < comp_height) {
  308. /* Block is within the mirrorable area. */
  309. src_ptr = src_buffer[offset_x]
  310. [comp_height - dst_blk_y - offset_y - 1];
  311. for (i = 0; i < DCTSIZE; i++) {
  312. for (j = 0; j < DCTSIZE; j++) {
  313. dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j];
  314. j++;
  315. dst_ptr[j*DCTSIZE+i] = -src_ptr[i*DCTSIZE+j];
  316. }
  317. }
  318. } else {
  319. /* Edge blocks are transposed but not mirrored. */
  320. src_ptr = src_buffer[offset_x][dst_blk_y + offset_y];
  321. for (i = 0; i < DCTSIZE; i++)
  322. for (j = 0; j < DCTSIZE; j++)
  323. dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j];
  324. }
  325. }
  326. }
  327. }
  328. }
  329. }
  330. }
  331. LOCAL(void)
  332. do_rot_180 (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
  333. jvirt_barray_ptr *src_coef_arrays,
  334. jvirt_barray_ptr *dst_coef_arrays)
  335. /* 180 degree rotation is equivalent to
  336. * 1. Vertical mirroring;
  337. * 2. Horizontal mirroring.
  338. * These two steps are merged into a single processing routine.
  339. */
  340. {
  341. JDIMENSION MCU_cols, MCU_rows, comp_width, comp_height, dst_blk_x, dst_blk_y;
  342. int ci, i, j, offset_y;
  343. JBLOCKARRAY src_buffer, dst_buffer;
  344. JBLOCKROW src_row_ptr, dst_row_ptr;
  345. JCOEFPTR src_ptr, dst_ptr;
  346. jpeg_component_info *compptr;
  347. MCU_cols = dstinfo->image_width / (dstinfo->max_h_samp_factor * DCTSIZE);
  348. MCU_rows = dstinfo->image_height / (dstinfo->max_v_samp_factor * DCTSIZE);
  349. for (ci = 0; ci < dstinfo->num_components; ci++) {
  350. compptr = dstinfo->comp_info + ci;
  351. comp_width = MCU_cols * compptr->h_samp_factor;
  352. comp_height = MCU_rows * compptr->v_samp_factor;
  353. for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks;
  354. dst_blk_y += compptr->v_samp_factor) {
  355. dst_buffer = (*srcinfo->mem->access_virt_barray)
  356. ((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y,
  357. (JDIMENSION) compptr->v_samp_factor, TRUE);
  358. if (dst_blk_y < comp_height) {
  359. /* Row is within the vertically mirrorable area. */
  360. src_buffer = (*srcinfo->mem->access_virt_barray)
  361. ((j_common_ptr) srcinfo, src_coef_arrays[ci],
  362. comp_height - dst_blk_y - (JDIMENSION) compptr->v_samp_factor,
  363. (JDIMENSION) compptr->v_samp_factor, FALSE);
  364. } else {
  365. /* Bottom-edge rows are only mirrored horizontally. */
  366. src_buffer = (*srcinfo->mem->access_virt_barray)
  367. ((j_common_ptr) srcinfo, src_coef_arrays[ci], dst_blk_y,
  368. (JDIMENSION) compptr->v_samp_factor, FALSE);
  369. }
  370. for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
  371. if (dst_blk_y < comp_height) {
  372. /* Row is within the mirrorable area. */
  373. dst_row_ptr = dst_buffer[offset_y];
  374. src_row_ptr = src_buffer[compptr->v_samp_factor - offset_y - 1];
  375. /* Process the blocks that can be mirrored both ways. */
  376. for (dst_blk_x = 0; dst_blk_x < comp_width; dst_blk_x++) {
  377. dst_ptr = dst_row_ptr[dst_blk_x];
  378. src_ptr = src_row_ptr[comp_width - dst_blk_x - 1];
  379. for (i = 0; i < DCTSIZE; i += 2) {
  380. /* For even row, negate every odd column. */
  381. for (j = 0; j < DCTSIZE; j += 2) {
  382. *dst_ptr++ = *src_ptr++;
  383. *dst_ptr++ = - *src_ptr++;
  384. }
  385. /* For odd row, negate every even column. */
  386. for (j = 0; j < DCTSIZE; j += 2) {
  387. *dst_ptr++ = - *src_ptr++;
  388. *dst_ptr++ = *src_ptr++;
  389. }
  390. }
  391. }
  392. /* Any remaining right-edge blocks are only mirrored vertically. */
  393. for (; dst_blk_x < compptr->width_in_blocks; dst_blk_x++) {
  394. dst_ptr = dst_row_ptr[dst_blk_x];
  395. src_ptr = src_row_ptr[dst_blk_x];
  396. for (i = 0; i < DCTSIZE; i += 2) {
  397. for (j = 0; j < DCTSIZE; j++)
  398. *dst_ptr++ = *src_ptr++;
  399. for (j = 0; j < DCTSIZE; j++)
  400. *dst_ptr++ = - *src_ptr++;
  401. }
  402. }
  403. } else {
  404. /* Remaining rows are just mirrored horizontally. */
  405. dst_row_ptr = dst_buffer[offset_y];
  406. src_row_ptr = src_buffer[offset_y];
  407. /* Process the blocks that can be mirrored. */
  408. for (dst_blk_x = 0; dst_blk_x < comp_width; dst_blk_x++) {
  409. dst_ptr = dst_row_ptr[dst_blk_x];
  410. src_ptr = src_row_ptr[comp_width - dst_blk_x - 1];
  411. for (i = 0; i < DCTSIZE2; i += 2) {
  412. *dst_ptr++ = *src_ptr++;
  413. *dst_ptr++ = - *src_ptr++;
  414. }
  415. }
  416. /* Any remaining right-edge blocks are only copied. */
  417. for (; dst_blk_x < compptr->width_in_blocks; dst_blk_x++) {
  418. dst_ptr = dst_row_ptr[dst_blk_x];
  419. src_ptr = src_row_ptr[dst_blk_x];
  420. for (i = 0; i < DCTSIZE2; i++)
  421. *dst_ptr++ = *src_ptr++;
  422. }
  423. }
  424. }
  425. }
  426. }
  427. }
  428. LOCAL(void)
  429. do_transverse (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
  430. jvirt_barray_ptr *src_coef_arrays,
  431. jvirt_barray_ptr *dst_coef_arrays)
  432. /* Transverse transpose is equivalent to
  433. * 1. 180 degree rotation;
  434. * 2. Transposition;
  435. * or
  436. * 1. Horizontal mirroring;
  437. * 2. Transposition;
  438. * 3. Horizontal mirroring.
  439. * These steps are merged into a single processing routine.
  440. */
  441. {
  442. JDIMENSION MCU_cols, MCU_rows, comp_width, comp_height, dst_blk_x, dst_blk_y;
  443. int ci, i, j, offset_x, offset_y;
  444. JBLOCKARRAY src_buffer, dst_buffer;
  445. JCOEFPTR src_ptr, dst_ptr;
  446. jpeg_component_info *compptr;
  447. MCU_cols = dstinfo->image_width / (dstinfo->max_h_samp_factor * DCTSIZE);
  448. MCU_rows = dstinfo->image_height / (dstinfo->max_v_samp_factor * DCTSIZE);
  449. for (ci = 0; ci < dstinfo->num_components; ci++) {
  450. compptr = dstinfo->comp_info + ci;
  451. comp_width = MCU_cols * compptr->h_samp_factor;
  452. comp_height = MCU_rows * compptr->v_samp_factor;
  453. for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks;
  454. dst_blk_y += compptr->v_samp_factor) {
  455. dst_buffer = (*srcinfo->mem->access_virt_barray)
  456. ((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y,
  457. (JDIMENSION) compptr->v_samp_factor, TRUE);
  458. for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
  459. for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks;
  460. dst_blk_x += compptr->h_samp_factor) {
  461. src_buffer = (*srcinfo->mem->access_virt_barray)
  462. ((j_common_ptr) srcinfo, src_coef_arrays[ci], dst_blk_x,
  463. (JDIMENSION) compptr->h_samp_factor, FALSE);
  464. for (offset_x = 0; offset_x < compptr->h_samp_factor; offset_x++) {
  465. if (dst_blk_y < comp_height) {
  466. src_ptr = src_buffer[offset_x]
  467. [comp_height - dst_blk_y - offset_y - 1];
  468. if (dst_blk_x < comp_width) {
  469. /* Block is within the mirrorable area. */
  470. dst_ptr = dst_buffer[offset_y]
  471. [comp_width - dst_blk_x - offset_x - 1];
  472. for (i = 0; i < DCTSIZE; i++) {
  473. for (j = 0; j < DCTSIZE; j++) {
  474. dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j];
  475. j++;
  476. dst_ptr[j*DCTSIZE+i] = -src_ptr[i*DCTSIZE+j];
  477. }
  478. i++;
  479. for (j = 0; j < DCTSIZE; j++) {
  480. dst_ptr[j*DCTSIZE+i] = -src_ptr[i*DCTSIZE+j];
  481. j++;
  482. dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j];
  483. }
  484. }
  485. } else {
  486. /* Right-edge blocks are mirrored in y only */
  487. dst_ptr = dst_buffer[offset_y][dst_blk_x + offset_x];
  488. for (i = 0; i < DCTSIZE; i++) {
  489. for (j = 0; j < DCTSIZE; j++) {
  490. dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j];
  491. j++;
  492. dst_ptr[j*DCTSIZE+i] = -src_ptr[i*DCTSIZE+j];
  493. }
  494. }
  495. }
  496. } else {
  497. src_ptr = src_buffer[offset_x][dst_blk_y + offset_y];
  498. if (dst_blk_x < comp_width) {
  499. /* Bottom-edge blocks are mirrored in x only */
  500. dst_ptr = dst_buffer[offset_y]
  501. [comp_width - dst_blk_x - offset_x - 1];
  502. for (i = 0; i < DCTSIZE; i++) {
  503. for (j = 0; j < DCTSIZE; j++)
  504. dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j];
  505. i++;
  506. for (j = 0; j < DCTSIZE; j++)
  507. dst_ptr[j*DCTSIZE+i] = -src_ptr[i*DCTSIZE+j];
  508. }
  509. } else {
  510. /* At lower right corner, just transpose, no mirroring */
  511. dst_ptr = dst_buffer[offset_y][dst_blk_x + offset_x];
  512. for (i = 0; i < DCTSIZE; i++)
  513. for (j = 0; j < DCTSIZE; j++)
  514. dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j];
  515. }
  516. }
  517. }
  518. }
  519. }
  520. }
  521. }
  522. }
  523. /* Request any required workspace.
  524. *
  525. * We allocate the workspace virtual arrays from the source decompression
  526. * object, so that all the arrays (both the original data and the workspace)
  527. * will be taken into account while making memory management decisions.
  528. * Hence, this routine must be called after jpeg_read_header (which reads
  529. * the image dimensions) and before jpeg_read_coefficients (which realizes
  530. * the source's virtual arrays).
  531. */
  532. GLOBAL(void)
  533. jtransform_request_workspace (j_decompress_ptr srcinfo,
  534. jpeg_transform_info *info)
  535. {
  536. jvirt_barray_ptr *coef_arrays = NULL;
  537. jpeg_component_info *compptr;
  538. int ci;
  539. if (info->force_grayscale &&
  540. srcinfo->jpeg_color_space == JCS_YCbCr &&
  541. srcinfo->num_components == 3) {
  542. /* We'll only process the first component */
  543. info->num_components = 1;
  544. } else {
  545. /* Process all the components */
  546. info->num_components = srcinfo->num_components;
  547. }
  548. switch (info->transform) {
  549. case JXFORM_NONE:
  550. case JXFORM_FLIP_H:
  551. /* Don't need a workspace array */
  552. break;
  553. case JXFORM_FLIP_V:
  554. case JXFORM_ROT_180:
  555. /* Need workspace arrays having same dimensions as source image.
  556. * Note that we allocate arrays padded out to the next iMCU boundary,
  557. * so that transform routines need not worry about missing edge blocks.
  558. */
  559. coef_arrays = (jvirt_barray_ptr *)
  560. (*srcinfo->mem->alloc_small) ((j_common_ptr) srcinfo, JPOOL_IMAGE,
  561. SIZEOF(jvirt_barray_ptr) * info->num_components);
  562. for (ci = 0; ci < info->num_components; ci++) {
  563. compptr = srcinfo->comp_info + ci;
  564. coef_arrays[ci] = (*srcinfo->mem->request_virt_barray)
  565. ((j_common_ptr) srcinfo, JPOOL_IMAGE, FALSE,
  566. (JDIMENSION) jround_up((long) compptr->width_in_blocks,
  567. (long) compptr->h_samp_factor),
  568. (JDIMENSION) jround_up((long) compptr->height_in_blocks,
  569. (long) compptr->v_samp_factor),
  570. (JDIMENSION) compptr->v_samp_factor);
  571. }
  572. break;
  573. case JXFORM_TRANSPOSE:
  574. case JXFORM_TRANSVERSE:
  575. case JXFORM_ROT_90:
  576. case JXFORM_ROT_270:
  577. /* Need workspace arrays having transposed dimensions.
  578. * Note that we allocate arrays padded out to the next iMCU boundary,
  579. * so that transform routines need not worry about missing edge blocks.
  580. */
  581. coef_arrays = (jvirt_barray_ptr *)
  582. (*srcinfo->mem->alloc_small) ((j_common_ptr) srcinfo, JPOOL_IMAGE,
  583. SIZEOF(jvirt_barray_ptr) * info->num_components);
  584. for (ci = 0; ci < info->num_components; ci++) {
  585. compptr = srcinfo->comp_info + ci;
  586. coef_arrays[ci] = (*srcinfo->mem->request_virt_barray)
  587. ((j_common_ptr) srcinfo, JPOOL_IMAGE, FALSE,
  588. (JDIMENSION) jround_up((long) compptr->height_in_blocks,
  589. (long) compptr->v_samp_factor),
  590. (JDIMENSION) jround_up((long) compptr->width_in_blocks,
  591. (long) compptr->h_samp_factor),
  592. (JDIMENSION) compptr->h_samp_factor);
  593. }
  594. break;
  595. }
  596. info->workspace_coef_arrays = coef_arrays;
  597. }
  598. /* Transpose destination image parameters */
  599. LOCAL(void)
  600. transpose_critical_parameters (j_compress_ptr dstinfo)
  601. {
  602. int tblno, i, j, ci, itemp;
  603. jpeg_component_info *compptr;
  604. JQUANT_TBL *qtblptr;
  605. JDIMENSION dtemp;
  606. UINT16 qtemp;
  607. /* Transpose basic image dimensions */
  608. dtemp = dstinfo->image_width;
  609. dstinfo->image_width = dstinfo->image_height;
  610. dstinfo->image_height = dtemp;
  611. /* Transpose sampling factors */
  612. for (ci = 0; ci < dstinfo->num_components; ci++) {
  613. compptr = dstinfo->comp_info + ci;
  614. itemp = compptr->h_samp_factor;
  615. compptr->h_samp_factor = compptr->v_samp_factor;
  616. compptr->v_samp_factor = itemp;
  617. }
  618. /* Transpose quantization tables */
  619. for (tblno = 0; tblno < NUM_QUANT_TBLS; tblno++) {
  620. qtblptr = dstinfo->quant_tbl_ptrs[tblno];
  621. if (qtblptr != NULL) {
  622. for (i = 0; i < DCTSIZE; i++) {
  623. for (j = 0; j < i; j++) {
  624. qtemp = qtblptr->quantval[i*DCTSIZE+j];
  625. qtblptr->quantval[i*DCTSIZE+j] = qtblptr->quantval[j*DCTSIZE+i];
  626. qtblptr->quantval[j*DCTSIZE+i] = qtemp;
  627. }
  628. }
  629. }
  630. }
  631. }
  632. /* Trim off any partial iMCUs on the indicated destination edge */
  633. LOCAL(void)
  634. trim_right_edge (j_compress_ptr dstinfo)
  635. {
  636. int ci, max_h_samp_factor;
  637. JDIMENSION MCU_cols;
  638. /* We have to compute max_h_samp_factor ourselves,
  639. * because it hasn't been set yet in the destination
  640. * (and we don't want to use the source's value).
  641. */
  642. max_h_samp_factor = 1;
  643. for (ci = 0; ci < dstinfo->num_components; ci++) {
  644. int h_samp_factor = dstinfo->comp_info[ci].h_samp_factor;
  645. max_h_samp_factor = MAX(max_h_samp_factor, h_samp_factor);
  646. }
  647. MCU_cols = dstinfo->image_width / (max_h_samp_factor * DCTSIZE);
  648. if (MCU_cols > 0) /* can't trim to 0 pixels */
  649. dstinfo->image_width = MCU_cols * (max_h_samp_factor * DCTSIZE);
  650. }
  651. LOCAL(void)
  652. trim_bottom_edge (j_compress_ptr dstinfo)
  653. {
  654. int ci, max_v_samp_factor;
  655. JDIMENSION MCU_rows;
  656. /* We have to compute max_v_samp_factor ourselves,
  657. * because it hasn't been set yet in the destination
  658. * (and we don't want to use the source's value).
  659. */
  660. max_v_samp_factor = 1;
  661. for (ci = 0; ci < dstinfo->num_components; ci++) {
  662. int v_samp_factor = dstinfo->comp_info[ci].v_samp_factor;
  663. max_v_samp_factor = MAX(max_v_samp_factor, v_samp_factor);
  664. }
  665. MCU_rows = dstinfo->image_height / (max_v_samp_factor * DCTSIZE);
  666. if (MCU_rows > 0) /* can't trim to 0 pixels */
  667. dstinfo->image_height = MCU_rows * (max_v_samp_factor * DCTSIZE);
  668. }
  669. /* Adjust output image parameters as needed.
  670. *
  671. * This must be called after jpeg_copy_critical_parameters()
  672. * and before jpeg_write_coefficients().
  673. *
  674. * The return value is the set of virtual coefficient arrays to be written
  675. * (either the ones allocated by jtransform_request_workspace, or the
  676. * original source data arrays). The caller will need to pass this value
  677. * to jpeg_write_coefficients().
  678. */
  679. GLOBAL(jvirt_barray_ptr *)
  680. jtransform_adjust_parameters (j_decompress_ptr,
  681. j_compress_ptr dstinfo,
  682. jvirt_barray_ptr *src_coef_arrays,
  683. jpeg_transform_info *info)
  684. {
  685. /* If force-to-grayscale is requested, adjust destination parameters */
  686. if (info->force_grayscale) {
  687. /* We use jpeg_set_colorspace to make sure subsidiary settings get fixed
  688. * properly. Among other things, the target h_samp_factor & v_samp_factor
  689. * will get set to 1, which typically won't match the source.
  690. * In fact we do this even if the source is already grayscale; that
  691. * provides an easy way of coercing a grayscale JPEG with funny sampling
  692. * factors to the customary 1,1. (Some decoders fail on other factors.)
  693. */
  694. if ((dstinfo->jpeg_color_space == JCS_YCbCr &&
  695. dstinfo->num_components == 3) ||
  696. (dstinfo->jpeg_color_space == JCS_GRAYSCALE &&
  697. dstinfo->num_components == 1)) {
  698. /* We have to preserve the source's quantization table number. */
  699. int sv_quant_tbl_no = dstinfo->comp_info[0].quant_tbl_no;
  700. jpeg_set_colorspace(dstinfo, JCS_GRAYSCALE);
  701. dstinfo->comp_info[0].quant_tbl_no = sv_quant_tbl_no;
  702. } else {
  703. /* Sorry, can't do it */
  704. ERREXIT(dstinfo, JERR_CONVERSION_NOTIMPL);
  705. }
  706. }
  707. /* Correct the destination's image dimensions etc if necessary */
  708. switch (info->transform) {
  709. case JXFORM_NONE:
  710. /* Nothing to do */
  711. break;
  712. case JXFORM_FLIP_H:
  713. if (info->trim)
  714. trim_right_edge(dstinfo);
  715. break;
  716. case JXFORM_FLIP_V:
  717. if (info->trim)
  718. trim_bottom_edge(dstinfo);
  719. break;
  720. case JXFORM_TRANSPOSE:
  721. transpose_critical_parameters(dstinfo);
  722. /* transpose does NOT have to trim anything */
  723. break;
  724. case JXFORM_TRANSVERSE:
  725. transpose_critical_parameters(dstinfo);
  726. if (info->trim) {
  727. trim_right_edge(dstinfo);
  728. trim_bottom_edge(dstinfo);
  729. }
  730. break;
  731. case JXFORM_ROT_90:
  732. transpose_critical_parameters(dstinfo);
  733. if (info->trim)
  734. trim_right_edge(dstinfo);
  735. break;
  736. case JXFORM_ROT_180:
  737. if (info->trim) {
  738. trim_right_edge(dstinfo);
  739. trim_bottom_edge(dstinfo);
  740. }
  741. break;
  742. case JXFORM_ROT_270:
  743. transpose_critical_parameters(dstinfo);
  744. if (info->trim)
  745. trim_bottom_edge(dstinfo);
  746. break;
  747. }
  748. /* Return the appropriate output data set */
  749. if (info->workspace_coef_arrays != NULL)
  750. return info->workspace_coef_arrays;
  751. return src_coef_arrays;
  752. }
  753. /* Execute the actual transformation, if any.
  754. *
  755. * This must be called *after* jpeg_write_coefficients, because it depends
  756. * on jpeg_write_coefficients to have computed subsidiary values such as
  757. * the per-component width and height fields in the destination object.
  758. *
  759. * Note that some transformations will modify the source data arrays!
  760. */
  761. GLOBAL(void)
  762. jtransform_execute_transformation (j_decompress_ptr srcinfo,
  763. j_compress_ptr dstinfo,
  764. jvirt_barray_ptr *src_coef_arrays,
  765. jpeg_transform_info *info)
  766. {
  767. jvirt_barray_ptr *dst_coef_arrays = info->workspace_coef_arrays;
  768. switch (info->transform) {
  769. case JXFORM_NONE:
  770. break;
  771. case JXFORM_FLIP_H:
  772. do_flip_h(srcinfo, dstinfo, src_coef_arrays);
  773. break;
  774. case JXFORM_FLIP_V:
  775. do_flip_v(srcinfo, dstinfo, src_coef_arrays, dst_coef_arrays);
  776. break;
  777. case JXFORM_TRANSPOSE:
  778. do_transpose(srcinfo, dstinfo, src_coef_arrays, dst_coef_arrays);
  779. break;
  780. case JXFORM_TRANSVERSE:
  781. do_transverse(srcinfo, dstinfo, src_coef_arrays, dst_coef_arrays);
  782. break;
  783. case JXFORM_ROT_90:
  784. do_rot_90(srcinfo, dstinfo, src_coef_arrays, dst_coef_arrays);
  785. break;
  786. case JXFORM_ROT_180:
  787. do_rot_180(srcinfo, dstinfo, src_coef_arrays, dst_coef_arrays);
  788. break;
  789. case JXFORM_ROT_270:
  790. do_rot_270(srcinfo, dstinfo, src_coef_arrays, dst_coef_arrays);
  791. break;
  792. }
  793. }
  794. #endif /* TRANSFORMS_SUPPORTED */
  795. /* Setup decompression object to save desired markers in memory.
  796. * This must be called before jpeg_read_header() to have the desired effect.
  797. */
  798. GLOBAL(void)
  799. jcopy_markers_setup (j_decompress_ptr srcinfo, JCOPY_OPTION option)
  800. {
  801. #ifdef SAVE_MARKERS_SUPPORTED
  802. int m;
  803. /* Save comments except under NONE option */
  804. if (option != JCOPYOPT_NONE) {
  805. jpeg_save_markers(srcinfo, JPEG_COM, 0xFFFF);
  806. }
  807. /* Save all types of APPn markers iff ALL option */
  808. if (option == JCOPYOPT_ALL) {
  809. for (m = 0; m < 16; m++)
  810. jpeg_save_markers(srcinfo, JPEG_APP0 + m, 0xFFFF);
  811. }
  812. #endif /* SAVE_MARKERS_SUPPORTED */
  813. }
  814. /* Copy markers saved in the given source object to the destination object.
  815. * This should be called just after jpeg_start_compress() or
  816. * jpeg_write_coefficients().
  817. * Note that those routines will have written the SOI, and also the
  818. * JFIF APP0 or Adobe APP14 markers if selected.
  819. */
  820. GLOBAL(void)
  821. jcopy_markers_execute (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
  822. JCOPY_OPTION)
  823. {
  824. jpeg_saved_marker_ptr marker;
  825. /* In the current implementation, we don't actually need to examine the
  826. * option flag here; we just copy everything that got saved.
  827. * But to avoid confusion, we do not output JFIF and Adobe APP14 markers
  828. * if the encoder library already wrote one.
  829. */
  830. for (marker = srcinfo->marker_list; marker != NULL; marker = marker->next) {
  831. if (dstinfo->write_JFIF_header &&
  832. marker->marker == JPEG_APP0 &&
  833. marker->data_length >= 5 &&
  834. GETJOCTET(marker->data[0]) == 0x4A &&
  835. GETJOCTET(marker->data[1]) == 0x46 &&
  836. GETJOCTET(marker->data[2]) == 0x49 &&
  837. GETJOCTET(marker->data[3]) == 0x46 &&
  838. GETJOCTET(marker->data[4]) == 0)
  839. continue; /* reject duplicate JFIF */
  840. if (dstinfo->write_Adobe_marker &&
  841. marker->marker == JPEG_APP0+14 &&
  842. marker->data_length >= 5 &&
  843. GETJOCTET(marker->data[0]) == 0x41 &&
  844. GETJOCTET(marker->data[1]) == 0x64 &&
  845. GETJOCTET(marker->data[2]) == 0x6F &&
  846. GETJOCTET(marker->data[3]) == 0x62 &&
  847. GETJOCTET(marker->data[4]) == 0x65)
  848. continue; /* reject duplicate Adobe */
  849. #ifdef NEED_FAR_POINTERS
  850. /* We could use jpeg_write_marker if the data weren't FAR... */
  851. {
  852. unsigned int i;
  853. jpeg_write_m_header(dstinfo, marker->marker, marker->data_length);
  854. for (i = 0; i < marker->data_length; i++)
  855. jpeg_write_m_byte(dstinfo, marker->data[i]);
  856. }
  857. #else
  858. jpeg_write_marker(dstinfo, marker->marker,
  859. marker->data, marker->data_length);
  860. #endif
  861. }
  862. }