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 #include "Thumbnail.h"
18
19 #define LOG_NDEBUG 0
20 #define LOG_TAG "EmulatedCamera_Thumbnail"
21 #include <log/log.h>
22 #include <libexif/exif-data.h>
23 #include <libyuv.h>
24
25 #include "JpegCompressor.h"
26
27 #include <vector>
28
29 /*
30 * The YU12 format is a YUV format with an 8-bit Y-component and the U and V
31 * components are stored as 8 bits each but they are shared between a block of
32 * 2x2 pixels. So when calculating bits per pixel the 16 bits of U and V are
33 * shared between 4 pixels leading to 4 bits of U and V per pixel. Together
34 * with the 8 bits of Y this gives us 12 bits per pixel..
35 *
36 * The components are not grouped by pixels but separated into one Y-plane, one
37 * U-plane and one V-plane.
38 */
39
40 namespace android {
41
createRawThumbnail(const unsigned char * sourceImage,int sourceWidth,int sourceHeight,int thumbnailWidth,int thumbnailHeight,std::vector<unsigned char> * thumbnail)42 static bool createRawThumbnail(const unsigned char* sourceImage,
43 int sourceWidth, int sourceHeight,
44 int thumbnailWidth, int thumbnailHeight,
45 std::vector<unsigned char>* thumbnail) {
46 const unsigned char* ySourcePlane = sourceImage;
47 const unsigned char* uSourcePlane = sourceImage + sourceWidth * sourceHeight;
48 const unsigned char* vSourcePlane = uSourcePlane + sourceWidth * sourceHeight / 4;
49
50 // Create enough space in the output vector for the result
51 thumbnail->resize((thumbnailWidth * thumbnailHeight * 12) / 8);
52
53 // The downscaled U and V planes will also be linear instead of interleaved,
54 // allocate space for them here
55 const size_t destUVPlaneSize = (thumbnailWidth * thumbnailHeight) / 4;
56 std::vector<unsigned char> destPlanes(destUVPlaneSize * 2);
57 unsigned char* yDestPlane = &(*thumbnail)[0];
58 unsigned char* uDestPlane = yDestPlane + thumbnailWidth * thumbnailHeight;
59 unsigned char* vDestPlane = uDestPlane + thumbnailWidth * thumbnailHeight / 4;
60
61 // The strides for the U and V planes are half the width because the U and V
62 // components are common to 2x2 pixel blocks
63 int result = libyuv::I420Scale(ySourcePlane, sourceWidth,
64 uSourcePlane, sourceWidth / 2,
65 vSourcePlane, sourceWidth / 2,
66 sourceWidth, sourceHeight,
67 yDestPlane, thumbnailWidth,
68 uDestPlane, thumbnailWidth / 2,
69 vDestPlane, thumbnailWidth / 2,
70 thumbnailWidth, thumbnailHeight,
71 libyuv::kFilterBilinear);
72 if (result != 0) {
73 ALOGE("Unable to create thumbnail, downscaling failed with error: %d",
74 result);
75 return false;
76 }
77
78 return true;
79 }
80
createThumbnail(const unsigned char * sourceImage,int sourceWidth,int sourceHeight,int thumbWidth,int thumbHeight,int quality,ExifData * exifData)81 bool createThumbnail(const unsigned char* sourceImage,
82 int sourceWidth, int sourceHeight,
83 int thumbWidth, int thumbHeight, int quality,
84 ExifData* exifData) {
85 if (thumbWidth <= 0 || thumbHeight <= 0) {
86 ALOGE("%s: Invalid thumbnail width=%d or height=%d, must be > 0",
87 __FUNCTION__, thumbWidth, thumbHeight);
88 return false;
89 }
90
91 // First downscale the source image into a thumbnail-sized raw image
92 std::vector<unsigned char> rawThumbnail;
93 if (!createRawThumbnail(sourceImage, sourceWidth, sourceHeight,
94 thumbWidth, thumbHeight, &rawThumbnail)) {
95 // The thumbnail function will log an appropriate error if needed
96 return false;
97 }
98
99 // And then compress it into JPEG format without any EXIF data
100 NV21JpegCompressor compressor;
101 status_t result = compressor.compressRawImage(&rawThumbnail[0],
102 thumbWidth, thumbHeight,
103 quality, nullptr /* EXIF */);
104 if (result != NO_ERROR) {
105 ALOGE("%s: Unable to compress thumbnail", __FUNCTION__);
106 return false;
107 }
108
109 // And finally put it in the EXIF data. This transfers ownership of the
110 // malloc'd memory to the EXIF data structure. As long as the EXIF data
111 // structure is free'd using the EXIF library this memory will be free'd.
112 exifData->size = compressor.getCompressedSize();
113 exifData->data = reinterpret_cast<unsigned char*>(malloc(exifData->size));
114 if (exifData->data == nullptr) {
115 ALOGE("%s: Unable to allocate %u bytes of memory for thumbnail",
116 __FUNCTION__, exifData->size);
117 exifData->size = 0;
118 return false;
119 }
120 compressor.getCompressedImage(exifData->data);
121 return true;
122 }
123
124 } // namespace android
125
126