TYPE3
[iec.git] / src / type3_AndroidCloud / anbox-master / android / camera / JpegStub.cpp
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 #define LOG_NDEBUG 0
18 #define LOG_TAG "EmulatedCamera_JPEGStub"
19 #include <errno.h>
20 #include <cutils/log.h>
21 #include <YuvToJpegEncoder.h>
22
23 #include "JpegStub.h"
24
25
26 extern "C" void JpegStub_init(JpegStub* stub, int* strides) {
27     stub->mInternalEncoder = (void*) new Yuv420SpToJpegEncoder(strides);
28     stub->mInternalStream = (void*)new SkDynamicMemoryWStream();
29 }
30
31 extern "C" void JpegStub_cleanup(JpegStub* stub) {
32     delete((Yuv420SpToJpegEncoder*)stub->mInternalEncoder);
33     delete((SkDynamicMemoryWStream*)stub->mInternalStream);
34 }
35
36 extern "C" int JpegStub_compress(JpegStub* stub, const void* image,
37         int width, int height, int quality)
38 {
39     void* pY = const_cast<void*>(image);
40     int offsets[2];
41     offsets[0] = 0;
42     offsets[1] = width * height;
43
44     Yuv420SpToJpegEncoder* encoder =
45         (Yuv420SpToJpegEncoder*)stub->mInternalEncoder;
46     SkDynamicMemoryWStream* stream =
47         (SkDynamicMemoryWStream*)stub->mInternalStream;
48     if (encoder->encode(stream, pY, width, height, offsets, quality)) {
49         ALOGV("%s: Compressed JPEG: %d[%dx%d] -> %zu bytes",
50               __FUNCTION__, (width * height * 12) / 8,
51               width, height, stream->getOffset());
52         return 0;
53     } else {
54         ALOGE("%s: JPEG compression failed", __FUNCTION__);
55         return errno ? errno: EINVAL;
56     }
57 }
58
59 extern "C" void JpegStub_getCompressedImage(JpegStub* stub, void* buff) {
60     SkDynamicMemoryWStream* stream =
61         (SkDynamicMemoryWStream*)stub->mInternalStream;
62     stream->copyTo(buff);
63 }
64
65 extern "C" size_t JpegStub_getCompressedSize(JpegStub* stub) {
66     SkDynamicMemoryWStream* stream =
67         (SkDynamicMemoryWStream*)stub->mInternalStream;
68     return stream->getOffset();
69 }