LCOV - code coverage report
Current view: directory - frmts/gtiff/libtiff - tif_ojpeg.c (source / functions) Found Hit Coverage
Test: gdal_filtered.info Lines: 1235 608 49.2 %
Date: 2012-12-26 Functions: 63 35 55.6 %

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

Generated by: LCOV version 1.7