2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file FMedia_ImageImpl.cpp
20 * @brief This file contains the implementation of subsystem's Image.
23 #include <unique_ptr.h>
25 #include <FMediaImage.h>
26 #include <FMediaImageUtil.h>
27 #include <FMediaIImageEventListener.h>
28 #include <FBaseSysLog.h>
29 #include "FMedia_MediaUtil.h"
30 #include "FMedia_ImageImpl.h"
31 #include "FMedia_ImageUtil.h"
32 #include "FMedia_ImageUtilImpl.h"
33 #include "FMedia_ImageDecoder.h"
34 #include "FMedia_ImageEncoder.h"
35 #include "FMedia_ImageUriDataFactory.h"
36 #include "FGrp_BitmapImpl.h"
37 #include "FUi_CoordinateSystemUtils.h"
40 using namespace Tizen::Graphics;
41 using namespace Tizen::Base;
42 using namespace Tizen::Io;
43 using namespace Tizen::Ui;
45 namespace Tizen { namespace Media
48 static const float _SCALE_DOWN_RATIO = 0.5;
49 static const int _RGB565_PIXEL_DEPTH = 2;
50 static const int _MIN_ENCODE_QUALITY = 20; // Changed from 50 to 20 to allow greater compression.
51 static const int _MAX_ENCODE_QUALITY = 99;
52 static const int _DEFAULT_ENCODE_QUALITY = 50;
53 static const int _MAX_ENCODE_LOOP = 2;
54 static const int _MAX_ENCODE_RESIZE_LOOP = 2;
55 //static const int MAX_IMGFILE_SIZE = 4096000;
57 _ImageImpl::_ImageImpl(void)
61 _ImageImpl::~_ImageImpl(void)
66 _ImageImpl::GetInstance(Image* pImage)
70 return pImage->__pImageImpl;
77 _ImageImpl::GetInstance(const Image* pImage)
81 return pImage->__pImageImpl;
88 _ImageImpl::Construct(void)
94 _ImageImpl::DecodeToBitmapN(const Tizen::Base::String& srcImagePath, Tizen::Graphics::BitmapPixelFormat pixelFormat)
96 std::unique_ptr<ByteBuffer> pBuf;
99 pBuf.reset(_MediaUtil::FileToBufferN(srcImagePath));
100 SysTryReturn(NID_MEDIA, pBuf.get() != null, null, GetLastResult(),
101 "[%s] FileToBufferN %S", GetErrorMessage(GetLastResult()), srcImagePath.GetPointer());
103 pBmp = DecodeToBitmapN(*pBuf.get(), pixelFormat);
108 _ImageImpl::DecodeToBitmapN(const Tizen::Base::ByteBuffer& srcImageBuf, Tizen::Graphics::BitmapPixelFormat pixelFormat)
110 result r = E_SUCCESS;
111 std::unique_ptr<ByteBuffer> pBuf;
112 std::unique_ptr<Bitmap> pBmp;
117 r = dec.Construct(srcImageBuf, _ImageUtilImpl::ToMediaPixelFormat(pixelFormat));
118 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Decoder construct failed.", GetErrorMessage(r));
120 pBuf.reset(dec.DecodeN());
121 SysTryReturn(NID_MEDIA, pBuf.get() != null, null, GetLastResult(),
122 "[%s] Propagated", GetErrorMessage(GetLastResult()));
124 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagated. ", GetErrorMessage(r));
126 r = dec.GetDimension(outDim.width, outDim.height);
127 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagated. ", GetErrorMessage(r));
129 pBmp.reset(new (std::nothrow) Bitmap);
130 SysTryReturn(NID_MEDIA, pBmp != null, null, E_OUT_OF_MEMORY,
131 "[%s] new Bitmap", GetErrorMessage(GetLastResult()));
132 r = pBmp->Construct(*pBuf.get(), outDim, pixelFormat, BUFFER_SCALING_AUTO);
133 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagated. ", GetErrorMessage(r));
135 SetLastResult(E_SUCCESS);
136 return pBmp.release();
140 _ImageImpl::DecodeToBitmapN(const Tizen::Base::String& srcImagePath, Tizen::Graphics::BitmapPixelFormat pixelFormat, const Tizen::Graphics::Dimension &destDim,
141 Tizen::Graphics::BufferScaling bufferScaling)
143 std::unique_ptr<ByteBuffer> pBuf;
146 pBuf.reset(_MediaUtil::FileToBufferN(srcImagePath));
147 SysTryReturn(NID_MEDIA, pBuf.get() != null, null, GetLastResult(),
148 "[%s] FileToBufferN %S", GetErrorMessage(GetLastResult()), srcImagePath.GetPointer());
150 pBmp = DecodeToBitmapN(*pBuf.get(), pixelFormat, destDim, bufferScaling);
155 _ImageImpl::DecodeToBitmapN(const Tizen::Base::ByteBuffer& srcImageBuf, Tizen::Graphics::BitmapPixelFormat pixelFormat, const Tizen::Graphics::Dimension &destDim,
156 Tizen::Graphics::BufferScaling bufferScaling)
158 result r = E_SUCCESS;
159 std::unique_ptr<ByteBuffer> pBuf;
160 std::unique_ptr<Bitmap> pBmp;
165 r = dec.Construct(srcImageBuf, _ImageUtilImpl::ToMediaPixelFormat(pixelFormat));
166 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Decoder construct failed.", GetErrorMessage(r));
168 // Reset decoder output demension
169 dec.SetOutputDimension(destDim.width, destDim.height, true);
171 pBuf.reset(dec.DecodeN());
172 SysTryReturn(NID_MEDIA, pBuf.get() != null, null, GetLastResult(),
173 "[%s] Propagated", GetErrorMessage(GetLastResult()));
175 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagated. ", GetErrorMessage(r));
177 r = dec.GetDimension(outDim.width, outDim.height);
178 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagated. ", GetErrorMessage(r));
180 pBmp.reset(new (std::nothrow) Bitmap);
181 SysTryReturn(NID_MEDIA, pBmp != null, null, E_OUT_OF_MEMORY,
182 "[%s] new Bitmap", GetErrorMessage(GetLastResult()));
183 r = pBmp->Construct(*pBuf.get(), outDim, pixelFormat, bufferScaling);
184 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagated. ", GetErrorMessage(r));
186 SetLastResult(E_SUCCESS);
187 return pBmp.release();
191 _ImageImpl::DecodeToBitmapN(const Tizen::Base::ByteBuffer& srcImageBuf, Tizen::Graphics::BitmapPixelFormat pixelFormat,
192 const Tizen::Graphics::FloatDimension &destDim)
194 result r = E_SUCCESS;
195 std::unique_ptr<ByteBuffer> pBuf;
196 std::unique_ptr<Bitmap> pBmp;
197 Dimension orgDim, tmpDim;
198 FloatDimension outDim;
203 r = dec.Construct(srcImageBuf, _ImageUtilImpl::ToMediaPixelFormat(pixelFormat));
204 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Decoder construct failed.", GetErrorMessage(r));
206 r = dec.GetDimension(orgDim.width, orgDim.height);
207 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagated. ", GetErrorMessage(r));
209 // Reset decoder output demension
210 dec.SetOutputDimension(_CoordinateSystemUtils::ConvertToInteger(destDim.width),
211 _CoordinateSystemUtils::ConvertToInteger(destDim.height),
214 pBuf.reset(dec.DecodeN());
215 SysTryReturn(NID_MEDIA, pBuf.get() != null, null, GetLastResult(),
216 "[%s] Propagated", GetErrorMessage(GetLastResult()));
218 r = dec.GetDimension(tmpDim.width, tmpDim.height);
219 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagated. ", GetErrorMessage(r));
221 r = _ImageUtil::GetResizedDimension(orgDim.width, orgDim.height, destDim.width, destDim.height, outDim.width, outDim.height);
222 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagated. ", GetErrorMessage(r));
224 pBmp.reset(new (std::nothrow) Bitmap);
225 SysTryReturn(NID_MEDIA, pBmp.get() != null, null, E_OUT_OF_MEMORY,
226 "[%s] new Bitmap", GetErrorMessage(GetLastResult()));
228 pBmp.reset(_BitmapImpl::GetNonScaledBitmapN(*pBuf.get(), tmpDim, pixelFormat, destDim));
229 SetLastResult(E_SUCCESS);
230 return pBmp.release();
234 _ImageImpl::DecodeToBitmapN(const Tizen::Base::String& srcImagePath, Tizen::Graphics::BitmapPixelFormat pixelFormat,
235 const Tizen::Graphics::FloatDimension &destDim)
237 std::unique_ptr<ByteBuffer> pBuf;
240 pBuf.reset(_MediaUtil::FileToBufferN(srcImagePath));
241 SysTryReturn(NID_MEDIA, pBuf.get() != null, null, GetLastResult(),
242 "[%s] FileToBufferN %S", GetErrorMessage(GetLastResult()), srcImagePath.GetPointer());
244 pBmp = DecodeToBitmapN(*pBuf.get(), pixelFormat, destDim);
248 Tizen::Base::ByteBuffer*
249 _ImageImpl::DecodeToBufferN(const Tizen::Base::String& srcImagePath,
250 Tizen::Graphics::Dimension &restDim,
251 const Tizen::Graphics::Dimension &dstDim,
252 Tizen::Graphics::BitmapPixelFormat pixelFormat,
253 ImageFormat imgFormat, bool keepAspectRatio)
255 std::unique_ptr<ByteBuffer> pBuf;
256 ByteBuffer* pRetBuf = null;
258 //SysTryCatch(NID_MEDIA, IS_VALID_DIMENSION(dstDim), , E_OUT_OF_RANGE,
259 //"[E_OUT_OF_RANGE] dest dimension:%d,%d", dstDim.width, dstDim.height);
261 pBuf.reset(_MediaUtil::FileToBufferN(srcImagePath));
262 SysTryReturn(NID_MEDIA, pBuf.get() != null, null, GetLastResult(),
263 "[%s] FileToBufferN %S", GetErrorMessage(GetLastResult()), srcImagePath.GetPointer());
265 pRetBuf = DecodeToBufferN(*pBuf.get(), restDim, dstDim, pixelFormat, imgFormat, keepAspectRatio);
269 Tizen::Base::ByteBuffer*
270 _ImageImpl::DecodeToBufferN(const Tizen::Base::ByteBuffer &srcImageBuf,
271 Tizen::Graphics::Dimension &retDim,
272 const Tizen::Graphics::Dimension &dstDim,
273 Tizen::Graphics::BitmapPixelFormat pixelFormat,
274 ImageFormat imgFormat, bool keepAspectRatio)
276 result r = E_SUCCESS;
277 std::unique_ptr<ByteBuffer> pBuf;
280 //SysTryCatch(NID_MEDIA, IS_VALID_BITMAP_PIXEL(pixelFormat), , E_INVALID_ARG,
281 //"[E_INVALID_ARG] pixelFormat:%d", pixelFormat);
283 r = dec.Construct(srcImageBuf, _ImageUtilImpl::ToMediaPixelFormat(pixelFormat), imgFormat);
284 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Decoder construct failed.", GetErrorMessage(r));
286 // Reset decoder output demension
287 dec.SetOutputDimension(dstDim.width, dstDim.height, keepAspectRatio);
289 pBuf.reset(dec.DecodeN());
291 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagated. ", GetErrorMessage(r));
293 r = dec.GetDimension(retDim.width, retDim.height);
294 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagated. ", GetErrorMessage(r));
296 SetLastResult(E_SUCCESS);
297 return pBuf.release();
300 Tizen::Base::ByteBuffer*
301 _ImageImpl::DecodeToBufferN(const Tizen::Base::String& srcImagePath,
302 Tizen::Graphics::BitmapPixelFormat pixelFormat,
303 int& imageWidth, int& imageHeight, bool keepAspectRatio)
305 ByteBuffer *pBuf = null;
308 pBuf = DecodeToBufferN(srcImagePath, dim, Dimension(0, 0), pixelFormat, IMG_FORMAT_NONE, keepAspectRatio);
309 imageWidth = dim.width;
310 imageHeight = dim.height;
315 Tizen::Base::ByteBuffer*
316 _ImageImpl::DecodeN(const Tizen::Base::String& srcImagePath,
317 Tizen::Graphics::BitmapPixelFormat pixelFormat,
318 int destWidth, int destHeight, bool keepAspectRatio)
322 return DecodeToBufferN(srcImagePath, dim, Dimension(destWidth, destHeight), pixelFormat, IMG_FORMAT_NONE, keepAspectRatio);
326 _ImageImpl::DecodeUrl(const Tizen::Base::Utility::Uri& srcImageUrl,
327 Tizen::Graphics::BitmapPixelFormat colorFormat,
328 int destWidth, int destHeight, RequestId& reqId,
329 const IImageDecodeUrlEventListener& listener, long timeout)
331 result r = E_SUCCESS;
333 _ImageUriDataFactory *pFactory = null;
335 SysTryCatch(NID_MEDIA, (colorFormat > Tizen::Graphics::BITMAP_PIXEL_FORMAT_MIN)
336 && colorFormat < Tizen::Graphics::BITMAP_PIXEL_FORMAT_MAX, , E_INVALID_ARG,
337 "[E_INVALID_ARG] Wrong color format.");
339 SysTryCatch(NID_MEDIA, destWidth > 0 && destHeight > 0, , E_OUT_OF_RANGE,
340 "[E_OUT_OF_RANGE] The designated width and height SHOUL be greater than 0.");
342 SysTryCatch(NID_MEDIA, timeout == _TIMEOUT_INFINITE || timeout > 0 ,
343 r = E_OUT_OF_RANGE, E_OUT_OF_RANGE,
344 "[E_OUT_OF_RANGE] The designated timeout SHOUL be greater than 0");
346 pFactory = _ImageUriDataFactory::GetInstance();
347 SysTryCatch(NID_MEDIA, pFactory != null, r = GetLastResult(), r,
348 "[%s] Failed to GetInstance().", GetErrorMessage(r));
350 r = pFactory->DecodeUrl(srcImageUrl, colorFormat, destWidth,destHeight, reqId, listener, timeout);
351 SysTryCatch(NID_MEDIA, r == E_SUCCESS , , r,"[%s] Propagated.", GetErrorMessage(r));
360 _ImageImpl::EncodeToBufferN(const Bitmap& srcBmp, ImageFormat dstFormat)
362 std::unique_ptr<ByteBuffer> pBuf;
366 // ClearLastResult();
368 Tizen::Graphics::Bitmap& tmpBmp = const_cast<Tizen::Graphics::Bitmap&>(srcBmp);
371 tmpBmp.Lock(bmpInfo);
373 dim.SetSize(bmpInfo.width, bmpInfo.height);
375 // TODO: handle case that pitch != width * bpp/8
376 SysTryReturn(NID_MEDIA, bmpInfo.pitch == bmpInfo.width*bmpInfo.bitsPerPixel/8, null, E_SYSTEM,
377 "[E_SYSTEM] pitch=%d bpp=%d", bmpInfo.pitch, bmpInfo.bitsPerPixel);
379 pBuf.reset(_ImageEncoder::EncodeN(dstFormat, (const byte*)bmpInfo.pPixels, bmpInfo.pitch * bmpInfo.height,
380 dim.width, dim.height,
381 _ImageUtilImpl::ToMediaPixelFormat(srcBmp.GetPixelColorFormat()),
382 _DEFAULT_ENCODE_QUALITY));
383 SysTryReturn(NID_MEDIA, pBuf.get() != null, null, GetLastResult(), "[%s] Propagated. ", GetErrorMessage(GetLastResult()));
385 return pBuf.release();
389 _ImageImpl::EncodeToFile(const Bitmap& srcBmp, ImageFormat dstFormat,
390 const String& dstPath, bool overwrite)
392 result r = E_SUCCESS;
393 std::unique_ptr<ByteBuffer> pBuf;
395 pBuf.reset(EncodeToBufferN(srcBmp, dstFormat));
396 SysTryReturn(NID_MEDIA, pBuf.get() != null, GetLastResult(), GetLastResult(),
397 "[%s] Propagated.", GetErrorMessage(GetLastResult()));
399 r = _MediaUtil::BufferToFile(*pBuf.get(), dstPath, overwrite);
400 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagated. ", GetErrorMessage(r));
406 Tizen::Base::ByteBuffer*
407 _ImageImpl::CompressJpegN(const Tizen::Base::ByteBuffer& srcImageBuf, int limitSize)
409 result r = E_SUCCESS;
410 std::unique_ptr<ByteBuffer> pDecBuf;
411 std::unique_ptr<ByteBuffer> pEncBuf;
414 volatile int limit = limitSize;
415 MediaPixelFormat pixelFormat = MEDIA_PIXEL_FORMAT_BGRA8888;
417 // TODO : use yuv pixel format
419 r = dec.Construct(srcImageBuf, pixelFormat);
420 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Decoder construct failed.", GetErrorMessage(r));
422 pDecBuf.reset(dec.DecodeN());
424 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagated. ", GetErrorMessage(r));
425 SysTryReturn(NID_MEDIA, pDecBuf.get() != null, null, E_SYSTEM, "[E_SYSTEM] Propagated.");
427 r = dec.GetDimension(srcDim.width, srcDim.height);
428 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagated. ", GetErrorMessage(r));
430 for (int i = 0; i < _MAX_ENCODE_RESIZE_LOOP; i++)
433 ByteBuffer *pTmpBuf = null;
435 pEncBuf.reset(EncodeToBufferLimitN(*pDecBuf.get(), srcDim, pixelFormat, IMG_FORMAT_JPG, limit));
437 if (pEncBuf.get() != null)
442 // halve and make even number
443 dstDim.width = srcDim.width >> 2;
444 dstDim.height = srcDim.height >> 2;
446 dstDim.width = dstDim.width << 1;
447 dstDim.height = dstDim.height << 1;
449 SysTryReturn(NID_MEDIA, srcDim != dstDim && dstDim.width > 0 && dstDim.height > 0, null, E_SYSTEM,
450 "[E_SYSTEM] Resize failed:src:%dx%d dst:%dx%d, %d %d, %d",
451 srcDim.width, srcDim.height, dstDim.width, dstDim.height, i, _SCALE_DOWN_RATIO,
454 pTmpBuf = _ImageUtilImpl::ResizeN(*pDecBuf.get(), srcDim, pixelFormat, dstDim);
455 SysTryReturn(NID_MEDIA, pTmpBuf != null, null, GetLastResult(),
456 "[%s] ResizeN: %dx%d->%dx%d", GetErrorMessage(GetLastResult()),
457 srcDim.width, srcDim.height, dstDim.width, dstDim.height);
460 pDecBuf.reset(pTmpBuf);
462 SysTryReturn(NID_MEDIA, pEncBuf.get() != null, null, E_SYSTEM, "[E_SYSTEM] Compress failed");
463 SetLastResult(E_SUCCESS);
464 return pEncBuf.release();
468 _ImageImpl::CompressJpeg(const Tizen::Base::String& srcImagePath,
469 const Tizen::Base::String& dstImagePath, int limitSize)
472 result r = E_SUCCESS;
473 std::unique_ptr<ByteBuffer> pSrcBuf;
474 std::unique_ptr<ByteBuffer> pEncBuf;
476 pSrcBuf.reset(_MediaUtil::FileToBufferN(srcImagePath));
477 SysTryReturn(NID_MEDIA, pSrcBuf.get() != null, GetLastResult(), GetLastResult(),
478 "[%s] Propagated. ", GetErrorMessage(GetLastResult()));
480 // SysTryCatch(NID_MEDIA, pSrcBuf->GetCapacity() < MAX_IMGFILE_SIZE, r = E_OVERFLOW, E_OVERFLOW,
481 // "[E_OUT_OF_RANGE] Input file size is smaller than limitSize.");
483 SysTryReturn(NID_MEDIA, pSrcBuf.get()->GetCapacity() > limitSize, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
484 "[E_OUT_OF_RANGE] Input file size is smaller than limitSize.");
486 pEncBuf.reset(CompressJpegN(*pSrcBuf, limitSize));
487 SysTryReturn(NID_MEDIA, pEncBuf != null, GetLastResult(), GetLastResult(),
488 "[%s] Propagated. ", GetErrorMessage(GetLastResult()));
490 if (File::IsFileExist(dstImagePath))
492 File::Remove(dstImagePath);
495 r = file.Construct(dstImagePath, "wb", true);
496 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagated. ", GetErrorMessage(r));
497 r = file.Write(*pEncBuf);
498 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagated. ", GetErrorMessage(r));
503 Tizen::Base::ByteBuffer*
504 _ImageImpl::ConvertToBufferN(const Tizen::Base::String& srcImagePath, ImageFormat destImageFormat)
506 std::unique_ptr<Bitmap> pBmp;
507 ByteBuffer* pBuf = null;
509 pBmp.reset(DecodeToBitmapN(srcImagePath, BITMAP_PIXEL_FORMAT_ARGB8888, Dimension(0, 0),
510 BUFFER_SCALING_NONE));
511 SysTryReturn(NID_MEDIA, pBmp.get(), null, GetLastResult(),
512 "[%s] Propagated. ", GetErrorMessage(GetLastResult()));
514 pBuf = EncodeToBufferN(*pBmp.get(), destImageFormat);
515 SysTryReturn(NID_MEDIA, pBuf != null, null, GetLastResult(),
516 "[%s] Propagated.", GetErrorMessage(GetLastResult()));
518 SetLastResult(E_SUCCESS);
522 Tizen::Base::ByteBuffer*
523 _ImageImpl::EncodeToBufferLimitN(const Tizen::Base::ByteBuffer &srcBuf,
524 const Tizen::Graphics::Dimension& dim,
525 MediaPixelFormat pixelFormat, ImageFormat imgFormat, int limit)
527 int minQuality = _MIN_ENCODE_QUALITY;
528 int maxQuality = _MAX_ENCODE_QUALITY;
533 std::unique_ptr<ByteBuffer> pMaxBuf;
534 std::unique_ptr<ByteBuffer> pMinBuf;
536 pMaxBuf.reset(EncodeToBufferQualityN(srcBuf, dim, pixelFormat, imgFormat, maxQuality));
537 SysTryReturn(NID_MEDIA, pMaxBuf.get() != null, pMinBuf.get(), E_SYSTEM, "[E_SYSTEM] maxBuf is null");
538 maxDiff = pMaxBuf->GetLimit() - limit;
539 SysLog(NID_MEDIA, "maxDiff:%d buf:%d limit:%d q:%d",
540 maxDiff, pMaxBuf->GetLimit(), limit, maxQuality);
543 SetLastResult(E_SUCCESS);
544 return pMaxBuf.release();
546 pMinBuf.reset(EncodeToBufferQualityN(srcBuf, dim, pixelFormat, imgFormat, minQuality));
547 SysTryReturn(NID_MEDIA, pMinBuf.get() != null, pMinBuf.release(), E_SYSTEM, "[E_SYSTEM] minBuf is null");
548 minDiff = pMinBuf->GetLimit() - limit;
549 SysLog(NID_MEDIA, "minDiff:%d buf:%d limit:%d q:%d",
550 minDiff, pMinBuf->GetLimit(), limit, minQuality);
553 SetLastResult(E_OVERFLOW);
558 SetLastResult(E_SUCCESS);
559 return pMinBuf.release();
562 for (int i = 0; i < _MAX_ENCODE_LOOP; i++)
564 int prevQuality = curQuality;
566 ByteBuffer *pCurBuf = null;
568 curQuality = (minQuality + maxQuality) / 2;
569 if (prevQuality == curQuality)
573 pCurBuf = EncodeToBufferQualityN(srcBuf, dim, pixelFormat, imgFormat, curQuality);
574 if (pCurBuf == null && pMinBuf.get() != null)
578 SysTryReturn(NID_MEDIA, pCurBuf != null, pMinBuf.release(), E_SYSTEM,
579 "[E_SYSTEM] curBuf is null : %d %d %d", dim.width, dim.height, curQuality);
580 curDiff = pCurBuf->GetLimit() - limit;
581 SysLog(NID_MEDIA, "curDiff:%d buf:%d limit:%d q:%d",
582 curDiff, pCurBuf->GetLimit(), limit, curQuality);
585 SetLastResult(E_SUCCESS);
588 else if (curDiff > 0)
591 maxQuality = curQuality;
592 pMaxBuf.reset(pCurBuf);
594 else if (curDiff < 0)
596 pMinBuf.reset(pCurBuf);
598 minQuality = curQuality;
603 SetLastResult(E_SUCCESS);
607 // should not come here.
608 SetLastResult(E_SYSTEM);
610 return pMinBuf.release();
613 Tizen::Base::ByteBuffer*
614 _ImageImpl::EncodeToBufferQualityN(const Tizen::Base::ByteBuffer &srcBuf,
615 const Tizen::Graphics::Dimension& dim,
616 MediaPixelFormat pixelFormat, ImageFormat imgFormat, int quality)
618 ByteBuffer* pBuf = null;
620 pBuf = _ImageEncoder::EncodeN(imgFormat, srcBuf.GetPointer(), srcBuf.GetLimit(),
621 dim.width, dim.height, pixelFormat, quality);
622 SysTryReturn(NID_MEDIA, pBuf != null, pBuf, GetLastResult(),
623 "[%s] enc.EncodeN. ", GetErrorMessage(GetLastResult()));
629 _ImageImpl::GetImageInfo(const Tizen::Base::String& path, ImageFormat &imgFormat,
630 Tizen::Graphics::Dimension &dim)
632 return _ImageDecoder::GetImageInfo(path, imgFormat, dim.width, dim.height);