2 * Copyright (C) 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #define LOG_TAG "EmulatedCamera_JPEGStub"
20 #include <cutils/log.h>
21 #include <YuvToJpegEncoder.h>
26 extern "C" void JpegStub_init(JpegStub* stub, int* strides) {
27 stub->mInternalEncoder = (void*) new Yuv420SpToJpegEncoder(strides);
28 stub->mInternalStream = (void*)new SkDynamicMemoryWStream();
31 extern "C" void JpegStub_cleanup(JpegStub* stub) {
32 delete((Yuv420SpToJpegEncoder*)stub->mInternalEncoder);
33 delete((SkDynamicMemoryWStream*)stub->mInternalStream);
36 extern "C" int JpegStub_compress(JpegStub* stub, const void* image,
37 int width, int height, int quality)
39 void* pY = const_cast<void*>(image);
42 offsets[1] = width * height;
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());
54 ALOGE("%s: JPEG compression failed", __FUNCTION__);
55 return errno ? errno: EINVAL;
59 extern "C" void JpegStub_getCompressedImage(JpegStub* stub, void* buff) {
60 SkDynamicMemoryWStream* stream =
61 (SkDynamicMemoryWStream*)stub->mInternalStream;
65 extern "C" size_t JpegStub_getCompressedSize(JpegStub* stub) {
66 SkDynamicMemoryWStream* stream =
67 (SkDynamicMemoryWStream*)stub->mInternalStream;
68 return stream->getOffset();