tif_ojpeg.c 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605
  1. /* WARNING: The type of JPEG encapsulation defined by the TIFF Version 6.0
  2. specification is now totally obsolete and deprecated for new applications and
  3. images. This file was was created solely in order to read unconverted images
  4. still present on some users' computer systems. It will never be extended
  5. to write such files. Writing new-style JPEG compressed TIFFs is implemented
  6. in tif_jpeg.c.
  7. The code is carefully crafted to robustly read all gathered JPEG-in-TIFF
  8. testfiles, and anticipate as much as possible all other... But still, it may
  9. fail on some. If you encounter problems, please report them on the TIFF
  10. mailing list and/or to Joris Van Damme <info@awaresystems.be>.
  11. Please read the file called "TIFF Technical Note #2" if you need to be
  12. convinced this compression scheme is bad and breaks TIFF. That document
  13. is linked to from the LibTiff site <http://www.remotesensing.org/libtiff/>
  14. and from AWare Systems' TIFF section
  15. <http://www.awaresystems.be/imaging/tiff.html>. It is also absorbed
  16. in Adobe's specification supplements, marked "draft" up to this day, but
  17. supported by the TIFF community.
  18. This file interfaces with Release 6B of the JPEG Library written by the
  19. Independent JPEG Group. Previous versions of this file required a hack inside
  20. the LibJpeg library. This version no longer requires that. Remember to
  21. remove the hack if you update from the old version.
  22. Copyright (c) Joris Van Damme <info@awaresystems.be>
  23. Copyright (c) AWare Systems <http://www.awaresystems.be/>
  24. The licence agreement for this file is the same as the rest of the LibTiff
  25. library.
  26. IN NO EVENT SHALL JORIS VAN DAMME OR AWARE SYSTEMS BE LIABLE FOR
  27. ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  28. OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  29. WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  30. LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  31. OF THIS SOFTWARE.
  32. Joris Van Damme and/or AWare Systems may be available for custom
  33. development. If you like what you see, and need anything similar or related,
  34. contact <info@awaresystems.be>.
  35. */
  36. /* What is what, and what is not?
  37. This decoder starts with an input stream, that is essentially the JpegInterchangeFormat
  38. stream, if any, followed by the strile data, if any. This stream is read in
  39. OJPEGReadByte and related functions.
  40. It analyzes the start of this stream, until it encounters non-marker data, i.e.
  41. compressed image data. Some of the header markers it sees have no actual content,
  42. like the SOI marker, and APP/COM markers that really shouldn't even be there. Some
  43. other markers do have content, and the valuable bits and pieces of information
  44. in these markers are saved, checking all to verify that the stream is more or
  45. less within expected bounds. This happens inside the OJPEGReadHeaderInfoSecStreamXxx
  46. functions.
  47. Some OJPEG imagery contains no valid JPEG header markers. This situation is picked
  48. up on if we've seen no SOF marker when we're at the start of the compressed image
  49. data. In this case, the tables are read from JpegXxxTables tags, and the other
  50. bits and pieces of information is initialized to its most basic value. This is
  51. implemented in the OJPEGReadHeaderInfoSecTablesXxx functions.
  52. When this is complete, a good and valid JPEG header can be assembled, and this is
  53. passed through to LibJpeg. When that's done, the remainder of the input stream, i.e.
  54. the compressed image data, can be passed through unchanged. This is done in
  55. OJPEGWriteStream functions.
  56. LibTiff rightly expects to know the subsampling values before decompression. Just like
  57. in new-style JPEG-in-TIFF, though, or even more so, actually, the YCbCrsubsampling
  58. tag is notoriously unreliable. To correct these tag values with the ones inside
  59. the JPEG stream, the first part of the input stream is pre-scanned in
  60. OJPEGSubsamplingCorrect, making no note of any other data, reporting no warnings
  61. or errors, up to the point where either these values are read, or it's clear they
  62. aren't there. This means that some of the data is read twice, but we feel speed
  63. in correcting these values is important enough to warrant this sacrifice. Although
  64. there is currently no define or other configuration mechanism to disable this behavior,
  65. the actual header scanning is build to robustly respond with error report if it
  66. should encounter an uncorrected mismatch of subsampling values. See
  67. OJPEGReadHeaderInfoSecStreamSof.
  68. The restart interval and restart markers are the most tricky part... The restart
  69. interval can be specified in a tag. It can also be set inside the input JPEG stream.
  70. It can be used inside the input JPEG stream. If reading from strile data, we've
  71. consistently discovered the need to insert restart markers in between the different
  72. striles, as is also probably the most likely interpretation of the original TIFF 6.0
  73. specification. With all this setting of interval, and actual use of markers that is not
  74. predictable at the time of valid JPEG header assembly, the restart thing may turn
  75. out the Achilles heel of this implementation. Fortunately, most OJPEG writer vendors
  76. succeed in reading back what they write, which may be the reason why we've been able
  77. to discover ways that seem to work.
  78. Some special provision is made for planarconfig separate OJPEG files. These seem
  79. to consistently contain header info, a SOS marker, a plane, SOS marker, plane, SOS,
  80. and plane. This may or may not be a valid JPEG configuration, we don't know and don't
  81. care. We want LibTiff to be able to access the planes individually, without huge
  82. buffering inside LibJpeg, anyway. So we compose headers to feed to LibJpeg, in this
  83. case, that allow us to pass a single plane such that LibJpeg sees a valid
  84. single-channel JPEG stream. Locating subsequent SOS markers, and thus subsequent
  85. planes, is done inside OJPEGReadSecondarySos.
  86. The benefit of the scheme is... that it works, basically. We know of no other that
  87. does. It works without checking software tag, or otherwise going about things in an
  88. OJPEG flavor specific manner. Instead, it is a single scheme, that covers the cases
  89. with and without JpegInterchangeFormat, with and without striles, with part of
  90. the header in JpegInterchangeFormat and remainder in first strile, etc. It is forgiving
  91. and robust, may likely work with OJPEG flavors we've not seen yet, and makes most out
  92. of the data.
  93. Another nice side-effect is that a complete JPEG single valid stream is build if
  94. planarconfig is not separate (vast majority). We may one day use that to build
  95. converters to JPEG, and/or to new-style JPEG compression inside TIFF.
  96. A disadvantage is the lack of random access to the individual striles. This is the
  97. reason for much of the complicated restart-and-position stuff inside OJPEGPreDecode.
  98. Applications would do well accessing all striles in order, as this will result in
  99. a single sequential scan of the input stream, and no restarting of LibJpeg decoding
  100. session.
  101. */
  102. #define WIN32_LEAN_AND_MEAN
  103. #define VC_EXTRALEAN
  104. #include "tiffiop.h"
  105. #ifdef OJPEG_SUPPORT
  106. /* Configuration defines here are:
  107. * JPEG_ENCAP_EXTERNAL: The normal way to call libjpeg, uses longjump. In some environments,
  108. * like eg LibTiffDelphi, this is not possible. For this reason, the actual calls to
  109. * libjpeg, with longjump stuff, are encapsulated in dedicated functions. When
  110. * JPEG_ENCAP_EXTERNAL is defined, these encapsulating functions are declared external
  111. * to this unit, and can be defined elsewhere to use stuff other then longjump.
  112. * The default mode, without JPEG_ENCAP_EXTERNAL, implements the call encapsulators
  113. * here, internally, with normal longjump.
  114. * SETJMP, LONGJMP, JMP_BUF: On some machines/environments a longjump equivalent is
  115. * conveniently available, but still it may be worthwhile to use _setjmp or sigsetjmp
  116. * in place of plain setjmp. These macros will make it easier. It is useless
  117. * to fiddle with these if you define JPEG_ENCAP_EXTERNAL.
  118. * OJPEG_BUFFER: Define the size of the desired buffer here. Should be small enough so as to guarantee
  119. * instant processing, optimal streaming and optimal use of processor cache, but also big
  120. * enough so as to not result in significant call overhead. It should be at least a few
  121. * bytes to accommodate some structures (this is verified in asserts), but it would not be
  122. * sensible to make it this small anyway, and it should be at most 64K since it is indexed
  123. * with uint16. We recommend 2K.
  124. * EGYPTIANWALK: You could also define EGYPTIANWALK here, but it is not used anywhere and has
  125. * absolutely no effect. That is why most people insist the EGYPTIANWALK is a bit silly.
  126. */
  127. /* define LIBJPEG_ENCAP_EXTERNAL */
  128. #define SETJMP(jbuf) setjmp(jbuf)
  129. #define LONGJMP(jbuf,code) longjmp(jbuf,code)
  130. #define JMP_BUF jmp_buf
  131. #define OJPEG_BUFFER 2048
  132. /* define EGYPTIANWALK */
  133. #define JPEG_MARKER_SOF0 0xC0
  134. #define JPEG_MARKER_SOF1 0xC1
  135. #define JPEG_MARKER_SOF3 0xC3
  136. #define JPEG_MARKER_DHT 0xC4
  137. #define JPEG_MARKER_RST0 0XD0
  138. #define JPEG_MARKER_SOI 0xD8
  139. #define JPEG_MARKER_EOI 0xD9
  140. #define JPEG_MARKER_SOS 0xDA
  141. #define JPEG_MARKER_DQT 0xDB
  142. #define JPEG_MARKER_DRI 0xDD
  143. #define JPEG_MARKER_APP0 0xE0
  144. #define JPEG_MARKER_COM 0xFE
  145. #define FIELD_OJPEG_JPEGINTERCHANGEFORMAT (FIELD_CODEC+0)
  146. #define FIELD_OJPEG_JPEGINTERCHANGEFORMATLENGTH (FIELD_CODEC+1)
  147. #define FIELD_OJPEG_JPEGQTABLES (FIELD_CODEC+2)
  148. #define FIELD_OJPEG_JPEGDCTABLES (FIELD_CODEC+3)
  149. #define FIELD_OJPEG_JPEGACTABLES (FIELD_CODEC+4)
  150. #define FIELD_OJPEG_JPEGPROC (FIELD_CODEC+5)
  151. #define FIELD_OJPEG_JPEGRESTARTINTERVAL (FIELD_CODEC+6)
  152. static const TIFFField ojpegFields[] = {
  153. {TIFFTAG_JPEGIFOFFSET,1,1,TIFF_LONG8,0,TIFF_SETGET_UINT64,TIFF_SETGET_UNDEFINED,FIELD_OJPEG_JPEGINTERCHANGEFORMAT,TRUE,FALSE,"JpegInterchangeFormat",NULL},
  154. {TIFFTAG_JPEGIFBYTECOUNT,1,1,TIFF_LONG8,0,TIFF_SETGET_UINT64,TIFF_SETGET_UNDEFINED,FIELD_OJPEG_JPEGINTERCHANGEFORMATLENGTH,TRUE,FALSE,"JpegInterchangeFormatLength",NULL},
  155. {TIFFTAG_JPEGQTABLES,TIFF_VARIABLE2,TIFF_VARIABLE2,TIFF_LONG8,0,TIFF_SETGET_C32_UINT64,TIFF_SETGET_UNDEFINED,FIELD_OJPEG_JPEGQTABLES,FALSE,TRUE,"JpegQTables",NULL},
  156. {TIFFTAG_JPEGDCTABLES,TIFF_VARIABLE2,TIFF_VARIABLE2,TIFF_LONG8,0,TIFF_SETGET_C32_UINT64,TIFF_SETGET_UNDEFINED,FIELD_OJPEG_JPEGDCTABLES,FALSE,TRUE,"JpegDcTables",NULL},
  157. {TIFFTAG_JPEGACTABLES,TIFF_VARIABLE2,TIFF_VARIABLE2,TIFF_LONG8,0,TIFF_SETGET_C32_UINT64,TIFF_SETGET_UNDEFINED,FIELD_OJPEG_JPEGACTABLES,FALSE,TRUE,"JpegAcTables",NULL},
  158. {TIFFTAG_JPEGPROC,1,1,TIFF_SHORT,0,TIFF_SETGET_UINT16,TIFF_SETGET_UNDEFINED,FIELD_OJPEG_JPEGPROC,FALSE,FALSE,"JpegProc",NULL},
  159. {TIFFTAG_JPEGRESTARTINTERVAL,1,1,TIFF_SHORT,0,TIFF_SETGET_UINT16,TIFF_SETGET_UNDEFINED,FIELD_OJPEG_JPEGRESTARTINTERVAL,FALSE,FALSE,"JpegRestartInterval",NULL},
  160. };
  161. #ifndef LIBJPEG_ENCAP_EXTERNAL
  162. #include <setjmp.h>
  163. #endif
  164. /* We undefine FAR to avoid conflict with JPEG definition */
  165. #ifdef FAR
  166. #undef FAR
  167. #endif
  168. /*
  169. Libjpeg's jmorecfg.h defines INT16 and INT32, but only if XMD_H is
  170. not defined. Unfortunately, the MinGW and Borland compilers include
  171. a typedef for INT32, which causes a conflict. MSVC does not include
  172. a conflicting typedef given the headers which are included.
  173. */
  174. #if defined(__BORLANDC__) || defined(__MINGW32__)
  175. # define XMD_H 1
  176. #endif
  177. /* Define "boolean" as unsigned char, not int, per Windows custom. */
  178. #if defined(__WIN32__) && !defined(__MINGW32__)
  179. # ifndef __RPCNDR_H__ /* don't conflict if rpcndr.h already read */
  180. typedef unsigned char boolean;
  181. # endif
  182. # define HAVE_BOOLEAN /* prevent jmorecfg.h from redefining it */
  183. #endif
  184. #include "jpeglib.h"
  185. #include "jerror.h"
  186. typedef struct jpeg_error_mgr jpeg_error_mgr;
  187. typedef struct jpeg_common_struct jpeg_common_struct;
  188. typedef struct jpeg_decompress_struct jpeg_decompress_struct;
  189. typedef struct jpeg_source_mgr jpeg_source_mgr;
  190. typedef enum {
  191. osibsNotSetYet,
  192. osibsJpegInterchangeFormat,
  193. osibsStrile,
  194. osibsEof
  195. } OJPEGStateInBufferSource;
  196. typedef enum {
  197. ososSoi,
  198. ososQTable0,ososQTable1,ososQTable2,ososQTable3,
  199. ososDcTable0,ososDcTable1,ososDcTable2,ososDcTable3,
  200. ososAcTable0,ososAcTable1,ososAcTable2,ososAcTable3,
  201. ososDri,
  202. ososSof,
  203. ososSos,
  204. ososCompressed,
  205. ososRst,
  206. ososEoi
  207. } OJPEGStateOutState;
  208. typedef struct {
  209. TIFF* tif;
  210. int decoder_ok;
  211. int error_in_raw_data_decoding;
  212. #ifndef LIBJPEG_ENCAP_EXTERNAL
  213. JMP_BUF exit_jmpbuf;
  214. #endif
  215. TIFFVGetMethod vgetparent;
  216. TIFFVSetMethod vsetparent;
  217. TIFFPrintMethod printdir;
  218. uint64 file_size;
  219. uint32 image_width;
  220. uint32 image_length;
  221. uint32 strile_width;
  222. uint32 strile_length;
  223. uint32 strile_length_total;
  224. uint8 samples_per_pixel;
  225. uint8 plane_sample_offset;
  226. uint8 samples_per_pixel_per_plane;
  227. uint64 jpeg_interchange_format;
  228. uint64 jpeg_interchange_format_length;
  229. uint8 jpeg_proc;
  230. uint8 subsamplingcorrect;
  231. uint8 subsamplingcorrect_done;
  232. uint8 subsampling_tag;
  233. uint8 subsampling_hor;
  234. uint8 subsampling_ver;
  235. uint8 subsampling_force_desubsampling_inside_decompression;
  236. uint8 qtable_offset_count;
  237. uint8 dctable_offset_count;
  238. uint8 actable_offset_count;
  239. uint64 qtable_offset[3];
  240. uint64 dctable_offset[3];
  241. uint64 actable_offset[3];
  242. uint8* qtable[4];
  243. uint8* dctable[4];
  244. uint8* actable[4];
  245. uint16 restart_interval;
  246. uint8 restart_index;
  247. uint8 sof_log;
  248. uint8 sof_marker_id;
  249. uint32 sof_x;
  250. uint32 sof_y;
  251. uint8 sof_c[3];
  252. uint8 sof_hv[3];
  253. uint8 sof_tq[3];
  254. uint8 sos_cs[3];
  255. uint8 sos_tda[3];
  256. struct {
  257. uint8 log;
  258. OJPEGStateInBufferSource in_buffer_source;
  259. uint32 in_buffer_next_strile;
  260. uint64 in_buffer_file_pos;
  261. uint64 in_buffer_file_togo;
  262. } sos_end[3];
  263. uint8 readheader_done;
  264. uint8 writeheader_done;
  265. uint16 write_cursample;
  266. uint32 write_curstrile;
  267. uint8 libjpeg_session_active;
  268. uint8 libjpeg_jpeg_query_style;
  269. jpeg_error_mgr libjpeg_jpeg_error_mgr;
  270. jpeg_decompress_struct libjpeg_jpeg_decompress_struct;
  271. jpeg_source_mgr libjpeg_jpeg_source_mgr;
  272. uint8 subsampling_convert_log;
  273. uint32 subsampling_convert_ylinelen;
  274. uint32 subsampling_convert_ylines;
  275. uint32 subsampling_convert_clinelen;
  276. uint32 subsampling_convert_clines;
  277. uint32 subsampling_convert_ybuflen;
  278. uint32 subsampling_convert_cbuflen;
  279. uint32 subsampling_convert_ycbcrbuflen;
  280. uint8* subsampling_convert_ycbcrbuf;
  281. uint8* subsampling_convert_ybuf;
  282. uint8* subsampling_convert_cbbuf;
  283. uint8* subsampling_convert_crbuf;
  284. uint32 subsampling_convert_ycbcrimagelen;
  285. uint8** subsampling_convert_ycbcrimage;
  286. uint32 subsampling_convert_clinelenout;
  287. uint32 subsampling_convert_state;
  288. uint32 bytes_per_line; /* if the codec outputs subsampled data, a 'line' in bytes_per_line */
  289. uint32 lines_per_strile; /* and lines_per_strile means subsampling_ver desubsampled rows */
  290. OJPEGStateInBufferSource in_buffer_source;
  291. uint32 in_buffer_next_strile;
  292. uint32 in_buffer_strile_count;
  293. uint64 in_buffer_file_pos;
  294. uint8 in_buffer_file_pos_log;
  295. uint64 in_buffer_file_togo;
  296. uint16 in_buffer_togo;
  297. uint8* in_buffer_cur;
  298. uint8 in_buffer[OJPEG_BUFFER];
  299. OJPEGStateOutState out_state;
  300. uint8 out_buffer[OJPEG_BUFFER];
  301. uint8* skip_buffer;
  302. } OJPEGState;
  303. static int OJPEGVGetField(TIFF* tif, uint32 tag, va_list ap);
  304. static int OJPEGVSetField(TIFF* tif, uint32 tag, va_list ap);
  305. static void OJPEGPrintDir(TIFF* tif, FILE* fd, long flags);
  306. static int OJPEGFixupTags(TIFF* tif);
  307. static int OJPEGSetupDecode(TIFF* tif);
  308. static int OJPEGPreDecode(TIFF* tif, uint16 s);
  309. static int OJPEGPreDecodeSkipRaw(TIFF* tif);
  310. static int OJPEGPreDecodeSkipScanlines(TIFF* tif);
  311. static int OJPEGDecode(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s);
  312. static int OJPEGDecodeRaw(TIFF* tif, uint8* buf, tmsize_t cc);
  313. static int OJPEGDecodeScanlines(TIFF* tif, uint8* buf, tmsize_t cc);
  314. static void OJPEGPostDecode(TIFF* tif, uint8* buf, tmsize_t cc);
  315. static int OJPEGSetupEncode(TIFF* tif);
  316. static int OJPEGPreEncode(TIFF* tif, uint16 s);
  317. static int OJPEGEncode(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s);
  318. static int OJPEGPostEncode(TIFF* tif);
  319. static void OJPEGCleanup(TIFF* tif);
  320. static void OJPEGSubsamplingCorrect(TIFF* tif);
  321. static int OJPEGReadHeaderInfo(TIFF* tif);
  322. static int OJPEGReadSecondarySos(TIFF* tif, uint16 s);
  323. static int OJPEGWriteHeaderInfo(TIFF* tif);
  324. static void OJPEGLibjpegSessionAbort(TIFF* tif);
  325. static int OJPEGReadHeaderInfoSec(TIFF* tif);
  326. static int OJPEGReadHeaderInfoSecStreamDri(TIFF* tif);
  327. static int OJPEGReadHeaderInfoSecStreamDqt(TIFF* tif);
  328. static int OJPEGReadHeaderInfoSecStreamDht(TIFF* tif);
  329. static int OJPEGReadHeaderInfoSecStreamSof(TIFF* tif, uint8 marker_id);
  330. static int OJPEGReadHeaderInfoSecStreamSos(TIFF* tif);
  331. static int OJPEGReadHeaderInfoSecTablesQTable(TIFF* tif);
  332. static int OJPEGReadHeaderInfoSecTablesDcTable(TIFF* tif);
  333. static int OJPEGReadHeaderInfoSecTablesAcTable(TIFF* tif);
  334. static int OJPEGReadBufferFill(OJPEGState* sp);
  335. static int OJPEGReadByte(OJPEGState* sp, uint8* byte);
  336. static int OJPEGReadBytePeek(OJPEGState* sp, uint8* byte);
  337. static void OJPEGReadByteAdvance(OJPEGState* sp);
  338. static int OJPEGReadWord(OJPEGState* sp, uint16* word);
  339. static int OJPEGReadBlock(OJPEGState* sp, uint16 len, void* mem);
  340. static void OJPEGReadSkip(OJPEGState* sp, uint16 len);
  341. static int OJPEGWriteStream(TIFF* tif, void** mem, uint32* len);
  342. static void OJPEGWriteStreamSoi(TIFF* tif, void** mem, uint32* len);
  343. static void OJPEGWriteStreamQTable(TIFF* tif, uint8 table_index, void** mem, uint32* len);
  344. static void OJPEGWriteStreamDcTable(TIFF* tif, uint8 table_index, void** mem, uint32* len);
  345. static void OJPEGWriteStreamAcTable(TIFF* tif, uint8 table_index, void** mem, uint32* len);
  346. static void OJPEGWriteStreamDri(TIFF* tif, void** mem, uint32* len);
  347. static void OJPEGWriteStreamSof(TIFF* tif, void** mem, uint32* len);
  348. static void OJPEGWriteStreamSos(TIFF* tif, void** mem, uint32* len);
  349. static int OJPEGWriteStreamCompressed(TIFF* tif, void** mem, uint32* len);
  350. static void OJPEGWriteStreamRst(TIFF* tif, void** mem, uint32* len);
  351. static void OJPEGWriteStreamEoi(TIFF* tif, void** mem, uint32* len);
  352. #ifdef LIBJPEG_ENCAP_EXTERNAL
  353. extern int jpeg_create_decompress_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo);
  354. extern int jpeg_read_header_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, uint8 require_image);
  355. extern int jpeg_start_decompress_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo);
  356. extern int jpeg_read_scanlines_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, void* scanlines, uint32 max_lines);
  357. extern int jpeg_read_raw_data_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, void* data, uint32 max_lines);
  358. extern void jpeg_encap_unwind(TIFF* tif);
  359. #else
  360. static int jpeg_create_decompress_encap(OJPEGState* sp, jpeg_decompress_struct* j);
  361. static int jpeg_read_header_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, uint8 require_image);
  362. static int jpeg_start_decompress_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo);
  363. static int jpeg_read_scanlines_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, void* scanlines, uint32 max_lines);
  364. static int jpeg_read_raw_data_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, void* data, uint32 max_lines);
  365. static void jpeg_encap_unwind(TIFF* tif);
  366. #endif
  367. static void OJPEGLibjpegJpegErrorMgrOutputMessage(jpeg_common_struct* cinfo);
  368. static void OJPEGLibjpegJpegErrorMgrErrorExit(jpeg_common_struct* cinfo);
  369. static void OJPEGLibjpegJpegSourceMgrInitSource(jpeg_decompress_struct* cinfo);
  370. static boolean OJPEGLibjpegJpegSourceMgrFillInputBuffer(jpeg_decompress_struct* cinfo);
  371. static void OJPEGLibjpegJpegSourceMgrSkipInputData(jpeg_decompress_struct* cinfo, long num_bytes);
  372. static boolean OJPEGLibjpegJpegSourceMgrResyncToRestart(jpeg_decompress_struct* cinfo, int desired);
  373. static void OJPEGLibjpegJpegSourceMgrTermSource(jpeg_decompress_struct* cinfo);
  374. int
  375. TIFFInitOJPEG(TIFF* tif, int scheme)
  376. {
  377. static const char module[]="TIFFInitOJPEG";
  378. OJPEGState* sp;
  379. (void)scheme;
  380. assert(scheme==COMPRESSION_OJPEG);
  381. /*
  382. * Merge codec-specific tag information.
  383. */
  384. if (!_TIFFMergeFields(tif, ojpegFields, TIFFArrayCount(ojpegFields))) {
  385. TIFFErrorExt(tif->tif_clientdata, module,
  386. "Merging Old JPEG codec-specific tags failed");
  387. return 0;
  388. }
  389. /* state block */
  390. sp=_TIFFmalloc(sizeof(OJPEGState));
  391. if (sp==NULL)
  392. {
  393. TIFFErrorExt(tif->tif_clientdata,module,"No space for OJPEG state block");
  394. return(0);
  395. }
  396. _TIFFmemset(sp,0,sizeof(OJPEGState));
  397. sp->tif=tif;
  398. sp->jpeg_proc=1;
  399. sp->subsampling_hor=2;
  400. sp->subsampling_ver=2;
  401. TIFFSetField(tif,TIFFTAG_YCBCRSUBSAMPLING,2,2);
  402. /* tif codec methods */
  403. tif->tif_fixuptags=OJPEGFixupTags;
  404. tif->tif_setupdecode=OJPEGSetupDecode;
  405. tif->tif_predecode=OJPEGPreDecode;
  406. tif->tif_postdecode=OJPEGPostDecode;
  407. tif->tif_decoderow=OJPEGDecode;
  408. tif->tif_decodestrip=OJPEGDecode;
  409. tif->tif_decodetile=OJPEGDecode;
  410. tif->tif_setupencode=OJPEGSetupEncode;
  411. tif->tif_preencode=OJPEGPreEncode;
  412. tif->tif_postencode=OJPEGPostEncode;
  413. tif->tif_encoderow=OJPEGEncode;
  414. tif->tif_encodestrip=OJPEGEncode;
  415. tif->tif_encodetile=OJPEGEncode;
  416. tif->tif_cleanup=OJPEGCleanup;
  417. tif->tif_data=(uint8*)sp;
  418. /* tif tag methods */
  419. sp->vgetparent=tif->tif_tagmethods.vgetfield;
  420. tif->tif_tagmethods.vgetfield=OJPEGVGetField;
  421. sp->vsetparent=tif->tif_tagmethods.vsetfield;
  422. tif->tif_tagmethods.vsetfield=OJPEGVSetField;
  423. sp->printdir=tif->tif_tagmethods.printdir;
  424. tif->tif_tagmethods.printdir=OJPEGPrintDir;
  425. /* Some OJPEG files don't have strip or tile offsets or bytecounts tags.
  426. Some others do, but have totally meaningless or corrupt values
  427. in these tags. In these cases, the JpegInterchangeFormat stream is
  428. reliable. In any case, this decoder reads the compressed data itself,
  429. from the most reliable locations, and we need to notify encapsulating
  430. LibTiff not to read raw strips or tiles for us. */
  431. tif->tif_flags|=TIFF_NOREADRAW;
  432. return(1);
  433. }
  434. static int
  435. OJPEGVGetField(TIFF* tif, uint32 tag, va_list ap)
  436. {
  437. OJPEGState* sp=(OJPEGState*)tif->tif_data;
  438. switch(tag)
  439. {
  440. case TIFFTAG_JPEGIFOFFSET:
  441. *va_arg(ap,uint64*)=(uint64)sp->jpeg_interchange_format;
  442. break;
  443. case TIFFTAG_JPEGIFBYTECOUNT:
  444. *va_arg(ap,uint64*)=(uint64)sp->jpeg_interchange_format_length;
  445. break;
  446. case TIFFTAG_YCBCRSUBSAMPLING:
  447. if (sp->subsamplingcorrect_done==0)
  448. OJPEGSubsamplingCorrect(tif);
  449. *va_arg(ap,uint16*)=(uint16)sp->subsampling_hor;
  450. *va_arg(ap,uint16*)=(uint16)sp->subsampling_ver;
  451. break;
  452. case TIFFTAG_JPEGQTABLES:
  453. *va_arg(ap,uint32*)=(uint32)sp->qtable_offset_count;
  454. *va_arg(ap,const void**)=(const void*)sp->qtable_offset;
  455. break;
  456. case TIFFTAG_JPEGDCTABLES:
  457. *va_arg(ap,uint32*)=(uint32)sp->dctable_offset_count;
  458. *va_arg(ap,const void**)=(const void*)sp->dctable_offset;
  459. break;
  460. case TIFFTAG_JPEGACTABLES:
  461. *va_arg(ap,uint32*)=(uint32)sp->actable_offset_count;
  462. *va_arg(ap,const void**)=(const void*)sp->actable_offset;
  463. break;
  464. case TIFFTAG_JPEGPROC:
  465. *va_arg(ap,uint16*)=(uint16)sp->jpeg_proc;
  466. break;
  467. case TIFFTAG_JPEGRESTARTINTERVAL:
  468. *va_arg(ap,uint16*)=sp->restart_interval;
  469. break;
  470. default:
  471. return (*sp->vgetparent)(tif,tag,ap);
  472. }
  473. return (1);
  474. }
  475. static int
  476. OJPEGVSetField(TIFF* tif, uint32 tag, va_list ap)
  477. {
  478. static const char module[]="OJPEGVSetField";
  479. OJPEGState* sp=(OJPEGState*)tif->tif_data;
  480. uint32 ma;
  481. uint64* mb;
  482. uint32 n;
  483. const TIFFField* fip;
  484. switch(tag)
  485. {
  486. case TIFFTAG_JPEGIFOFFSET:
  487. sp->jpeg_interchange_format=(uint64)va_arg(ap,uint64);
  488. break;
  489. case TIFFTAG_JPEGIFBYTECOUNT:
  490. sp->jpeg_interchange_format_length=(uint64)va_arg(ap,uint64);
  491. break;
  492. case TIFFTAG_YCBCRSUBSAMPLING:
  493. sp->subsampling_tag=1;
  494. sp->subsampling_hor=(uint8)va_arg(ap,uint16_vap);
  495. sp->subsampling_ver=(uint8)va_arg(ap,uint16_vap);
  496. tif->tif_dir.td_ycbcrsubsampling[0]=sp->subsampling_hor;
  497. tif->tif_dir.td_ycbcrsubsampling[1]=sp->subsampling_ver;
  498. break;
  499. case TIFFTAG_JPEGQTABLES:
  500. ma=(uint32)va_arg(ap,uint32);
  501. if (ma!=0)
  502. {
  503. if (ma>3)
  504. {
  505. TIFFErrorExt(tif->tif_clientdata,module,"JpegQTables tag has incorrect count");
  506. return(0);
  507. }
  508. sp->qtable_offset_count=(uint8)ma;
  509. mb=(uint64*)va_arg(ap,uint64*);
  510. for (n=0; n<ma; n++)
  511. sp->qtable_offset[n]=mb[n];
  512. }
  513. break;
  514. case TIFFTAG_JPEGDCTABLES:
  515. ma=(uint32)va_arg(ap,uint32);
  516. if (ma!=0)
  517. {
  518. if (ma>3)
  519. {
  520. TIFFErrorExt(tif->tif_clientdata,module,"JpegDcTables tag has incorrect count");
  521. return(0);
  522. }
  523. sp->dctable_offset_count=(uint8)ma;
  524. mb=(uint64*)va_arg(ap,uint64*);
  525. for (n=0; n<ma; n++)
  526. sp->dctable_offset[n]=mb[n];
  527. }
  528. break;
  529. case TIFFTAG_JPEGACTABLES:
  530. ma=(uint32)va_arg(ap,uint32);
  531. if (ma!=0)
  532. {
  533. if (ma>3)
  534. {
  535. TIFFErrorExt(tif->tif_clientdata,module,"JpegAcTables tag has incorrect count");
  536. return(0);
  537. }
  538. sp->actable_offset_count=(uint8)ma;
  539. mb=(uint64*)va_arg(ap,uint64*);
  540. for (n=0; n<ma; n++)
  541. sp->actable_offset[n]=mb[n];
  542. }
  543. break;
  544. case TIFFTAG_JPEGPROC:
  545. sp->jpeg_proc=(uint8)va_arg(ap,uint16_vap);
  546. break;
  547. case TIFFTAG_JPEGRESTARTINTERVAL:
  548. sp->restart_interval=(uint16)va_arg(ap,uint16_vap);
  549. break;
  550. default:
  551. return (*sp->vsetparent)(tif,tag,ap);
  552. }
  553. fip = TIFFFieldWithTag(tif,tag);
  554. if( fip == NULL ) /* shouldn't happen */
  555. return(0);
  556. TIFFSetFieldBit(tif,fip->field_bit);
  557. tif->tif_flags|=TIFF_DIRTYDIRECT;
  558. return(1);
  559. }
  560. static void
  561. OJPEGPrintDir(TIFF* tif, FILE* fd, long flags)
  562. {
  563. OJPEGState* sp=(OJPEGState*)tif->tif_data;
  564. uint8 m;
  565. (void)flags;
  566. assert(sp!=NULL);
  567. if (TIFFFieldSet(tif,FIELD_OJPEG_JPEGINTERCHANGEFORMAT))
  568. fprintf(fd," JpegInterchangeFormat: " TIFF_UINT64_FORMAT "\n",(TIFF_UINT64_T)sp->jpeg_interchange_format);
  569. if (TIFFFieldSet(tif,FIELD_OJPEG_JPEGINTERCHANGEFORMATLENGTH))
  570. fprintf(fd," JpegInterchangeFormatLength: " TIFF_UINT64_FORMAT "\n",(TIFF_UINT64_T)sp->jpeg_interchange_format_length);
  571. if (TIFFFieldSet(tif,FIELD_OJPEG_JPEGQTABLES))
  572. {
  573. fprintf(fd," JpegQTables:");
  574. for (m=0; m<sp->qtable_offset_count; m++)
  575. fprintf(fd," " TIFF_UINT64_FORMAT,(TIFF_UINT64_T)sp->qtable_offset[m]);
  576. fprintf(fd,"\n");
  577. }
  578. if (TIFFFieldSet(tif,FIELD_OJPEG_JPEGDCTABLES))
  579. {
  580. fprintf(fd," JpegDcTables:");
  581. for (m=0; m<sp->dctable_offset_count; m++)
  582. fprintf(fd," " TIFF_UINT64_FORMAT,(TIFF_UINT64_T)sp->dctable_offset[m]);
  583. fprintf(fd,"\n");
  584. }
  585. if (TIFFFieldSet(tif,FIELD_OJPEG_JPEGACTABLES))
  586. {
  587. fprintf(fd," JpegAcTables:");
  588. for (m=0; m<sp->actable_offset_count; m++)
  589. fprintf(fd," " TIFF_UINT64_FORMAT,(TIFF_UINT64_T)sp->actable_offset[m]);
  590. fprintf(fd,"\n");
  591. }
  592. if (TIFFFieldSet(tif,FIELD_OJPEG_JPEGPROC))
  593. fprintf(fd," JpegProc: %u\n",(unsigned int)sp->jpeg_proc);
  594. if (TIFFFieldSet(tif,FIELD_OJPEG_JPEGRESTARTINTERVAL))
  595. fprintf(fd," JpegRestartInterval: %u\n",(unsigned int)sp->restart_interval);
  596. if (sp->printdir)
  597. (*sp->printdir)(tif, fd, flags);
  598. }
  599. static int
  600. OJPEGFixupTags(TIFF* tif)
  601. {
  602. (void) tif;
  603. return(1);
  604. }
  605. static int
  606. OJPEGSetupDecode(TIFF* tif)
  607. {
  608. static const char module[]="OJPEGSetupDecode";
  609. TIFFWarningExt(tif->tif_clientdata,module,"Deprecated and troublesome old-style JPEG compression mode, please convert to new-style JPEG compression and notify vendor of writing software");
  610. return(1);
  611. }
  612. static int
  613. OJPEGPreDecode(TIFF* tif, uint16 s)
  614. {
  615. OJPEGState* sp=(OJPEGState*)tif->tif_data;
  616. uint32 m;
  617. if (sp->subsamplingcorrect_done==0)
  618. OJPEGSubsamplingCorrect(tif);
  619. if (sp->readheader_done==0)
  620. {
  621. if (OJPEGReadHeaderInfo(tif)==0)
  622. return(0);
  623. }
  624. if (sp->sos_end[s].log==0)
  625. {
  626. if (OJPEGReadSecondarySos(tif,s)==0)
  627. return(0);
  628. }
  629. if (isTiled(tif))
  630. m=tif->tif_curtile;
  631. else
  632. m=tif->tif_curstrip;
  633. if ((sp->writeheader_done!=0) && ((sp->write_cursample!=s) || (sp->write_curstrile>m)))
  634. {
  635. if (sp->libjpeg_session_active!=0)
  636. OJPEGLibjpegSessionAbort(tif);
  637. sp->writeheader_done=0;
  638. }
  639. if (sp->writeheader_done==0)
  640. {
  641. sp->plane_sample_offset=(uint8)s;
  642. sp->write_cursample=s;
  643. sp->write_curstrile=s*tif->tif_dir.td_stripsperimage;
  644. if ((sp->in_buffer_file_pos_log==0) ||
  645. (sp->in_buffer_file_pos-sp->in_buffer_togo!=sp->sos_end[s].in_buffer_file_pos))
  646. {
  647. sp->in_buffer_source=sp->sos_end[s].in_buffer_source;
  648. sp->in_buffer_next_strile=sp->sos_end[s].in_buffer_next_strile;
  649. sp->in_buffer_file_pos=sp->sos_end[s].in_buffer_file_pos;
  650. sp->in_buffer_file_pos_log=0;
  651. sp->in_buffer_file_togo=sp->sos_end[s].in_buffer_file_togo;
  652. sp->in_buffer_togo=0;
  653. sp->in_buffer_cur=0;
  654. }
  655. if (OJPEGWriteHeaderInfo(tif)==0)
  656. return(0);
  657. }
  658. while (sp->write_curstrile<m)
  659. {
  660. if (sp->libjpeg_jpeg_query_style==0)
  661. {
  662. if (OJPEGPreDecodeSkipRaw(tif)==0)
  663. return(0);
  664. }
  665. else
  666. {
  667. if (OJPEGPreDecodeSkipScanlines(tif)==0)
  668. return(0);
  669. }
  670. sp->write_curstrile++;
  671. }
  672. sp->decoder_ok = 1;
  673. return(1);
  674. }
  675. static int
  676. OJPEGPreDecodeSkipRaw(TIFF* tif)
  677. {
  678. OJPEGState* sp=(OJPEGState*)tif->tif_data;
  679. uint32 m;
  680. m=sp->lines_per_strile;
  681. if (sp->subsampling_convert_state!=0)
  682. {
  683. if (sp->subsampling_convert_clines-sp->subsampling_convert_state>=m)
  684. {
  685. sp->subsampling_convert_state+=m;
  686. if (sp->subsampling_convert_state==sp->subsampling_convert_clines)
  687. sp->subsampling_convert_state=0;
  688. return(1);
  689. }
  690. m-=sp->subsampling_convert_clines-sp->subsampling_convert_state;
  691. sp->subsampling_convert_state=0;
  692. sp->error_in_raw_data_decoding=0;
  693. }
  694. while (m>=sp->subsampling_convert_clines)
  695. {
  696. if (jpeg_read_raw_data_encap(sp,&(sp->libjpeg_jpeg_decompress_struct),sp->subsampling_convert_ycbcrimage,sp->subsampling_ver*8)==0)
  697. return(0);
  698. m-=sp->subsampling_convert_clines;
  699. }
  700. if (m>0)
  701. {
  702. if (jpeg_read_raw_data_encap(sp,&(sp->libjpeg_jpeg_decompress_struct),sp->subsampling_convert_ycbcrimage,sp->subsampling_ver*8)==0)
  703. return(0);
  704. sp->subsampling_convert_state=m;
  705. }
  706. return(1);
  707. }
  708. static int
  709. OJPEGPreDecodeSkipScanlines(TIFF* tif)
  710. {
  711. static const char module[]="OJPEGPreDecodeSkipScanlines";
  712. OJPEGState* sp=(OJPEGState*)tif->tif_data;
  713. uint32 m;
  714. if (sp->skip_buffer==NULL)
  715. {
  716. sp->skip_buffer=_TIFFmalloc(sp->bytes_per_line);
  717. if (sp->skip_buffer==NULL)
  718. {
  719. TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
  720. return(0);
  721. }
  722. }
  723. for (m=0; m<sp->lines_per_strile; m++)
  724. {
  725. if (jpeg_read_scanlines_encap(sp,&(sp->libjpeg_jpeg_decompress_struct),&sp->skip_buffer,1)==0)
  726. return(0);
  727. }
  728. return(1);
  729. }
  730. static int
  731. OJPEGDecode(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s)
  732. {
  733. static const char module[]="OJPEGDecode";
  734. OJPEGState* sp=(OJPEGState*)tif->tif_data;
  735. (void)s;
  736. if( !sp->decoder_ok )
  737. {
  738. TIFFErrorExt(tif->tif_clientdata,module,"Cannot decode: decoder not correctly initialized");
  739. return 0;
  740. }
  741. if( sp->error_in_raw_data_decoding )
  742. {
  743. return 0;
  744. }
  745. if (sp->libjpeg_jpeg_query_style==0)
  746. {
  747. if (OJPEGDecodeRaw(tif,buf,cc)==0)
  748. return(0);
  749. }
  750. else
  751. {
  752. if (OJPEGDecodeScanlines(tif,buf,cc)==0)
  753. return(0);
  754. }
  755. return(1);
  756. }
  757. static int
  758. OJPEGDecodeRaw(TIFF* tif, uint8* buf, tmsize_t cc)
  759. {
  760. static const char module[]="OJPEGDecodeRaw";
  761. OJPEGState* sp=(OJPEGState*)tif->tif_data;
  762. uint8* m;
  763. tmsize_t n;
  764. uint8* oy;
  765. uint8* ocb;
  766. uint8* ocr;
  767. uint8* p;
  768. uint32 q;
  769. uint8* r;
  770. uint8 sx,sy;
  771. if (cc%sp->bytes_per_line!=0)
  772. {
  773. TIFFErrorExt(tif->tif_clientdata,module,"Fractional scanline not read");
  774. return(0);
  775. }
  776. assert(cc>0);
  777. m=buf;
  778. n=cc;
  779. do
  780. {
  781. if (sp->subsampling_convert_state==0)
  782. {
  783. if (jpeg_read_raw_data_encap(sp,&(sp->libjpeg_jpeg_decompress_struct),sp->subsampling_convert_ycbcrimage,sp->subsampling_ver*8)==0)
  784. {
  785. sp->error_in_raw_data_decoding = 1;
  786. return(0);
  787. }
  788. }
  789. oy=sp->subsampling_convert_ybuf+sp->subsampling_convert_state*sp->subsampling_ver*sp->subsampling_convert_ylinelen;
  790. ocb=sp->subsampling_convert_cbbuf+sp->subsampling_convert_state*sp->subsampling_convert_clinelen;
  791. ocr=sp->subsampling_convert_crbuf+sp->subsampling_convert_state*sp->subsampling_convert_clinelen;
  792. p=m;
  793. for (q=0; q<sp->subsampling_convert_clinelenout; q++)
  794. {
  795. r=oy;
  796. for (sy=0; sy<sp->subsampling_ver; sy++)
  797. {
  798. for (sx=0; sx<sp->subsampling_hor; sx++)
  799. *p++=*r++;
  800. r+=sp->subsampling_convert_ylinelen-sp->subsampling_hor;
  801. }
  802. oy+=sp->subsampling_hor;
  803. *p++=*ocb++;
  804. *p++=*ocr++;
  805. }
  806. sp->subsampling_convert_state++;
  807. if (sp->subsampling_convert_state==sp->subsampling_convert_clines)
  808. sp->subsampling_convert_state=0;
  809. m+=sp->bytes_per_line;
  810. n-=sp->bytes_per_line;
  811. } while(n>0);
  812. return(1);
  813. }
  814. static int
  815. OJPEGDecodeScanlines(TIFF* tif, uint8* buf, tmsize_t cc)
  816. {
  817. static const char module[]="OJPEGDecodeScanlines";
  818. OJPEGState* sp=(OJPEGState*)tif->tif_data;
  819. uint8* m;
  820. tmsize_t n;
  821. if (cc%sp->bytes_per_line!=0)
  822. {
  823. TIFFErrorExt(tif->tif_clientdata,module,"Fractional scanline not read");
  824. return(0);
  825. }
  826. assert(cc>0);
  827. m=buf;
  828. n=cc;
  829. do
  830. {
  831. if (jpeg_read_scanlines_encap(sp,&(sp->libjpeg_jpeg_decompress_struct),&m,1)==0)
  832. return(0);
  833. m+=sp->bytes_per_line;
  834. n-=sp->bytes_per_line;
  835. } while(n>0);
  836. return(1);
  837. }
  838. static void
  839. OJPEGPostDecode(TIFF* tif, uint8* buf, tmsize_t cc)
  840. {
  841. OJPEGState* sp=(OJPEGState*)tif->tif_data;
  842. (void)buf;
  843. (void)cc;
  844. sp->write_curstrile++;
  845. if (sp->write_curstrile%tif->tif_dir.td_stripsperimage==0)
  846. {
  847. assert(sp->libjpeg_session_active!=0);
  848. OJPEGLibjpegSessionAbort(tif);
  849. sp->writeheader_done=0;
  850. }
  851. }
  852. static int
  853. OJPEGSetupEncode(TIFF* tif)
  854. {
  855. static const char module[]="OJPEGSetupEncode";
  856. TIFFErrorExt(tif->tif_clientdata,module,"OJPEG encoding not supported; use new-style JPEG compression instead");
  857. return(0);
  858. }
  859. static int
  860. OJPEGPreEncode(TIFF* tif, uint16 s)
  861. {
  862. static const char module[]="OJPEGPreEncode";
  863. (void)s;
  864. TIFFErrorExt(tif->tif_clientdata,module,"OJPEG encoding not supported; use new-style JPEG compression instead");
  865. return(0);
  866. }
  867. static int
  868. OJPEGEncode(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s)
  869. {
  870. static const char module[]="OJPEGEncode";
  871. (void)buf;
  872. (void)cc;
  873. (void)s;
  874. TIFFErrorExt(tif->tif_clientdata,module,"OJPEG encoding not supported; use new-style JPEG compression instead");
  875. return(0);
  876. }
  877. static int
  878. OJPEGPostEncode(TIFF* tif)
  879. {
  880. static const char module[]="OJPEGPostEncode";
  881. TIFFErrorExt(tif->tif_clientdata,module,"OJPEG encoding not supported; use new-style JPEG compression instead");
  882. return(0);
  883. }
  884. static void
  885. OJPEGCleanup(TIFF* tif)
  886. {
  887. OJPEGState* sp=(OJPEGState*)tif->tif_data;
  888. if (sp!=0)
  889. {
  890. tif->tif_tagmethods.vgetfield=sp->vgetparent;
  891. tif->tif_tagmethods.vsetfield=sp->vsetparent;
  892. tif->tif_tagmethods.printdir=sp->printdir;
  893. if (sp->qtable[0]!=0)
  894. _TIFFfree(sp->qtable[0]);
  895. if (sp->qtable[1]!=0)
  896. _TIFFfree(sp->qtable[1]);
  897. if (sp->qtable[2]!=0)
  898. _TIFFfree(sp->qtable[2]);
  899. if (sp->qtable[3]!=0)
  900. _TIFFfree(sp->qtable[3]);
  901. if (sp->dctable[0]!=0)
  902. _TIFFfree(sp->dctable[0]);
  903. if (sp->dctable[1]!=0)
  904. _TIFFfree(sp->dctable[1]);
  905. if (sp->dctable[2]!=0)
  906. _TIFFfree(sp->dctable[2]);
  907. if (sp->dctable[3]!=0)
  908. _TIFFfree(sp->dctable[3]);
  909. if (sp->actable[0]!=0)
  910. _TIFFfree(sp->actable[0]);
  911. if (sp->actable[1]!=0)
  912. _TIFFfree(sp->actable[1]);
  913. if (sp->actable[2]!=0)
  914. _TIFFfree(sp->actable[2]);
  915. if (sp->actable[3]!=0)
  916. _TIFFfree(sp->actable[3]);
  917. if (sp->libjpeg_session_active!=0)
  918. OJPEGLibjpegSessionAbort(tif);
  919. if (sp->subsampling_convert_ycbcrbuf!=0)
  920. _TIFFfree(sp->subsampling_convert_ycbcrbuf);
  921. if (sp->subsampling_convert_ycbcrimage!=0)
  922. _TIFFfree(sp->subsampling_convert_ycbcrimage);
  923. if (sp->skip_buffer!=0)
  924. _TIFFfree(sp->skip_buffer);
  925. _TIFFfree(sp);
  926. tif->tif_data=NULL;
  927. _TIFFSetDefaultCompressionState(tif);
  928. }
  929. }
  930. static void
  931. OJPEGSubsamplingCorrect(TIFF* tif)
  932. {
  933. static const char module[]="OJPEGSubsamplingCorrect";
  934. OJPEGState* sp=(OJPEGState*)tif->tif_data;
  935. uint8 mh;
  936. uint8 mv;
  937. assert(sp->subsamplingcorrect_done==0);
  938. if ((tif->tif_dir.td_samplesperpixel!=3) || ((tif->tif_dir.td_photometric!=PHOTOMETRIC_YCBCR) &&
  939. (tif->tif_dir.td_photometric!=PHOTOMETRIC_ITULAB)))
  940. {
  941. if (sp->subsampling_tag!=0)
  942. TIFFWarningExt(tif->tif_clientdata,module,"Subsampling tag not appropriate for this Photometric and/or SamplesPerPixel");
  943. sp->subsampling_hor=1;
  944. sp->subsampling_ver=1;
  945. sp->subsampling_force_desubsampling_inside_decompression=0;
  946. }
  947. else
  948. {
  949. sp->subsamplingcorrect_done=1;
  950. mh=sp->subsampling_hor;
  951. mv=sp->subsampling_ver;
  952. sp->subsamplingcorrect=1;
  953. OJPEGReadHeaderInfoSec(tif);
  954. if (sp->subsampling_force_desubsampling_inside_decompression!=0)
  955. {
  956. sp->subsampling_hor=1;
  957. sp->subsampling_ver=1;
  958. }
  959. sp->subsamplingcorrect=0;
  960. if (((sp->subsampling_hor!=mh) || (sp->subsampling_ver!=mv)) && (sp->subsampling_force_desubsampling_inside_decompression==0))
  961. {
  962. if (sp->subsampling_tag==0)
  963. TIFFWarningExt(tif->tif_clientdata,module,"Subsampling tag is not set, yet subsampling inside JPEG data [%d,%d] does not match default values [2,2]; assuming subsampling inside JPEG data is correct",sp->subsampling_hor,sp->subsampling_ver);
  964. else
  965. TIFFWarningExt(tif->tif_clientdata,module,"Subsampling inside JPEG data [%d,%d] does not match subsampling tag values [%d,%d]; assuming subsampling inside JPEG data is correct",sp->subsampling_hor,sp->subsampling_ver,mh,mv);
  966. }
  967. if (sp->subsampling_force_desubsampling_inside_decompression!=0)
  968. {
  969. if (sp->subsampling_tag==0)
  970. TIFFWarningExt(tif->tif_clientdata,module,"Subsampling tag is not set, yet subsampling inside JPEG data does not match default values [2,2] (nor any other values allowed in TIFF); assuming subsampling inside JPEG data is correct and desubsampling inside JPEG decompression");
  971. else
  972. TIFFWarningExt(tif->tif_clientdata,module,"Subsampling inside JPEG data does not match subsampling tag values [%d,%d] (nor any other values allowed in TIFF); assuming subsampling inside JPEG data is correct and desubsampling inside JPEG decompression",mh,mv);
  973. }
  974. if (sp->subsampling_force_desubsampling_inside_decompression==0)
  975. {
  976. if (sp->subsampling_hor<sp->subsampling_ver)
  977. TIFFWarningExt(tif->tif_clientdata,module,"Subsampling values [%d,%d] are not allowed in TIFF",sp->subsampling_hor,sp->subsampling_ver);
  978. }
  979. }
  980. sp->subsamplingcorrect_done=1;
  981. }
  982. static int
  983. OJPEGReadHeaderInfo(TIFF* tif)
  984. {
  985. static const char module[]="OJPEGReadHeaderInfo";
  986. OJPEGState* sp=(OJPEGState*)tif->tif_data;
  987. assert(sp->readheader_done==0);
  988. sp->image_width=tif->tif_dir.td_imagewidth;
  989. sp->image_length=tif->tif_dir.td_imagelength;
  990. if (isTiled(tif))
  991. {
  992. sp->strile_width=tif->tif_dir.td_tilewidth;
  993. sp->strile_length=tif->tif_dir.td_tilelength;
  994. sp->strile_length_total=((sp->image_length+sp->strile_length-1)/sp->strile_length)*sp->strile_length;
  995. }
  996. else
  997. {
  998. sp->strile_width=sp->image_width;
  999. sp->strile_length=tif->tif_dir.td_rowsperstrip;
  1000. if( sp->strile_length == (uint32)-1 )
  1001. sp->strile_length = sp->image_length;
  1002. sp->strile_length_total=sp->image_length;
  1003. }
  1004. if (tif->tif_dir.td_samplesperpixel==1)
  1005. {
  1006. sp->samples_per_pixel=1;
  1007. sp->plane_sample_offset=0;
  1008. sp->samples_per_pixel_per_plane=sp->samples_per_pixel;
  1009. sp->subsampling_hor=1;
  1010. sp->subsampling_ver=1;
  1011. }
  1012. else
  1013. {
  1014. if (tif->tif_dir.td_samplesperpixel!=3)
  1015. {
  1016. TIFFErrorExt(tif->tif_clientdata,module,"SamplesPerPixel %d not supported for this compression scheme",sp->samples_per_pixel);
  1017. return(0);
  1018. }
  1019. sp->samples_per_pixel=3;
  1020. sp->plane_sample_offset=0;
  1021. if (tif->tif_dir.td_planarconfig==PLANARCONFIG_CONTIG)
  1022. sp->samples_per_pixel_per_plane=3;
  1023. else
  1024. sp->samples_per_pixel_per_plane=1;
  1025. }
  1026. if (sp->strile_length<sp->image_length)
  1027. {
  1028. if (((sp->subsampling_hor!=1) && (sp->subsampling_hor!=2) && (sp->subsampling_hor!=4)) ||
  1029. ((sp->subsampling_ver!=1) && (sp->subsampling_ver!=2) && (sp->subsampling_ver!=4)))
  1030. {
  1031. TIFFErrorExt(tif->tif_clientdata,module,"Invalid subsampling values");
  1032. return(0);
  1033. }
  1034. if (sp->strile_length%(sp->subsampling_ver*8)!=0)
  1035. {
  1036. TIFFErrorExt(tif->tif_clientdata,module,"Incompatible vertical subsampling and image strip/tile length");
  1037. return(0);
  1038. }
  1039. sp->restart_interval=(uint16)(((sp->strile_width+sp->subsampling_hor*8-1)/(sp->subsampling_hor*8))*(sp->strile_length/(sp->subsampling_ver*8)));
  1040. }
  1041. if (OJPEGReadHeaderInfoSec(tif)==0)
  1042. return(0);
  1043. sp->sos_end[0].log=1;
  1044. sp->sos_end[0].in_buffer_source=sp->in_buffer_source;
  1045. sp->sos_end[0].in_buffer_next_strile=sp->in_buffer_next_strile;
  1046. sp->sos_end[0].in_buffer_file_pos=sp->in_buffer_file_pos-sp->in_buffer_togo;
  1047. sp->sos_end[0].in_buffer_file_togo=sp->in_buffer_file_togo+sp->in_buffer_togo;
  1048. sp->readheader_done=1;
  1049. return(1);
  1050. }
  1051. static int
  1052. OJPEGReadSecondarySos(TIFF* tif, uint16 s)
  1053. {
  1054. OJPEGState* sp=(OJPEGState*)tif->tif_data;
  1055. uint8 m;
  1056. assert(s>0);
  1057. assert(s<3);
  1058. assert(sp->sos_end[0].log!=0);
  1059. assert(sp->sos_end[s].log==0);
  1060. sp->plane_sample_offset=(uint8)(s-1);
  1061. while(sp->sos_end[sp->plane_sample_offset].log==0)
  1062. sp->plane_sample_offset--;
  1063. sp->in_buffer_source=sp->sos_end[sp->plane_sample_offset].in_buffer_source;
  1064. sp->in_buffer_next_strile=sp->sos_end[sp->plane_sample_offset].in_buffer_next_strile;
  1065. sp->in_buffer_file_pos=sp->sos_end[sp->plane_sample_offset].in_buffer_file_pos;
  1066. sp->in_buffer_file_pos_log=0;
  1067. sp->in_buffer_file_togo=sp->sos_end[sp->plane_sample_offset].in_buffer_file_togo;
  1068. sp->in_buffer_togo=0;
  1069. sp->in_buffer_cur=0;
  1070. while(sp->plane_sample_offset<s)
  1071. {
  1072. do
  1073. {
  1074. if (OJPEGReadByte(sp,&m)==0)
  1075. return(0);
  1076. if (m==255)
  1077. {
  1078. do
  1079. {
  1080. if (OJPEGReadByte(sp,&m)==0)
  1081. return(0);
  1082. if (m!=255)
  1083. break;
  1084. } while(1);
  1085. if (m==JPEG_MARKER_SOS)
  1086. break;
  1087. }
  1088. } while(1);
  1089. sp->plane_sample_offset++;
  1090. if (OJPEGReadHeaderInfoSecStreamSos(tif)==0)
  1091. return(0);
  1092. sp->sos_end[sp->plane_sample_offset].log=1;
  1093. sp->sos_end[sp->plane_sample_offset].in_buffer_source=sp->in_buffer_source;
  1094. sp->sos_end[sp->plane_sample_offset].in_buffer_next_strile=sp->in_buffer_next_strile;
  1095. sp->sos_end[sp->plane_sample_offset].in_buffer_file_pos=sp->in_buffer_file_pos-sp->in_buffer_togo;
  1096. sp->sos_end[sp->plane_sample_offset].in_buffer_file_togo=sp->in_buffer_file_togo+sp->in_buffer_togo;
  1097. }
  1098. return(1);
  1099. }
  1100. static int
  1101. OJPEGWriteHeaderInfo(TIFF* tif)
  1102. {
  1103. static const char module[]="OJPEGWriteHeaderInfo";
  1104. OJPEGState* sp=(OJPEGState*)tif->tif_data;
  1105. uint8** m;
  1106. uint32 n;
  1107. /* if a previous attempt failed, don't try again */
  1108. if (sp->libjpeg_session_active != 0)
  1109. return 0;
  1110. sp->out_state=ososSoi;
  1111. sp->restart_index=0;
  1112. jpeg_std_error(&(sp->libjpeg_jpeg_error_mgr));
  1113. sp->libjpeg_jpeg_error_mgr.output_message=OJPEGLibjpegJpegErrorMgrOutputMessage;
  1114. sp->libjpeg_jpeg_error_mgr.error_exit=OJPEGLibjpegJpegErrorMgrErrorExit;
  1115. sp->libjpeg_jpeg_decompress_struct.err=&(sp->libjpeg_jpeg_error_mgr);
  1116. sp->libjpeg_jpeg_decompress_struct.client_data=(void*)tif;
  1117. if (jpeg_create_decompress_encap(sp,&(sp->libjpeg_jpeg_decompress_struct))==0)
  1118. return(0);
  1119. sp->libjpeg_session_active=1;
  1120. sp->libjpeg_jpeg_source_mgr.bytes_in_buffer=0;
  1121. sp->libjpeg_jpeg_source_mgr.init_source=OJPEGLibjpegJpegSourceMgrInitSource;
  1122. sp->libjpeg_jpeg_source_mgr.fill_input_buffer=OJPEGLibjpegJpegSourceMgrFillInputBuffer;
  1123. sp->libjpeg_jpeg_source_mgr.skip_input_data=OJPEGLibjpegJpegSourceMgrSkipInputData;
  1124. sp->libjpeg_jpeg_source_mgr.resync_to_restart=OJPEGLibjpegJpegSourceMgrResyncToRestart;
  1125. sp->libjpeg_jpeg_source_mgr.term_source=OJPEGLibjpegJpegSourceMgrTermSource;
  1126. sp->libjpeg_jpeg_decompress_struct.src=&(sp->libjpeg_jpeg_source_mgr);
  1127. if (jpeg_read_header_encap(sp,&(sp->libjpeg_jpeg_decompress_struct),1)==0)
  1128. return(0);
  1129. if ((sp->subsampling_force_desubsampling_inside_decompression==0) && (sp->samples_per_pixel_per_plane>1))
  1130. {
  1131. sp->libjpeg_jpeg_decompress_struct.raw_data_out=1;
  1132. #if JPEG_LIB_VERSION >= 70
  1133. sp->libjpeg_jpeg_decompress_struct.do_fancy_upsampling=FALSE;
  1134. #endif
  1135. sp->libjpeg_jpeg_query_style=0;
  1136. if (sp->subsampling_convert_log==0)
  1137. {
  1138. assert(sp->subsampling_convert_ycbcrbuf==0);
  1139. assert(sp->subsampling_convert_ycbcrimage==0);
  1140. sp->subsampling_convert_ylinelen=((sp->strile_width+sp->subsampling_hor*8-1)/(sp->subsampling_hor*8)*sp->subsampling_hor*8);
  1141. sp->subsampling_convert_ylines=sp->subsampling_ver*8;
  1142. sp->subsampling_convert_clinelen=sp->subsampling_convert_ylinelen/sp->subsampling_hor;
  1143. sp->subsampling_convert_clines=8;
  1144. sp->subsampling_convert_ybuflen=sp->subsampling_convert_ylinelen*sp->subsampling_convert_ylines;
  1145. sp->subsampling_convert_cbuflen=sp->subsampling_convert_clinelen*sp->subsampling_convert_clines;
  1146. sp->subsampling_convert_ycbcrbuflen=sp->subsampling_convert_ybuflen+2*sp->subsampling_convert_cbuflen;
  1147. /* The calloc is not normally necessary, except in some edge/broken cases */
  1148. /* for example for a tiled image of height 1 with a tile height of 1 and subsampling_hor=subsampling_ver=2 */
  1149. /* In that case, libjpeg will only fill the 8 first lines of the 16 lines */
  1150. /* See https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=16844 */
  1151. /* Even if this case is allowed (?), its handling is broken because OJPEGPreDecode() should also likely */
  1152. /* reset subsampling_convert_state to 0 when changing tile. */
  1153. sp->subsampling_convert_ycbcrbuf=_TIFFcalloc(1, sp->subsampling_convert_ycbcrbuflen);
  1154. if (sp->subsampling_convert_ycbcrbuf==0)
  1155. {
  1156. TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
  1157. return(0);
  1158. }
  1159. sp->subsampling_convert_ybuf=sp->subsampling_convert_ycbcrbuf;
  1160. sp->subsampling_convert_cbbuf=sp->subsampling_convert_ybuf+sp->subsampling_convert_ybuflen;
  1161. sp->subsampling_convert_crbuf=sp->subsampling_convert_cbbuf+sp->subsampling_convert_cbuflen;
  1162. sp->subsampling_convert_ycbcrimagelen=3+sp->subsampling_convert_ylines+2*sp->subsampling_convert_clines;
  1163. sp->subsampling_convert_ycbcrimage=_TIFFmalloc(sp->subsampling_convert_ycbcrimagelen*sizeof(uint8*));
  1164. if (sp->subsampling_convert_ycbcrimage==0)
  1165. {
  1166. TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
  1167. return(0);
  1168. }
  1169. m=sp->subsampling_convert_ycbcrimage;
  1170. *m++=(uint8*)(sp->subsampling_convert_ycbcrimage+3);
  1171. *m++=(uint8*)(sp->subsampling_convert_ycbcrimage+3+sp->subsampling_convert_ylines);
  1172. *m++=(uint8*)(sp->subsampling_convert_ycbcrimage+3+sp->subsampling_convert_ylines+sp->subsampling_convert_clines);
  1173. for (n=0; n<sp->subsampling_convert_ylines; n++)
  1174. *m++=sp->subsampling_convert_ybuf+n*sp->subsampling_convert_ylinelen;
  1175. for (n=0; n<sp->subsampling_convert_clines; n++)
  1176. *m++=sp->subsampling_convert_cbbuf+n*sp->subsampling_convert_clinelen;
  1177. for (n=0; n<sp->subsampling_convert_clines; n++)
  1178. *m++=sp->subsampling_convert_crbuf+n*sp->subsampling_convert_clinelen;
  1179. sp->subsampling_convert_clinelenout=sp->strile_width/sp->subsampling_hor + ((sp->strile_width % sp->subsampling_hor) != 0 ? 1 : 0);
  1180. sp->subsampling_convert_state=0;
  1181. sp->error_in_raw_data_decoding=0;
  1182. sp->bytes_per_line=sp->subsampling_convert_clinelenout*(sp->subsampling_ver*sp->subsampling_hor+2);
  1183. sp->lines_per_strile=sp->strile_length/sp->subsampling_ver + ((sp->strile_length % sp->subsampling_ver) != 0 ? 1 : 0);
  1184. sp->subsampling_convert_log=1;
  1185. }
  1186. }
  1187. else
  1188. {
  1189. sp->libjpeg_jpeg_decompress_struct.jpeg_color_space=JCS_UNKNOWN;
  1190. sp->libjpeg_jpeg_decompress_struct.out_color_space=JCS_UNKNOWN;
  1191. sp->libjpeg_jpeg_query_style=1;
  1192. sp->bytes_per_line=sp->samples_per_pixel_per_plane*sp->strile_width;
  1193. sp->lines_per_strile=sp->strile_length;
  1194. }
  1195. if (jpeg_start_decompress_encap(sp,&(sp->libjpeg_jpeg_decompress_struct))==0)
  1196. return(0);
  1197. if(sp->libjpeg_jpeg_decompress_struct.image_width != sp->strile_width ) {
  1198. TIFFErrorExt(tif->tif_clientdata,module,
  1199. "jpeg_start_decompress() returned image_width = %d, "
  1200. "expected %d",
  1201. sp->libjpeg_jpeg_decompress_struct.image_width,
  1202. sp->strile_width);
  1203. return 0;
  1204. }
  1205. if(sp->libjpeg_jpeg_decompress_struct.max_h_samp_factor != sp->subsampling_hor ||
  1206. sp->libjpeg_jpeg_decompress_struct.max_v_samp_factor != sp->subsampling_ver) {
  1207. TIFFErrorExt(tif->tif_clientdata,module,
  1208. "jpeg_start_decompress() returned max_h_samp_factor = %d "
  1209. "and max_v_samp_factor = %d, expected %d and %d",
  1210. sp->libjpeg_jpeg_decompress_struct.max_h_samp_factor,
  1211. sp->libjpeg_jpeg_decompress_struct.max_v_samp_factor,
  1212. sp->subsampling_hor,
  1213. sp->subsampling_ver);
  1214. return 0;
  1215. }
  1216. sp->writeheader_done=1;
  1217. return(1);
  1218. }
  1219. static void
  1220. OJPEGLibjpegSessionAbort(TIFF* tif)
  1221. {
  1222. OJPEGState* sp=(OJPEGState*)tif->tif_data;
  1223. assert(sp->libjpeg_session_active!=0);
  1224. jpeg_destroy((jpeg_common_struct*)(&(sp->libjpeg_jpeg_decompress_struct)));
  1225. sp->libjpeg_session_active=0;
  1226. }
  1227. static int
  1228. OJPEGReadHeaderInfoSec(TIFF* tif)
  1229. {
  1230. static const char module[]="OJPEGReadHeaderInfoSec";
  1231. OJPEGState* sp=(OJPEGState*)tif->tif_data;
  1232. uint8 m;
  1233. uint16 n;
  1234. uint8 o;
  1235. if (sp->file_size==0)
  1236. sp->file_size=TIFFGetFileSize(tif);
  1237. if (sp->jpeg_interchange_format!=0)
  1238. {
  1239. if (sp->jpeg_interchange_format>=sp->file_size)
  1240. {
  1241. sp->jpeg_interchange_format=0;
  1242. sp->jpeg_interchange_format_length=0;
  1243. }
  1244. else
  1245. {
  1246. if ((sp->jpeg_interchange_format_length==0) ||
  1247. (sp->jpeg_interchange_format > TIFF_UINT64_MAX - sp->jpeg_interchange_format_length) ||
  1248. (sp->jpeg_interchange_format+sp->jpeg_interchange_format_length>sp->file_size))
  1249. sp->jpeg_interchange_format_length=sp->file_size-sp->jpeg_interchange_format;
  1250. }
  1251. }
  1252. sp->in_buffer_source=osibsNotSetYet;
  1253. sp->in_buffer_next_strile=0;
  1254. sp->in_buffer_strile_count=tif->tif_dir.td_nstrips;
  1255. sp->in_buffer_file_togo=0;
  1256. sp->in_buffer_togo=0;
  1257. do
  1258. {
  1259. if (OJPEGReadBytePeek(sp,&m)==0)
  1260. return(0);
  1261. if (m!=255)
  1262. break;
  1263. OJPEGReadByteAdvance(sp);
  1264. do
  1265. {
  1266. if (OJPEGReadByte(sp,&m)==0)
  1267. return(0);
  1268. } while(m==255);
  1269. switch(m)
  1270. {
  1271. case JPEG_MARKER_SOI:
  1272. /* this type of marker has no data, and should be skipped */
  1273. break;
  1274. case JPEG_MARKER_COM:
  1275. case JPEG_MARKER_APP0:
  1276. case JPEG_MARKER_APP0+1:
  1277. case JPEG_MARKER_APP0+2:
  1278. case JPEG_MARKER_APP0+3:
  1279. case JPEG_MARKER_APP0+4:
  1280. case JPEG_MARKER_APP0+5:
  1281. case JPEG_MARKER_APP0+6:
  1282. case JPEG_MARKER_APP0+7:
  1283. case JPEG_MARKER_APP0+8:
  1284. case JPEG_MARKER_APP0+9:
  1285. case JPEG_MARKER_APP0+10:
  1286. case JPEG_MARKER_APP0+11:
  1287. case JPEG_MARKER_APP0+12:
  1288. case JPEG_MARKER_APP0+13:
  1289. case JPEG_MARKER_APP0+14:
  1290. case JPEG_MARKER_APP0+15:
  1291. /* this type of marker has data, but it has no use to us (and no place here) and should be skipped */
  1292. if (OJPEGReadWord(sp,&n)==0)
  1293. return(0);
  1294. if (n<2)
  1295. {
  1296. if (sp->subsamplingcorrect==0)
  1297. TIFFErrorExt(tif->tif_clientdata,module,"Corrupt JPEG data");
  1298. return(0);
  1299. }
  1300. if (n>2)
  1301. OJPEGReadSkip(sp,n-2);
  1302. break;
  1303. case JPEG_MARKER_DRI:
  1304. if (OJPEGReadHeaderInfoSecStreamDri(tif)==0)
  1305. return(0);
  1306. break;
  1307. case JPEG_MARKER_DQT:
  1308. if (OJPEGReadHeaderInfoSecStreamDqt(tif)==0)
  1309. return(0);
  1310. break;
  1311. case JPEG_MARKER_DHT:
  1312. if (OJPEGReadHeaderInfoSecStreamDht(tif)==0)
  1313. return(0);
  1314. break;
  1315. case JPEG_MARKER_SOF0:
  1316. case JPEG_MARKER_SOF1:
  1317. case JPEG_MARKER_SOF3:
  1318. if (OJPEGReadHeaderInfoSecStreamSof(tif,m)==0)
  1319. return(0);
  1320. if (sp->subsamplingcorrect!=0)
  1321. return(1);
  1322. break;
  1323. case JPEG_MARKER_SOS:
  1324. if (sp->subsamplingcorrect!=0)
  1325. return(1);
  1326. assert(sp->plane_sample_offset==0);
  1327. if (OJPEGReadHeaderInfoSecStreamSos(tif)==0)
  1328. return(0);
  1329. break;
  1330. default:
  1331. TIFFErrorExt(tif->tif_clientdata,module,"Unknown marker type %d in JPEG data",m);
  1332. return(0);
  1333. }
  1334. } while(m!=JPEG_MARKER_SOS);
  1335. if (sp->subsamplingcorrect)
  1336. return(1);
  1337. if (sp->sof_log==0)
  1338. {
  1339. if (OJPEGReadHeaderInfoSecTablesQTable(tif)==0)
  1340. return(0);
  1341. sp->sof_marker_id=JPEG_MARKER_SOF0;
  1342. for (o=0; o<sp->samples_per_pixel; o++)
  1343. sp->sof_c[o]=o;
  1344. sp->sof_hv[0]=((sp->subsampling_hor<<4)|sp->subsampling_ver);
  1345. for (o=1; o<sp->samples_per_pixel; o++)
  1346. sp->sof_hv[o]=17;
  1347. sp->sof_x=sp->strile_width;
  1348. sp->sof_y=sp->strile_length_total;
  1349. sp->sof_log=1;
  1350. if (OJPEGReadHeaderInfoSecTablesDcTable(tif)==0)
  1351. return(0);
  1352. if (OJPEGReadHeaderInfoSecTablesAcTable(tif)==0)
  1353. return(0);
  1354. for (o=1; o<sp->samples_per_pixel; o++)
  1355. sp->sos_cs[o]=o;
  1356. }
  1357. return(1);
  1358. }
  1359. static int
  1360. OJPEGReadHeaderInfoSecStreamDri(TIFF* tif)
  1361. {
  1362. /* This could easily cause trouble in some cases... but no such cases have
  1363. occurred so far */
  1364. static const char module[]="OJPEGReadHeaderInfoSecStreamDri";
  1365. OJPEGState* sp=(OJPEGState*)tif->tif_data;
  1366. uint16 m;
  1367. if (OJPEGReadWord(sp,&m)==0)
  1368. return(0);
  1369. if (m!=4)
  1370. {
  1371. TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DRI marker in JPEG data");
  1372. return(0);
  1373. }
  1374. if (OJPEGReadWord(sp,&m)==0)
  1375. return(0);
  1376. sp->restart_interval=m;
  1377. return(1);
  1378. }
  1379. static int
  1380. OJPEGReadHeaderInfoSecStreamDqt(TIFF* tif)
  1381. {
  1382. /* this is a table marker, and it is to be saved as a whole for exact pushing on the jpeg stream later on */
  1383. static const char module[]="OJPEGReadHeaderInfoSecStreamDqt";
  1384. OJPEGState* sp=(OJPEGState*)tif->tif_data;
  1385. uint16 m;
  1386. uint32 na;
  1387. uint8* nb;
  1388. uint8 o;
  1389. if (OJPEGReadWord(sp,&m)==0)
  1390. return(0);
  1391. if (m<=2)
  1392. {
  1393. if (sp->subsamplingcorrect==0)
  1394. TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DQT marker in JPEG data");
  1395. return(0);
  1396. }
  1397. if (sp->subsamplingcorrect!=0)
  1398. OJPEGReadSkip(sp,m-2);
  1399. else
  1400. {
  1401. m-=2;
  1402. do
  1403. {
  1404. if (m<65)
  1405. {
  1406. TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DQT marker in JPEG data");
  1407. return(0);
  1408. }
  1409. na=sizeof(uint32)+69;
  1410. nb=_TIFFmalloc(na);
  1411. if (nb==0)
  1412. {
  1413. TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
  1414. return(0);
  1415. }
  1416. *(uint32*)nb=na;
  1417. nb[sizeof(uint32)]=255;
  1418. nb[sizeof(uint32)+1]=JPEG_MARKER_DQT;
  1419. nb[sizeof(uint32)+2]=0;
  1420. nb[sizeof(uint32)+3]=67;
  1421. if (OJPEGReadBlock(sp,65,&nb[sizeof(uint32)+4])==0) {
  1422. _TIFFfree(nb);
  1423. return(0);
  1424. }
  1425. o=nb[sizeof(uint32)+4]&15;
  1426. if (3<o)
  1427. {
  1428. TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DQT marker in JPEG data");
  1429. _TIFFfree(nb);
  1430. return(0);
  1431. }
  1432. if (sp->qtable[o]!=0)
  1433. _TIFFfree(sp->qtable[o]);
  1434. sp->qtable[o]=nb;
  1435. m-=65;
  1436. } while(m>0);
  1437. }
  1438. return(1);
  1439. }
  1440. static int
  1441. OJPEGReadHeaderInfoSecStreamDht(TIFF* tif)
  1442. {
  1443. /* this is a table marker, and it is to be saved as a whole for exact pushing on the jpeg stream later on */
  1444. /* TODO: the following assumes there is only one table in this marker... but i'm not quite sure that assumption is guaranteed correct */
  1445. static const char module[]="OJPEGReadHeaderInfoSecStreamDht";
  1446. OJPEGState* sp=(OJPEGState*)tif->tif_data;
  1447. uint16 m;
  1448. uint32 na;
  1449. uint8* nb;
  1450. uint8 o;
  1451. if (OJPEGReadWord(sp,&m)==0)
  1452. return(0);
  1453. if (m<=2)
  1454. {
  1455. if (sp->subsamplingcorrect==0)
  1456. TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DHT marker in JPEG data");
  1457. return(0);
  1458. }
  1459. if (sp->subsamplingcorrect!=0)
  1460. {
  1461. OJPEGReadSkip(sp,m-2);
  1462. }
  1463. else
  1464. {
  1465. na=sizeof(uint32)+2+m;
  1466. nb=_TIFFmalloc(na);
  1467. if (nb==0)
  1468. {
  1469. TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
  1470. return(0);
  1471. }
  1472. *(uint32*)nb=na;
  1473. nb[sizeof(uint32)]=255;
  1474. nb[sizeof(uint32)+1]=JPEG_MARKER_DHT;
  1475. nb[sizeof(uint32)+2]=(m>>8);
  1476. nb[sizeof(uint32)+3]=(m&255);
  1477. if (OJPEGReadBlock(sp,m-2,&nb[sizeof(uint32)+4])==0) {
  1478. _TIFFfree(nb);
  1479. return(0);
  1480. }
  1481. o=nb[sizeof(uint32)+4];
  1482. if ((o&240)==0)
  1483. {
  1484. if (3<o)
  1485. {
  1486. TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DHT marker in JPEG data");
  1487. _TIFFfree(nb);
  1488. return(0);
  1489. }
  1490. if (sp->dctable[o]!=0)
  1491. _TIFFfree(sp->dctable[o]);
  1492. sp->dctable[o]=nb;
  1493. }
  1494. else
  1495. {
  1496. if ((o&240)!=16)
  1497. {
  1498. TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DHT marker in JPEG data");
  1499. _TIFFfree(nb);
  1500. return(0);
  1501. }
  1502. o&=15;
  1503. if (3<o)
  1504. {
  1505. TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DHT marker in JPEG data");
  1506. _TIFFfree(nb);
  1507. return(0);
  1508. }
  1509. if (sp->actable[o]!=0)
  1510. _TIFFfree(sp->actable[o]);
  1511. sp->actable[o]=nb;
  1512. }
  1513. }
  1514. return(1);
  1515. }
  1516. static int
  1517. OJPEGReadHeaderInfoSecStreamSof(TIFF* tif, uint8 marker_id)
  1518. {
  1519. /* this marker needs to be checked, and part of its data needs to be saved for regeneration later on */
  1520. static const char module[]="OJPEGReadHeaderInfoSecStreamSof";
  1521. OJPEGState* sp=(OJPEGState*)tif->tif_data;
  1522. uint16 m;
  1523. uint16 n;
  1524. uint8 o;
  1525. uint16 p;
  1526. uint16 q;
  1527. if (sp->sof_log!=0)
  1528. {
  1529. TIFFErrorExt(tif->tif_clientdata,module,"Corrupt JPEG data");
  1530. return(0);
  1531. }
  1532. if (sp->subsamplingcorrect==0)
  1533. sp->sof_marker_id=marker_id;
  1534. /* Lf: data length */
  1535. if (OJPEGReadWord(sp,&m)==0)
  1536. return(0);
  1537. if (m<11)
  1538. {
  1539. if (sp->subsamplingcorrect==0)
  1540. TIFFErrorExt(tif->tif_clientdata,module,"Corrupt SOF marker in JPEG data");
  1541. return(0);
  1542. }
  1543. m-=8;
  1544. if (m%3!=0)
  1545. {
  1546. if (sp->subsamplingcorrect==0)
  1547. TIFFErrorExt(tif->tif_clientdata,module,"Corrupt SOF marker in JPEG data");
  1548. return(0);
  1549. }
  1550. n=m/3;
  1551. if (sp->subsamplingcorrect==0)
  1552. {
  1553. if (n!=sp->samples_per_pixel)
  1554. {
  1555. TIFFErrorExt(tif->tif_clientdata,module,"JPEG compressed data indicates unexpected number of samples");
  1556. return(0);
  1557. }
  1558. }
  1559. /* P: Sample precision */
  1560. if (OJPEGReadByte(sp,&o)==0)
  1561. return(0);
  1562. if (o!=8)
  1563. {
  1564. if (sp->subsamplingcorrect==0)
  1565. TIFFErrorExt(tif->tif_clientdata,module,"JPEG compressed data indicates unexpected number of bits per sample");
  1566. return(0);
  1567. }
  1568. /* Y: Number of lines, X: Number of samples per line */
  1569. if (sp->subsamplingcorrect)
  1570. OJPEGReadSkip(sp,4);
  1571. else
  1572. {
  1573. /* Y: Number of lines */
  1574. if (OJPEGReadWord(sp,&p)==0)
  1575. return(0);
  1576. if (((uint32)p<sp->image_length) && ((uint32)p<sp->strile_length_total))
  1577. {
  1578. TIFFErrorExt(tif->tif_clientdata,module,"JPEG compressed data indicates unexpected height");
  1579. return(0);
  1580. }
  1581. sp->sof_y=p;
  1582. /* X: Number of samples per line */
  1583. if (OJPEGReadWord(sp,&p)==0)
  1584. return(0);
  1585. if (((uint32)p<sp->image_width) && ((uint32)p<sp->strile_width))
  1586. {
  1587. TIFFErrorExt(tif->tif_clientdata,module,"JPEG compressed data indicates unexpected width");
  1588. return(0);
  1589. }
  1590. if ((uint32)p>sp->strile_width)
  1591. {
  1592. TIFFErrorExt(tif->tif_clientdata,module,"JPEG compressed data image width exceeds expected image width");
  1593. return(0);
  1594. }
  1595. sp->sof_x=p;
  1596. }
  1597. /* Nf: Number of image components in frame */
  1598. if (OJPEGReadByte(sp,&o)==0)
  1599. return(0);
  1600. if (o!=n)
  1601. {
  1602. if (sp->subsamplingcorrect==0)
  1603. TIFFErrorExt(tif->tif_clientdata,module,"Corrupt SOF marker in JPEG data");
  1604. return(0);
  1605. }
  1606. /* per component stuff */
  1607. /* TODO: double-check that flow implies that n cannot be as big as to make us overflow sof_c, sof_hv and sof_tq arrays */
  1608. for (q=0; q<n; q++)
  1609. {
  1610. /* C: Component identifier */
  1611. if (OJPEGReadByte(sp,&o)==0)
  1612. return(0);
  1613. if (sp->subsamplingcorrect==0)
  1614. sp->sof_c[q]=o;
  1615. /* H: Horizontal sampling factor, and V: Vertical sampling factor */
  1616. if (OJPEGReadByte(sp,&o)==0)
  1617. return(0);
  1618. if (sp->subsamplingcorrect!=0)
  1619. {
  1620. if (q==0)
  1621. {
  1622. sp->subsampling_hor=(o>>4);
  1623. sp->subsampling_ver=(o&15);
  1624. if (((sp->subsampling_hor!=1) && (sp->subsampling_hor!=2) && (sp->subsampling_hor!=4)) ||
  1625. ((sp->subsampling_ver!=1) && (sp->subsampling_ver!=2) && (sp->subsampling_ver!=4)))
  1626. sp->subsampling_force_desubsampling_inside_decompression=1;
  1627. }
  1628. else
  1629. {
  1630. if (o!=17)
  1631. sp->subsampling_force_desubsampling_inside_decompression=1;
  1632. }
  1633. }
  1634. else
  1635. {
  1636. sp->sof_hv[q]=o;
  1637. if (sp->subsampling_force_desubsampling_inside_decompression==0)
  1638. {
  1639. if (q==0)
  1640. {
  1641. if (o!=((sp->subsampling_hor<<4)|sp->subsampling_ver))
  1642. {
  1643. TIFFErrorExt(tif->tif_clientdata,module,"JPEG compressed data indicates unexpected subsampling values");
  1644. return(0);
  1645. }
  1646. }
  1647. else
  1648. {
  1649. if (o!=17)
  1650. {
  1651. TIFFErrorExt(tif->tif_clientdata,module,"JPEG compressed data indicates unexpected subsampling values");
  1652. return(0);
  1653. }
  1654. }
  1655. }
  1656. }
  1657. /* Tq: Quantization table destination selector */
  1658. if (OJPEGReadByte(sp,&o)==0)
  1659. return(0);
  1660. if (sp->subsamplingcorrect==0)
  1661. sp->sof_tq[q]=o;
  1662. }
  1663. if (sp->subsamplingcorrect==0)
  1664. sp->sof_log=1;
  1665. return(1);
  1666. }
  1667. static int
  1668. OJPEGReadHeaderInfoSecStreamSos(TIFF* tif)
  1669. {
  1670. /* this marker needs to be checked, and part of its data needs to be saved for regeneration later on */
  1671. static const char module[]="OJPEGReadHeaderInfoSecStreamSos";
  1672. OJPEGState* sp=(OJPEGState*)tif->tif_data;
  1673. uint16 m;
  1674. uint8 n;
  1675. uint8 o;
  1676. assert(sp->subsamplingcorrect==0);
  1677. if (sp->sof_log==0)
  1678. {
  1679. TIFFErrorExt(tif->tif_clientdata,module,"Corrupt SOS marker in JPEG data");
  1680. return(0);
  1681. }
  1682. /* Ls */
  1683. if (OJPEGReadWord(sp,&m)==0)
  1684. return(0);
  1685. if (m!=6+sp->samples_per_pixel_per_plane*2)
  1686. {
  1687. TIFFErrorExt(tif->tif_clientdata,module,"Corrupt SOS marker in JPEG data");
  1688. return(0);
  1689. }
  1690. /* Ns */
  1691. if (OJPEGReadByte(sp,&n)==0)
  1692. return(0);
  1693. if (n!=sp->samples_per_pixel_per_plane)
  1694. {
  1695. TIFFErrorExt(tif->tif_clientdata,module,"Corrupt SOS marker in JPEG data");
  1696. return(0);
  1697. }
  1698. /* Cs, Td, and Ta */
  1699. for (o=0; o<sp->samples_per_pixel_per_plane; o++)
  1700. {
  1701. /* Cs */
  1702. if (OJPEGReadByte(sp,&n)==0)
  1703. return(0);
  1704. sp->sos_cs[sp->plane_sample_offset+o]=n;
  1705. /* Td and Ta */
  1706. if (OJPEGReadByte(sp,&n)==0)
  1707. return(0);
  1708. sp->sos_tda[sp->plane_sample_offset+o]=n;
  1709. }
  1710. /* skip Ss, Se, Ah, en Al -> no check, as per Tom Lane recommendation, as per LibJpeg source */
  1711. OJPEGReadSkip(sp,3);
  1712. return(1);
  1713. }
  1714. static int
  1715. OJPEGReadHeaderInfoSecTablesQTable(TIFF* tif)
  1716. {
  1717. static const char module[]="OJPEGReadHeaderInfoSecTablesQTable";
  1718. OJPEGState* sp=(OJPEGState*)tif->tif_data;
  1719. uint8 m;
  1720. uint8 n;
  1721. uint32 oa;
  1722. uint8* ob;
  1723. uint32 p;
  1724. if (sp->qtable_offset[0]==0)
  1725. {
  1726. TIFFErrorExt(tif->tif_clientdata,module,"Missing JPEG tables");
  1727. return(0);
  1728. }
  1729. sp->in_buffer_file_pos_log=0;
  1730. for (m=0; m<sp->samples_per_pixel; m++)
  1731. {
  1732. if ((sp->qtable_offset[m]!=0) && ((m==0) || (sp->qtable_offset[m]!=sp->qtable_offset[m-1])))
  1733. {
  1734. for (n=0; n<m-1; n++)
  1735. {
  1736. if (sp->qtable_offset[m]==sp->qtable_offset[n])
  1737. {
  1738. TIFFErrorExt(tif->tif_clientdata,module,"Corrupt JpegQTables tag value");
  1739. return(0);
  1740. }
  1741. }
  1742. oa=sizeof(uint32)+69;
  1743. ob=_TIFFmalloc(oa);
  1744. if (ob==0)
  1745. {
  1746. TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
  1747. return(0);
  1748. }
  1749. *(uint32*)ob=oa;
  1750. ob[sizeof(uint32)]=255;
  1751. ob[sizeof(uint32)+1]=JPEG_MARKER_DQT;
  1752. ob[sizeof(uint32)+2]=0;
  1753. ob[sizeof(uint32)+3]=67;
  1754. ob[sizeof(uint32)+4]=m;
  1755. TIFFSeekFile(tif,sp->qtable_offset[m],SEEK_SET);
  1756. p=(uint32)TIFFReadFile(tif,&ob[sizeof(uint32)+5],64);
  1757. if (p!=64)
  1758. {
  1759. _TIFFfree(ob);
  1760. return(0);
  1761. }
  1762. if (sp->qtable[m]!=0)
  1763. _TIFFfree(sp->qtable[m]);
  1764. sp->qtable[m]=ob;
  1765. sp->sof_tq[m]=m;
  1766. }
  1767. else
  1768. sp->sof_tq[m]=sp->sof_tq[m-1];
  1769. }
  1770. return(1);
  1771. }
  1772. static int
  1773. OJPEGReadHeaderInfoSecTablesDcTable(TIFF* tif)
  1774. {
  1775. static const char module[]="OJPEGReadHeaderInfoSecTablesDcTable";
  1776. OJPEGState* sp=(OJPEGState*)tif->tif_data;
  1777. uint8 m;
  1778. uint8 n;
  1779. uint8 o[16];
  1780. uint32 p;
  1781. uint32 q;
  1782. uint32 ra;
  1783. uint8* rb;
  1784. if (sp->dctable_offset[0]==0)
  1785. {
  1786. TIFFErrorExt(tif->tif_clientdata,module,"Missing JPEG tables");
  1787. return(0);
  1788. }
  1789. sp->in_buffer_file_pos_log=0;
  1790. for (m=0; m<sp->samples_per_pixel; m++)
  1791. {
  1792. if ((sp->dctable_offset[m]!=0) && ((m==0) || (sp->dctable_offset[m]!=sp->dctable_offset[m-1])))
  1793. {
  1794. for (n=0; n<m-1; n++)
  1795. {
  1796. if (sp->dctable_offset[m]==sp->dctable_offset[n])
  1797. {
  1798. TIFFErrorExt(tif->tif_clientdata,module,"Corrupt JpegDcTables tag value");
  1799. return(0);
  1800. }
  1801. }
  1802. TIFFSeekFile(tif,sp->dctable_offset[m],SEEK_SET);
  1803. p=(uint32)TIFFReadFile(tif,o,16);
  1804. if (p!=16)
  1805. return(0);
  1806. q=0;
  1807. for (n=0; n<16; n++)
  1808. q+=o[n];
  1809. ra=sizeof(uint32)+21+q;
  1810. rb=_TIFFmalloc(ra);
  1811. if (rb==0)
  1812. {
  1813. TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
  1814. return(0);
  1815. }
  1816. *(uint32*)rb=ra;
  1817. rb[sizeof(uint32)]=255;
  1818. rb[sizeof(uint32)+1]=JPEG_MARKER_DHT;
  1819. rb[sizeof(uint32)+2]=(uint8)((19+q)>>8);
  1820. rb[sizeof(uint32)+3]=((19+q)&255);
  1821. rb[sizeof(uint32)+4]=m;
  1822. for (n=0; n<16; n++)
  1823. rb[sizeof(uint32)+5+n]=o[n];
  1824. p=(uint32)TIFFReadFile(tif,&(rb[sizeof(uint32)+21]),q);
  1825. if (p!=q)
  1826. {
  1827. _TIFFfree(rb);
  1828. return(0);
  1829. }
  1830. if (sp->dctable[m]!=0)
  1831. _TIFFfree(sp->dctable[m]);
  1832. sp->dctable[m]=rb;
  1833. sp->sos_tda[m]=(m<<4);
  1834. }
  1835. else
  1836. sp->sos_tda[m]=sp->sos_tda[m-1];
  1837. }
  1838. return(1);
  1839. }
  1840. static int
  1841. OJPEGReadHeaderInfoSecTablesAcTable(TIFF* tif)
  1842. {
  1843. static const char module[]="OJPEGReadHeaderInfoSecTablesAcTable";
  1844. OJPEGState* sp=(OJPEGState*)tif->tif_data;
  1845. uint8 m;
  1846. uint8 n;
  1847. uint8 o[16];
  1848. uint32 p;
  1849. uint32 q;
  1850. uint32 ra;
  1851. uint8* rb;
  1852. if (sp->actable_offset[0]==0)
  1853. {
  1854. TIFFErrorExt(tif->tif_clientdata,module,"Missing JPEG tables");
  1855. return(0);
  1856. }
  1857. sp->in_buffer_file_pos_log=0;
  1858. for (m=0; m<sp->samples_per_pixel; m++)
  1859. {
  1860. if ((sp->actable_offset[m]!=0) && ((m==0) || (sp->actable_offset[m]!=sp->actable_offset[m-1])))
  1861. {
  1862. for (n=0; n<m-1; n++)
  1863. {
  1864. if (sp->actable_offset[m]==sp->actable_offset[n])
  1865. {
  1866. TIFFErrorExt(tif->tif_clientdata,module,"Corrupt JpegAcTables tag value");
  1867. return(0);
  1868. }
  1869. }
  1870. TIFFSeekFile(tif,sp->actable_offset[m],SEEK_SET);
  1871. p=(uint32)TIFFReadFile(tif,o,16);
  1872. if (p!=16)
  1873. return(0);
  1874. q=0;
  1875. for (n=0; n<16; n++)
  1876. q+=o[n];
  1877. ra=sizeof(uint32)+21+q;
  1878. rb=_TIFFmalloc(ra);
  1879. if (rb==0)
  1880. {
  1881. TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
  1882. return(0);
  1883. }
  1884. *(uint32*)rb=ra;
  1885. rb[sizeof(uint32)]=255;
  1886. rb[sizeof(uint32)+1]=JPEG_MARKER_DHT;
  1887. rb[sizeof(uint32)+2]=(uint8)((19+q)>>8);
  1888. rb[sizeof(uint32)+3]=((19+q)&255);
  1889. rb[sizeof(uint32)+4]=(16|m);
  1890. for (n=0; n<16; n++)
  1891. rb[sizeof(uint32)+5+n]=o[n];
  1892. p=(uint32)TIFFReadFile(tif,&(rb[sizeof(uint32)+21]),q);
  1893. if (p!=q)
  1894. {
  1895. _TIFFfree(rb);
  1896. return(0);
  1897. }
  1898. if (sp->actable[m]!=0)
  1899. _TIFFfree(sp->actable[m]);
  1900. sp->actable[m]=rb;
  1901. sp->sos_tda[m]=(sp->sos_tda[m]|m);
  1902. }
  1903. else
  1904. sp->sos_tda[m]=(sp->sos_tda[m]|(sp->sos_tda[m-1]&15));
  1905. }
  1906. return(1);
  1907. }
  1908. static int
  1909. OJPEGReadBufferFill(OJPEGState* sp)
  1910. {
  1911. uint16 m;
  1912. tmsize_t n;
  1913. /* TODO: double-check: when subsamplingcorrect is set, no call to TIFFErrorExt or TIFFWarningExt should be made
  1914. * in any other case, seek or read errors should be passed through */
  1915. do
  1916. {
  1917. if (sp->in_buffer_file_togo!=0)
  1918. {
  1919. if (sp->in_buffer_file_pos_log==0)
  1920. {
  1921. TIFFSeekFile(sp->tif,sp->in_buffer_file_pos,SEEK_SET);
  1922. sp->in_buffer_file_pos_log=1;
  1923. }
  1924. m=OJPEG_BUFFER;
  1925. if ((uint64)m>sp->in_buffer_file_togo)
  1926. m=(uint16)sp->in_buffer_file_togo;
  1927. n=TIFFReadFile(sp->tif,sp->in_buffer,(tmsize_t)m);
  1928. if (n==0)
  1929. return(0);
  1930. assert(n>0);
  1931. assert(n<=OJPEG_BUFFER);
  1932. assert(n<65536);
  1933. assert((uint64)n<=sp->in_buffer_file_togo);
  1934. m=(uint16)n;
  1935. sp->in_buffer_togo=m;
  1936. sp->in_buffer_cur=sp->in_buffer;
  1937. sp->in_buffer_file_togo-=m;
  1938. sp->in_buffer_file_pos+=m;
  1939. break;
  1940. }
  1941. sp->in_buffer_file_pos_log=0;
  1942. switch(sp->in_buffer_source)
  1943. {
  1944. case osibsNotSetYet:
  1945. if (sp->jpeg_interchange_format!=0)
  1946. {
  1947. sp->in_buffer_file_pos=sp->jpeg_interchange_format;
  1948. sp->in_buffer_file_togo=sp->jpeg_interchange_format_length;
  1949. }
  1950. sp->in_buffer_source=osibsJpegInterchangeFormat;
  1951. break;
  1952. case osibsJpegInterchangeFormat:
  1953. sp->in_buffer_source=osibsStrile;
  1954. break;
  1955. case osibsStrile:
  1956. if (sp->in_buffer_next_strile==sp->in_buffer_strile_count)
  1957. sp->in_buffer_source=osibsEof;
  1958. else
  1959. {
  1960. int err = 0;
  1961. sp->in_buffer_file_pos=TIFFGetStrileOffsetWithErr(sp->tif, sp->in_buffer_next_strile, &err);
  1962. if( err )
  1963. return 0;
  1964. if (sp->in_buffer_file_pos!=0)
  1965. {
  1966. uint64 bytecount = TIFFGetStrileByteCountWithErr(sp->tif, sp->in_buffer_next_strile, &err);
  1967. if( err )
  1968. return 0;
  1969. if (sp->in_buffer_file_pos>=sp->file_size)
  1970. sp->in_buffer_file_pos=0;
  1971. else if (bytecount==0)
  1972. sp->in_buffer_file_togo=sp->file_size-sp->in_buffer_file_pos;
  1973. else
  1974. {
  1975. sp->in_buffer_file_togo=bytecount;
  1976. if (sp->in_buffer_file_togo==0)
  1977. sp->in_buffer_file_pos=0;
  1978. else if (sp->in_buffer_file_pos > TIFF_UINT64_MAX - sp->in_buffer_file_togo ||
  1979. sp->in_buffer_file_pos+sp->in_buffer_file_togo>sp->file_size)
  1980. sp->in_buffer_file_togo=sp->file_size-sp->in_buffer_file_pos;
  1981. }
  1982. }
  1983. sp->in_buffer_next_strile++;
  1984. }
  1985. break;
  1986. default:
  1987. return(0);
  1988. }
  1989. } while (1);
  1990. return(1);
  1991. }
  1992. static int
  1993. OJPEGReadByte(OJPEGState* sp, uint8* byte)
  1994. {
  1995. if (sp->in_buffer_togo==0)
  1996. {
  1997. if (OJPEGReadBufferFill(sp)==0)
  1998. return(0);
  1999. assert(sp->in_buffer_togo>0);
  2000. }
  2001. *byte=*(sp->in_buffer_cur);
  2002. sp->in_buffer_cur++;
  2003. sp->in_buffer_togo--;
  2004. return(1);
  2005. }
  2006. static int
  2007. OJPEGReadBytePeek(OJPEGState* sp, uint8* byte)
  2008. {
  2009. if (sp->in_buffer_togo==0)
  2010. {
  2011. if (OJPEGReadBufferFill(sp)==0)
  2012. return(0);
  2013. assert(sp->in_buffer_togo>0);
  2014. }
  2015. *byte=*(sp->in_buffer_cur);
  2016. return(1);
  2017. }
  2018. static void
  2019. OJPEGReadByteAdvance(OJPEGState* sp)
  2020. {
  2021. assert(sp->in_buffer_togo>0);
  2022. sp->in_buffer_cur++;
  2023. sp->in_buffer_togo--;
  2024. }
  2025. static int
  2026. OJPEGReadWord(OJPEGState* sp, uint16* word)
  2027. {
  2028. uint8 m;
  2029. if (OJPEGReadByte(sp,&m)==0)
  2030. return(0);
  2031. *word=(m<<8);
  2032. if (OJPEGReadByte(sp,&m)==0)
  2033. return(0);
  2034. *word|=m;
  2035. return(1);
  2036. }
  2037. static int
  2038. OJPEGReadBlock(OJPEGState* sp, uint16 len, void* mem)
  2039. {
  2040. uint16 mlen;
  2041. uint8* mmem;
  2042. uint16 n;
  2043. assert(len>0);
  2044. mlen=len;
  2045. mmem=mem;
  2046. do
  2047. {
  2048. if (sp->in_buffer_togo==0)
  2049. {
  2050. if (OJPEGReadBufferFill(sp)==0)
  2051. return(0);
  2052. assert(sp->in_buffer_togo>0);
  2053. }
  2054. n=mlen;
  2055. if (n>sp->in_buffer_togo)
  2056. n=sp->in_buffer_togo;
  2057. _TIFFmemcpy(mmem,sp->in_buffer_cur,n);
  2058. sp->in_buffer_cur+=n;
  2059. sp->in_buffer_togo-=n;
  2060. mlen-=n;
  2061. mmem+=n;
  2062. } while(mlen>0);
  2063. return(1);
  2064. }
  2065. static void
  2066. OJPEGReadSkip(OJPEGState* sp, uint16 len)
  2067. {
  2068. uint16 m;
  2069. uint16 n;
  2070. m=len;
  2071. n=m;
  2072. if (n>sp->in_buffer_togo)
  2073. n=sp->in_buffer_togo;
  2074. sp->in_buffer_cur+=n;
  2075. sp->in_buffer_togo-=n;
  2076. m-=n;
  2077. if (m>0)
  2078. {
  2079. assert(sp->in_buffer_togo==0);
  2080. n=m;
  2081. if ((uint64)n>sp->in_buffer_file_togo)
  2082. n=(uint16)sp->in_buffer_file_togo;
  2083. sp->in_buffer_file_pos+=n;
  2084. sp->in_buffer_file_togo-=n;
  2085. sp->in_buffer_file_pos_log=0;
  2086. /* we don't skip past jpeginterchangeformat/strile block...
  2087. * if that is asked from us, we're dealing with totally bazurk
  2088. * data anyway, and we've not seen this happening on any
  2089. * testfile, so we might as well likely cause some other
  2090. * meaningless error to be passed at some later time
  2091. */
  2092. }
  2093. }
  2094. static int
  2095. OJPEGWriteStream(TIFF* tif, void** mem, uint32* len)
  2096. {
  2097. OJPEGState* sp=(OJPEGState*)tif->tif_data;
  2098. *len=0;
  2099. do
  2100. {
  2101. assert(sp->out_state<=ososEoi);
  2102. switch(sp->out_state)
  2103. {
  2104. case ososSoi:
  2105. OJPEGWriteStreamSoi(tif,mem,len);
  2106. break;
  2107. case ososQTable0:
  2108. OJPEGWriteStreamQTable(tif,0,mem,len);
  2109. break;
  2110. case ososQTable1:
  2111. OJPEGWriteStreamQTable(tif,1,mem,len);
  2112. break;
  2113. case ososQTable2:
  2114. OJPEGWriteStreamQTable(tif,2,mem,len);
  2115. break;
  2116. case ososQTable3:
  2117. OJPEGWriteStreamQTable(tif,3,mem,len);
  2118. break;
  2119. case ososDcTable0:
  2120. OJPEGWriteStreamDcTable(tif,0,mem,len);
  2121. break;
  2122. case ososDcTable1:
  2123. OJPEGWriteStreamDcTable(tif,1,mem,len);
  2124. break;
  2125. case ososDcTable2:
  2126. OJPEGWriteStreamDcTable(tif,2,mem,len);
  2127. break;
  2128. case ososDcTable3:
  2129. OJPEGWriteStreamDcTable(tif,3,mem,len);
  2130. break;
  2131. case ososAcTable0:
  2132. OJPEGWriteStreamAcTable(tif,0,mem,len);
  2133. break;
  2134. case ososAcTable1:
  2135. OJPEGWriteStreamAcTable(tif,1,mem,len);
  2136. break;
  2137. case ososAcTable2:
  2138. OJPEGWriteStreamAcTable(tif,2,mem,len);
  2139. break;
  2140. case ososAcTable3:
  2141. OJPEGWriteStreamAcTable(tif,3,mem,len);
  2142. break;
  2143. case ososDri:
  2144. OJPEGWriteStreamDri(tif,mem,len);
  2145. break;
  2146. case ososSof:
  2147. OJPEGWriteStreamSof(tif,mem,len);
  2148. break;
  2149. case ososSos:
  2150. OJPEGWriteStreamSos(tif,mem,len);
  2151. break;
  2152. case ososCompressed:
  2153. if (OJPEGWriteStreamCompressed(tif,mem,len)==0)
  2154. return(0);
  2155. break;
  2156. case ososRst:
  2157. OJPEGWriteStreamRst(tif,mem,len);
  2158. break;
  2159. case ososEoi:
  2160. OJPEGWriteStreamEoi(tif,mem,len);
  2161. break;
  2162. }
  2163. } while (*len==0);
  2164. return(1);
  2165. }
  2166. static void
  2167. OJPEGWriteStreamSoi(TIFF* tif, void** mem, uint32* len)
  2168. {
  2169. OJPEGState* sp=(OJPEGState*)tif->tif_data;
  2170. assert(OJPEG_BUFFER>=2);
  2171. sp->out_buffer[0]=255;
  2172. sp->out_buffer[1]=JPEG_MARKER_SOI;
  2173. *len=2;
  2174. *mem=(void*)sp->out_buffer;
  2175. sp->out_state++;
  2176. }
  2177. static void
  2178. OJPEGWriteStreamQTable(TIFF* tif, uint8 table_index, void** mem, uint32* len)
  2179. {
  2180. OJPEGState* sp=(OJPEGState*)tif->tif_data;
  2181. if (sp->qtable[table_index]!=0)
  2182. {
  2183. *mem=(void*)(sp->qtable[table_index]+sizeof(uint32));
  2184. *len=*((uint32*)sp->qtable[table_index])-sizeof(uint32);
  2185. }
  2186. sp->out_state++;
  2187. }
  2188. static void
  2189. OJPEGWriteStreamDcTable(TIFF* tif, uint8 table_index, void** mem, uint32* len)
  2190. {
  2191. OJPEGState* sp=(OJPEGState*)tif->tif_data;
  2192. if (sp->dctable[table_index]!=0)
  2193. {
  2194. *mem=(void*)(sp->dctable[table_index]+sizeof(uint32));
  2195. *len=*((uint32*)sp->dctable[table_index])-sizeof(uint32);
  2196. }
  2197. sp->out_state++;
  2198. }
  2199. static void
  2200. OJPEGWriteStreamAcTable(TIFF* tif, uint8 table_index, void** mem, uint32* len)
  2201. {
  2202. OJPEGState* sp=(OJPEGState*)tif->tif_data;
  2203. if (sp->actable[table_index]!=0)
  2204. {
  2205. *mem=(void*)(sp->actable[table_index]+sizeof(uint32));
  2206. *len=*((uint32*)sp->actable[table_index])-sizeof(uint32);
  2207. }
  2208. sp->out_state++;
  2209. }
  2210. static void
  2211. OJPEGWriteStreamDri(TIFF* tif, void** mem, uint32* len)
  2212. {
  2213. OJPEGState* sp=(OJPEGState*)tif->tif_data;
  2214. assert(OJPEG_BUFFER>=6);
  2215. if (sp->restart_interval!=0)
  2216. {
  2217. sp->out_buffer[0]=255;
  2218. sp->out_buffer[1]=JPEG_MARKER_DRI;
  2219. sp->out_buffer[2]=0;
  2220. sp->out_buffer[3]=4;
  2221. sp->out_buffer[4]=(sp->restart_interval>>8);
  2222. sp->out_buffer[5]=(sp->restart_interval&255);
  2223. *len=6;
  2224. *mem=(void*)sp->out_buffer;
  2225. }
  2226. sp->out_state++;
  2227. }
  2228. static void
  2229. OJPEGWriteStreamSof(TIFF* tif, void** mem, uint32* len)
  2230. {
  2231. OJPEGState* sp=(OJPEGState*)tif->tif_data;
  2232. uint8 m;
  2233. assert(OJPEG_BUFFER>=2+8+sp->samples_per_pixel_per_plane*3);
  2234. assert(255>=8+sp->samples_per_pixel_per_plane*3);
  2235. sp->out_buffer[0]=255;
  2236. sp->out_buffer[1]=sp->sof_marker_id;
  2237. /* Lf */
  2238. sp->out_buffer[2]=0;
  2239. sp->out_buffer[3]=8+sp->samples_per_pixel_per_plane*3;
  2240. /* P */
  2241. sp->out_buffer[4]=8;
  2242. /* Y */
  2243. sp->out_buffer[5]=(uint8)(sp->sof_y>>8);
  2244. sp->out_buffer[6]=(sp->sof_y&255);
  2245. /* X */
  2246. sp->out_buffer[7]=(uint8)(sp->sof_x>>8);
  2247. sp->out_buffer[8]=(sp->sof_x&255);
  2248. /* Nf */
  2249. sp->out_buffer[9]=sp->samples_per_pixel_per_plane;
  2250. for (m=0; m<sp->samples_per_pixel_per_plane; m++)
  2251. {
  2252. /* C */
  2253. sp->out_buffer[10+m*3]=sp->sof_c[sp->plane_sample_offset+m];
  2254. /* H and V */
  2255. sp->out_buffer[10+m*3+1]=sp->sof_hv[sp->plane_sample_offset+m];
  2256. /* Tq */
  2257. sp->out_buffer[10+m*3+2]=sp->sof_tq[sp->plane_sample_offset+m];
  2258. }
  2259. *len=10+sp->samples_per_pixel_per_plane*3;
  2260. *mem=(void*)sp->out_buffer;
  2261. sp->out_state++;
  2262. }
  2263. static void
  2264. OJPEGWriteStreamSos(TIFF* tif, void** mem, uint32* len)
  2265. {
  2266. OJPEGState* sp=(OJPEGState*)tif->tif_data;
  2267. uint8 m;
  2268. assert(OJPEG_BUFFER>=2+6+sp->samples_per_pixel_per_plane*2);
  2269. assert(255>=6+sp->samples_per_pixel_per_plane*2);
  2270. sp->out_buffer[0]=255;
  2271. sp->out_buffer[1]=JPEG_MARKER_SOS;
  2272. /* Ls */
  2273. sp->out_buffer[2]=0;
  2274. sp->out_buffer[3]=6+sp->samples_per_pixel_per_plane*2;
  2275. /* Ns */
  2276. sp->out_buffer[4]=sp->samples_per_pixel_per_plane;
  2277. for (m=0; m<sp->samples_per_pixel_per_plane; m++)
  2278. {
  2279. /* Cs */
  2280. sp->out_buffer[5+m*2]=sp->sos_cs[sp->plane_sample_offset+m];
  2281. /* Td and Ta */
  2282. sp->out_buffer[5+m*2+1]=sp->sos_tda[sp->plane_sample_offset+m];
  2283. }
  2284. /* Ss */
  2285. sp->out_buffer[5+sp->samples_per_pixel_per_plane*2]=0;
  2286. /* Se */
  2287. sp->out_buffer[5+sp->samples_per_pixel_per_plane*2+1]=63;
  2288. /* Ah and Al */
  2289. sp->out_buffer[5+sp->samples_per_pixel_per_plane*2+2]=0;
  2290. *len=8+sp->samples_per_pixel_per_plane*2;
  2291. *mem=(void*)sp->out_buffer;
  2292. sp->out_state++;
  2293. }
  2294. static int
  2295. OJPEGWriteStreamCompressed(TIFF* tif, void** mem, uint32* len)
  2296. {
  2297. OJPEGState* sp=(OJPEGState*)tif->tif_data;
  2298. if (sp->in_buffer_togo==0)
  2299. {
  2300. if (OJPEGReadBufferFill(sp)==0)
  2301. return(0);
  2302. assert(sp->in_buffer_togo>0);
  2303. }
  2304. *len=sp->in_buffer_togo;
  2305. *mem=(void*)sp->in_buffer_cur;
  2306. sp->in_buffer_togo=0;
  2307. if (sp->in_buffer_file_togo==0)
  2308. {
  2309. switch(sp->in_buffer_source)
  2310. {
  2311. case osibsStrile:
  2312. if (sp->in_buffer_next_strile<sp->in_buffer_strile_count)
  2313. sp->out_state=ososRst;
  2314. else
  2315. sp->out_state=ososEoi;
  2316. break;
  2317. case osibsEof:
  2318. sp->out_state=ososEoi;
  2319. break;
  2320. default:
  2321. break;
  2322. }
  2323. }
  2324. return(1);
  2325. }
  2326. static void
  2327. OJPEGWriteStreamRst(TIFF* tif, void** mem, uint32* len)
  2328. {
  2329. OJPEGState* sp=(OJPEGState*)tif->tif_data;
  2330. assert(OJPEG_BUFFER>=2);
  2331. sp->out_buffer[0]=255;
  2332. sp->out_buffer[1]=JPEG_MARKER_RST0+sp->restart_index;
  2333. sp->restart_index++;
  2334. if (sp->restart_index==8)
  2335. sp->restart_index=0;
  2336. *len=2;
  2337. *mem=(void*)sp->out_buffer;
  2338. sp->out_state=ososCompressed;
  2339. }
  2340. static void
  2341. OJPEGWriteStreamEoi(TIFF* tif, void** mem, uint32* len)
  2342. {
  2343. OJPEGState* sp=(OJPEGState*)tif->tif_data;
  2344. assert(OJPEG_BUFFER>=2);
  2345. sp->out_buffer[0]=255;
  2346. sp->out_buffer[1]=JPEG_MARKER_EOI;
  2347. *len=2;
  2348. *mem=(void*)sp->out_buffer;
  2349. }
  2350. #ifndef LIBJPEG_ENCAP_EXTERNAL
  2351. static int
  2352. jpeg_create_decompress_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo)
  2353. {
  2354. if( SETJMP(sp->exit_jmpbuf) )
  2355. return 0;
  2356. else {
  2357. jpeg_create_decompress(cinfo);
  2358. return 1;
  2359. }
  2360. }
  2361. #endif
  2362. #ifndef LIBJPEG_ENCAP_EXTERNAL
  2363. static int
  2364. jpeg_read_header_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, uint8 require_image)
  2365. {
  2366. if( SETJMP(sp->exit_jmpbuf) )
  2367. return 0;
  2368. else {
  2369. jpeg_read_header(cinfo,require_image);
  2370. return 1;
  2371. }
  2372. }
  2373. #endif
  2374. #ifndef LIBJPEG_ENCAP_EXTERNAL
  2375. static int
  2376. jpeg_start_decompress_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo)
  2377. {
  2378. if( SETJMP(sp->exit_jmpbuf) )
  2379. return 0;
  2380. else {
  2381. jpeg_start_decompress(cinfo);
  2382. return 1;
  2383. }
  2384. }
  2385. #endif
  2386. #ifndef LIBJPEG_ENCAP_EXTERNAL
  2387. static int
  2388. jpeg_read_scanlines_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, void* scanlines, uint32 max_lines)
  2389. {
  2390. if( SETJMP(sp->exit_jmpbuf) )
  2391. return 0;
  2392. else {
  2393. jpeg_read_scanlines(cinfo,scanlines,max_lines);
  2394. return 1;
  2395. }
  2396. }
  2397. #endif
  2398. #ifndef LIBJPEG_ENCAP_EXTERNAL
  2399. static int
  2400. jpeg_read_raw_data_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, void* data, uint32 max_lines)
  2401. {
  2402. if( SETJMP(sp->exit_jmpbuf) )
  2403. return 0;
  2404. else {
  2405. jpeg_read_raw_data(cinfo,data,max_lines);
  2406. return 1;
  2407. }
  2408. }
  2409. #endif
  2410. #ifndef LIBJPEG_ENCAP_EXTERNAL
  2411. static void
  2412. jpeg_encap_unwind(TIFF* tif)
  2413. {
  2414. OJPEGState* sp=(OJPEGState*)tif->tif_data;
  2415. LONGJMP(sp->exit_jmpbuf,1);
  2416. }
  2417. #endif
  2418. static void
  2419. OJPEGLibjpegJpegErrorMgrOutputMessage(jpeg_common_struct* cinfo)
  2420. {
  2421. char buffer[JMSG_LENGTH_MAX];
  2422. (*cinfo->err->format_message)(cinfo,buffer);
  2423. TIFFWarningExt(((TIFF*)(cinfo->client_data))->tif_clientdata,"LibJpeg","%s",buffer);
  2424. }
  2425. static void
  2426. OJPEGLibjpegJpegErrorMgrErrorExit(jpeg_common_struct* cinfo)
  2427. {
  2428. char buffer[JMSG_LENGTH_MAX];
  2429. (*cinfo->err->format_message)(cinfo,buffer);
  2430. TIFFErrorExt(((TIFF*)(cinfo->client_data))->tif_clientdata,"LibJpeg","%s",buffer);
  2431. jpeg_encap_unwind((TIFF*)(cinfo->client_data));
  2432. }
  2433. static void
  2434. OJPEGLibjpegJpegSourceMgrInitSource(jpeg_decompress_struct* cinfo)
  2435. {
  2436. (void)cinfo;
  2437. }
  2438. static boolean
  2439. OJPEGLibjpegJpegSourceMgrFillInputBuffer(jpeg_decompress_struct* cinfo)
  2440. {
  2441. TIFF* tif=(TIFF*)cinfo->client_data;
  2442. OJPEGState* sp=(OJPEGState*)tif->tif_data;
  2443. void* mem=0;
  2444. uint32 len=0U;
  2445. if (OJPEGWriteStream(tif,&mem,&len)==0)
  2446. {
  2447. TIFFErrorExt(tif->tif_clientdata,"LibJpeg","Premature end of JPEG data");
  2448. jpeg_encap_unwind(tif);
  2449. }
  2450. sp->libjpeg_jpeg_source_mgr.bytes_in_buffer=len;
  2451. sp->libjpeg_jpeg_source_mgr.next_input_byte=mem;
  2452. return(1);
  2453. }
  2454. static void
  2455. OJPEGLibjpegJpegSourceMgrSkipInputData(jpeg_decompress_struct* cinfo, long num_bytes)
  2456. {
  2457. TIFF* tif=(TIFF*)cinfo->client_data;
  2458. (void)num_bytes;
  2459. TIFFErrorExt(tif->tif_clientdata,"LibJpeg","Unexpected error");
  2460. jpeg_encap_unwind(tif);
  2461. }
  2462. #ifdef _MSC_VER
  2463. #pragma warning( push )
  2464. #pragma warning( disable : 4702 ) /* unreachable code */
  2465. #endif
  2466. static boolean
  2467. OJPEGLibjpegJpegSourceMgrResyncToRestart(jpeg_decompress_struct* cinfo, int desired)
  2468. {
  2469. TIFF* tif=(TIFF*)cinfo->client_data;
  2470. (void)desired;
  2471. TIFFErrorExt(tif->tif_clientdata,"LibJpeg","Unexpected error");
  2472. jpeg_encap_unwind(tif);
  2473. return(0);
  2474. }
  2475. #ifdef _MSC_VER
  2476. #pragma warning( pop )
  2477. #endif
  2478. static void
  2479. OJPEGLibjpegJpegSourceMgrTermSource(jpeg_decompress_struct* cinfo)
  2480. {
  2481. (void)cinfo;
  2482. }
  2483. #endif
  2484. /*
  2485. * Local Variables:
  2486. * mode: c
  2487. * c-basic-offset: 8
  2488. * fill-column: 78
  2489. * End:
  2490. */