⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tifftechnote2.txt

📁 jpeg压缩解压缩算法及源码
💻 TXT
📖 第 1 页 / 共 3 页
字号:
------------------------------In lossy JPEG compression, it is customary to convert color source datato YCbCr and then downsample it before JPEG compression.  This gives2:1 data compression with hardly any visible image degradation, and itpermits additional space savings within the JPEG compression step proper.However, these steps are not considered part of the ISO JPEG standard.The ISO standard is "color blind": it accepts data in any color space.For TIFF purposes, the JPEG compression tag is considered to represent theISO JPEG compression standard only.  The ISO standard is applied to thesame data that would be stored in the TIFF file if no compression wereused.  Therefore, if color conversion or downsampling are used, they mustbe reflected in the regular TIFF fields; these steps are not considered tobe implicit in the JPEG compression tag value.  PhotometricInterpretationand related fields shall describe the color space actually stored in thefile.  With the TIFF 6.0 field definitions, downsampling is permissibleonly for YCbCr data, and it must correspond to the YCbCrSubSampling field.(Note that the default value for this field is not 1,1; so the default forYCbCr is to apply downsampling!)  It is likely that future versions of TIFFwill provide additional PhotometricInterpretation values and a more generalway of defining subsampling, so as to allow more flexibility inJPEG-compressed files.  But that issue is not addressed in this Tech Note.Implementors should note that many popular JPEG codecs(compressor/decompressors) provide automatic color conversion anddownsampling, so that the application may supply full-size RGB data whichis nonetheless converted to downsampled YCbCr.  This is an implementationconvenience which does not excuse the TIFF control layer from itsresponsibility to know what is really going on.  ThePhotometricInterpretation and subsampling fields written to the file mustdescribe what is actually in the file.A JPEG-compressed TIFF file will typically have PhotometricInterpretation =YCbCr and YCbCrSubSampling = [2,1] or [2,2], unless the source data wasgrayscale or CMYK.Basic representation of JPEG-compressed images----------------------------------------------JPEG compression works in either strip-based or tile-based TIFF files.Rather than repeating "strip or tile" constantly, we will use the term"segment" to mean either a strip or a tile.When the Compression field has the value 7, each image segment containsa complete JPEG datastream which is valid according to the ISO JPEGstandard (ISO/IEC 10918-1).  Any sequential JPEG process can be used,including lossless JPEG, but progressive and hierarchical processes are notsupported.  Since JPEG is useful only for continuous-tone images, thePhotometricInterpretation of the image shall not be 3 (palette color) nor4 (transparency mask).  The bit depth of the data is also restricted asspecified below.Each image segment in a JPEG-compressed TIFF file shall contain a validJPEG datastream according to the ISO JPEG standard's rules forinterchange-format or abbreviated-image-format data.  The datastream shallcontain a single JPEG frame storing that segment of the image.  Therequired JPEG markers within a segment are:	SOI	(must appear at very beginning of segment)	SOFn	SOS	(one for each scan, if there is more than one scan)	EOI	(must appear at very end of segment)The actual compressed data follows SOS; it may contain RSTn markers if DRIis used.Additional JPEG "tables and miscellaneous" markers may appear between SOIand SOFn, between SOFn and SOS, and before each subsequent SOS if there ismore than one scan.  These markers include:	DQT	DHT	DAC	(not to appear unless arithmetic coding is used)	DRI	APPn	(shall be ignored by TIFF readers)	COM	(shall be ignored by TIFF readers)DNL markers shall not be used in TIFF files.  Readers should abort if anyother marker type is found, especially the JPEG reserved markers;occurrence of such a marker is likely to indicate a JPEG extension.The tables/miscellaneous markers may appear in any order.  Readers arecautioned that although the SOFn marker refers to DQT tables, JPEG does notrequire those tables to precede the SOFn, only the SOS.  Missing-tablechecks should be made when SOS is reached.If no JPEGTables field is used, then each image segment shall be a completeJPEG interchange datastream.  Each segment must define all the tables itreferences.  To allow readers to decode segments in any order, no segmentmay rely on tables being carried over from a previous segment.When a JPEGTables field is used, image segments may omit tables that havebeen specified in the JPEGTables field.  Further details appear below.The SOFn marker shall be of type SOF0 for strict baseline JPEG data, oftype SOF1 for non-baseline lossy JPEG data, or of type SOF3 for losslessJPEG data.  (SOF9 or SOF11 would be used for arithmetic coding.)  Allsegments of a JPEG-compressed TIFF image shall use the same JPEGcompression process, in particular the same SOFn type.The data precision field of the SOFn marker shall agree with the TIFFBitsPerSample field.  (Note that when PlanarConfiguration=1, this impliesthat all components must have the same BitsPerSample value; whenPlanarConfiguration=2, different components could have different bitdepths.)  For SOF0 only precision 8 is permitted; for SOF1, precision 8 or12 is permitted; for SOF3, precisions 2 to 16 are permitted.The image dimensions given in the SOFn marker shall agree with the logicaldimensions of that particular strip or tile.  For strip images, the SOFnimage width shall equal ImageWidth and the height shall equal RowsPerStrip,except in the last strip; its SOFn height shall equal the number of rowsremaining in the ImageLength.  (In other words, no padding data is countedin the SOFn dimensions.)  For tile images, each SOFn shall have widthTileWidth and height TileHeight; adding and removing any padding needed inthe edge tiles is the concern of some higher level of the TIFF software.(The dimensional rules are slightly different when PlanarConfiguration=2,as described below.)The ISO JPEG standard only permits images up to 65535 pixels in width orheight, due to 2-byte fields in the SOFn markers.  In TIFF, this limitsthe size of an individual JPEG-compressed strip or tile, but the totalimage size can be greater.The number of components in the JPEG datastream shall equal SamplesPerPixelfor PlanarConfiguration=1, and shall be 1 for PlanarConfiguration=2.  Thecomponents shall be stored in the same order as they are described at theTIFF field level.  (This applies both to their order in the SOFn marker,and to the order in which they are scanned if multiple JPEG scans areused.)  The component ID bytes are arbitrary so long as each componentwithin an image segment is given a distinct ID.  To avoid any possibleconfusion, we require that all segments of a TIFF image use the same IDcode for a given component.In PlanarConfiguration 1, the sampling factors given in SOFn markers shallagree with the sampling factors defined by the related TIFF fields (or withthe default values that are specified in the absence of those fields).When DCT-based JPEG is used in a strip TIFF file, RowsPerStrip is requiredto be a multiple of 8 times the largest vertical sampling factor, i.e., amultiple of the height of an interleaved MCU.  (For simplicity ofspecification, we require this even if the data is not actuallyinterleaved.)  For example, if YCbCrSubSampling = [2,2] then RowsPerStripmust be a multiple of 16.  An exception to this rule is made forsingle-strip images (RowsPerStrip >= ImageLength): the exact value ofRowsPerStrip is unimportant in that case.  This rule ensures that no datapadding is needed at the bottom of a strip, except perhaps the last strip.Any padding required at the right edge of the image, or at the bottom ofthe last strip, is expected to occur internally to the JPEG codec.When DCT-based JPEG is used in a tiled TIFF file, TileLength is requiredto be a multiple of 8 times the largest vertical sampling factor, i.e.,a multiple of the height of an interleaved MCU; and TileWidth is requiredto be a multiple of 8 times the largest horizontal sampling factor, i.e.,a multiple of the width of an interleaved MCU.  (For simplicity ofspecification, we require this even if the data is not actuallyinterleaved.)  All edge padding required will therefore occur in the courseof normal TIFF tile padding; it is not special to JPEG.Lossless JPEG does not impose these constraints on strip and tile sizes,since it is not DCT-based.Note that within JPEG datastreams, multibyte values appear in the MSB-firstorder specified by the JPEG standard, regardless of the byte ordering ofthe surrounding TIFF file.JPEGTables field----------------The only auxiliary TIFF field added for Compression=7 is the optionalJPEGTables field.  The purpose of JPEGTables is to predefine JPEGquantization and/or Huffman tables for subsequent use by JPEG imagesegments.  When this is done, these rather bulky tables need not beduplicated in each segment, thus saving space and processing time.JPEGTables may be used even in a single-segment file, although there is nospace savings in that case.JPEGTables:	Tag = 347 (15B.H)	Type = UNDEFINED	N = number of bytes in tables datastream, typically a few hundredJPEGTables provides default JPEG quantization and/or Huffman tables whichare used whenever a segment datastream does not contain its own tables, asspecified below.Notice that the JPEGTables field is required to have type code UNDEFINED,not type code BYTE.  This is to cue readers that expanding individual bytesto short or long integers is not appropriate.  A TIFF reader will generallyneed to store the field value as an uninterpreted byte sequence until it isfed to the JPEG decoder.Multibyte quantities within the tables follow the ISO JPEG convention ofMSB-first storage, regardless of the byte ordering of the surrounding TIFFfile.When the JPEGTables field is present, it shall contain a valid JPEG"abbreviated table specification" datastream.  This datastream shall beginwith SOI and end with EOI.  It may contain zero or more JPEG "tables andmiscellaneous" markers, namely:	DQT	DHT	DAC	(not to appear unless arithmetic coding is used)	DRI	APPn	(shall be ignored by TIFF readers)	COM	(shall be ignored by TIFF readers)Since JPEG defines the SOI marker to reset the DAC and DRI state, these twomarkers' values cannot be carried over into any image datastream, and thusthey are effectively no-ops in the JPEGTables field.  To avoid confusion,it is recommended that writers not place DAC or DRI markers in JPEGTables.However readers must properly skip over them if they appear.When JPEGTables is present, readers shall load the table specificationscontained in JPEGTables before processing image segment datastreams.Image segments may simply refer to these preloaded tables without definingthem.  An image segment can still define and use its own tables, subject tothe restrictions below.An image segment may not redefine any table defined in JPEGTables.  (Thisrestriction is imposed to allow readers to process image segments in randomorder without having to reload JPEGTables between segments.)  Therefore, useof JPEGTables divides the available table slots into two groups: "global"slots are defined in JPEGTables and may be used but not redefined bysegments; "local" slots are available for local definition and use in eachsegment.  To permit random access, a segment may not reference any localtables that it does not itself define.Special considerations for PlanarConfiguration 2------------------------------------------------In PlanarConfiguration 2, each image segment contains data for only onecolor component.  To avoid confusing the JPEG codec, we wish the segmentsto look like valid single-channel (i.e., grayscale) JPEG datastreams.  Thismeans that different rules must be used for the SOFn parameters.In PlanarConfiguration 2, the dimensions given in the SOFn of a subsampledcomponent shall be scaled down by the sampling factors compared to the SOFndimensions that would be used in PlanarConfiguration 1.  This is necessary

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -