Home | History | Annotate | Download | only in jpeg
      1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
      2 
      3 Licensed under the Apache License, Version 2.0 (the "License");
      4 you may not use this file except in compliance with the License.
      5 You may obtain a copy of the License at
      6 
      7     http://www.apache.org/licenses/LICENSE-2.0
      8 
      9 Unless required by applicable law or agreed to in writing, software
     10 distributed under the License is distributed on an "AS IS" BASIS,
     11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 See the License for the specific language governing permissions and
     13 limitations under the License.
     14 ==============================================================================*/
     15 
     16 // This file defines functions to compress and uncompress JPEG files
     17 // to and from memory.  It provides interfaces for raw images
     18 // (data array and size fields).
     19 // Direct manipulation of JPEG strings are supplied: Flip, Rotate, Crop..
     20 
     21 #ifndef TENSORFLOW_LIB_JPEG_JPEG_MEM_H_
     22 #define TENSORFLOW_LIB_JPEG_JPEG_MEM_H_
     23 
     24 #include <functional>
     25 #include <string>
     26 
     27 #include "tensorflow/core/lib/core/stringpiece.h"
     28 #include "tensorflow/core/platform/jpeg.h"
     29 #include "tensorflow/core/platform/types.h"
     30 
     31 namespace tensorflow {
     32 namespace jpeg {
     33 
     34 // Flags for Uncompress
     35 struct UncompressFlags {
     36   // ratio can be 1, 2, 4, or 8 and represent the denominator for the scaling
     37   // factor (eg ratio = 4 means that the resulting image will be at 1/4 original
     38   // size in both directions).
     39   int ratio = 1;
     40 
     41   // The number of bytes per pixel (1, 3 or 4), or 0 for autodetect.
     42   int components = 0;
     43 
     44   // If true, decoder will use a slower but nicer upscaling of the chroma
     45   // planes (yuv420/422 only).
     46   bool fancy_upscaling = true;
     47 
     48   // If true, will attempt to fill in missing lines of truncated files
     49   bool try_recover_truncated_jpeg = false;
     50 
     51   // The minimum required fraction of lines read before the image is accepted.
     52   float min_acceptable_fraction = 1.0;
     53 
     54   // The distance in bytes from one scanline to the other.  Should be at least
     55   // equal to width*components*sizeof(JSAMPLE).  If 0 is passed, the stride
     56   // used will be this minimal value.
     57   int stride = 0;
     58 
     59   // Setting of J_DCT_METHOD enum in jpeglib.h, for choosing which
     60   // algorithm to use for DCT/IDCT.
     61   //
     62   // Setting this has a quality/speed trade-off implication.
     63   J_DCT_METHOD dct_method = JDCT_DEFAULT;
     64 
     65   // Settings of crop window before decompression.
     66   bool crop = false;
     67   // Vertical coordinate of the top-left corner of the result in the input.
     68   int crop_x = 0;
     69   // Horizontal coordinate of the top-left corner of the result in the input.
     70   int crop_y = 0;
     71   // Width of the output image.
     72   int crop_width = 0;
     73   // Height of the output image.
     74   int crop_height = 0;
     75 };
     76 
     77 // Uncompress some raw JPEG data given by the pointer srcdata and the length
     78 // datasize.
     79 // - width and height are the address where to store the size of the
     80 //   uncompressed image in pixels.  May be nullptr.
     81 // - components is the address where the number of read components are
     82 //   stored.  This is *output only*: to request a specific number of
     83 //   components use flags.components.  May be nullptr.
     84 // - nwarn is the address in which to store the number of warnings.
     85 //   May be nullptr.
     86 // The function returns a pointer to the raw uncompressed data or NULL if
     87 // there was an error. The caller of the function is responsible for
     88 // freeing the memory (using delete []).
     89 uint8* Uncompress(const void* srcdata, int datasize,
     90                   const UncompressFlags& flags, int* width, int* height,
     91                   int* components,  // Output only: useful with autodetect
     92                   int64* nwarn);
     93 
     94 // Version of Uncompress that allocates memory via a callback.  The callback
     95 // arguments are (width, height, components).  If the size is known ahead of
     96 // time this function can return an existing buffer; passing a callback allows
     97 // the buffer to be shaped based on the JPEG header.  The caller is responsible
     98 // for freeing the memory *even along error paths*.
     99 uint8* Uncompress(const void* srcdata, int datasize,
    100                   const UncompressFlags& flags, int64* nwarn,
    101                   std::function<uint8*(int, int, int)> allocate_output);
    102 
    103 // Read jpeg header and get image information.  Returns true on success.
    104 // The width, height, and components points may be null.
    105 bool GetImageInfo(const void* srcdata, int datasize, int* width, int* height,
    106                   int* components);
    107 
    108 // Note: (format & 0xff) = number of components (<=> bytes per pixels)
    109 enum Format {
    110   FORMAT_GRAYSCALE = 0x001,  // 1 byte/pixel
    111   FORMAT_RGB = 0x003,        // 3 bytes/pixel RGBRGBRGBRGB...
    112   FORMAT_RGBA = 0x004,       // 4 bytes/pixel RGBARGBARGBARGBA...
    113   FORMAT_ABGR = 0x104        // 4 bytes/pixel ABGRABGRABGR...
    114 };
    115 
    116 // Flags for compression
    117 struct CompressFlags {
    118   // Encoding of the input data for compression
    119   Format format;
    120 
    121   // Quality of the compression from 0-100
    122   int quality = 95;
    123 
    124   // If true, create a jpeg image that loads progressively
    125   bool progressive = false;
    126 
    127   // If true, reduce jpeg size without changing quality (at the cost of CPU/RAM)
    128   bool optimize_jpeg_size = false;
    129 
    130   // See http://en.wikipedia.org/wiki/Chroma_subsampling
    131   bool chroma_downsampling = true;
    132 
    133   // Resolution
    134   int density_unit = 1;  // 1 = in, 2 = cm
    135   int x_density = 300;
    136   int y_density = 300;
    137 
    138   // If not empty, embed this XMP metadata in the image header
    139   StringPiece xmp_metadata;
    140 
    141   // The distance in bytes from one scanline to the other.  Should be at least
    142   // equal to width*components*sizeof(JSAMPLE).  If 0 is passed, the stride
    143   // used will be this minimal value.
    144   int stride = 0;
    145 };
    146 
    147 // Compress some raw image given in srcdata, the data is a 2D array of size
    148 // stride*height with one of the formats enumerated above.
    149 // The encoded data is returned as a string.
    150 // If not empty, XMP metadata can be embedded in the image header
    151 // On error, returns the empty string (which is never a valid jpeg).
    152 string Compress(const void* srcdata, int width, int height,
    153                 const CompressFlags& flags);
    154 
    155 // On error, returns false and sets output to empty.
    156 bool Compress(const void* srcdata, int width, int height,
    157               const CompressFlags& flags, string* output);
    158 
    159 }  // namespace jpeg
    160 }  // namespace tensorflow
    161 
    162 #endif  // TENSORFLOW_LIB_JPEG_JPEG_MEM_H_
    163