transupp.pas 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427
  1. Unit transupp;
  2. {* transupp.c
  3. * transupp.h
  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. This file contains image transformation routines and other utility code
  8. used by the jpegtran sample application. These are NOT part of the core
  9. JPEG library. But we keep these routines separate from jpegtran.c to
  10. ease the task of maintaining jpegtran-like programs that have other user
  11. interfaces.
  12. NOTE: all the routines declared here have very specific requirements
  13. about when they are to be executed during the reading and writing of the
  14. source and destination files. See the comments in transupp.c, or see
  15. jpegtran.c for an example of correct usage. }
  16. interface
  17. {$I jconfig.inc}
  18. uses
  19. jmorecfg,
  20. jinclude,
  21. jpeglib;
  22. { Short forms of external names for systems with brain-damaged linkers. }
  23. {$ifdef NEED_SHORT_EXTERNAL_NAMES}
  24. jtransform_request_workspace jTrRequest
  25. jtransform_adjust_parameters jTrAdjust
  26. jtransform_execute_transformation jTrExec
  27. jcopy_markers_setup jCMrkSetup
  28. jcopy_markers_execute jCMrkExec
  29. {$endif} { NEED_SHORT_EXTERNAL_NAMES }
  30. { Codes for supported types of image transformations. }
  31. type
  32. JXFORM_CODE = (
  33. JXFORM_NONE, { no transformation }
  34. {$ifdef CROP_SUPPORTED}
  35. JXFORM_CUT, { cut out part of the image }
  36. {$endif}
  37. JXFORM_FLIP_H, { horizontal flip }
  38. JXFORM_FLIP_V, { vertical flip }
  39. JXFORM_TRANSPOSE, { transpose across UL-to-LR axis }
  40. JXFORM_TRANSVERSE, { transpose across UR-to-LL axis }
  41. JXFORM_ROT_90, { 90-degree clockwise rotation }
  42. JXFORM_ROT_180, { 180-degree rotation }
  43. JXFORM_ROT_270 { 270-degree clockwise (or 90 ccw) }
  44. );
  45. {
  46. Although rotating and flipping data expressed as DCT coefficients is not
  47. hard, there is an asymmetry in the JPEG format specification for images
  48. whose dimensions aren't multiples of the iMCU size. The right and bottom
  49. image edges are padded out to the next iMCU boundary with junk data; but
  50. no padding is possible at the top and left edges. If we were to flip
  51. the whole image including the pad data, then pad garbage would become
  52. visible at the top and/or left, and real pixels would disappear into the
  53. pad margins --- perhaps permanently, since encoders & decoders may not
  54. bother to preserve DCT blocks that appear to be completely outside the
  55. nominal image area. So, we have to exclude any partial iMCUs from the
  56. basic transformation.
  57. Transpose is the only transformation that can handle partial iMCUs at the
  58. right and bottom edges completely cleanly. flip_h can flip partial iMCUs
  59. at the bottom, but leaves any partial iMCUs at the right edge untouched.
  60. Similarly flip_v leaves any partial iMCUs at the bottom edge untouched.
  61. The other transforms are defined as combinations of these basic transforms
  62. and process edge blocks in a way that preserves the equivalence.
  63. The "trim" option causes untransformable partial iMCUs to be dropped;
  64. this is not strictly lossless, but it usually gives the best-looking
  65. result for odd-size images. Note that when this option is active,
  66. the expected mathematical equivalences between the transforms may not hold.
  67. (For example, -rot 270 -trim trims only the bottom edge, but -rot 90 -trim
  68. followed by -rot 180 -trim trims both edges.)
  69. We also offer a "force to grayscale" option, which simply discards the
  70. chrominance channels of a YCbCr image. This is lossless in the sense that
  71. the luminance channel is preserved exactly. It's not the same kind of
  72. thing as the rotate/flip transformations, but it's convenient to handle it
  73. as part of this package, mainly because the transformation routines have to
  74. be aware of the option to know how many components to work on.
  75. }
  76. type
  77. jpeg_transform_info = record
  78. { Options: set by caller }
  79. transform : JXFORM_CODE; { image transform operator }
  80. trim : boolean; { if TRUE, trim partial MCUs as needed }
  81. force_grayscale : boolean; { if TRUE, convert color image to grayscale }
  82. {$ifdef CROP_SUPPORTED}
  83. xoffs, yoffs, newwidth, newheight : JDIMENSION;
  84. {$endif}
  85. { Internal workspace: caller should not touch these }
  86. num_components : int; { # of components in workspace }
  87. workspace_coef_arrays : jvirt_barray_tbl_ptr; { workspace for transformations }
  88. end;
  89. {$ifdef TRANSFORMS_SUPPORTED}
  90. { Request any required workspace }
  91. procedure jtransform_request_workspace(srcinfo : j_decompress_ptr;
  92. var info : jpeg_transform_info);
  93. { Adjust output image parameters }
  94. function jtransform_adjust_parameters(
  95. srcinfo : j_decompress_ptr;
  96. dstinfo : j_compress_ptr;
  97. src_coef_arrays : jvirt_barray_tbl_ptr;
  98. var info : jpeg_transform_info) : jvirt_barray_tbl_ptr;
  99. { Execute the actual transformation, if any }
  100. procedure jtransform_execute_transformation(
  101. srcinfo : j_decompress_ptr;
  102. dstinfo : j_compress_ptr;
  103. src_coef_arrays : jvirt_barray_tbl_ptr;
  104. var info : jpeg_transform_info);
  105. {$endif} { TRANSFORMS_SUPPORTED }
  106. { Support for copying optional markers from source to destination file. }
  107. type
  108. JCOPY_OPTION = (
  109. JCOPYOPT_NONE, { copy no optional markers }
  110. JCOPYOPT_COMMENTS, { copy only comment (COM) markers }
  111. JCOPYOPT_ALL { copy all optional markers }
  112. );
  113. const
  114. JCOPYOPT_DEFAULT = JCOPYOPT_COMMENTS; { recommended default }
  115. { Setup decompression object to save desired markers in memory }
  116. procedure jcopy_markers_setup(srcinfo : j_decompress_ptr;
  117. option : JCOPY_OPTION);
  118. { Copy markers saved in the given source object to the destination object }
  119. procedure jcopy_markers_execute(srcinfo : j_decompress_ptr;
  120. dstinfo : j_compress_ptr;
  121. option : JCOPY_OPTION);
  122. implementation
  123. { Although this file really shouldn't have access to the library internals,
  124. it's helpful to let it call jround_up() and jcopy_block_row(). }
  125. uses
  126. jutils,
  127. jdeferr,
  128. jerror,
  129. {$ifdef SAVE_MARKERS_SUPPORTED}
  130. jdmarker,
  131. {$endif}
  132. jcapimin,
  133. jcparam; { set color space }
  134. {$ifdef TRANSFORMS_SUPPORTED}
  135. { Lossless image transformation routines. These routines work on DCT
  136. coefficient arrays and thus do not require any lossy decompression
  137. or recompression of the image.
  138. Thanks to Guido Vollbeding for the initial design and code of this feature.
  139. Horizontal flipping is done in-place, using a single top-to-bottom
  140. pass through the virtual source array. It will thus be much the
  141. fastest option for images larger than main memory.
  142. The other routines require a set of destination virtual arrays, so they
  143. need twice as much memory as jpegtran normally does. The destination
  144. arrays are always written in normal scan order (top to bottom) because
  145. the virtual array manager expects this. The source arrays will be scanned
  146. in the corresponding order, which means multiple passes through the source
  147. arrays for most of the transforms. That could result in much thrashing
  148. if the image is larger than main memory.
  149. Some notes about the operating environment of the individual transform
  150. routines:
  151. 1. Both the source and destination virtual arrays are allocated from the
  152. source JPEG object, and therefore should be manipulated by calling the
  153. source's memory manager.
  154. 2. The destination's component count should be used. It may be smaller
  155. than the source's when forcing to grayscale.
  156. 3. Likewise the destination's sampling factors should be used. When
  157. forcing to grayscale the destination's sampling factors will be all 1,
  158. and we may as well take that as the effective iMCU size.
  159. 4. When "trim" is in effect, the destination's dimensions will be the
  160. trimmed values but the source's will be untrimmed.
  161. 5. All the routines assume that the source and destination buffers are
  162. padded out to a full iMCU boundary. This is true, although for the
  163. source buffer it is an undocumented property of jdcoefct.c.
  164. Notes 2,3,4 boil down to this: generally we should use the destination's
  165. dimensions and ignore the source's. }
  166. {LOCAL}
  167. procedure do_flip_h (srcinfo : j_decompress_ptr;
  168. dstinfo : j_compress_ptr;
  169. src_coef_arrays : jvirt_barray_tbl_ptr);
  170. { Horizontal flip; done in-place, so no separate dest array is required }
  171. var
  172. MCU_cols, comp_width, blk_x, blk_y : JDIMENSION;
  173. ci, k, offset_y : int;
  174. buffer : JBLOCKARRAY;
  175. ptr1, ptr2 : JCOEF_PTR;
  176. temp1, temp2 : JCOEF;
  177. compptr : jpeg_component_info_ptr;
  178. begin
  179. { Horizontal mirroring of DCT blocks is accomplished by swapping
  180. pairs of blocks in-place. Within a DCT block, we perform horizontal
  181. mirroring by changing the signs of odd-numbered columns.
  182. Partial iMCUs at the right edge are left untouched. }
  183. MCU_cols := dstinfo^.image_width div (dstinfo^.max_h_samp_factor * DCTSIZE);
  184. for ci := 0 to dstinfo^.num_components-1 do
  185. begin
  186. compptr := jpeg_component_info_ptr(dstinfo^.comp_info);
  187. Inc(compptr, ci);
  188. comp_width := MCU_cols * compptr^.h_samp_factor;
  189. blk_y := 0;
  190. while (blk_y < compptr^.height_in_blocks) do
  191. begin
  192. buffer := srcinfo^.mem^.access_virt_barray
  193. (j_common_ptr(srcinfo), src_coef_arrays^[ci], blk_y,
  194. JDIMENSION (compptr^.v_samp_factor), TRUE);
  195. for offset_y := 0 to compptr^.v_samp_factor-1 do
  196. begin
  197. blk_x := 0;
  198. while (blk_x * 2 < comp_width) do
  199. begin
  200. ptr1 := JCOEF_PTR(@(buffer^[offset_y]^[blk_x]));
  201. ptr2 := JCOEF_PTR(@(buffer^[offset_y]^[comp_width - blk_x - 1]));
  202. { this unrolled loop doesn't need to know which row it's on... }
  203. k := 0;
  204. while (k < DCTSIZE2) do
  205. begin
  206. temp1 := ptr1^; { swap even column }
  207. temp2 := ptr2^;
  208. ptr1^ := temp2;
  209. Inc(ptr1);
  210. ptr2^ := temp1;
  211. Inc(ptr2);
  212. temp1 := ptr1^; { swap odd column with sign change }
  213. temp2 := ptr2^;
  214. ptr1^ := -temp2;
  215. Inc(ptr1);
  216. ptr2^ := -temp1;
  217. Inc(ptr2);
  218. Inc(k, 2);
  219. end;
  220. Inc(blk_x);
  221. end;
  222. end;
  223. Inc(blk_y, compptr^.v_samp_factor);
  224. end; { while }
  225. end; { for ci }
  226. end; { do_flip_h }
  227. {LOCAL}
  228. procedure do_flip_v (srcinfo : j_decompress_ptr;
  229. dstinfo : j_compress_ptr;
  230. src_coef_arrays : jvirt_barray_tbl_ptr;
  231. dst_coef_arrays : jvirt_barray_tbl_ptr);
  232. { Vertical flip }
  233. var
  234. MCU_rows, comp_height, dst_blk_x, dst_blk_y : JDIMENSION;
  235. ci, i, j, offset_y : int;
  236. src_buffer, dst_buffer : JBLOCKARRAY;
  237. src_row_ptr, dst_row_ptr : JBLOCKROW;
  238. src_ptr, dst_ptr : JCOEF_PTR;
  239. compptr : jpeg_component_info_ptr;
  240. begin
  241. { We output into a separate array because we can't touch different
  242. rows of the source virtual array simultaneously. Otherwise, this
  243. is a pretty straightforward analog of horizontal flip.
  244. Within a DCT block, vertical mirroring is done by changing the signs
  245. of odd-numbered rows.
  246. Partial iMCUs at the bottom edge are copied verbatim. }
  247. MCU_rows := dstinfo^.image_height div (dstinfo^.max_v_samp_factor * DCTSIZE);
  248. for ci := 0 to dstinfo^.num_components-1 do
  249. begin
  250. compptr := jpeg_component_info_ptr(dstinfo^.comp_info);
  251. Inc(compptr, ci);
  252. comp_height := MCU_rows * compptr^.v_samp_factor;
  253. dst_blk_y := 0;
  254. while (dst_blk_y < compptr^.height_in_blocks) do
  255. begin
  256. dst_buffer := srcinfo^.mem^.access_virt_barray
  257. (j_common_ptr(srcinfo), dst_coef_arrays^[ci], dst_blk_y,
  258. JDIMENSION(compptr^.v_samp_factor), TRUE);
  259. if (dst_blk_y < comp_height) then
  260. begin
  261. { Row is within the mirrorable area. }
  262. src_buffer := srcinfo^.mem^.access_virt_barray
  263. (j_common_ptr(srcinfo), src_coef_arrays^[ci],
  264. comp_height - dst_blk_y - JDIMENSION(compptr^.v_samp_factor),
  265. JDIMENSION (compptr^.v_samp_factor), FALSE);
  266. end
  267. else
  268. begin
  269. { Bottom-edge blocks will be copied verbatim. }
  270. src_buffer := srcinfo^.mem^.access_virt_barray
  271. (j_common_ptr(srcinfo), src_coef_arrays^[ci], dst_blk_y,
  272. JDIMENSION (compptr^.v_samp_factor), FALSE);
  273. end;
  274. for offset_y := 0 to compptr^.v_samp_factor-1 do
  275. begin
  276. if (dst_blk_y < comp_height) then
  277. begin
  278. { Row is within the mirrorable area. }
  279. dst_row_ptr := dst_buffer^[offset_y];
  280. src_row_ptr := src_buffer^[compptr^.v_samp_factor - offset_y - 1];
  281. for dst_blk_x := 0 to compptr^.width_in_blocks-1 do
  282. begin
  283. dst_ptr := JCOEF_PTR(@(dst_row_ptr^[dst_blk_x]));
  284. src_ptr := JCOEF_PTR(@(src_row_ptr^[dst_blk_x]));
  285. i := 0;
  286. while (i < DCTSIZE) do
  287. begin
  288. { copy even row }
  289. for j := 0 to DCTSIZE-1 do
  290. begin
  291. dst_ptr^ := src_ptr^;
  292. Inc(dst_ptr);
  293. Inc(src_ptr);
  294. end;
  295. { copy odd row with sign change }
  296. for j := 0 to DCTSIZE-1 do
  297. begin
  298. dst_ptr^ := - (src_ptr^);
  299. Inc(dst_ptr);
  300. Inc(src_ptr);
  301. end;
  302. Inc(i, 2);
  303. end;
  304. end;
  305. end
  306. else
  307. begin
  308. { Just copy row verbatim. }
  309. jcopy_block_row(src_buffer^[offset_y], dst_buffer^[offset_y],
  310. compptr^.width_in_blocks);
  311. end;
  312. end;
  313. Inc(dst_blk_y, compptr^.v_samp_factor);
  314. end; { while }
  315. end; { for ci }
  316. end; { do_flip_v }
  317. {$ifdef CROP_SUPPORTED}
  318. {LOCAL}
  319. procedure do_transform (srcinfo : j_decompress_ptr;
  320. dstinfo : j_compress_ptr;
  321. src_coef_arrays : jvirt_barray_tbl_ptr;
  322. dst_coef_arrays : jvirt_barray_tbl_ptr;
  323. xoffs : JDIMENSION;
  324. yoffs : JDIMENSION);
  325. { transform src_coef_arrays so that the xoffs,yoffs (rounded to an even
  326. dct block) are the new origin of the image. copy rather than move because
  327. I'd never finish if I tried to understand the byzantine memory management.
  328. }
  329. var
  330. ci : int;
  331. compptr : jpeg_component_info_ptr;
  332. src_buffer, dst_buffer : JBLOCKARRAY;
  333. dst_blk_x, dst_blk_y : JDIMENSION;
  334. begin
  335. xoffs := xoffs div dstinfo^.max_h_samp_factor * DCTSIZE;
  336. yoffs := yoffs div dstinfo^.max_v_samp_factor * DCTSIZE;
  337. for ci := 0 to dstinfo^.num_components-1 do
  338. begin
  339. compptr := jpeg_component_info_ptr(dstinfo^.comp_info);
  340. Inc(compptr, ci);
  341. dst_blk_y := 0;
  342. while (dst_blk_y < compptr^.height_in_blocks) do
  343. begin
  344. dst_buffer := srcinfo^.mem^.access_virt_barray
  345. (j_common_ptr(srcinfo), dst_coef_arrays^[ci], dst_blk_y, 1, TRUE);
  346. src_buffer := srcinfo^.mem^.access_virt_barray
  347. (j_common_ptr(srcinfo), src_coef_arrays^[ci],
  348. dst_blk_y + yoffs * JDIMENSION(compptr^.v_samp_factor), 1, FALSE);
  349. jcopy_block_row(JBLOCKROW(@src_buffer^[0]^[xoffs * compptr^.h_samp_factor]),
  350. dst_buffer^[0], compptr^.width_in_blocks);
  351. Inc(dst_blk_y);
  352. end;
  353. end;
  354. end; { do_transform }
  355. {$endif}
  356. {LOCAL}
  357. procedure do_transpose (srcinfo : j_decompress_ptr;
  358. dstinfo : j_compress_ptr;
  359. src_coef_arrays : jvirt_barray_tbl_ptr;
  360. dst_coef_arrays : jvirt_barray_tbl_ptr);
  361. { Transpose source into destination }
  362. var
  363. dst_blk_x, dst_blk_y : JDIMENSION;
  364. ci, i, j, offset_x, offset_y : int;
  365. src_buffer, dst_buffer : JBLOCKARRAY;
  366. src_ptr, dst_ptr : JCOEFPTR;
  367. compptr : jpeg_component_info_ptr;
  368. begin
  369. { Transposing pixels within a block just requires transposing the
  370. DCT coefficients.
  371. Partial iMCUs at the edges require no special treatment; we simply
  372. process all the available DCT blocks for every component. }
  373. for ci := 0 to dstinfo^.num_components-1 do
  374. begin
  375. compptr := jpeg_component_info_ptr(dstinfo^.comp_info);
  376. Inc(compptr, ci);
  377. dst_blk_y := 0;
  378. while (dst_blk_y < compptr^.height_in_blocks) do
  379. begin
  380. dst_buffer := srcinfo^.mem^.access_virt_barray
  381. (j_common_ptr(srcinfo), dst_coef_arrays^[ci], dst_blk_y,
  382. JDIMENSION (compptr^.v_samp_factor), TRUE);
  383. for offset_y := 0 to compptr^.v_samp_factor-1 do
  384. begin
  385. dst_blk_x := 0;
  386. while (dst_blk_x < compptr^.width_in_blocks) do
  387. begin
  388. src_buffer := srcinfo^.mem^.access_virt_barray
  389. (j_common_ptr(srcinfo), src_coef_arrays^[ci], dst_blk_x,
  390. JDIMENSION (compptr^.h_samp_factor), FALSE);
  391. for offset_x := 0 to compptr^.h_samp_factor-1 do
  392. begin
  393. src_ptr := JCOEFPTR(@(src_buffer^[offset_x]^
  394. [dst_blk_y + offset_y]));
  395. dst_ptr := JCOEFPTR(@(dst_buffer^[offset_y]^
  396. [dst_blk_x + offset_x]));
  397. for i := 0 to DCTSIZE-1 do
  398. for j := 0 to DCTSIZE-1 do
  399. dst_ptr^[j*DCTSIZE+i] := src_ptr^[i*DCTSIZE+j];
  400. end;
  401. Inc(dst_blk_x, compptr^.h_samp_factor);
  402. end;
  403. end;
  404. Inc(dst_blk_y, compptr^.v_samp_factor);
  405. end; { while }
  406. end; { for ci }
  407. end; { do_transpose }
  408. {LOCAL}
  409. procedure do_rot_90 (srcinfo : j_decompress_ptr;
  410. dstinfo : j_compress_ptr;
  411. src_coef_arrays : jvirt_barray_tbl_ptr;
  412. dst_coef_arrays : jvirt_barray_tbl_ptr);
  413. { 90 degree rotation is equivalent to
  414. 1. Transposing the image;
  415. 2. Horizontal mirroring.
  416. These two steps are merged into a single processing routine. }
  417. var
  418. MCU_cols, comp_width, dst_blk_x, dst_blk_y : JDIMENSION;
  419. ci, i, j, offset_x, offset_y : int;
  420. src_buffer, dst_buffer : JBLOCKARRAY;
  421. src_ptr, dst_ptr : JCOEFPTR;
  422. compptr : jpeg_component_info_ptr;
  423. begin
  424. { Because of the horizontal mirror step, we can't process partial iMCUs
  425. at the (output) right edge properly. They just get transposed and
  426. not mirrored. }
  427. MCU_cols := dstinfo^.image_width div (dstinfo^.max_h_samp_factor * DCTSIZE);
  428. for ci := 0 to dstinfo^.num_components-1 do
  429. begin
  430. compptr := jpeg_component_info_ptr(dstinfo^.comp_info);
  431. Inc(compptr, ci);
  432. comp_width := MCU_cols * compptr^.h_samp_factor;
  433. dst_blk_y := 0;
  434. while ( dst_blk_y < compptr^.height_in_blocks) do
  435. begin
  436. dst_buffer := srcinfo^.mem^.access_virt_barray
  437. (j_common_ptr(srcinfo), dst_coef_arrays^[ci], dst_blk_y,
  438. JDIMENSION (compptr^.v_samp_factor), TRUE);
  439. for offset_y := 0 to compptr^.v_samp_factor-1 do
  440. begin
  441. dst_blk_x := 0;
  442. while (dst_blk_x < compptr^.width_in_blocks) do
  443. begin
  444. src_buffer := srcinfo^.mem^.access_virt_barray
  445. (j_common_ptr(srcinfo), src_coef_arrays^[ci], dst_blk_x,
  446. JDIMENSION (compptr^.h_samp_factor), FALSE);
  447. for offset_x := 0 to compptr^.h_samp_factor-1 do
  448. begin
  449. src_ptr := JCOEFPTR(@(src_buffer^[offset_x]^
  450. [dst_blk_y + offset_y]));
  451. if (dst_blk_x < comp_width) then
  452. begin
  453. { Block is within the mirrorable area. }
  454. dst_ptr := JCOEFPTR(@(dst_buffer^[offset_y]^
  455. [comp_width - dst_blk_x - offset_x - 1]));
  456. i := 0;
  457. while (i < DCTSIZE) do
  458. begin
  459. for j := 0 to DCTSIZE-1 do
  460. dst_ptr^[j*DCTSIZE+i] := src_ptr^[i*DCTSIZE+j];
  461. Inc(i);
  462. for j := 0 to DCTSIZE-1 do
  463. dst_ptr^[j*DCTSIZE+i] := -src_ptr^[i*DCTSIZE+j];
  464. Inc(i);
  465. end;
  466. end
  467. else
  468. begin
  469. { Edge blocks are transposed but not mirrored. }
  470. dst_ptr := JCOEFPTR(@(dst_buffer^[offset_y]^
  471. [dst_blk_x + offset_x]));
  472. for i := 0 to DCTSIZE-1 do
  473. for j := 0 to DCTSIZE-1 do
  474. dst_ptr^[j*DCTSIZE+i] := src_ptr^[i*DCTSIZE+j];
  475. end;
  476. end;
  477. Inc(dst_blk_x, compptr^.h_samp_factor);
  478. end;
  479. end;
  480. Inc(dst_blk_y, compptr^.v_samp_factor);
  481. end; { while }
  482. end; { for ci }
  483. end; { do_rot_90 }
  484. {LOCAL}
  485. procedure do_rot_270 (srcinfo : j_decompress_ptr;
  486. dstinfo : j_compress_ptr;
  487. src_coef_arrays : jvirt_barray_tbl_ptr;
  488. dst_coef_arrays : jvirt_barray_tbl_ptr);
  489. { 270 degree rotation is equivalent to
  490. 1. Horizontal mirroring;
  491. 2. Transposing the image.
  492. These two steps are merged into a single processing routine. }
  493. var
  494. MCU_rows, comp_height, dst_blk_x, dst_blk_y : JDIMENSION;
  495. ci, i, j, offset_x, offset_y : int;
  496. src_buffer, dst_buffer : JBLOCKARRAY;
  497. src_ptr, dst_ptr : JCOEFPTR;
  498. compptr : jpeg_component_info_ptr;
  499. begin
  500. { Because of the horizontal mirror step, we can't process partial iMCUs
  501. at the (output) bottom edge properly. They just get transposed and
  502. not mirrored. }
  503. MCU_rows := dstinfo^.image_height div (dstinfo^.max_v_samp_factor * DCTSIZE);
  504. for ci := 0 to dstinfo^.num_components-1 do
  505. begin
  506. compptr := jpeg_component_info_ptr(dstinfo^.comp_info);
  507. Inc(compptr, ci);
  508. comp_height := MCU_rows * compptr^.v_samp_factor;
  509. dst_blk_y := 0;
  510. while (dst_blk_y < compptr^.height_in_blocks) do
  511. begin
  512. dst_buffer := srcinfo^.mem^.access_virt_barray
  513. (j_common_ptr(srcinfo), dst_coef_arrays^[ci], dst_blk_y,
  514. JDIMENSION (compptr^.v_samp_factor), TRUE);
  515. for offset_y := 0 to compptr^.v_samp_factor-1 do
  516. begin
  517. dst_blk_x := 0;
  518. while (dst_blk_x < compptr^.width_in_blocks) do
  519. begin
  520. src_buffer := srcinfo^.mem^.access_virt_barray
  521. (j_common_ptr(srcinfo), src_coef_arrays^[ci], dst_blk_x,
  522. JDIMENSION (compptr^.h_samp_factor), FALSE);
  523. for offset_x := 0 to compptr^.h_samp_factor-1 do
  524. begin
  525. dst_ptr := JCOEFPTR(@(dst_buffer^[offset_y]^
  526. [dst_blk_x + offset_x]));
  527. if (dst_blk_y < comp_height) then
  528. begin
  529. { Block is within the mirrorable area. }
  530. src_ptr := JCOEFPTR(@(src_buffer^[offset_x]^
  531. [comp_height - dst_blk_y - offset_y - 1]));
  532. for i := 0 to DCTSIZE-1 do
  533. begin
  534. j := 0;
  535. while (j < DCTSIZE) do
  536. begin
  537. dst_ptr^[j*DCTSIZE+i] := src_ptr^[i*DCTSIZE+j];
  538. Inc(j);
  539. dst_ptr^[j*DCTSIZE+i] := -src_ptr^[i*DCTSIZE+j];
  540. Inc(j);
  541. end;
  542. end;
  543. end
  544. else
  545. begin
  546. { Edge blocks are transposed but not mirrored. }
  547. src_ptr := JCOEFPTR(@(src_buffer^[offset_x]^
  548. [dst_blk_y + offset_y]));
  549. for i := 0 to DCTSIZE-1 do
  550. for j := 0 to DCTSIZE-1 do
  551. dst_ptr^[j*DCTSIZE+i] := src_ptr^[i*DCTSIZE+j];
  552. end;
  553. end;
  554. Inc(dst_blk_x, compptr^.h_samp_factor);
  555. end;
  556. end;
  557. Inc(dst_blk_y, compptr^.v_samp_factor);
  558. end; { while }
  559. end; { for ci }
  560. end; { do_rot_270 }
  561. {LOCAL}
  562. procedure do_rot_180 (srcinfo : j_decompress_ptr;
  563. dstinfo : j_compress_ptr;
  564. src_coef_arrays : jvirt_barray_tbl_ptr;
  565. dst_coef_arrays : jvirt_barray_tbl_ptr);
  566. { 180 degree rotation is equivalent to
  567. 1. Vertical mirroring;
  568. 2. Horizontal mirroring.
  569. These two steps are merged into a single processing routine. }
  570. var
  571. MCU_cols, MCU_rows, comp_width, comp_height, dst_blk_x, dst_blk_y : JDIMENSION;
  572. ci, i, j, offset_y : int;
  573. src_buffer, dst_buffer : JBLOCKARRAY;
  574. src_row_ptr, dst_row_ptr : JBLOCKROW;
  575. src_ptr, dst_ptr : JCOEF_PTR;
  576. compptr : jpeg_component_info_ptr;
  577. begin
  578. MCU_cols := dstinfo^.image_width div (dstinfo^.max_h_samp_factor * DCTSIZE);
  579. MCU_rows := dstinfo^.image_height div (dstinfo^.max_v_samp_factor * DCTSIZE);
  580. for ci := 0 to dstinfo^.num_components-1 do
  581. begin
  582. compptr := jpeg_component_info_ptr(dstinfo^.comp_info);
  583. Inc(compptr, ci);
  584. comp_width := MCU_cols * compptr^.h_samp_factor;
  585. comp_height := MCU_rows * compptr^.v_samp_factor;
  586. dst_blk_y := 0;
  587. while (dst_blk_y < compptr^.height_in_blocks) do
  588. begin
  589. dst_buffer := srcinfo^.mem^.access_virt_barray
  590. (j_common_ptr(srcinfo), dst_coef_arrays^[ci], dst_blk_y,
  591. JDIMENSION (compptr^.v_samp_factor), TRUE);
  592. if (dst_blk_y < comp_height) then
  593. begin
  594. { Row is within the vertically mirrorable area. }
  595. src_buffer := srcinfo^.mem^.access_virt_barray
  596. (j_common_ptr(srcinfo), src_coef_arrays^[ci],
  597. comp_height - dst_blk_y - JDIMENSION (compptr^.v_samp_factor),
  598. JDIMENSION (compptr^.v_samp_factor), FALSE);
  599. end
  600. else
  601. begin
  602. { Bottom-edge rows are only mirrored horizontally. }
  603. src_buffer := srcinfo^.mem^.access_virt_barray
  604. (j_common_ptr(srcinfo), src_coef_arrays^[ci], dst_blk_y,
  605. JDIMENSION (compptr^.v_samp_factor), FALSE);
  606. end;
  607. for offset_y := 0 to compptr^.v_samp_factor-1 do
  608. begin
  609. if (dst_blk_y < comp_height) then
  610. begin
  611. { Row is within the mirrorable area. }
  612. dst_row_ptr := dst_buffer^[offset_y];
  613. src_row_ptr := src_buffer^[compptr^.v_samp_factor - offset_y - 1];
  614. { Process the blocks that can be mirrored both ways. }
  615. for dst_blk_x := 0 to comp_width-1 do
  616. begin
  617. dst_ptr := JCOEF_PTR(@(dst_row_ptr^[dst_blk_x]));
  618. src_ptr := JCOEF_PTR(@(src_row_ptr^[comp_width - dst_blk_x - 1]));
  619. i := 0;
  620. while (i < DCTSIZE) do
  621. begin
  622. { For even row, negate every odd column. }
  623. j := 0;
  624. while (j < DCTSIZE) do
  625. begin
  626. dst_ptr^ := src_ptr^;
  627. Inc(dst_ptr);
  628. Inc(src_ptr);
  629. dst_ptr^ := - src_ptr^;
  630. Inc(dst_ptr);
  631. Inc(src_ptr);
  632. Inc(j, 2);
  633. end;
  634. { For odd row, negate every even column. }
  635. j := 0;
  636. while (j < DCTSIZE) do
  637. begin
  638. dst_ptr^ := - src_ptr^;
  639. Inc(dst_ptr);
  640. Inc(src_ptr);
  641. dst_ptr^ := src_ptr^;
  642. Inc(dst_ptr);
  643. Inc(src_ptr);
  644. Inc(j, 2);
  645. end;
  646. Inc(i, 2);
  647. end; { while i }
  648. end;
  649. { Any remaining right-edge blocks are only mirrored vertically. }
  650. for dst_blk_x := comp_width to compptr^.width_in_blocks-1 do
  651. begin
  652. dst_ptr := JCOEF_PTR(@(dst_row_ptr^[dst_blk_x]));
  653. src_ptr := JCOEF_PTR(@(src_row_ptr^[dst_blk_x]));
  654. i := 0;
  655. while (i < DCTSIZE) do
  656. begin
  657. for j := 0 to DCTSIZE-1 do
  658. begin
  659. dst_ptr^ := src_ptr^;
  660. Inc(dst_ptr);
  661. Inc(src_ptr);
  662. end;
  663. for j := 0 to DCTSIZE-1 do
  664. begin
  665. dst_ptr^ := - src_ptr^;
  666. Inc(dst_ptr);
  667. Inc(src_ptr);
  668. end;
  669. Inc(i, 2);
  670. end
  671. end
  672. end
  673. else
  674. begin
  675. { Remaining rows are just mirrored horizontally. }
  676. dst_row_ptr := dst_buffer^[offset_y];
  677. src_row_ptr := src_buffer^[offset_y];
  678. { Process the blocks that can be mirrored. }
  679. for dst_blk_x := 0 to comp_width-1 do
  680. begin
  681. dst_ptr := JCOEF_PTR(@(dst_row_ptr^[dst_blk_x]));
  682. src_ptr := JCOEF_PTR(@(src_row_ptr^[comp_width - dst_blk_x - 1]));
  683. i := 0;
  684. while (i < DCTSIZE2) do
  685. begin
  686. dst_ptr^ := src_ptr^;
  687. Inc(dst_ptr);
  688. Inc(src_ptr);
  689. dst_ptr^ := - src_ptr^;
  690. Inc(dst_ptr);
  691. Inc(src_ptr);
  692. Inc(i, 2);
  693. end;
  694. end;
  695. { Any remaining right-edge blocks are only copied. }
  696. for dst_blk_x := comp_width to compptr^.width_in_blocks-1 do
  697. begin
  698. dst_ptr := JCOEF_PTR(@(dst_row_ptr^[dst_blk_x]));
  699. src_ptr := JCOEF_PTR(@(src_row_ptr^[dst_blk_x]));
  700. for i := 0 to DCTSIZE2-1 do
  701. begin
  702. dst_ptr^ := src_ptr^;
  703. Inc(dst_ptr);
  704. Inc(src_ptr);
  705. end;
  706. end;
  707. end;
  708. end;
  709. Inc(dst_blk_y, compptr^.v_samp_factor) ;
  710. end; { while }
  711. end; { for ci }
  712. end; { do_rot_180 }
  713. {LOCAL}
  714. procedure do_transverse (srcinfo : j_decompress_ptr;
  715. dstinfo : j_compress_ptr;
  716. src_coef_arrays : jvirt_barray_tbl_ptr;
  717. dst_coef_arrays : jvirt_barray_tbl_ptr);
  718. { Transverse transpose is equivalent to
  719. 1. 180 degree rotation;
  720. 2. Transposition;
  721. or
  722. 1. Horizontal mirroring;
  723. 2. Transposition;
  724. 3. Horizontal mirroring.
  725. These steps are merged into a single processing routine. }
  726. var
  727. MCU_cols, MCU_rows, comp_width, comp_height, dst_blk_x, dst_blk_y : JDIMENSION;
  728. ci, i, j, offset_x, offset_y : int;
  729. src_buffer, dst_buffer : JBLOCKARRAY;
  730. src_ptr, dst_ptr : JCOEFPTR;
  731. compptr : jpeg_component_info_ptr;
  732. begin
  733. MCU_cols := dstinfo^.image_width div (dstinfo^.max_h_samp_factor * DCTSIZE);
  734. MCU_rows := dstinfo^.image_height div (dstinfo^.max_v_samp_factor * DCTSIZE);
  735. for ci := 0 to dstinfo^.num_components-1 do
  736. begin
  737. compptr := jpeg_component_info_ptr(dstinfo^.comp_info);
  738. Inc(compptr, ci);
  739. comp_width := MCU_cols * compptr^.h_samp_factor;
  740. comp_height := MCU_rows * compptr^.v_samp_factor;
  741. dst_blk_y := 0;
  742. while (dst_blk_y < compptr^.height_in_blocks) do
  743. begin
  744. dst_buffer := srcinfo^.mem^.access_virt_barray
  745. (j_common_ptr(srcinfo), dst_coef_arrays^[ci], dst_blk_y,
  746. JDIMENSION (compptr^.v_samp_factor), TRUE);
  747. for offset_y := 0 to compptr^.v_samp_factor-1 do
  748. begin
  749. dst_blk_x := 0;
  750. while ( dst_blk_x < compptr^.width_in_blocks) do
  751. begin
  752. src_buffer := srcinfo^.mem^.access_virt_barray
  753. (j_common_ptr(srcinfo), src_coef_arrays^[ci], dst_blk_x,
  754. JDIMENSION (compptr^.h_samp_factor), FALSE);
  755. for offset_x := 0 to compptr^.h_samp_factor-1 do
  756. begin
  757. if (dst_blk_y < comp_height) then
  758. begin
  759. src_ptr := JCOEFPTR(@(src_buffer^[offset_x]^
  760. [comp_height - dst_blk_y - offset_y - 1]));
  761. if (dst_blk_x < comp_width) then
  762. begin
  763. { Block is within the mirrorable area. }
  764. dst_ptr := JCOEFPTR(@(dst_buffer^[offset_y]^
  765. [comp_width - dst_blk_x - offset_x - 1]));
  766. i := 0;
  767. while (i < DCTSIZE) do
  768. begin
  769. j := 0;
  770. while (j < DCTSIZE) do
  771. begin
  772. dst_ptr^[j*DCTSIZE+i] := src_ptr^[i*DCTSIZE+j];
  773. Inc(j);
  774. dst_ptr^[j*DCTSIZE+i] := -src_ptr^[i*DCTSIZE+j];
  775. Inc(j);
  776. end;
  777. Inc(i);
  778. j := 0;
  779. while (j < DCTSIZE) do
  780. begin
  781. dst_ptr^[j*DCTSIZE+i] := -src_ptr^[i*DCTSIZE+j];
  782. Inc(j);
  783. dst_ptr^[j*DCTSIZE+i] := src_ptr^[i*DCTSIZE+j];
  784. Inc(j);
  785. end;
  786. Inc(i);
  787. end
  788. end
  789. else
  790. begin
  791. { Right-edge blocks are mirrored in y only }
  792. dst_ptr := JCOEFPTR(@(dst_buffer^[offset_y]^
  793. [dst_blk_x + offset_x]));
  794. for i := 0 to DCTSIZE-1 do
  795. begin
  796. j := 0;
  797. while (j < DCTSIZE) do
  798. begin
  799. dst_ptr^[j*DCTSIZE+i] := src_ptr^[i*DCTSIZE+j];
  800. Inc(j);
  801. dst_ptr^[j*DCTSIZE+i] := -src_ptr^[i*DCTSIZE+j];
  802. Inc(j);
  803. end;
  804. end;
  805. end;
  806. end
  807. else
  808. begin
  809. src_ptr := JCOEFPTR(@(src_buffer^[offset_x]^
  810. [dst_blk_y + offset_y]));
  811. if (dst_blk_x < comp_width) then
  812. begin
  813. { Bottom-edge blocks are mirrored in x only }
  814. dst_ptr := JCOEFPTR(@(dst_buffer^[offset_y]^
  815. [comp_width - dst_blk_x - offset_x - 1]));
  816. i := 0;
  817. while (i < DCTSIZE) do
  818. begin
  819. for j := 0 to DCTSIZE-1 do
  820. dst_ptr^[j*DCTSIZE+i] := src_ptr^[i*DCTSIZE+j];
  821. Inc(i);
  822. for j := 0 to DCTSIZE-1 do
  823. dst_ptr^[j*DCTSIZE+i] := -src_ptr^[i*DCTSIZE+j];
  824. Inc(i);
  825. end;
  826. end
  827. else
  828. begin
  829. { At lower right corner, just transpose, no mirroring }
  830. dst_ptr := JCOEFPTR(@(dst_buffer^[offset_y]^
  831. [dst_blk_x + offset_x]));
  832. for i := 0 to DCTSIZE-1 do
  833. for j := 0 to DCTSIZE-1 do
  834. dst_ptr^[j*DCTSIZE+i] := src_ptr^[i*DCTSIZE+j];
  835. end;
  836. end;
  837. end;
  838. Inc(dst_blk_x, compptr^.h_samp_factor);
  839. end;
  840. end;
  841. Inc(dst_blk_y, compptr^.v_samp_factor);
  842. end; { while }
  843. end; { for ci }
  844. end; { do_transverse }
  845. { Request any required workspace.
  846. We allocate the workspace virtual arrays from the source decompression
  847. object, so that all the arrays (both the original data and the workspace)
  848. will be taken into account while making memory management decisions.
  849. Hence, this routine must be called after jpeg_read_header (which reads
  850. the image dimensions) and before jpeg_read_coefficients (which realizes
  851. the source's virtual arrays). }
  852. {GLOBAL}
  853. procedure jtransform_request_workspace (
  854. srcinfo : j_decompress_ptr;
  855. var info : jpeg_transform_info);
  856. var
  857. coef_arrays : jvirt_barray_tbl_ptr;
  858. compptr : jpeg_component_info_ptr;
  859. ci : int;
  860. begin
  861. coef_arrays := NIL;
  862. if (info.force_grayscale) and (srcinfo^.jpeg_color_space = JCS_YCbCr)
  863. and (srcinfo^.num_components = 3) then
  864. begin
  865. { We'll only process the first component }
  866. info.num_components := 1;
  867. end
  868. else
  869. begin
  870. { Process all the components }
  871. info.num_components := srcinfo^.num_components;
  872. end;
  873. case (info.transform) of
  874. JXFORM_NONE,
  875. JXFORM_FLIP_H:;
  876. { Don't need a workspace array }
  877. {$ifdef CROP_SUPPORTED}
  878. JXFORM_CUT,
  879. { really cut needs smaller arrays if you want to figure it out }
  880. {$endif}
  881. JXFORM_FLIP_V,
  882. JXFORM_ROT_180:
  883. begin
  884. { Need workspace arrays having same dimensions as source image.
  885. Note that we allocate arrays padded out to the next iMCU boundary,
  886. so that transform routines need not worry about missing edge blocks. }
  887. coef_arrays := jvirt_barray_tbl_ptr (
  888. srcinfo^.mem^.alloc_small (j_common_ptr(srcinfo), JPOOL_IMAGE,
  889. SIZEOF(jvirt_barray_ptr) * info.num_components) );
  890. for ci := 0 to info.num_components-1 do
  891. begin
  892. compptr := jpeg_component_info_ptr(srcinfo^.comp_info);
  893. Inc(compptr, ci);
  894. coef_arrays^[ci] := srcinfo^.mem^.request_virt_barray
  895. (j_common_ptr(srcinfo), JPOOL_IMAGE, FALSE,
  896. JDIMENSION (jround_up( long (compptr^.width_in_blocks),
  897. long (compptr^.h_samp_factor)) ),
  898. JDIMENSION (jround_up( long (compptr^.height_in_blocks),
  899. long (compptr^.v_samp_factor)) ),
  900. JDIMENSION (compptr^.v_samp_factor));
  901. end;
  902. end;
  903. JXFORM_TRANSPOSE,
  904. JXFORM_TRANSVERSE,
  905. JXFORM_ROT_90,
  906. JXFORM_ROT_270:
  907. begin
  908. { Need workspace arrays having transposed dimensions.
  909. Note that we allocate arrays padded out to the next iMCU boundary,
  910. so that transform routines need not worry about missing edge blocks. }
  911. coef_arrays := jvirt_barray_tbl_ptr(
  912. srcinfo^.mem^.alloc_small (j_common_ptr(srcinfo), JPOOL_IMAGE,
  913. SIZEOF(jvirt_barray_ptr) * info.num_components) );
  914. for ci := 0 to info.num_components-1 do
  915. begin
  916. compptr := jpeg_component_info_ptr(srcinfo^.comp_info);
  917. Inc(compptr, ci);
  918. coef_arrays^[ci] := srcinfo^.mem^.request_virt_barray
  919. (j_common_ptr(srcinfo), JPOOL_IMAGE, FALSE,
  920. JDIMENSION ( jround_up( long(compptr^.height_in_blocks),
  921. long(compptr^.v_samp_factor) ) ),
  922. JDIMENSION ( jround_up( long(compptr^.width_in_blocks),
  923. long(compptr^.h_samp_factor) ) ),
  924. JDIMENSION ( compptr^.h_samp_factor ) );
  925. end;
  926. end;
  927. end;
  928. info.workspace_coef_arrays := coef_arrays;
  929. end;
  930. { Transpose destination image parameters }
  931. {LOCAL}
  932. procedure transpose_critical_parameters (dstinfo : j_compress_ptr);
  933. var
  934. tblno, i, j, ci, itemp : int;
  935. compptr : jpeg_component_info_ptr;
  936. qtblptr : JQUANT_TBL_PTR;
  937. dtemp : JDIMENSION;
  938. qtemp : UINT16;
  939. begin
  940. { Transpose basic image dimensions }
  941. dtemp := dstinfo^.image_width;
  942. dstinfo^.image_width := dstinfo^.image_height;
  943. dstinfo^.image_height := dtemp;
  944. { Transpose sampling factors }
  945. for ci := 0 to dstinfo^.num_components-1 do
  946. begin
  947. compptr := jpeg_component_info_ptr(dstinfo^.comp_info);
  948. Inc(compptr, ci);
  949. itemp := compptr^.h_samp_factor;
  950. compptr^.h_samp_factor := compptr^.v_samp_factor;
  951. compptr^.v_samp_factor := itemp;
  952. end;
  953. { Transpose quantization tables }
  954. for tblno := 0 to NUM_QUANT_TBLS-1 do
  955. begin
  956. qtblptr := dstinfo^.quant_tbl_ptrs[tblno];
  957. if (qtblptr <> NIL) then
  958. begin
  959. for i := 0 to DCTSIZE-1 do
  960. begin
  961. for j := 0 to i-1 do
  962. begin
  963. qtemp := qtblptr^.quantval[i*DCTSIZE+j];
  964. qtblptr^.quantval[i*DCTSIZE+j] := qtblptr^.quantval[j*DCTSIZE+i];
  965. qtblptr^.quantval[j*DCTSIZE+i] := qtemp;
  966. end;
  967. end;
  968. end;
  969. end;
  970. end;
  971. { Trim off any partial iMCUs on the indicated destination edge }
  972. {LOCAL}
  973. procedure trim_right_edge (dstinfo : j_compress_ptr);
  974. var
  975. ci, max_h_samp_factor : int;
  976. MCU_cols : JDIMENSION;
  977. var
  978. h_samp_factor : int;
  979. begin
  980. { We have to compute max_h_samp_factor ourselves,
  981. because it hasn't been set yet in the destination
  982. (and we don't want to use the source's value). }
  983. max_h_samp_factor := 1;
  984. for ci := 0 to dstinfo^.num_components-1 do
  985. begin
  986. h_samp_factor := dstinfo^.comp_info^[ci].h_samp_factor;
  987. {max_h_samp_factor := MAX(max_h_samp_factor, h_samp_factor);}
  988. if h_samp_factor > max_h_samp_factor then
  989. max_h_samp_factor := h_samp_factor;
  990. end;
  991. MCU_cols := dstinfo^.image_width div (max_h_samp_factor * DCTSIZE);
  992. if (MCU_cols > 0) then { can't trim to 0 pixels }
  993. dstinfo^.image_width := MCU_cols * (max_h_samp_factor * DCTSIZE);
  994. end;
  995. {LOCAL}
  996. procedure trim_bottom_edge (dstinfo : j_compress_ptr);
  997. var
  998. ci, max_v_samp_factor : int;
  999. MCU_rows : JDIMENSION;
  1000. var
  1001. v_samp_factor : int;
  1002. begin
  1003. { We have to compute max_v_samp_factor ourselves,
  1004. because it hasn't been set yet in the destination
  1005. (and we don't want to use the source's value). }
  1006. max_v_samp_factor := 1;
  1007. for ci := 0 to dstinfo^.num_components-1 do
  1008. begin
  1009. v_samp_factor := dstinfo^.comp_info^[ci].v_samp_factor;
  1010. {max_v_samp_factor := MAX(max_v_samp_factor, v_samp_factor);}
  1011. if v_samp_factor > max_v_samp_factor then
  1012. max_v_samp_factor := v_samp_factor;
  1013. end;
  1014. MCU_rows := dstinfo^.image_height div (max_v_samp_factor * DCTSIZE);
  1015. if (MCU_rows > 0) then { can't trim to 0 pixels }
  1016. dstinfo^.image_height := MCU_rows * (max_v_samp_factor * DCTSIZE);
  1017. end;
  1018. {$ifdef CROP_SUPPORTED}
  1019. { For cropping, realize and constrain the target area, and reshape the
  1020. dstinfo to hold the resulting image.
  1021. Input was supplied as WxH[+-]X[+-]Y offsets. Negative offsets are
  1022. relative to the lower righthand corner of the image. The region is
  1023. expanded so that all boundaries fall on even MCU blocks by rounding
  1024. the offsets *down* (at the do_transform() step) and the size *up*. }
  1025. {LOCAL}
  1026. procedure set_dest_size(dstinfo : j_compress_ptr;
  1027. var info : jpeg_transform_info);
  1028. var
  1029. ci, max_samp_factor : int;
  1030. MCU_size, newsize, offset, factor : JDIMENSION;
  1031. var
  1032. samp_factor : int;
  1033. begin
  1034. { Initially the dstinfo is the same size as the srcinfo.
  1035. Use it to constrain the offsets: }
  1036. if (info.xoffs < 0) then
  1037. Inc(info.xoffs, dstinfo^.image_width);
  1038. if (info.yoffs < 0) then
  1039. Inc(info.yoffs, dstinfo^.image_height);
  1040. if (info.xoffs < 0) or (info.xoffs >= dstinfo^.image_width) or
  1041. (info.yoffs < 0) or (info.yoffs >= dstinfo^.image_height) then
  1042. begin
  1043. {jpegtran_error('-cut offsets fall outside source image');}
  1044. ERREXIT(j_common_ptr(dstinfo), JERR_CONVERSION_NOTIMPL);
  1045. end;
  1046. { use it to constrain the size: }
  1047. if (info.newwidth + info.xoffs > dstinfo^.image_width) then
  1048. info.newwidth := dstinfo^.image_width - info.xoffs;
  1049. if (info.newheight + info.yoffs > dstinfo^.image_height) then
  1050. info.newheight := dstinfo^.image_height - info.yoffs;
  1051. { We have to compute max_v/h_samp_factors ourselves,
  1052. because it hasn't been set yet in the destination
  1053. (and we don't want to use the source's value). }
  1054. max_samp_factor := 1;
  1055. for ci := 0 to dstinfo^.num_components-1 do
  1056. begin
  1057. samp_factor := dstinfo^.comp_info^[ci].v_samp_factor;
  1058. {max_samp_factor := MAX(max_samp_factor, samp_factor);}
  1059. if (max_samp_factor < samp_factor) then
  1060. max_samp_factor := samp_factor;
  1061. end;
  1062. { Find original (rounded down) and new (rounded up) heights in full
  1063. dct blocks, choose the smaller of the two. }
  1064. factor := max_samp_factor * DCTSIZE;
  1065. MCU_size := dstinfo^.image_height div factor;
  1066. newsize := (info.newheight + (info.yoffs mod factor) + factor - 1) div factor;
  1067. {MCU_size := MIN(MCU_size, newsize);}
  1068. if (MCU_size > newsize) then
  1069. MCU_size := newsize;
  1070. if (MCU_size > 0) then { can't trim to 0 pixels }
  1071. dstinfo^.image_height := MCU_size * factor
  1072. else
  1073. begin
  1074. {jpegtran_error('degenerate -cut height');}
  1075. ERREXIT(j_common_ptr(dstinfo), JERR_CONVERSION_NOTIMPL);
  1076. end;
  1077. max_samp_factor := 1;
  1078. for ci := 0 to dstinfo^.num_components-1 do
  1079. begin
  1080. samp_factor := dstinfo^.comp_info^[ci].h_samp_factor;
  1081. {max_samp_factor := MAX(max_samp_factor, samp_factor);}
  1082. if (max_samp_factor < samp_factor) then
  1083. max_samp_factor := samp_factor;
  1084. end;
  1085. { Find original (rounded down) and new (rounded up) heights in full
  1086. dct blocks, choose the smaller of the two. }
  1087. factor := max_samp_factor * DCTSIZE;
  1088. MCU_size := dstinfo^.image_width div factor;
  1089. newsize := (info.newwidth + (info.xoffs mod factor) + factor - 1) div factor;
  1090. {MCU_size := MIN(MCU_size, newsize);}
  1091. if (MCU_size > newsize) then
  1092. MCU_size := newsize;
  1093. if (MCU_size > 0) then { can't trim to 0 pixels }
  1094. dstinfo^.image_width := MCU_size * factor
  1095. else
  1096. begin
  1097. {jpegtran_error('degenerate -cut width');}
  1098. ERREXIT(j_common_ptr(dstinfo), JERR_CONVERSION_NOTIMPL);
  1099. end;
  1100. end;
  1101. {$endif}
  1102. { Adjust output image parameters as needed.
  1103. This must be called after jpeg_copy_critical_parameters()
  1104. and before jpeg_write_coefficients().
  1105. The return value is the set of virtual coefficient arrays to be written
  1106. (either the ones allocated by jtransform_request_workspace, or the
  1107. original source data arrays). The caller will need to pass this value
  1108. to jpeg_write_coefficients(). }
  1109. {GLOBAL}
  1110. function jtransform_adjust_parameters
  1111. (srcinfo : j_decompress_ptr;
  1112. dstinfo : j_compress_ptr;
  1113. src_coef_arrays : jvirt_barray_tbl_ptr;
  1114. var info : jpeg_transform_info) : jvirt_barray_tbl_ptr;
  1115. var
  1116. sv_quant_tbl_no : int;
  1117. begin
  1118. { If force-to-grayscale is requested, adjust destination parameters }
  1119. if (info.force_grayscale) then
  1120. begin
  1121. { We use jpeg_set_colorspace to make sure subsidiary settings get fixed
  1122. properly. Among other things, the target h_samp_factor & v_samp_factor
  1123. will get set to 1, which typically won't match the source.
  1124. In fact we do this even if the source is already grayscale; that
  1125. provides an easy way of coercing a grayscale JPEG with funny sampling
  1126. factors to the customary 1,1. (Some decoders fail on other factors.) }
  1127. if ((dstinfo^.jpeg_color_space = JCS_YCbCr) and
  1128. (dstinfo^.num_components = 3)) or
  1129. ((dstinfo^.jpeg_color_space = JCS_GRAYSCALE) and
  1130. (dstinfo^.num_components = 1)) then
  1131. begin
  1132. { We have to preserve the source's quantization table number. }
  1133. sv_quant_tbl_no := dstinfo^.comp_info^[0].quant_tbl_no;
  1134. jpeg_set_colorspace(dstinfo, JCS_GRAYSCALE);
  1135. dstinfo^.comp_info^[0].quant_tbl_no := sv_quant_tbl_no;
  1136. end
  1137. else
  1138. begin
  1139. { Sorry, can't do it }
  1140. ERREXIT(j_common_ptr(dstinfo), JERR_CONVERSION_NOTIMPL);
  1141. end;
  1142. end;
  1143. { Correct the destination's image dimensions etc if necessary }
  1144. case (info.transform) of
  1145. JXFORM_NONE:;
  1146. { Nothing to do }
  1147. {$ifdef CROP_SUPPORTED}
  1148. JXFORM_CUT:
  1149. set_dest_size(dstinfo, info);
  1150. {$endif}
  1151. JXFORM_FLIP_H:
  1152. if (info.trim) then
  1153. trim_right_edge(dstinfo);
  1154. JXFORM_FLIP_V:
  1155. if (info.trim) then
  1156. trim_bottom_edge(dstinfo);
  1157. JXFORM_TRANSPOSE:
  1158. transpose_critical_parameters(dstinfo);
  1159. { transpose does NOT have to trim anything }
  1160. JXFORM_TRANSVERSE:
  1161. begin
  1162. transpose_critical_parameters(dstinfo);
  1163. if (info.trim) then
  1164. begin
  1165. trim_right_edge(dstinfo);
  1166. trim_bottom_edge(dstinfo);
  1167. end;
  1168. end;
  1169. JXFORM_ROT_90:
  1170. begin
  1171. transpose_critical_parameters(dstinfo);
  1172. if (info.trim) then
  1173. trim_right_edge(dstinfo);
  1174. end;
  1175. JXFORM_ROT_180:
  1176. if (info.trim) then
  1177. begin
  1178. trim_right_edge(dstinfo);
  1179. trim_bottom_edge(dstinfo);
  1180. end;
  1181. JXFORM_ROT_270:
  1182. begin
  1183. transpose_critical_parameters(dstinfo);
  1184. if (info.trim) then
  1185. trim_bottom_edge(dstinfo);
  1186. end;
  1187. end;
  1188. { Return the appropriate output data set }
  1189. if (info.workspace_coef_arrays <> NIL) then
  1190. jtransform_adjust_parameters := info.workspace_coef_arrays
  1191. else
  1192. jtransform_adjust_parameters := src_coef_arrays;
  1193. end;
  1194. { Execute the actual transformation, if any.
  1195. This must be called *after* jpeg_write_coefficients, because it depends
  1196. on jpeg_write_coefficients to have computed subsidiary values such as
  1197. the per-component width and height fields in the destination object.
  1198. Note that some transformations will modify the source data arrays! }
  1199. {GLOBAL}
  1200. procedure jtransform_execute_transformation (
  1201. srcinfo : j_decompress_ptr;
  1202. dstinfo : j_compress_ptr;
  1203. src_coef_arrays : jvirt_barray_tbl_ptr;
  1204. var info : jpeg_transform_info);
  1205. var
  1206. dst_coef_arrays : jvirt_barray_tbl_ptr;
  1207. begin
  1208. dst_coef_arrays := info.workspace_coef_arrays;
  1209. case (info.transform) of
  1210. JXFORM_NONE:;
  1211. {$ifdef CROP_SUPPORTED}
  1212. JXFORM_CUT:
  1213. do_transform(srcinfo, dstinfo, src_coef_arrays, dst_coef_arrays,
  1214. info.xoffs, info.yoffs);
  1215. {$endif}
  1216. JXFORM_FLIP_H:
  1217. do_flip_h(srcinfo, dstinfo, src_coef_arrays);
  1218. JXFORM_FLIP_V:
  1219. do_flip_v(srcinfo, dstinfo, src_coef_arrays, dst_coef_arrays);
  1220. JXFORM_TRANSPOSE:
  1221. do_transpose(srcinfo, dstinfo, src_coef_arrays, dst_coef_arrays);
  1222. JXFORM_TRANSVERSE:
  1223. do_transverse(srcinfo, dstinfo, src_coef_arrays, dst_coef_arrays);
  1224. JXFORM_ROT_90:
  1225. do_rot_90(srcinfo, dstinfo, src_coef_arrays, dst_coef_arrays);
  1226. JXFORM_ROT_180:
  1227. do_rot_180(srcinfo, dstinfo, src_coef_arrays, dst_coef_arrays);
  1228. JXFORM_ROT_270:
  1229. do_rot_270(srcinfo, dstinfo, src_coef_arrays, dst_coef_arrays);
  1230. end;
  1231. end;
  1232. {$endif} { TRANSFORMS_SUPPORTED }
  1233. { Setup decompression object to save desired markers in memory.
  1234. This must be called before jpeg_read_header() to have the desired effect. }
  1235. {GLOBAL}
  1236. procedure jcopy_markers_setup (srcinfo : j_decompress_ptr;
  1237. option : JCOPY_OPTION);
  1238. var
  1239. m : int;
  1240. begin
  1241. {$ifdef SAVE_MARKERS_SUPPORTED}
  1242. { Save comments except under NONE option }
  1243. if (option <> JCOPYOPT_NONE) then
  1244. begin
  1245. jpeg_save_markers(srcinfo, JPEG_COM, $FFFF);
  1246. end;
  1247. { Save all types of APPn markers iff ALL option }
  1248. if (option = JCOPYOPT_ALL) then
  1249. begin
  1250. for m := 0 to 16-1 do
  1251. jpeg_save_markers(srcinfo, JPEG_APP0 + m, $FFFF);
  1252. end;
  1253. {$endif} { SAVE_MARKERS_SUPPORTED }
  1254. end;
  1255. { Copy markers saved in the given source object to the destination object.
  1256. This should be called just after jpeg_start_compress() or
  1257. jpeg_write_coefficients().
  1258. Note that those routines will have written the SOI, and also the
  1259. JFIF APP0 or Adobe APP14 markers if selected. }
  1260. {GLOBAL}
  1261. procedure jcopy_markers_execute (srcinfo : j_decompress_ptr;
  1262. dstinfo : j_compress_ptr;
  1263. option : JCOPY_OPTION);
  1264. var
  1265. marker : jpeg_saved_marker_ptr;
  1266. {$ifdef NEED_FAR_POINTERS}
  1267. var
  1268. i : uint;
  1269. {$endif}
  1270. begin
  1271. { In the current implementation, we don't actually need to examine the
  1272. option flag here; we just copy everything that got saved.
  1273. But to avoid confusion, we do not output JFIF and Adobe APP14 markers
  1274. if the encoder library already wrote one. }
  1275. marker := srcinfo^.marker_list;
  1276. while (marker <> NIL) do
  1277. begin
  1278. if (dstinfo^.write_JFIF_header) and
  1279. (marker^.marker = JPEG_APP0) and
  1280. (marker^.data_length >= 5) and
  1281. ( GETJOCTET(marker^.data^[0]) = $4A ) and
  1282. ( GETJOCTET(marker^.data^[1]) = $46 ) and
  1283. ( GETJOCTET(marker^.data^[2]) = $49 ) and
  1284. ( GETJOCTET(marker^.data^[3]) = $46 ) and
  1285. ( GETJOCTET(marker^.data^[4]) = 0 ) then
  1286. begin
  1287. marker := marker^.next;
  1288. continue; { reject duplicate JFIF }
  1289. end;
  1290. if (dstinfo^.write_Adobe_marker ) and
  1291. ( marker^.marker = JPEG_APP0+14 ) and
  1292. ( marker^.data_length >= 5 ) and
  1293. ( GETJOCTET(marker^.data^[0]) = $41 ) and
  1294. ( GETJOCTET(marker^.data^[1]) = $64 ) and
  1295. ( GETJOCTET(marker^.data^[2]) = $6F ) and
  1296. ( GETJOCTET(marker^.data^[3]) = $62 ) and
  1297. ( GETJOCTET(marker^.data^[4]) = $65 ) then
  1298. begin
  1299. marker := marker^.next;
  1300. continue; { reject duplicate Adobe }
  1301. end;
  1302. {$ifdef NEED_FAR_POINTERS}
  1303. { We could use jpeg_write_marker if the data weren't FAR... }
  1304. begin
  1305. jpeg_write_m_header(dstinfo, marker^.marker, marker^.data_length);
  1306. for i := 0 to marker^.data_length-1 do
  1307. jpeg_write_m_byte(dstinfo, marker^.data^[i]);
  1308. end;
  1309. {$else}
  1310. jpeg_write_marker(dstinfo, marker^.marker,
  1311. JOCTETPTR(marker^.data), marker^.data_length);
  1312. {$endif}
  1313. marker := marker^.next;
  1314. end;
  1315. end;
  1316. end.