1 /* 2 * Copyright 2015 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #ifndef SkJpegDecoderMgr_DEFINED 9 #define SkJpegDecoderMgr_DEFINED 10 11 #include "include/codec/SkCodec.h" 12 #include "include/private/SkEncodedInfo.h" 13 #include "include/private/base/SkNoncopyable.h" 14 #include "src/codec/SkJpegPriv.h" 15 #include "src/codec/SkJpegSourceMgr.h" 16 17 extern "C" { 18 #include "jpeglib.h" 19 #include "jmorecfg.h" 20 } 21 22 #include <memory> 23 24 class SkStream; 25 26 class JpegDecoderMgr : SkNoncopyable { 27 public: 28 29 /* 30 * Print a useful error message and return false 31 */ 32 bool returnFalse(const char caller[]); 33 34 /* 35 * Print a useful error message and return a decode failure 36 */ 37 SkCodec::Result returnFailure(const char caller[], SkCodec::Result result); 38 39 /* 40 * Create the decode manager 41 * Does not take ownership of stream 42 */ 43 JpegDecoderMgr(SkStream* stream); 44 45 /* 46 * Initialize decompress struct 47 * Initialize the source manager 48 */ 49 void init(); 50 51 /* 52 * Returns true if it successfully sets outColor to the encoded color, 53 * and false otherwise. 54 */ 55 bool getEncodedColor(SkEncodedInfo::Color* outColor); 56 57 /* 58 * Free memory used by the decode manager 59 */ 60 ~JpegDecoderMgr(); 61 62 /* 63 * Get the skjpeg_error_mgr in order to set an error return jmp_buf 64 */ errorMgr()65 skjpeg_error_mgr* errorMgr() { return &fErrorMgr; } 66 67 /* 68 * Get function for the decompress info struct 69 */ dinfo()70 jpeg_decompress_struct* dinfo() { return &fDInfo; } 71 72 // Get the source manager. 73 SkJpegSourceMgr* getSourceMgr(); 74 75 private: 76 // Wrapper that calls into the full SkJpegSourceMgr interface. 77 struct SourceMgr : jpeg_source_mgr { 78 static void InitSource(j_decompress_ptr dinfo); 79 static void SkipInputData(j_decompress_ptr dinfo, long num_bytes_long); 80 static boolean FillInputBuffer(j_decompress_ptr dinfo); 81 static void TermSource(j_decompress_ptr dinfo); 82 83 SourceMgr(std::unique_ptr<SkJpegSourceMgr> mgr); 84 std::unique_ptr<SkJpegSourceMgr> fSourceMgr; 85 }; 86 87 jpeg_decompress_struct fDInfo; 88 SourceMgr fSrcMgr; 89 skjpeg_error_mgr fErrorMgr; 90 jpeg_progress_mgr fProgressMgr; 91 bool fInit; 92 }; 93 94 #endif 95