1 /*
2  * Copyright (C) 2013 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 #include "JpegStub.h"
18 
19 #define LOG_NDEBUG 0
20 #define LOG_TAG "EmulatedCamera_JPEGStub"
21 #include <errno.h>
22 #include <log/log.h>
23 #include <stdlib.h>
24 
25 #include "Compressor.h"
26 
JpegStub_init(JpegStub * stub)27 extern "C" void JpegStub_init(JpegStub* stub) {
28     stub->mCompressor = static_cast<void*>(new Compressor());
29 }
30 
JpegStub_cleanup(JpegStub * stub)31 extern "C" void JpegStub_cleanup(JpegStub* stub) {
32     delete reinterpret_cast<Compressor*>(stub->mCompressor);
33     stub->mCompressor = nullptr;
34 }
35 
JpegStub_compress(JpegStub * stub,const void * buffer,int width,int height,int quality,ExifData * exifData)36 extern "C" int JpegStub_compress(JpegStub* stub,
37                                  const void* buffer,
38                                  int width,
39                                  int height,
40                                  int quality,
41                                  ExifData* exifData)
42 {
43     Compressor* compressor = reinterpret_cast<Compressor*>(stub->mCompressor);
44 
45     if (compressor->compress(reinterpret_cast<const unsigned char*>(buffer),
46                               width, height, quality, exifData)) {
47         ALOGV("%s: Compressed JPEG: %d[%dx%d] -> %zu bytes",
48               __FUNCTION__, (width * height * 12) / 8,
49               width, height, compressor->getCompressedData().size());
50         return 0;
51     }
52     ALOGE("%s: JPEG compression failed", __FUNCTION__);
53     return errno ? errno : EINVAL;
54 }
55 
JpegStub_getCompressedImage(JpegStub * stub,void * buff)56 extern "C" void JpegStub_getCompressedImage(JpegStub* stub, void* buff) {
57     Compressor* compressor = reinterpret_cast<Compressor*>(stub->mCompressor);
58 
59     const std::vector<unsigned char>& data = compressor->getCompressedData();
60     memcpy(buff, &data[0], data.size());
61 }
62 
JpegStub_getCompressedSize(JpegStub * stub)63 extern "C" size_t JpegStub_getCompressedSize(JpegStub* stub) {
64     Compressor* compressor = reinterpret_cast<Compressor*>(stub->mCompressor);
65 
66     return compressor->getCompressedData().size();
67 }
68