Home | History | Annotate | Download | only in png
      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 // Functions to read and write images in PNG format.
     17 //
     18 // The advantage over image/codec/png{enc,dec}ocder.h is that this library
     19 // supports both 8 and 16 bit images.
     20 //
     21 // The decoding routine accepts binary image data as a StringPiece.  These are
     22 // implicitly constructed from strings or char* so they're completely
     23 // transparent to the caller.  They're also very cheap to construct so this
     24 // doesn't introduce any additional overhead.
     25 //
     26 // The primary benefit of StringPieces being, in this case, that APIs already
     27 // returning StringPieces (e.g., Bigtable Scanner) or Cords (e.g., IOBuffer;
     28 // only when they're flat, though) or protocol buffer fields typed to either of
     29 // these can be decoded without copying the data into a C++ string.
     30 
     31 #ifndef TENSORFLOW_LIB_PNG_PNG_IO_H_
     32 #define TENSORFLOW_LIB_PNG_PNG_IO_H_
     33 
     34 #include <string>
     35 #include <utility>
     36 #include <vector>
     37 
     38 #include "tensorflow/core/lib/core/stringpiece.h"
     39 #include "tensorflow/core/platform/png.h"
     40 
     41 namespace tensorflow {
     42 namespace png {
     43 
     44 // Handy container for decoding informations and struct pointers
     45 struct DecodeContext {
     46   const uint8* data;
     47   int data_left;
     48   png_structp png_ptr;
     49   png_infop info_ptr;
     50   png_uint_32 width, height;
     51   int num_passes;
     52   int color_type;
     53   int bit_depth;
     54   int channels;
     55   bool need_to_synthesize_16;
     56   bool error_condition;
     57   DecodeContext() : png_ptr(NULL), info_ptr(NULL) {}
     58 };
     59 
     60 bool DecodeHeader(StringPiece png_string, int* width, int* height,
     61                   int* components, int* channel_bit_depth,
     62                   std::vector<std::pair<string, string> >* metadata);
     63 
     64 // Sample usage for reading PNG:
     65 //
     66 // string png_string;  /* fill with input PNG format data */
     67 // DecodeContext context;
     68 // CHECK(CommonInitDecode(png_string, 3 /*RGB*/, 8 /*uint8*/, &context));
     69 // char* image_buffer = new char[3*context.width*context.height];
     70 // CHECK(CommonFinishDecode(bit_cast<png_byte*>(image_buffer),
     71 //       3*context.width /*stride*/, &context));
     72 //
     73 // desired_channels may be 0 to detected it from the input.
     74 
     75 bool CommonInitDecode(StringPiece png_string, int desired_channels,
     76                       int desired_channel_bits, DecodeContext* context);
     77 
     78 bool CommonFinishDecode(png_bytep data, int row_bytes, DecodeContext* context);
     79 
     80 // Normally called automatically from CommonFinishDecode.  If CommonInitDecode
     81 // is called but not CommonFinishDecode, call this to clean up.  Safe to call
     82 // extra times.
     83 void CommonFreeDecode(DecodeContext* context);
     84 
     85 // Sample usage for writing PNG:
     86 //
     87 // uint16* image_buffer = new uint16[width*height];  /* fill with pixels */
     88 // string png_string;
     89 // CHECK(WriteImageToBuffer(image_buffer, width, height, 2*width /*stride*/,
     90 //       1 /*gray*/, 16 /*uint16*/, &png_string, NULL));
     91 //
     92 // compression is in [-1,9], where 0 is fast and weak compression, 9 is slow
     93 // and strong, and -1 is the zlib default.
     94 
     95 bool WriteImageToBuffer(
     96     const void* image, int width, int height, int row_bytes, int num_channels,
     97     int channel_bits, int compression, string* png_string,
     98     const std::vector<std::pair<string, string> >* metadata);
     99 
    100 }  // namespace png
    101 }  // namespace tensorflow
    102 
    103 #endif  // TENSORFLOW_LIB_PNG_PNG_IO_H_
    104