Merge commit 'a9b046fb0ae5395c4d1d8a82cd0d3e354b5034ed'
authorMichael Niedermayer <michaelni@gmx.at>
Sun, 13 Apr 2014 01:45:45 +0000 (03:45 +0200)
committerMichael Niedermayer <michaelni@gmx.at>
Sun, 13 Apr 2014 01:45:45 +0000 (03:45 +0200)
* commit 'a9b046fb0ae5395c4d1d8a82cd0d3e354b5034ed':
  tiff: use a better name and enum values for PhotometricInterpretation

Conflicts:
libavcodec/tiff.c
libavcodec/tiff.h
libavcodec/tiffenc.c

Merged-by: Michael Niedermayer <michaelni@gmx.at>
1  2 
libavcodec/tiff.c
libavcodec/tiff.h
libavcodec/tiffenc.c

@@@ -53,8 -51,7 +53,8 @@@ typedef struct TiffContext 
      int palette_is_set;
      int le;
      enum TiffCompr compr;
-     int invert;
+     enum TiffPhotometric photometric;
 +    int planar;
      int fax_opts;
      int predictor;
      int fill_order;
@@@ -960,21 -546,28 +971,21 @@@ static int decode_frame(AVCodecContext 
      bytestream2_init(&s->gb, avpkt->data, avpkt->size);
  
      // parse image header
 -    if (avpkt->size < 8)
 -        return AVERROR_INVALIDDATA;
 -    id = bytestream2_get_le16(&s->gb);
 -    if (id == 0x4949)
 -        le = 1;
 -    else if (id == 0x4D4D)
 -        le = 0;
 -    else {
 -        av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
 +    if ((ret = ff_tdecode_header(&s->gb, &le, &off))) {
 +        av_log(avctx, AV_LOG_ERROR, "Invalid TIFF header\n");
 +        return ret;
 +    } else if (off >= UINT_MAX - 14 || avpkt->size < off + 14) {
 +        av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
          return AVERROR_INVALIDDATA;
      }
-     s->le         = le;
+     s->le          = le;
 +    // TIFF_BPP is not a required tag and defaults to 1
-     s->bppcount   = s->bpp = 1;
-     s->invert     = 0;
-     s->compr      = TIFF_RAW;
-     s->fill_order = 0;
++    s->bppcount    = s->bpp = 1;
+     s->photometric = TIFF_PHOTOMETRIC_NONE;
+     s->compr       = TIFF_RAW;
+     s->fill_order  = 0;
 -    // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
 -    // that further identifies the file as a TIFF file"
 -    if (tget_short(&s->gb, le) != 42) {
 -        av_log(avctx, AV_LOG_ERROR,
 -               "The answer to life, universe and everything is not correct!\n");
 -        return AVERROR_INVALIDDATA;
 -    }
 +    free_geotags(s);
 +
      // Reset these offsets so we can tell if they were set this frame
      s->stripsizesoff = s->strippos = 0;
      /* parse image file directory */
          }
      }
  
-     if (s->invert) {
+     if (s->photometric == TIFF_PHOTOMETRIC_WHITE_IS_ZERO) {
 -        dst = p->data[0];
 +        dst = p->data[plane];
          for (i = 0; i < s->height; i++) {
 -            for (j = 0; j < p->linesize[0]; j++)
 -                dst[j] = 255 - dst[j];
 +            for (j = 0; j < p->linesize[plane]; j++)
 +                dst[j] = (s->avctx->pix_fmt == AV_PIX_FMT_PAL8 ? (1<<s->bpp) - 1 : 255) - dst[j];
              dst += stride;
          }
      }
@@@ -40,12 -37,8 +40,12 @@@ enum TiffTags 
      TIFF_HEIGHT,
      TIFF_BPP,
      TIFF_COMPR,
-     TIFF_INVERT             = 0x106,
+     TIFF_PHOTOMETRIC        = 0x106,
      TIFF_FILL_ORDER         = 0x10A,
 +    TIFF_DOCUMENT_NAME      = 0x10D,
 +    TIFF_IMAGE_DESCRIPTION  = 0x10E,
 +    TIFF_MAKE               = 0x10F,
 +    TIFF_MODEL              = 0x110,
      TIFF_STRIP_OFFS         = 0x111,
      TIFF_SAMPLES_PER_PIXEL  = 0x115,
      TIFF_ROWSPERSTRIP       = 0x116,
@@@ -98,76 -74,35 +98,94 @@@ enum TiffCompr 
      TIFF_DEFLATE  = 0x80B2
  };
  
 -enum TiffTypes {
 -    TIFF_BYTE = 1,
 -    TIFF_STRING,
 -    TIFF_SHORT,
 -    TIFF_LONG,
 -    TIFF_RATIONAL,
 +enum TiffGeoTagKey {
 +    TIFF_GT_MODEL_TYPE_GEOKEY                = 1024,
 +    TIFF_GT_RASTER_TYPE_GEOKEY               = 1025,
 +    TIFF_GT_CITATION_GEOKEY                  = 1026,
 +    TIFF_GEOGRAPHIC_TYPE_GEOKEY              = 2048,
 +    TIFF_GEOG_CITATION_GEOKEY                = 2049,
 +    TIFF_GEOG_GEODETIC_DATUM_GEOKEY          = 2050,
 +    TIFF_GEOG_PRIME_MERIDIAN_GEOKEY          = 2051,
 +    TIFF_GEOG_LINEAR_UNITS_GEOKEY            = 2052,
 +    TIFF_GEOG_LINEAR_UNIT_SIZE_GEOKEY        = 2053,
 +    TIFF_GEOG_ANGULAR_UNITS_GEOKEY           = 2054,
 +    TIFF_GEOG_ANGULAR_UNIT_SIZE_GEOKEY       = 2055,
 +    TIFF_GEOG_ELLIPSOID_GEOKEY               = 2056,
 +    TIFF_GEOG_SEMI_MAJOR_AXIS_GEOKEY         = 2057,
 +    TIFF_GEOG_SEMI_MINOR_AXIS_GEOKEY         = 2058,
 +    TIFF_GEOG_INV_FLATTENING_GEOKEY          = 2059,
 +    TIFF_GEOG_AZIMUTH_UNITS_GEOKEY           = 2060,
 +    TIFF_GEOG_PRIME_MERIDIAN_LONG_GEOKEY     = 2061,
 +    TIFF_PROJECTED_CS_TYPE_GEOKEY            = 3072,
 +    TIFF_PCS_CITATION_GEOKEY                 = 3073,
 +    TIFF_PROJECTION_GEOKEY                   = 3074,
 +    TIFF_PROJ_COORD_TRANS_GEOKEY             = 3075,
 +    TIFF_PROJ_LINEAR_UNITS_GEOKEY            = 3076,
 +    TIFF_PROJ_LINEAR_UNIT_SIZE_GEOKEY        = 3077,
 +    TIFF_PROJ_STD_PARALLEL1_GEOKEY           = 3078,
 +    TIFF_PROJ_STD_PARALLEL2_GEOKEY           = 3079,
 +    TIFF_PROJ_NAT_ORIGIN_LONG_GEOKEY         = 3080,
 +    TIFF_PROJ_NAT_ORIGIN_LAT_GEOKEY          = 3081,
 +    TIFF_PROJ_FALSE_EASTING_GEOKEY           = 3082,
 +    TIFF_PROJ_FALSE_NORTHING_GEOKEY          = 3083,
 +    TIFF_PROJ_FALSE_ORIGIN_LONG_GEOKEY       = 3084,
 +    TIFF_PROJ_FALSE_ORIGIN_LAT_GEOKEY        = 3085,
 +    TIFF_PROJ_FALSE_ORIGIN_EASTING_GEOKEY    = 3086,
 +    TIFF_PROJ_FALSE_ORIGIN_NORTHING_GEOKEY   = 3087,
 +    TIFF_PROJ_CENTER_LONG_GEOKEY             = 3088,
 +    TIFF_PROJ_CENTER_LAT_GEOKEY              = 3089,
 +    TIFF_PROJ_CENTER_EASTING_GEOKEY          = 3090,
 +    TIFF_PROJ_CENTER_NORTHING_GEOKEY         = 3091,
 +    TIFF_PROJ_SCALE_AT_NAT_ORIGIN_GEOKEY     = 3092,
 +    TIFF_PROJ_SCALE_AT_CENTER_GEOKEY         = 3093,
 +    TIFF_PROJ_AZIMUTH_ANGLE_GEOKEY           = 3094,
 +    TIFF_PROJ_STRAIGHT_VERT_POLE_LONG_GEOKEY = 3095,
 +    TIFF_VERTICAL_CS_TYPE_GEOKEY             = 4096,
 +    TIFF_VERTICAL_CITATION_GEOKEY            = 4097,
 +    TIFF_VERTICAL_DATUM_GEOKEY               = 4098,
 +    TIFF_VERTICAL_UNITS_GEOKEY               = 4099
  };
  
 -/** sizes of various TIFF field types (string size = 100)*/
 -static const uint8_t type_sizes[6] = {
 -    0, 1, 100, 2, 4, 8
+ enum TiffPhotometric {
+     TIFF_PHOTOMETRIC_NONE       = -1,
+     TIFF_PHOTOMETRIC_WHITE_IS_ZERO,      /* mono or grayscale, 0 is white */
+     TIFF_PHOTOMETRIC_BLACK_IS_ZERO,      /* mono or grayscale, 0 is black */
+     TIFF_PHOTOMETRIC_RGB,                /* RGB or RGBA*/
+     TIFF_PHOTOMETRIC_PALETTE,            /* Uses a palette */
+     TIFF_PHOTOMETRIC_ALPHA_MASK,         /* Transparency mask */
+     TIFF_PHOTOMETRIC_SEPARATED,          /* CMYK or some other ink set */
+     TIFF_PHOTOMETRIC_YCBCR,              /* YCbCr */
+     TIFF_PHOTOMETRIC_CIE_LAB    = 8,     /* 1976 CIE L*a*b* */
+     TIFF_PHOTOMETRIC_ICC_LAB,            /* ICC L*a*b* */
+     TIFF_PHOTOMETRIC_ITU_LAB,            /* ITU L*a*b* */
+     TIFF_PHOTOMETRIC_CFA        = 32803, /* Color Filter Array (DNG) */
+     TIFF_PHOTOMETRIC_LOG_L      = 32844, /* CIE Log2(L) */
+     TIFF_PHOTOMETRIC_LOG_LUV,            /* CIE Log L*u*v* */
+     TIFF_PHOTOMETRIC_LINEAR_RAW = 34892, /* Linear Raw (DNG) */
+ };
 +enum TiffGeoTagType {
 +    GEOTIFF_SHORT  = 0,
 +    GEOTIFF_DOUBLE = 34736,
 +    GEOTIFF_STRING = 34737
  };
  
 +typedef struct TiffGeoTag {
 +    enum TiffGeoTagKey key;
 +    enum TiffTags type;
 +    int count;
 +    int offset;
 +    char *val;
 +} TiffGeoTag;
 +
 +typedef struct TiffGeoTagKeyName {
 +    const enum TiffGeoTagKey key;
 +    const char *const name;
 +} TiffGeoTagKeyName;
 +
 +typedef struct TiffGeoTagNameType {
 +    const char *const name;
 +    const enum TiffGeoTagType type;
 +} TiffGeoTagNameType;
 +
  #endif /* AVCODEC_TIFF_H */
@@@ -58,14 -56,8 +58,14 @@@ typedef struct TiffEncoderContext 
      unsigned int bpp;                       ///< bits per pixel
      int compr;                              ///< compression level
      int bpp_tab_size;                       ///< bpp_tab size
-     int photometric_interpretation;         ///< photometric interpretation
+     enum TiffPhotometric photometric_interpretation;  ///< photometric interpretation
      int strips;                             ///< number of strips
 +    uint32_t *strip_sizes;
 +    unsigned int strip_sizes_size;
 +    uint32_t *strip_offsets;
 +    unsigned int strip_offsets_size;
 +    uint8_t *yuv_line;
 +    unsigned int yuv_line_size;
      int rps;                                ///< row per strip
      uint8_t entries[TIFF_MAX_ENTRY * 12];   ///< entries in header
      int num_entries;                        ///< number of entries
@@@ -244,31 -227,34 +244,31 @@@ static int encode_frame(AVCodecContext 
      s->subsampling[0] = 1;
      s->subsampling[1] = 1;
  
 +    avctx->bits_per_coded_sample =
 +    s->bpp          = av_get_bits_per_pixel(desc);
 +    s->bpp_tab_size = desc->nb_components;
 +
      switch (avctx->pix_fmt) {
 -    case AV_PIX_FMT_RGB48LE:
 -    case AV_PIX_FMT_GRAY16LE:
 +    case AV_PIX_FMT_RGBA64LE:
      case AV_PIX_FMT_RGBA:
 +        alpha = 1;
 +    case AV_PIX_FMT_RGB48LE:
      case AV_PIX_FMT_RGB24:
-         s->photometric_interpretation = 2;
 -    case AV_PIX_FMT_GRAY8:
 -    case AV_PIX_FMT_PAL8:
 -        pfd    = av_pix_fmt_desc_get(avctx->pix_fmt);
 -        s->bpp = av_get_bits_per_pixel(pfd);
 -        if (pfd->flags & AV_PIX_FMT_FLAG_PAL)
 -            s->photometric_interpretation = TIFF_PHOTOMETRIC_PALETTE;
 -        else if (pfd->flags & AV_PIX_FMT_FLAG_RGB)
 -            s->photometric_interpretation = TIFF_PHOTOMETRIC_RGB;
 -        else
 -            s->photometric_interpretation = TIFF_PHOTOMETRIC_BLACK_IS_ZERO;
 -        s->bpp_tab_size = pfd->nb_components;
 -        for (i = 0; i < s->bpp_tab_size; i++)
 -            bpp_tab[i] = s->bpp / s->bpp_tab_size;
++        s->photometric_interpretation = TIFF_PHOTOMETRIC_RGB;
          break;
 +    case AV_PIX_FMT_GRAY8:
 +        avctx->bits_per_coded_sample = 0x28;
 +    case AV_PIX_FMT_GRAY8A:
 +        alpha = avctx->pix_fmt == AV_PIX_FMT_GRAY8A;
 +    case AV_PIX_FMT_GRAY16LE:
      case AV_PIX_FMT_MONOBLACK:
-         s->photometric_interpretation = 1;
 -        s->bpp                        = 1;
+         s->photometric_interpretation = TIFF_PHOTOMETRIC_BLACK_IS_ZERO;
 -        s->bpp_tab_size               = 0;
 +        break;
 +    case AV_PIX_FMT_PAL8:
-         s->photometric_interpretation = 3;
++        s->photometric_interpretation = TIFF_PHOTOMETRIC_PALETTE;
          break;
      case AV_PIX_FMT_MONOWHITE:
-         s->photometric_interpretation = 0;
 -        s->bpp                        = 1;
+         s->photometric_interpretation = TIFF_PHOTOMETRIC_WHITE_IS_ZERO;
 -        s->bpp_tab_size               = 0;
          break;
      case AV_PIX_FMT_YUV420P:
      case AV_PIX_FMT_YUV422P:
      case AV_PIX_FMT_YUV410P:
      case AV_PIX_FMT_YUV411P:
          av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &shift_h, &shift_v);
-         s->photometric_interpretation = 6;
+         s->photometric_interpretation = TIFF_PHOTOMETRIC_YCBCR;
 -        s->bpp                        = 8 + (16 >> (shift_h + shift_v));
          s->subsampling[0]             = 1 << shift_h;
          s->subsampling[1]             = 1 << shift_v;
 -        s->bpp_tab_size               = 3;
          is_yuv                        = 1;
          break;
      default:
      if (s->bpp_tab_size)
          add_entry(s, TIFF_BPP, TIFF_SHORT, s->bpp_tab_size, bpp_tab);
  
-     add_entry1(s, TIFF_COMPR,      TIFF_SHORT, s->compr);
-     add_entry1(s, TIFF_INVERT,     TIFF_SHORT, s->photometric_interpretation);
-     add_entry(s,  TIFF_STRIP_OFFS, TIFF_LONG,  strips, s->strip_offsets);
+     add_entry1(s, TIFF_COMPR,       TIFF_SHORT, s->compr);
+     add_entry1(s, TIFF_PHOTOMETRIC, TIFF_SHORT, s->photometric_interpretation);
 -    add_entry(s,  TIFF_STRIP_OFFS,  TIFF_LONG,  strips, strip_offsets);
++    add_entry(s,  TIFF_STRIP_OFFS,  TIFF_LONG,  strips, s->strip_offsets);
  
      if (s->bpp_tab_size)
          add_entry1(s, TIFF_SAMPLES_PER_PIXEL, TIFF_SHORT, s->bpp_tab_size);