1 /*
2 * Copyright (C) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "jpeg_decoder.h"
17 #include <map>
18 #include "hitrace_meter.h"
19 #include "image_log.h"
20 #include "image_trace.h"
21 #include "image_utils.h"
22 #include "jerror.h"
23 #include "media_errors.h"
24 #include "string_ex.h"
25 #if !defined(IOS_PLATFORM) && !defined(A_PLATFORM)
26 #include "surface_buffer.h"
27 #endif
28
29 #ifndef _WIN32
30 #include "securec.h"
31 #else
32 #include "memory.h"
33 #endif
34
35 #undef LOG_DOMAIN
36 #define LOG_DOMAIN LOG_TAG_DOMAIN_ID_PLUGIN
37
38 #undef LOG_TAG
39 #define LOG_TAG "JpegDecoder"
40
41 namespace OHOS {
42 namespace ImagePlugin {
43 using namespace MultimediaPlugin;
44 using namespace Media;
45 static constexpr uint32_t PL_ICC_MARKER = JPEG_APP0 + 2;
46 static constexpr uint32_t PL_MARKER_LENGTH_LIMIT = 0xFFFF;
47 namespace {
48 constexpr uint32_t NUM_100 = 100;
49 constexpr uint32_t PIXEL_BYTES_RGB_565 = 2;
50 constexpr uint32_t MARKER_SIZE = 2;
51 constexpr uint32_t MARKER_LENGTH = 2;
52 constexpr uint8_t MARKER_LENGTH_0_OFFSET = 0;
53 constexpr uint8_t MARKER_LENGTH_1_OFFSET = 1;
54 constexpr uint32_t MARKER_LENGTH_SHIFT = 8;
55 constexpr uint8_t JPG_MARKER_PREFIX_OFFSET = 0;
56 constexpr uint8_t JPG_MARKER_CODE_OFFSET = 1;
57 constexpr uint8_t JPG_MARKER_PREFIX = 0XFF;
58 constexpr uint8_t JPG_MARKER_SOI = 0XD8;
59 constexpr uint8_t JPG_MARKER_SOS = 0XDA;
60 constexpr uint8_t JPG_MARKER_RST = 0XD0;
61 constexpr uint8_t JPG_MARKER_RST0 = 0XD0;
62 constexpr uint8_t JPG_MARKER_RSTN = 0XD7;
63 constexpr uint8_t JPG_MARKER_APP = 0XE0;
64 constexpr uint8_t JPG_MARKER_APP0 = 0XE0;
65 constexpr uint8_t JPG_MARKER_APPN = 0XEF;
66 constexpr size_t TIMES_LEN = 19;
67 constexpr size_t DATE_LEN = 10;
68 constexpr float SCALES[] = { 0.1875f, 0.3125f, 0.4375f, 0.5625f, 0.6875f, 0.8125f, 0.9375f, 1.0f };
69 constexpr int SCALE_NUMS[] = { 2, 3, 4, 5, 6, 7, 8, 8 };
70 constexpr int SCALE_NUMS_LENGTH = 7;
71 const std::string BITS_PER_SAMPLE = "BitsPerSample";
72 const std::string ORIENTATION = "Orientation";
73 const std::string IMAGE_LENGTH = "ImageLength";
74 const std::string IMAGE_WIDTH = "ImageWidth";
75 const std::string GPS_LATITUDE = "GPSLatitude";
76 const std::string GPS_LONGITUDE = "GPSLongitude";
77 const std::string GPS_LATITUDE_REF = "GPSLatitudeRef";
78 const std::string GPS_LONGITUDE_REF = "GPSLongitudeRef";
79 const std::string DATE_TIME_ORIGINAL = "DateTimeOriginal";
80 const std::string DATE_TIME_ORIGINAL_MEDIA = "DateTimeOriginalForMedia";
81 const std::string EXPOSURE_TIME = "ExposureTime";
82 const std::string F_NUMBER = "FNumber";
83 const std::string ISO_SPEED_RATINGS = "ISOSpeedRatings";
84 const std::string SCENE_TYPE = "SceneType";
85 const std::string COMPRESSED_BITS_PER_PIXEL = "CompressedBitsPerPixel";
86 const std::string DATE_TIME = "DateTime";
87 const std::string GPS_TIME_STAMP = "GPSTimeStamp";
88 const std::string GPS_DATE_STAMP = "GPSDateStamp";
89 const std::string IMAGE_DESCRIPTION = "ImageDescription";
90 const std::string MAKE = "Make";
91 const std::string MODEL = "Model";
92 const std::string PHOTO_MODE = "PhotoMode";
93 const std::string SENSITIVITY_TYPE = "SensitivityType";
94 const std::string STANDARD_OUTPUT_SENSITIVITY = "StandardOutputSensitivity";
95 const std::string RECOMMENDED_EXPOSURE_INDEX = "RecommendedExposureIndex";
96 const std::string ISO_SPEED = "ISOSpeedRatings";
97 const std::string APERTURE_VALUE = "ApertureValue";
98 const std::string EXPOSURE_BIAS_VALUE = "ExposureBiasValue";
99 const std::string METERING_MODE = "MeteringMode";
100 const std::string LIGHT_SOURCE = "LightSource";
101 const std::string FLASH = "Flash";
102 const std::string FOCAL_LENGTH = "FocalLength";
103 const std::string USER_COMMENT = "UserComment";
104 const std::string PIXEL_X_DIMENSION = "PixelXDimension";
105 const std::string PIXEL_Y_DIMENSION = "PixelYDimension";
106 const std::string WHITE_BALANCE = "WhiteBalance";
107 const std::string FOCAL_LENGTH_IN_35_MM_FILM = "FocalLengthIn35mmFilm";
108 const std::string HW_MNOTE_CAPTURE_MODE = "HwMnoteCaptureMode";
109 const std::string HW_MNOTE_PHYSICAL_APERTURE = "HwMnotePhysicalAperture";
110 const std::string HW_MNOTE_TAG_ROLL_ANGLE = "HwMnoteRollAngle";
111 const std::string HW_MNOTE_TAG_PITCH_ANGLE = "HwMnotePitchAngle";
112 const std::string HW_MNOTE_TAG_SCENE_FOOD_CONF = "HwMnoteSceneFoodConf";
113 const std::string HW_MNOTE_TAG_SCENE_STAGE_CONF = "HwMnoteSceneStageConf";
114 const std::string HW_MNOTE_TAG_SCENE_BLUE_SKY_CONF = "HwMnoteSceneBlueSkyConf";
115 const std::string HW_MNOTE_TAG_SCENE_GREEN_PLANT_CONF = "HwMnoteSceneGreenPlantConf";
116 const std::string HW_MNOTE_TAG_SCENE_BEACH_CONF = "HwMnoteSceneBeachConf";
117 const std::string HW_MNOTE_TAG_SCENE_SNOW_CONF = "HwMnoteSceneSnowConf";
118 const std::string HW_MNOTE_TAG_SCENE_SUNSET_CONF = "HwMnoteSceneSunsetConf";
119 const std::string HW_MNOTE_TAG_SCENE_FLOWERS_CONF = "HwMnoteSceneFlowersConf";
120 const std::string HW_MNOTE_TAG_SCENE_NIGHT_CONF = "HwMnoteSceneNightConf";
121 const std::string HW_MNOTE_TAG_SCENE_TEXT_CONF = "HwMnoteSceneTextConf";
122 const std::string HW_MNOTE_TAG_FACE_COUNT = "HwMnoteFaceCount";
123 const std::string HW_MNOTE_TAG_FOCUS_MODE = "HwMnoteFocusMode";
124
125 static const std::map<std::string, uint32_t> PROPERTY_INT = {
126 {"Top-left", 0},
127 {"Bottom-right", 180},
128 {"Right-top", 90},
129 {"Left-bottom", 270},
130 };
131 constexpr uint32_t JPEG_APP1_SIZE = 2;
132 constexpr uint32_t ADDRESS_4 = 4;
133 constexpr int OFFSET_8 = 8;
134 } // namespace
135
136 PluginServer &JpegDecoder::pluginServer_ = DelayedRefSingleton<PluginServer>::GetInstance();
137
JpegSrcMgr(InputDataStream * stream)138 JpegSrcMgr::JpegSrcMgr(InputDataStream *stream) : inputStream(stream)
139 {
140 init_source = InitSrcStream;
141 fill_input_buffer = FillInputBuffer;
142 skip_input_data = SkipInputData;
143 resync_to_restart = jpeg_resync_to_restart;
144 term_source = TermSrcStream;
145 }
146
JpegDecoder()147 JpegDecoder::JpegDecoder() : srcMgr_(nullptr)
148 {
149 CreateDecoder();
150 #if !defined(_WIN32) && !defined(_APPLE) && !defined(A_PLATFORM) && !defined(IOS_PLATFORM)
151 CreateHwDecompressor();
152 #endif
153 }
154
CreateDecoder()155 void JpegDecoder::CreateDecoder()
156 {
157 // create decompress struct
158 jpeg_create_decompress(&decodeInfo_);
159
160 // set error output
161 decodeInfo_.err = jpeg_std_error(&jerr_);
162 jerr_.error_exit = ErrorExit;
163 if (decodeInfo_.err == nullptr) {
164 IMAGE_LOGE("create jpeg decoder failed.");
165 return;
166 }
167 decodeInfo_.err->output_message = &OutputErrorMessage;
168 }
169
~JpegDecoder()170 JpegDecoder::~JpegDecoder()
171 {
172 jpeg_destroy_decompress(&decodeInfo_);
173 if (hwJpegDecompress_ != nullptr) {
174 delete hwJpegDecompress_;
175 hwJpegDecompress_ = nullptr;
176 }
177 }
178
SetSource(InputDataStream & sourceStream)179 void JpegDecoder::SetSource(InputDataStream &sourceStream)
180 {
181 srcMgr_.inputStream = &sourceStream;
182 state_ = JpegDecodingState::SOURCE_INITED;
183 }
184
GetImageSize(uint32_t index,PlSize & size)185 uint32_t JpegDecoder::GetImageSize(uint32_t index, PlSize &size)
186 {
187 if (index >= JPEG_IMAGE_NUM) {
188 IMAGE_LOGE("decode image index:[%{public}u] out of range:[%{public}u].", index, JPEG_IMAGE_NUM);
189 return ERR_IMAGE_INVALID_PARAMETER;
190 }
191 if (state_ < JpegDecodingState::SOURCE_INITED) {
192 IMAGE_LOGE("get image size failed for state %{public}d.", state_);
193 return ERR_MEDIA_INVALID_OPERATION;
194 }
195 if (state_ >= JpegDecodingState::BASE_INFO_PARSED) {
196 size.width = decodeInfo_.image_width;
197 size.height = decodeInfo_.image_height;
198 return Media::SUCCESS;
199 }
200 // only state JpegDecodingState::SOURCE_INITED and JpegDecodingState::BASE_INFO_PARSING can go here.
201 uint32_t ret = DecodeHeader();
202 if (ret != Media::SUCCESS) {
203 IMAGE_LOGE("decode header error on get image size, ret:%{public}u.", ret);
204 state_ = JpegDecodingState::BASE_INFO_PARSING;
205 return ret;
206 }
207 size.width = decodeInfo_.image_width;
208 size.height = decodeInfo_.image_height;
209 state_ = JpegDecodingState::BASE_INFO_PARSED;
210 return Media::SUCCESS;
211 }
212
GetDecodeFormat(PlPixelFormat format,PlPixelFormat & outputFormat)213 J_COLOR_SPACE JpegDecoder::GetDecodeFormat(PlPixelFormat format, PlPixelFormat &outputFormat)
214 {
215 outputFormat = format;
216 J_COLOR_SPACE colorSpace = JCS_UNKNOWN;
217 switch (format) {
218 case PlPixelFormat::UNKNOWN:
219 case PlPixelFormat::RGBA_8888: {
220 colorSpace = JCS_EXT_RGBA;
221 outputFormat = PlPixelFormat::RGBA_8888;
222 break;
223 }
224 case PlPixelFormat::BGRA_8888: {
225 colorSpace = JCS_EXT_BGRA;
226 outputFormat = PlPixelFormat::BGRA_8888;
227 break;
228 }
229 case PlPixelFormat::ARGB_8888: {
230 colorSpace = JCS_EXT_ARGB;
231 break;
232 }
233 case PlPixelFormat::ALPHA_8: {
234 colorSpace = JCS_GRAYSCALE;
235 break;
236 }
237 case PlPixelFormat::RGB_565: {
238 colorSpace = JCS_RGB;
239 outputFormat = PlPixelFormat::RGB_888;
240 break;
241 }
242 case PlPixelFormat::RGB_888: {
243 // NOTICE: libjpeg make BE as default when we are LE
244 colorSpace = JCS_EXT_BGR;
245 break;
246 }
247 default: {
248 colorSpace = JCS_EXT_RGBA;
249 outputFormat = PlPixelFormat::RGBA_8888;
250 break;
251 }
252 }
253 return colorSpace;
254 }
255
CalculateInSampleSize(const jpeg_decompress_struct & dInfo,const PixelDecodeOptions & opts)256 static int CalculateInSampleSize(const jpeg_decompress_struct &dInfo, const PixelDecodeOptions &opts)
257 {
258 int inSampleSize = 1;
259 // Input height and width of image
260 int width = dInfo.image_width;
261 int height = dInfo.image_height;
262
263 if (opts.desiredSize.height > 0 && opts.desiredSize.width > 0) {
264 int reqHeight = opts.desiredSize.height;
265 int reqWidth = opts.desiredSize.width;
266
267 if (height > reqHeight || width > reqWidth) {
268 const int halfHeight = height >> 1;
269 const int halfWidth = width >> 1;
270
271 // Calculate the largest inSampleSize value that is a power of 2 and keeps both
272 // height and width larger than the requested height and width.
273 while ((halfHeight / inSampleSize) >= reqHeight && (halfWidth / inSampleSize) >= reqWidth) {
274 inSampleSize <<= 1;
275 }
276 }
277 }
278 return inSampleSize;
279 }
280
281 /*
282 * Calculate a valid scale fraction for this decoder, given an input sampleSize
283 */
GetScaledFraction(const int & inSampleSize,jpeg_decompress_struct & dInfo)284 static void GetScaledFraction(const int& inSampleSize, jpeg_decompress_struct& dInfo)
285 {
286 // libjpeg-turbo supports scaling only by 1/8, 1/4, 3/8, 1/2, 5/8, 3/4, 7/8, and 1/1
287 // Using binary search to find the appropriate scaling ratio based on SCALES and SCALE-NUM arrays
288 unsigned int num = 1;
289 unsigned int denom = 8;
290 float desiredScale = 1.0f / static_cast<float>(inSampleSize);
291
292 int left = 0;
293 int right = SCALE_NUMS_LENGTH;
294 while (left <= right) {
295 int mid = left + (right - left) / 2;
296 if (desiredScale >= SCALES[mid]) {
297 num = SCALE_NUMS[mid];
298 left = mid + 1;
299 } else {
300 right = mid - 1;
301 }
302 }
303 dInfo.scale_num = num;
304 dInfo.scale_denom = denom;
305 }
306
SetDecodeOptions(uint32_t index,const PixelDecodeOptions & opts,PlImageInfo & info)307 uint32_t JpegDecoder::SetDecodeOptions(uint32_t index, const PixelDecodeOptions &opts, PlImageInfo &info)
308 {
309 if (index >= JPEG_IMAGE_NUM) {
310 IMAGE_LOGE("decode image index:[%{public}u] out of range:[%{public}u].", index, JPEG_IMAGE_NUM);
311 return ERR_IMAGE_INVALID_PARAMETER;
312 }
313 if (state_ < JpegDecodingState::SOURCE_INITED) {
314 IMAGE_LOGE("set decode options failed for state %{public}d.", state_);
315 return ERR_MEDIA_INVALID_OPERATION;
316 }
317 if (state_ >= JpegDecodingState::IMAGE_DECODING) {
318 FinishOldDecompress();
319 state_ = JpegDecodingState::SOURCE_INITED;
320 }
321 if (state_ < JpegDecodingState::BASE_INFO_PARSED) {
322 uint32_t ret = DecodeHeader();
323 if (ret != Media::SUCCESS) {
324 state_ = JpegDecodingState::BASE_INFO_PARSING;
325 IMAGE_LOGE("decode header error on set decode options:%{public}u.", ret);
326 return ret;
327 }
328 state_ = JpegDecodingState::BASE_INFO_PARSED;
329 }
330 // only state JpegDecodingState::BASE_INFO_PARSED can go here.
331 int inSampleSize = CalculateInSampleSize(decodeInfo_, opts);
332 GetScaledFraction(inSampleSize, decodeInfo_);
333 uint32_t ret = StartDecompress(opts);
334 if (ret != Media::SUCCESS) {
335 IMAGE_LOGE("start decompress failed on set decode options:%{public}u.", ret);
336 return ret;
337 }
338 info.pixelFormat = outputFormat_;
339 info.size.width = decodeInfo_.output_width;
340 info.size.height = decodeInfo_.output_height;
341 info.alphaType = PlAlphaType::IMAGE_ALPHA_TYPE_OPAQUE;
342 opts_ = opts;
343 state_ = JpegDecodingState::IMAGE_DECODING;
344 return Media::SUCCESS;
345 }
346
GetRowBytes()347 uint32_t JpegDecoder::GetRowBytes()
348 {
349 uint32_t pixelBytes =
350 (decodeInfo_.out_color_space == JCS_RGB565) ? PIXEL_BYTES_RGB_565 : decodeInfo_.out_color_components;
351 return decodeInfo_.output_width * pixelBytes;
352 }
353
DoSwDecode(DecodeContext & context)354 uint32_t JpegDecoder::DoSwDecode(DecodeContext &context) __attribute__((no_sanitize("cfi")))
355 {
356 ImageTrace imageTrace("JpegDecoder::DoSwDecode");
357 if (setjmp(jerr_.setjmp_buffer)) {
358 IMAGE_LOGE("decode image failed.");
359 return ERR_IMAGE_DECODE_ABNORMAL;
360 }
361 uint32_t rowStride = GetRowBytes();
362 if (context.pixelsBuffer.buffer == nullptr) {
363 uint64_t byteCount = static_cast<uint64_t>(rowStride) * decodeInfo_.output_height;
364 #if !defined(_WIN32) && !defined(_APPLE) && !defined(A_PLATFORM) && !defined(IOS_PLATFORM)
365 if (context.allocatorType == Media::AllocatorType::SHARE_MEM_ALLOC) {
366 uint32_t id = context.pixelmapUniqueId_;
367 std::string name = "JPEG RawData, uniqueId: " + std::to_string(getpid()) + '_' + std::to_string(id);
368 int fd = AshmemCreate(name.c_str(), byteCount);
369 if (fd < 0) {
370 return ERR_SHAMEM_DATA_ABNORMAL;
371 }
372 int result = AshmemSetProt(fd, PROT_READ | PROT_WRITE);
373 if (result < 0) {
374 ::close(fd);
375 return ERR_SHAMEM_DATA_ABNORMAL;
376 }
377 void* ptr = ::mmap(nullptr, byteCount, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
378 if (ptr == MAP_FAILED) {
379 ::close(fd);
380 return ERR_SHAMEM_DATA_ABNORMAL;
381 }
382 context.pixelsBuffer.buffer = ptr;
383 void *fdBuffer = new int32_t();
384 if (fdBuffer == nullptr) {
385 IMAGE_LOGE("new fdBuffer fail");
386 ::munmap(ptr, byteCount);
387 ::close(fd);
388 context.pixelsBuffer.buffer = nullptr;
389 return ERR_SHAMEM_DATA_ABNORMAL;
390 }
391 *static_cast<int32_t *>(fdBuffer) = fd;
392 context.pixelsBuffer.context = fdBuffer;
393 context.pixelsBuffer.bufferSize = byteCount;
394 context.allocatorType = AllocatorType::SHARE_MEM_ALLOC;
395 context.freeFunc = nullptr;
396 } else if (context.allocatorType == Media::AllocatorType::DMA_ALLOC) {
397 sptr<SurfaceBuffer> sb = SurfaceBuffer::Create();
398 BufferRequestConfig requestConfig = {
399 .width = decodeInfo_.output_width,
400 .height = decodeInfo_.output_height,
401 .strideAlignment = 0x8, // set 0x8 as default value to alloc SurfaceBufferImpl
402 .format = GRAPHIC_PIXEL_FMT_RGBA_8888, // PixelFormat
403 .usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA,
404 .timeout = 0,
405 .colorGamut = GraphicColorGamut::GRAPHIC_COLOR_GAMUT_SRGB,
406 .transform = GraphicTransformType::GRAPHIC_ROTATE_NONE,
407 };
408 GSError ret = sb->Alloc(requestConfig);
409 if (ret != GSERROR_OK) {
410 IMAGE_LOGE("SurfaceBuffer Alloc failed, %{public}s", GSErrorStr(ret).c_str());
411 return ERR_DMA_NOT_EXIST;
412 }
413 void* nativeBuffer = sb.GetRefPtr();
414 int32_t err = ImageUtils::SurfaceBuffer_Reference(nativeBuffer);
415 if (err != OHOS::GSERROR_OK) {
416 IMAGE_LOGE("NativeBufferReference failed");
417 return ERR_DMA_DATA_ABNORMAL;
418 }
419
420 context.pixelsBuffer.buffer = sb->GetVirAddr();
421 context.pixelsBuffer.context = nativeBuffer;
422 context.pixelsBuffer.bufferSize = byteCount;
423 context.allocatorType = AllocatorType::DMA_ALLOC;
424 context.freeFunc = nullptr;
425 } else {
426 void *outputBuffer = malloc(byteCount);
427 if (outputBuffer == nullptr) {
428 IMAGE_LOGE("alloc output buffer size:[%{public}llu] error.",
429 static_cast<unsigned long long>(byteCount));
430 return ERR_IMAGE_MALLOC_ABNORMAL;
431 }
432 context.pixelsBuffer.buffer = outputBuffer;
433 context.pixelsBuffer.context = nullptr;
434 context.pixelsBuffer.bufferSize = byteCount;
435 context.allocatorType = AllocatorType::HEAP_ALLOC;
436 context.freeFunc = nullptr;
437 }
438 #else
439 void *outputBuffer = malloc(byteCount);
440 if (outputBuffer == nullptr) {
441 IMAGE_LOGE("alloc output buffer size:[%{public}llu] error.", static_cast<unsigned long long>(byteCount));
442 return ERR_IMAGE_MALLOC_ABNORMAL;
443 }
444 context.pixelsBuffer.buffer = outputBuffer;
445 context.pixelsBuffer.context = nullptr;
446 context.pixelsBuffer.bufferSize = byteCount;
447 context.allocatorType = AllocatorType::HEAP_ALLOC;
448 context.freeFunc = nullptr;
449 #endif
450 }
451 uint8_t *base = static_cast<uint8_t *>(context.pixelsBuffer.buffer);
452 if (base == nullptr) {
453 IMAGE_LOGE("decode image buffer is null.");
454 return ERR_IMAGE_INVALID_PARAMETER;
455 }
456
457 if (srcMgr_.inputStream->Seek(streamPosition_ - decodeInfo_.src->bytes_in_buffer)) {
458 auto dataPtr = srcMgr_.inputStream->GetDataPtr();
459 if (dataPtr) {
460 // sourceData_.data() maybe changed after IncrementalSourceStream::UpdateData(), so reset next_input_byte
461 decodeInfo_.src->next_input_byte = dataPtr + streamPosition_ - decodeInfo_.src->bytes_in_buffer;
462 }
463 }
464
465 srcMgr_.inputStream->Seek(streamPosition_);
466 uint8_t *buffer = nullptr;
467 #if !defined(A_PLATFORM) && !defined(IOS_PLATFORM)
468 if (context.allocatorType == Media::AllocatorType::DMA_ALLOC) {
469 SurfaceBuffer* sbBuffer = reinterpret_cast<SurfaceBuffer*> (context.pixelsBuffer.context);
470 rowStride = sbBuffer->GetStride();
471 }
472 #endif
473 while (decodeInfo_.output_scanline < decodeInfo_.output_height) {
474 buffer = base + rowStride * decodeInfo_.output_scanline;
475 uint32_t readLineNum = jpeg_read_scanlines(&decodeInfo_, &buffer, RW_LINE_NUM);
476 if (readLineNum < RW_LINE_NUM) {
477 streamPosition_ = srcMgr_.inputStream->Tell();
478 IMAGE_LOGE("read line fail, read num:%{public}u, total read num:%{public}u.", readLineNum,
479 decodeInfo_.output_scanline);
480 return ERR_IMAGE_SOURCE_DATA_INCOMPLETE;
481 }
482 }
483 streamPosition_ = srcMgr_.inputStream->Tell();
484
485 #ifdef IMAGE_COLORSPACE_FLAG
486 // parser icc profile info
487 uint32_t iccPaseredResult = iccProfileInfo_.ParsingICCProfile(&decodeInfo_);
488 if (iccPaseredResult == OHOS::Media::ERR_IMAGE_DENCODE_ICC_FAILED) {
489 IMAGE_LOGE("dencode image icc error.");
490 return iccPaseredResult;
491 }
492 #endif
493 return Media::SUCCESS;
494 }
495
Decode(uint32_t index,DecodeContext & context)496 uint32_t JpegDecoder::Decode(uint32_t index, DecodeContext &context)
497 {
498 ImageTrace imageTrace("JpegDecoder::Decode, index:%u", index);
499 if (index >= JPEG_IMAGE_NUM) {
500 IMAGE_LOGE("decode image index:[%{public}u] out of range:[%{public}u].", index, JPEG_IMAGE_NUM);
501 return ERR_IMAGE_INVALID_PARAMETER;
502 }
503 if (state_ < JpegDecodingState::IMAGE_DECODING) {
504 IMAGE_LOGE("decode failed for state %{public}d.", state_);
505 return ERR_MEDIA_INVALID_OPERATION;
506 }
507 if (state_ > JpegDecodingState::IMAGE_DECODING) {
508 FinishOldDecompress();
509 state_ = JpegDecodingState::SOURCE_INITED;
510 uint32_t ret = DecodeHeader();
511 if (ret != Media::SUCCESS) {
512 state_ = JpegDecodingState::BASE_INFO_PARSING;
513 IMAGE_LOGE("decode header error on decode:%{public}u.", ret);
514 return ret;
515 }
516 state_ = JpegDecodingState::BASE_INFO_PARSED;
517 ret = StartDecompress(opts_);
518 if (ret != Media::SUCCESS) {
519 IMAGE_LOGE("start decompress failed on decode:%{public}u.", ret);
520 return ret;
521 }
522 state_ = JpegDecodingState::IMAGE_DECODING;
523 }
524 // only state JpegDecodingState::IMAGE_DECODING can go here.
525 if (hwJpegDecompress_ != nullptr) {
526 srcMgr_.inputStream->Seek(streamPosition_);
527 uint32_t ret = hwJpegDecompress_->Decompress(&decodeInfo_, srcMgr_.inputStream, context);
528 if (ret == Media::SUCCESS) {
529 state_ = JpegDecodingState::IMAGE_DECODED;
530 IMAGE_LOGD("jpeg hardware decode success.");
531 return ret;
532 }
533 }
534 uint32_t ret = DoSwDecode(context);
535 if (ret == Media::SUCCESS) {
536 state_ = JpegDecodingState::IMAGE_DECODED;
537 IMAGE_LOGD("jpeg software decode success.");
538 return Media::SUCCESS;
539 }
540 if (ret == ERR_IMAGE_SOURCE_DATA_INCOMPLETE && opts_.allowPartialImage) {
541 state_ = JpegDecodingState::IMAGE_PARTIAL;
542 context.ifPartialOutput = true;
543 return Media::SUCCESS;
544 }
545 state_ = JpegDecodingState::IMAGE_ERROR;
546 return ret;
547 }
548
Reset()549 void JpegDecoder::Reset()
550 {
551 srcMgr_.inputStream = nullptr;
552 }
553
PromoteIncrementalDecode(uint32_t index,ProgDecodeContext & progContext)554 uint32_t JpegDecoder::PromoteIncrementalDecode(uint32_t index, ProgDecodeContext &progContext)
555 {
556 progContext.totalProcessProgress = 0;
557 if (index >= JPEG_IMAGE_NUM) {
558 IMAGE_LOGE("decode image index:[%{public}u] out of range:[%{public}u].", index, JPEG_IMAGE_NUM);
559 return ERR_IMAGE_INVALID_PARAMETER;
560 }
561 if (state_ != JpegDecodingState::IMAGE_DECODING) {
562 IMAGE_LOGE("incremental decode failed for state %{public}d.", state_);
563 return ERR_MEDIA_INVALID_OPERATION;
564 }
565
566 uint32_t ret = DoSwDecode(progContext.decodeContext);
567 if (ret == Media::SUCCESS) {
568 state_ = JpegDecodingState::IMAGE_DECODED;
569 }
570 // get promote decode progress, in percentage: 0~100.
571 progContext.totalProcessProgress =
572 decodeInfo_.output_height == 0 ? 0 : (decodeInfo_.output_scanline * NUM_100) / decodeInfo_.output_height;
573 IMAGE_LOGD("incremental decode progress %{public}u.", progContext.totalProcessProgress);
574 return ret;
575 }
576
CreateHwDecompressor()577 void JpegDecoder::CreateHwDecompressor()
578 {
579 std::map<std::string, AttrData> capabilities;
580 const std::string format = "image/jpeg";
581 capabilities.insert(std::map<std::string, AttrData>::value_type("encodeFormat", AttrData(format)));
582 hwJpegDecompress_ = pluginServer_.CreateObject<AbsImageDecompressComponent>(
583 AbsImageDecompressComponent::SERVICE_DEFAULT, capabilities);
584 if (hwJpegDecompress_ == nullptr) {
585 IMAGE_LOGE("get hardware jpeg decompress component failed.");
586 return;
587 }
588 }
589
FinishOldDecompress()590 void JpegDecoder::FinishOldDecompress()
591 {
592 if (state_ < JpegDecodingState::IMAGE_DECODING) {
593 return;
594 }
595 jpeg_destroy_decompress(&decodeInfo_);
596 CreateDecoder();
597 }
598
IsMarker(uint8_t rawMarkerPrefix,uint8_t rawMarkderCode,uint8_t markerCode)599 bool JpegDecoder::IsMarker(uint8_t rawMarkerPrefix, uint8_t rawMarkderCode, uint8_t markerCode)
600 {
601 if (rawMarkerPrefix != JPG_MARKER_PREFIX) {
602 return false;
603 }
604
605 // RSTn, n from 0 to 7
606 if (rawMarkderCode >= JPG_MARKER_RST0 && rawMarkderCode <= JPG_MARKER_RSTN && markerCode == JPG_MARKER_RST) {
607 return true;
608 }
609
610 // APPn, n from 0 to 15
611 if (rawMarkderCode >= JPG_MARKER_APP0 && rawMarkderCode <= JPG_MARKER_APPN && markerCode == JPG_MARKER_APP) {
612 return true;
613 }
614
615 if (rawMarkderCode == markerCode) {
616 return true;
617 }
618 return false;
619 }
620
FindMarker(InputDataStream & stream,uint8_t marker)621 bool JpegDecoder::FindMarker(InputDataStream &stream, uint8_t marker)
622 {
623 uint8_t buffer[MARKER_SIZE] = { 0 };
624 uint32_t readSize = 0;
625 stream.Seek(0);
626 while (true) {
627 uint32_t cur = stream.Tell();
628 if (!stream.Seek(cur + MARKER_SIZE)) {
629 return false;
630 }
631 stream.Seek(cur);
632
633 // read marker code
634 stream.Read(MARKER_SIZE, buffer, sizeof(buffer), readSize);
635 if (readSize != MARKER_SIZE) {
636 return false;
637 }
638
639 uint8_t markerPrefix = buffer[JPG_MARKER_PREFIX_OFFSET];
640 uint8_t markerCode = buffer[JPG_MARKER_CODE_OFFSET];
641 if (IsMarker(markerPrefix, markerCode, JPG_MARKER_SOS)) {
642 return true;
643 }
644
645 if (IsMarker(markerPrefix, markerCode, JPG_MARKER_SOI) || IsMarker(markerPrefix, markerCode, JPG_MARKER_RST)) {
646 continue;
647 }
648
649 cur = stream.Tell();
650 if (!stream.Seek(cur + MARKER_LENGTH)) {
651 return false;
652 }
653 stream.Seek(cur);
654 // read marker length
655 stream.Read(MARKER_LENGTH, buffer, sizeof(buffer), readSize);
656 if (readSize != MARKER_LENGTH) {
657 return false;
658 }
659 // skip data, length = sizeof(length) + sizeof(data)
660 uint32_t length = (buffer[MARKER_LENGTH_0_OFFSET] << MARKER_LENGTH_SHIFT) + buffer[MARKER_LENGTH_1_OFFSET];
661 if (!stream.Seek(cur + length)) {
662 return false;
663 }
664 }
665 }
666
DecodeHeader()667 uint32_t JpegDecoder::DecodeHeader()
668 {
669 if (setjmp(jerr_.setjmp_buffer)) {
670 IMAGE_LOGE("get image size failed.");
671 return ERR_IMAGE_DECODE_ABNORMAL;
672 }
673 if (state_ == JpegDecodingState::SOURCE_INITED) {
674 srcMgr_.inputStream->Seek(0);
675 } else {
676 srcMgr_.inputStream->Seek(streamPosition_);
677 }
678 decodeInfo_.src = &srcMgr_;
679
680 /**
681 * The function jpeg_read_header() shall read the JPEG datastream until the first SOS marker is encountered
682 * incremental decoding should have enough data(contains SOS marker) before calling jpeg_read_header.
683 */
684 if (!srcMgr_.inputStream->IsStreamCompleted()) {
685 uint32_t curPos = srcMgr_.inputStream->Tell();
686 while (true) {
687 if (!FindMarker(*srcMgr_.inputStream, JPG_MARKER_SOS)) {
688 srcMgr_.inputStream->Seek(curPos);
689 return ERR_IMAGE_SOURCE_DATA_INCOMPLETE;
690 }
691 srcMgr_.inputStream->Seek(curPos);
692 break;
693 }
694 }
695
696 // call jpeg_save_markers, use to get ICC profile.
697 jpeg_save_markers(&decodeInfo_, PL_ICC_MARKER, PL_MARKER_LENGTH_LIMIT);
698 int32_t ret = jpeg_read_header(&decodeInfo_, true);
699 streamPosition_ = srcMgr_.inputStream->Tell();
700 if (ret == JPEG_SUSPENDED) {
701 IMAGE_LOGD("image input data incomplete, decode header error:%{public}u.", ret);
702 return ERR_IMAGE_SOURCE_DATA_INCOMPLETE;
703 } else if (ret != JPEG_HEADER_OK) {
704 IMAGE_LOGE("image type is not jpeg, decode header error:%{public}u.", ret);
705 return ERR_IMAGE_GET_DATA_ABNORMAL;
706 }
707 return Media::SUCCESS;
708 }
709
StartDecompress(const PixelDecodeOptions & opts)710 uint32_t JpegDecoder::StartDecompress(const PixelDecodeOptions &opts)
711 {
712 if (setjmp(jerr_.setjmp_buffer)) {
713 IMAGE_LOGE("set output image info failed.");
714 return ERR_IMAGE_DECODE_ABNORMAL;
715 }
716 // set decode options
717 if (decodeInfo_.jpeg_color_space == JCS_CMYK || decodeInfo_.jpeg_color_space == JCS_YCCK) {
718 // can't support CMYK to alpha8 convert
719 if (opts.desiredPixelFormat == PlPixelFormat::ALPHA_8) {
720 IMAGE_LOGE("can't support colorspace CMYK to alpha convert.");
721 return ERR_IMAGE_UNKNOWN_FORMAT;
722 }
723 IMAGE_LOGD("jpeg colorspace is CMYK.");
724 decodeInfo_.out_color_space = JCS_CMYK;
725 outputFormat_ = PlPixelFormat::CMYK;
726 } else {
727 decodeInfo_.out_color_space = GetDecodeFormat(opts.desiredPixelFormat, outputFormat_);
728 if (decodeInfo_.out_color_space == JCS_UNKNOWN) {
729 IMAGE_LOGE("set jpeg output color space invalid.");
730 return ERR_IMAGE_UNKNOWN_FORMAT;
731 }
732 }
733 srcMgr_.inputStream->Seek(streamPosition_);
734 if (jpeg_start_decompress(&decodeInfo_) != TRUE) {
735 streamPosition_ = srcMgr_.inputStream->Tell();
736 IMAGE_LOGE("jpeg start decompress failed, invalid input.");
737 return ERR_IMAGE_INVALID_PARAMETER;
738 }
739 streamPosition_ = srcMgr_.inputStream->Tell();
740 return Media::SUCCESS;
741 }
742
ParseExifData()743 bool JpegDecoder::ParseExifData()
744 {
745 IMAGE_LOGD("ParseExifData enter");
746 uint32_t curPos = srcMgr_.inputStream->Tell();
747 srcMgr_.inputStream->Seek(0);
748 unsigned long fsize = static_cast<unsigned long>(srcMgr_.inputStream->GetStreamSize());
749 if (fsize <= 0) {
750 IMAGE_LOGE("Get stream size failed");
751 return false;
752 }
753 unsigned char *buf = new unsigned char[fsize];
754 uint32_t readSize = 0;
755 srcMgr_.inputStream->Read(fsize, buf, fsize, readSize);
756 IMAGE_LOGD("parsing EXIF: fsize %{public}lu", fsize);
757
758 int code = exifInfo_.ParseExifData(buf, fsize);
759 delete[] buf;
760 srcMgr_.inputStream->Seek(curPos);
761 if (code) {
762 IMAGE_LOGE("Error parsing EXIF: code %{public}d", code);
763 return false;
764 }
765 return true;
766 }
767
GetImagePropertyInt(uint32_t index,const std::string & key,int32_t & value)768 uint32_t JpegDecoder::GetImagePropertyInt(uint32_t index, const std::string &key, int32_t &value)
769 {
770 IMAGE_LOGD("[GetImagePropertyInt] enter jpeg plugin, key:%{public}s", key.c_str());
771 if (IsSameTextStr(key, ACTUAL_IMAGE_ENCODED_FORMAT)) {
772 IMAGE_LOGE("[GetImagePropertyInt] this key is used to check the original format of raw image!");
773 return Media::ERR_MEDIA_VALUE_INVALID;
774 }
775
776 if (!exifInfo_.IsExifDataParsed()) {
777 if (!ParseExifData()) {
778 IMAGE_LOGE("[GetImagePropertyInt] Parse exif data failed!");
779 return Media::ERROR;
780 }
781 }
782 if (IsSameTextStr(key, ORIENTATION)) {
783 if (PROPERTY_INT.find(exifInfo_.orientation_) != PROPERTY_INT.end()) {
784 value = PROPERTY_INT.at(exifInfo_.orientation_);
785 } else {
786 IMAGE_LOGE("[GetImagePropertyInt] The exifinfo:%{public}s is not found",
787 exifInfo_.orientation_.c_str());
788 return Media::ERR_MEDIA_VALUE_INVALID;
789 }
790 } else {
791 IMAGE_LOGE("[GetImagePropertyInt] The key:%{public}s is not supported int32_t", key.c_str());
792 return Media::ERR_MEDIA_VALUE_INVALID;
793 }
794 return Media::SUCCESS;
795 }
796
GetImagePropertyString(uint32_t index,const std::string & key,std::string & value)797 uint32_t JpegDecoder::GetImagePropertyString(uint32_t index, const std::string &key, std::string &value)
798 {
799 IMAGE_LOGD("[GetImagePropertyString] enter jpeg plugin, key:%{public}s", key.c_str());
800 if (IsSameTextStr(key, ACTUAL_IMAGE_ENCODED_FORMAT)) {
801 IMAGE_LOGE("[GetImagePropertyString] this key is used to check the original format of raw image!");
802 return Media::ERR_MEDIA_VALUE_INVALID;
803 }
804 if (!exifInfo_.IsExifDataParsed()) {
805 if (!ParseExifData()) {
806 IMAGE_LOGE("[GetImagePropertyString] Parse exif data failed!");
807 return Media::ERROR;
808 }
809 }
810 if (IsSameTextStr(key, BITS_PER_SAMPLE)) {
811 value = exifInfo_.bitsPerSample_;
812 } else if (IsSameTextStr(key, ORIENTATION)) {
813 value = exifInfo_.orientation_;
814 } else if (IsSameTextStr(key, IMAGE_LENGTH)) {
815 value = exifInfo_.imageLength_;
816 } else if (IsSameTextStr(key, IMAGE_WIDTH)) {
817 value = exifInfo_.imageWidth_;
818 } else if (IsSameTextStr(key, GPS_LATITUDE)) {
819 value = exifInfo_.gpsLatitude_;
820 } else if (IsSameTextStr(key, GPS_LONGITUDE)) {
821 value = exifInfo_.gpsLongitude_;
822 } else if (IsSameTextStr(key, GPS_LATITUDE_REF)) {
823 value = exifInfo_.gpsLatitudeRef_;
824 } else if (IsSameTextStr(key, GPS_LONGITUDE_REF)) {
825 value = exifInfo_.gpsLongitudeRef_;
826 } else if (IsSameTextStr(key, DATE_TIME_ORIGINAL)) {
827 value = exifInfo_.dateTimeOriginal_;
828 } else if (IsSameTextStr(key, DATE_TIME_ORIGINAL_MEDIA)) {
829 FormatTimeStamp(value, exifInfo_.dateTimeOriginal_);
830 } else if (GetImagePropertyString(key, value) != Media::SUCCESS) {
831 return Media::ERR_IMAGE_DECODE_EXIF_UNSUPPORT;
832 }
833 if (IsSameTextStr(value, EXIFInfo::DEFAULT_EXIF_VALUE)) {
834 IMAGE_LOGE("[GetImagePropertyString] enter jpeg plugin, ifd and entry are not matched!");
835 return Media::ERR_MEDIA_VALUE_INVALID;
836 }
837 IMAGE_LOGD("[GetImagePropertyString] enter jpeg plugin, value:%{public}s", value.c_str());
838 return Media::SUCCESS;
839 }
840
GetImagePropertyString(const std::string & key,std::string & value)841 uint32_t JpegDecoder::GetImagePropertyString(const std::string &key, std::string &value)
842 {
843 if (IsSameTextStr(key, EXPOSURE_TIME)) {
844 value = exifInfo_.exposureTime_;
845 } else if (IsSameTextStr(key, F_NUMBER)) {
846 value = exifInfo_.fNumber_;
847 } else if (IsSameTextStr(key, ISO_SPEED_RATINGS)) {
848 value = exifInfo_.isoSpeedRatings_;
849 } else if (IsSameTextStr(key, SCENE_TYPE)) {
850 value = exifInfo_.sceneType_;
851 } else if (IsSameTextStr(key, COMPRESSED_BITS_PER_PIXEL)) {
852 value = exifInfo_.compressedBitsPerPixel_;
853 } else if (IsSameTextStr(key, DATE_TIME)) {
854 value = exifInfo_.dateTime_;
855 } else if (IsSameTextStr(key, GPS_TIME_STAMP)) {
856 value = exifInfo_.gpsTimeStamp_;
857 } else if (IsSameTextStr(key, GPS_DATE_STAMP)) {
858 value = exifInfo_.gpsDateStamp_;
859 } else if (IsSameTextStr(key, IMAGE_DESCRIPTION)) {
860 value = exifInfo_.imageDescription_;
861 } else if (IsSameTextStr(key, MAKE)) {
862 value = exifInfo_.make_;
863 } else if (IsSameTextStr(key, MODEL)) {
864 value = exifInfo_.model_;
865 } else if (IsSameTextStr(key, PHOTO_MODE)) {
866 value = exifInfo_.photoMode_;
867 } else if (IsSameTextStr(key, SENSITIVITY_TYPE)) {
868 value = exifInfo_.sensitivityType_;
869 } else if (IsSameTextStr(key, STANDARD_OUTPUT_SENSITIVITY)) {
870 value = exifInfo_.standardOutputSensitivity_;
871 } else if (IsSameTextStr(key, RECOMMENDED_EXPOSURE_INDEX)) {
872 value = exifInfo_.recommendedExposureIndex_;
873 } else if (IsSameTextStr(key, ISO_SPEED)) {
874 value = exifInfo_.isoSpeedRatings_;
875 } else if (IsSameTextStr(key, APERTURE_VALUE)) {
876 value = exifInfo_.apertureValue_;
877 } else if (IsSameTextStr(key, EXPOSURE_BIAS_VALUE)) {
878 value = exifInfo_.exposureBiasValue_;
879 } else if (IsSameTextStr(key, METERING_MODE)) {
880 value = exifInfo_.meteringMode_;
881 } else if (IsSameTextStr(key, LIGHT_SOURCE)) {
882 value = exifInfo_.lightSource_;
883 } else if (IsSameTextStr(key, FLASH)) {
884 value = exifInfo_.flash_;
885 } else if (IsSameTextStr(key, FOCAL_LENGTH)) {
886 value = exifInfo_.focalLength_;
887 } else {
888 return GetImagePropertyStringEx(key, value);
889 }
890
891 return Media::SUCCESS;
892 }
893
GetImagePropertyStringEx(const std::string & key,std::string & value)894 uint32_t JpegDecoder::GetImagePropertyStringEx(const std::string &key, std::string &value)
895 {
896 if (IsSameTextStr(key, USER_COMMENT)) {
897 value = exifInfo_.userComment_;
898 } else if (IsSameTextStr(key, PIXEL_X_DIMENSION)) {
899 value = exifInfo_.pixelXDimension_;
900 } else if (IsSameTextStr(key, PIXEL_Y_DIMENSION)) {
901 value = exifInfo_.pixelYDimension_;
902 } else if (IsSameTextStr(key, WHITE_BALANCE)) {
903 value = exifInfo_.whiteBalance_;
904 } else if (IsSameTextStr(key, FOCAL_LENGTH_IN_35_MM_FILM)) {
905 value = exifInfo_.focalLengthIn35mmFilm_;
906 } else if (IsSameTextStr(key, HW_MNOTE_CAPTURE_MODE)) {
907 value = exifInfo_.hwMnoteCaptureMode_;
908 } else if (IsSameTextStr(key, HW_MNOTE_PHYSICAL_APERTURE)) {
909 value = exifInfo_.hwMnotePhysicalAperture_;
910 } else {
911 return GetMakerImagePropertyString(key, value);
912 }
913 return Media::SUCCESS;
914 }
915
GetMakerImagePropertyString(const std::string & key,std::string & value)916 uint32_t JpegDecoder::GetMakerImagePropertyString(const std::string &key, std::string &value)
917 {
918 if (exifInfo_.makerInfoTagValueMap.find(key) != exifInfo_.makerInfoTagValueMap.end()) {
919 value = exifInfo_.makerInfoTagValueMap[key];
920 return Media::SUCCESS;
921 }
922 return Media::ERR_IMAGE_DECODE_EXIF_UNSUPPORT;
923 }
924
InitOriginalTimes(std::string & dataTime)925 void InitOriginalTimes(std::string &dataTime)
926 {
927 for (size_t i = 0; i < dataTime.size() && i < TIMES_LEN; i++) {
928 if ((dataTime[i] < '0' || dataTime[i] > '9') && dataTime[i] != ' ') {
929 if (i < DATE_LEN) {
930 dataTime[i] = '-';
931 } else {
932 dataTime[i] = ':';
933 }
934 }
935 }
936 }
937
SetOriginalTimes(std::string & dataTime)938 std::string SetOriginalTimes(std::string &dataTime)
939 {
940 InitOriginalTimes(dataTime);
941 std::string data = "";
942 std::string time = "";
943 std::string::size_type position = dataTime.find(" ");
944 if (position == dataTime.npos) {
945 data = dataTime;
946 if (data.find("-") == data.npos) {
947 data += "-01-01";
948 } else if (data.find_first_of("-") == data.find_last_of("-")) {
949 data += "-01";
950 }
951 time += " 00:00:00";
952 } else {
953 data = dataTime.substr(0, position);
954 time = dataTime.substr(position);
955 if (data.find("-") == data.npos) {
956 data += "-01-01";
957 } else if (data.find_first_of("-") == data.find_last_of("-")) {
958 data += "-01";
959 }
960 if (time.find(":") == data.npos) {
961 time += ":00:00";
962 } else if (time.find_first_of(":") == time.find_last_of(":")) {
963 time += ":00";
964 } else {
965 std::string timeTmp = time;
966 time = timeTmp.substr(0, time.find("."));
967 }
968 }
969 return data + time;
970 }
971
FormatTimeStamp(std::string & value,std::string & src)972 void JpegDecoder::FormatTimeStamp(std::string &value, std::string &src)
973 {
974 value = "";
975 if (!IsSameTextStr(src, "")) {
976 value = SetOriginalTimes(src);
977 }
978 }
979
getExifTagFromKey(const std::string & key)980 ExifTag JpegDecoder::getExifTagFromKey(const std::string &key)
981 {
982 if (IsSameTextStr(key, BITS_PER_SAMPLE)) {
983 return EXIF_TAG_BITS_PER_SAMPLE;
984 } else if (IsSameTextStr(key, ORIENTATION)) {
985 return EXIF_TAG_ORIENTATION;
986 } else if (IsSameTextStr(key, IMAGE_LENGTH)) {
987 return EXIF_TAG_IMAGE_LENGTH;
988 } else if (IsSameTextStr(key, IMAGE_WIDTH)) {
989 return EXIF_TAG_IMAGE_WIDTH;
990 } else if (IsSameTextStr(key, GPS_LATITUDE)) {
991 return EXIF_TAG_GPS_LATITUDE;
992 } else if (IsSameTextStr(key, GPS_LONGITUDE)) {
993 return EXIF_TAG_GPS_LONGITUDE;
994 } else if (IsSameTextStr(key, GPS_LATITUDE_REF)) {
995 return EXIF_TAG_GPS_LATITUDE_REF;
996 } else if (IsSameTextStr(key, GPS_LONGITUDE_REF)) {
997 return EXIF_TAG_GPS_LONGITUDE_REF;
998 } else if (IsSameTextStr(key, DATE_TIME_ORIGINAL)) {
999 return EXIF_TAG_DATE_TIME_ORIGINAL;
1000 } else if (IsSameTextStr(key, EXPOSURE_TIME)) {
1001 return EXIF_TAG_EXPOSURE_TIME;
1002 } else if (IsSameTextStr(key, F_NUMBER)) {
1003 return EXIF_TAG_FNUMBER;
1004 } else if (IsSameTextStr(key, ISO_SPEED_RATINGS)) {
1005 return EXIF_TAG_ISO_SPEED_RATINGS;
1006 } else if (IsSameTextStr(key, SCENE_TYPE)) {
1007 return EXIF_TAG_SCENE_TYPE;
1008 } else if (IsSameTextStr(key, COMPRESSED_BITS_PER_PIXEL)) {
1009 return EXIF_TAG_COMPRESSED_BITS_PER_PIXEL;
1010 } else {
1011 return EXIF_TAG_PRINT_IMAGE_MATCHING;
1012 }
1013 }
1014
ModifyImageProperty(uint32_t index,const std::string & key,const std::string & value,const std::string & path)1015 uint32_t JpegDecoder::ModifyImageProperty(uint32_t index, const std::string &key,
1016 const std::string &value, const std::string &path)
1017 {
1018 IMAGE_LOGD("[ModifyImageProperty] with path:%{public}s, key:%{public}s, value:%{public}s",
1019 path.c_str(), key.c_str(), value.c_str());
1020 ExifTag tag = getExifTagFromKey(key);
1021 if (tag == EXIF_TAG_PRINT_IMAGE_MATCHING) {
1022 return Media::ERR_IMAGE_DECODE_EXIF_UNSUPPORT;
1023 }
1024
1025 uint32_t ret = exifInfo_.ModifyExifData(tag, value, path);
1026 if (ret != Media::SUCCESS) {
1027 return ret;
1028 }
1029 return Media::SUCCESS;
1030 }
1031
ModifyImageProperty(uint32_t index,const std::string & key,const std::string & value,const int fd)1032 uint32_t JpegDecoder::ModifyImageProperty(uint32_t index, const std::string &key,
1033 const std::string &value, const int fd)
1034 {
1035 IMAGE_LOGD("[ModifyImageProperty] with fd:%{public}d, key:%{public}s, value:%{public}s",
1036 fd, key.c_str(), value.c_str());
1037 ExifTag tag = getExifTagFromKey(key);
1038 if (tag == EXIF_TAG_PRINT_IMAGE_MATCHING) {
1039 return Media::ERR_IMAGE_DECODE_EXIF_UNSUPPORT;
1040 }
1041
1042 uint32_t ret = exifInfo_.ModifyExifData(tag, value, fd);
1043 if (ret != Media::SUCCESS) {
1044 return ret;
1045 }
1046 return Media::SUCCESS;
1047 }
1048
ModifyImageProperty(uint32_t index,const std::string & key,const std::string & value,uint8_t * data,uint32_t size)1049 uint32_t JpegDecoder::ModifyImageProperty(uint32_t index, const std::string &key,
1050 const std::string &value, uint8_t *data, uint32_t size)
1051 {
1052 IMAGE_LOGD("[ModifyImageProperty] with key:%{public}s, value:%{public}s",
1053 key.c_str(), value.c_str());
1054 ExifTag tag = getExifTagFromKey(key);
1055 if (tag == EXIF_TAG_PRINT_IMAGE_MATCHING) {
1056 return Media::ERR_IMAGE_DECODE_EXIF_UNSUPPORT;
1057 }
1058
1059 uint32_t ret = exifInfo_.ModifyExifData(tag, value, data, size);
1060 if (ret != Media::SUCCESS) {
1061 return ret;
1062 }
1063 return Media::SUCCESS;
1064 }
1065
GetFilterArea(const int & privacyType,std::vector<std::pair<uint32_t,uint32_t>> & ranges)1066 uint32_t JpegDecoder::GetFilterArea(const int &privacyType, std::vector<std::pair<uint32_t, uint32_t>> &ranges)
1067 {
1068 IMAGE_LOGD("[GetFilterArea] with privacyType:%{public}d ", privacyType);
1069 if (srcMgr_.inputStream == nullptr) {
1070 IMAGE_LOGE("[GetFilterArea] srcMgr_.inputStream is nullptr.");
1071 return Media::ERR_MEDIA_INVALID_OPERATION;
1072 }
1073 uint32_t curPos = srcMgr_.inputStream->Tell();
1074 srcMgr_.inputStream->Seek(ADDRESS_4);
1075 // app1SizeBuf is used to get value of EXIF data size
1076 uint8_t *app1SizeBuf = new uint8_t[JPEG_APP1_SIZE];
1077 uint32_t readSize = 0;
1078 if (!srcMgr_.inputStream->Read(JPEG_APP1_SIZE, app1SizeBuf, JPEG_APP1_SIZE, readSize)) {
1079 IMAGE_LOGE("[GetFilterArea] get app1 size failed.");
1080 return Media::ERR_MEDIA_INVALID_OPERATION;
1081 }
1082 uint32_t app1Size =
1083 static_cast<unsigned int>(app1SizeBuf[1]) | static_cast<unsigned int>(app1SizeBuf[0] << OFFSET_8);
1084 delete[] app1SizeBuf;
1085 uint32_t fsize = static_cast<uint32_t>(srcMgr_.inputStream->GetStreamSize());
1086 if (app1Size > fsize) {
1087 IMAGE_LOGE("[GetFilterArea] file format is illegal.");
1088 return Media::ERR_MEDIA_INVALID_OPERATION;
1089 }
1090
1091 srcMgr_.inputStream->Seek(0);
1092 uint32_t bufSize = app1Size + ADDRESS_4;
1093 // buf is from image file head to exif data end
1094 uint8_t *buf = new uint8_t[bufSize];
1095 srcMgr_.inputStream->Read(bufSize, buf, bufSize, readSize);
1096 uint32_t ret = exifInfo_.GetFilterArea(buf, bufSize, privacyType, ranges);
1097 delete[] buf;
1098 srcMgr_.inputStream->Seek(curPos);
1099 if (ret != Media::SUCCESS) {
1100 IMAGE_LOGE("[GetFilterArea]: failed to get area, errno %{public}d", ret);
1101 return ret;
1102 }
1103 return Media::SUCCESS;
1104 }
1105
1106 #ifdef IMAGE_COLORSPACE_FLAG
getGrColorSpace()1107 OHOS::ColorManager::ColorSpace JpegDecoder::getGrColorSpace()
1108 {
1109 OHOS::ColorManager::ColorSpace grColorSpace = iccProfileInfo_.getGrColorSpace();
1110 return grColorSpace;
1111 }
1112
IsSupportICCProfile()1113 bool JpegDecoder::IsSupportICCProfile()
1114 {
1115 bool isSupportICCProfile = iccProfileInfo_.IsSupportICCProfile();
1116 return isSupportICCProfile;
1117 }
1118 #endif
1119 } // namespace ImagePlugin
1120 } // namespace OHOS
1121