tif_ojpeg.c 99 KB

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