1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef GOLDFISH_CAMERA_JPEG_STUB_COMPRESSOR_H
18 #define GOLDFISH_CAMERA_JPEG_STUB_COMPRESSOR_H
19 
20 #include <setjmp.h>
21 #include <stdlib.h>
22 extern "C" {
23 #include <jpeglib.h>
24 #include <jerror.h>
25 }
26 
27 #include <vector>
28 
29 struct _ExifData;
30 typedef _ExifData ExifData;
31 
32 class Compressor {
33 public:
34     Compressor();
35 
36     /* Compress |data| which represents raw NV21 encoded data of dimensions
37      * |width| * |height|. |exifData| is optional EXIF data that will be
38      * attached to the compressed data if present, set to null if not needed.
39      */
40     bool compress(const unsigned char* data,
41                   int width, int height, int quality,
42                   ExifData* exifData);
43 
44     /* Get a reference to the compressed data, this will return an empty vector
45      * if compress has not been called yet
46      */
47     const std::vector<unsigned char>& getCompressedData() const;
48 
49 private:
50     struct DestinationManager : jpeg_destination_mgr {
51         DestinationManager();
52 
53         static void initDestination(j_compress_ptr cinfo);
54         static boolean emptyOutputBuffer(j_compress_ptr cinfo);
55         static void termDestination(j_compress_ptr cinfo);
56 
57         std::vector<unsigned char> mBuffer;
58     };
59     struct ErrorManager : jpeg_error_mgr {
60         ErrorManager();
61 
62         static void onJpegError(j_common_ptr cinfo);
63 
64         jmp_buf mJumpBuffer;
65     };
66 
67     jpeg_compress_struct mCompressInfo;
68     DestinationManager mDestManager;
69     ErrorManager mErrorManager;
70 
71     bool configureCompressor(int width, int height, int quality);
72     bool compressData(const unsigned char* data, ExifData* exifData);
73     bool attachExifData(ExifData* exifData);
74 };
75 
76 #endif  // GOLDFISH_CAMERA_JPEG_STUB_COMPRESSOR_H
77 
78