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_JpegEncoder.cpp
20 * @brief This file contains the implementation of _JpegEncoder class.
26 #include <FMediaImageTypes.h>
27 #include <FBaseSysLog.h>
28 #include "FMedia_JpegEncoder.h"
30 using namespace Tizen::Base;
32 namespace Tizen { namespace Media
35 static const int MIN_JPEG_ENC_WIDTH = 16;
36 static const int MIN_JPEG_ENC_HEIGHT = 16;
37 static const int MAX_JPEG_ENC_WIDTH = 5000;
38 static const int MAX_JPEG_ENC_HEIGHT = 5000;
40 static void JpegErrorExitStatic(struct jpeg_common_struct * pJpegInfo);
42 _JpegEncoder::_JpegEncoder(void)
44 __pixelFormat = MEDIA_PIXEL_FORMAT_NONE;
50 _JpegEncoder::~_JpegEncoder(void)
56 _JpegEncoder::Construct(int width, int height,
57 MediaPixelFormat srcPixelFormat,
58 MediaPixelFormat& reqPixelFormat,
61 SysTryReturnResult(NID_MEDIA, width >= MIN_JPEG_ENC_WIDTH, E_INVALID_ARG,
62 "Width (%d) should be greater than (%d)", width, MIN_JPEG_ENC_WIDTH);
63 SysTryReturnResult(NID_MEDIA, width <= MAX_JPEG_ENC_WIDTH, E_OVERFLOW,
64 "width (%d) should be lesser than (%d)", width, MAX_JPEG_ENC_WIDTH);
65 SysTryReturnResult(NID_MEDIA, height >= MIN_JPEG_ENC_HEIGHT, E_INVALID_ARG,
66 "Height (%d) should be greater than (%d)", height, MIN_JPEG_ENC_HEIGHT);
67 SysTryReturnResult(NID_MEDIA, height <= MAX_JPEG_ENC_HEIGHT, E_OVERFLOW,
68 "Height (%d) should be lesser than (%d)", height, MAX_JPEG_ENC_HEIGHT);
69 SysTryReturnResult(NID_MEDIA, quality > 0, E_INVALID_ARG,
70 "Quality (%d) should be greater than zero.", quality);
71 SysTryReturnResult(NID_MEDIA, quality <= 100, E_INVALID_ARG,
72 "Quality (%d) quality should be lesser than 100.", quality);
74 // TODO: add handling of srcPixelFormat
77 __pixelFormat = MEDIA_PIXEL_FORMAT_RGB888;
78 reqPixelFormat = MEDIA_PIXEL_FORMAT_RGB888;
85 Tizen::Base::ByteBuffer*
86 _JpegEncoder::EncodeN(const byte* srcBuf, int srcLength)
90 struct jpeg_compress_struct cinfo;
91 struct jpeg_error_mgr jerr;
92 JSAMPROW ppRows[2] = {0,0}; // pointer to a single row
94 unsigned long outSize = 0;
98 ByteBuffer* pRetBuf = null;
100 //SysTryReturn(NID_MEDIA, srcLength <= JPG_MAX_SIZE, E_OVERFLOW, E_OVERFLOW,
101 // "Buffer size is overflowed : size %d byte", srcLength);
102 //SysTryReturn(NID_MEDIA, ((dim.width <= JPG_MAX_WIDTH) &&
103 // (dim.height <= JPG_MAX_HEIGTH)), E_OVERFLOW, E_OVERFLOW,
104 // "Dimensions too large : width %d / height :%d", dim.width, dim.height);
107 cinfo.err = jpeg_std_error(&jerr);
108 jerr.error_exit = JpegErrorExitStatic;
109 cinfo.client_data = &errJump;
111 SysTryCatch(NID_MEDIA, !setjmp(errJump), r = E_INVALID_DATA, E_INVALID_DATA,
112 "[E_INVALID_DATA] error jump");
114 jpeg_create_compress(&cinfo);
116 // Set output to dest buf
117 jpeg_mem_dest(&cinfo, &pOutBuf, &outSize);
119 cinfo.image_width = __width;
120 cinfo.image_height = __height;
121 cinfo.input_components = 3; // TODO: GetBytesPerPixel(pixelFormat);
122 cinfo.in_color_space = JCS_RGB; // colorspace of input image // or JCS_YCbCr
124 jpeg_set_defaults(&cinfo);
125 // Make optional parameter settings here
126 jpeg_set_quality(&cinfo, __quality, false);
128 jpeg_start_compress(&cinfo, TRUE);
130 rowStride = __width * 3; // JSAMPLEs per row in image_buffer // 4, if use JCS_YCbCr
132 // position = (byte*) srcBuf;
133 while (cinfo.next_scanline < cinfo.image_height)
135 ppRows[0] = (byte*)srcBuf + cinfo.next_scanline * rowStride;
136 jpeg_write_scanlines(&cinfo, ppRows, 1);
139 jpeg_finish_compress(&cinfo);
140 jpeg_destroy_compress(&cinfo);
142 SysTryCatch(NID_MEDIA, outSize > 0, r = E_SYSTEM, E_SYSTEM,
143 "[E_SYSTEM] jpeg lib returned outSize (%d)", outSize);
145 SysTryCatch(NID_MEDIA, pOutBuf != null, r = E_SYSTEM, E_SYSTEM,
146 "[E_SYSTEM] outbuf is null.");
148 //dstLength = (int) outSize;
149 //dstBuf = new (std::nothrow) byte[outSize];
150 pRetBuf = new (std::nothrow) ByteBuffer();
151 SysTryCatch(NID_MEDIA, pRetBuf != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
152 "[E_OUT_OF_MEMORY] Could not create new ByteBuffer.");
154 r = pRetBuf->Construct(outSize);
155 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagated. Failed to construct ByteBuffer of size %d",
156 GetErrorMessage(r), outSize);
157 r = pRetBuf->SetArray(pOutBuf, 0, outSize);
158 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagated. Failed to SetArray for buffer %x, size %d",
159 GetErrorMessage(r), pOutBuf, outSize);
179 JpegErrorExitStatic(struct jpeg_common_struct * pJpegInfo)
183 jmp_buf *pBuf = (jmp_buf*)pJpegInfo->client_data;
192 _JpegEncoder::SetValue(const Tizen::Base::String& key, Tizen::Base::Object &value)
194 return E_UNSUPPORTED_OPERATION;