2
0

tif_ojpeg.c 75 KB

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