merge commits of 2.2.1 to public
[platform/framework/native/image.git] / inc / FMediaImageUtil.h
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 /**
19  * @file                FMediaImageUtil.h
20  * @brief               This is the header file for the %ImageUtil class.
21  *
22  * This header file contains the declarations of the %ImageUtil class.
23  */
24
25 #ifndef _FMEDIA_IMAGE_UTIL_H_
26 #define _FMEDIA_IMAGE_UTIL_H_
27
28 #include <FBase.h>
29 #include <FGraphics.h>
30
31 #include <FMediaTypes.h>
32 #include <FMediaImageTypes.h>
33
34 namespace Tizen { namespace Media
35 {
36
37 class _ImageUtilImpl;
38
39 /**
40  * @class        ImageUtil
41  * @brief        This class provides methods for flipping, rotating, resizing, and converting the color space of an image.
42  *
43  * @since        2.0
44  *
45  * The %ImageUtil class provides methods for image pixel format conversion, and image scaling, such as resizing, rotating, and flipping.
46  *
47  * For more information on the class features, see <a href="../org.tizen.native.appprogramming/html/guide/media/viewing_processing_still_images.htm">Viewing and Processing Still Images</a>.
48  *
49  * The following example demonstrates how to use the %ImageUtil class.
50  *
51  * @code
52
53 #include <FBase.h>
54 #include <FIo.h>
55 #include <FApp.h>
56 #include <FGraphics.h>
57 #include <FMedia.h>
58
59 using namespace Tizen::Base;
60 using namespace Tizen::Graphics;
61 using namespace Tizen::Media;
62
63 result
64 ImageUtilSample(void)
65 {
66         ByteBuffer yuvBuf, rgbBuf;
67         Bitmap bitmap;
68         Image image;
69         int width = 320, height = 240;
70         int yuvBufSize = width * height * 3 / 2;
71         int rgbBufSize = width * height * 4;
72         byte *pBuf = null;
73         Dimension dim(width, height);
74         String filePath = Tizen::App::App::GetInstance()->GetAppRootPath() + L"data/test.jpg";
75
76         pBuf = new byte[yuvBufSize];
77
78         // Fills source buffer with a 2x2 checkboard
79         for (int j = 0; j < height; j++)
80         {
81                 for (int i = 0; i < width; i++)
82                 {
83                         pBuf[j * width + i] = (((i >= width / 2) + (j >= height / 2)) & 1) ? 255 : 0; // Y
84                 }
85         }
86         memset(pBuf + width * height, 128, width * height / 4);   // Cb
87         memset(pBuf + width * height * 5 / 4, 128, width * height / 4);  // Cr
88
89         // Creates yuv and rgb buffers
90         yuvBuf.Construct(pBuf, 0, yuvBufSize, yuvBufSize);
91         yuvBuf.SetPosition(0);
92         yuvBuf.SetLimit(yuvBufSize);
93
94         rgbBuf.Construct(rgbBufSize);
95         rgbBuf.SetPosition(0);
96         rgbBuf.SetLimit(rgbBufSize);
97
98         image.Construct();
99
100         // Converts pixel format
101         ImageUtil::ConvertPixelFormat(yuvBuf, rgbBuf, MEDIA_PIXEL_FORMAT_YUV420P, MEDIA_PIXEL_FORMAT_BGRA8888, dim);
102
103         // Makes a Bitmap with converted rgb data
104         bitmap.Construct(rgbBuf, dim, BITMAP_PIXEL_FORMAT_ARGB8888, BUFFER_SCALING_NONE);
105         image.EncodeToFile(bitmap, IMG_FORMAT_JPG, filePath, true);
106
107         delete pBuf;
108         return E_SUCCESS;
109 }
110
111  * @endcode
112  *
113  */
114
115 class _OSP_EXPORT_ ImageUtil
116 {
117
118 public:
119         /**
120         *   Converts the pixel format of the image.
121         *
122         *   @since         2.0
123         *
124         *   @return             An error code
125         *   @param[in]  srcBuf                                    The source buffer
126         *   @param[out] destBuf                                   The destination buffer
127         *   @param[in]  srcPixelFormat                    The source pixel format
128         *   @param[in]  destPixelFormat                   The destination pixel format
129         *   @param[in]  dim                                               The width and the height of the source and the destination images @n
130         *                                                                                 The values of the width and the height must be greater than or equal to @c 1.
131         *   @exception   E_SUCCESS                            The method is successful.
132         *   @exception   E_UNSUPPORTED_FORMAT     The specified format is not supported.
133         *   @exception   E_INVALID_ARG                    Either of the following conditions has occurred:
134         *                                                                                 - The specified width and height are invalid.
135         *                                                                                 - The size of the specified @c srcBuf or the specified @c destBuf is insufficient.
136         *   @exception   E_INVALID_DATA                   The source image data in the buffer is invalid.
137         *   @exception   E_OUT_OF_MEMORY                  The memory is insufficient.
138         *   @exception   E_SYSTEM                                 A system error has occurred.
139         *   @remarks
140         *                         - The supported pixel formats are @c MEDIA_PIXEL_FORMAT_RGB565LE, @c MEDIA_PIXEL_FORMAT_BGRA8888, and @c MEDIA_PIXEL_FORMAT_YUV420P.
141         *                         - The conversion is possible between:
142         *                                  - @c MEDIA_PIXEL_FORMAT_YUV420P and @c MEDIA_PIXEL_FORMAT_RGB565LE.
143         *                                  - @c MEDIA_PIXEL_FORMAT_YUV420P and @c MEDIA_PIXEL_FORMAT_BGRA8888.
144         *                         - The position and the limit of @c destBuf are set along with the size of the converted data.
145         */
146         static result ConvertPixelFormat(const Tizen::Base::ByteBuffer& srcBuf, Tizen::Base::ByteBuffer& destBuf, MediaPixelFormat srcPixelFormat, MediaPixelFormat destPixelFormat, const Tizen::Graphics::Dimension& dim);
147
148         /**
149         *   Resizes the image.
150         *
151         *   @since         2.0
152         *
153         *   @return              An error code
154         *   @param[in]   srcBuf                                         The source buffer
155         *   @param[out]  destBuf                                        The destination buffer
156         *   @param[in]   srcDim                                         The width and the height of the source image @n
157         *                                                                                       The values of the width and the height must be greater than or equal to @c 1.
158         *   @param[in]   destDim                                        The width and the height of the destination image @n
159         *                                                                                       The values of the width and the height must be greater than or equal to @c 1.
160         *   @param[in]   pixelFormat                            The source and the destination pixel format
161         *   @exception   E_SUCCESS                                  The method is successful.
162         *   @exception   E_UNSUPPORTED_FORMAT           The specified format is not supported.
163         *   @exception   E_INVALID_ARG                      Either of the following conditions has occurred:
164         *                                                                                       - The specified width and height are invalid.
165         *                                                                                       - The size of the specified @c srcBuf or the specified @c destBuf is insufficient.
166         *   @exception   E_INVALID_DATA                         The source image data in the buffer is invalid.
167         *   @exception   E_OUT_OF_MEMORY                        The memory is insufficient.
168         *   @exception   E_SYSTEM                                       A system error has occurred.
169         *   @remarks
170         *                         - The supported pixel formats are @c MEDIA_PIXEL_FORMAT_RGB565LE, @c MEDIA_PIXEL_FORMAT_BGRA8888, and @c MEDIA_PIXEL_FORMAT_YUV420P.
171         *                         - The position and the limit of @c destBuf are set along with the size of the resized data.
172         *                         - The minimum resolution is 16x16.
173         */
174         static result Resize(const Tizen::Base::ByteBuffer& srcBuf, Tizen::Base::ByteBuffer& destBuf, const Tizen::Graphics::Dimension& srcDim, const Tizen::Graphics::Dimension& destDim, MediaPixelFormat pixelFormat);
175
176         /**
177         *   Rotates the image.
178         *
179         *   @since         2.0
180         *
181         *   @return              An error code
182         *   @param[in]   srcBuf                                         The source buffer
183         *   @param[out]  destBuf                                        The destination buffer
184         *   @param[in]   dim                                            The width and the height of the source image @n
185         *                                                                                       The values of the width and the height must be equal to or greater than @c 1.
186         *   @param[in]   rotate                                         The rotation type
187         *   @param[in]   pixelFormat                            The source and the destination pixel formats
188         *   @exception   E_SUCCESS                                      The method is successful.
189         *   @exception   E_UNSUPPORTED_FORMAT           The specified format is not supported.
190         *   @exception   E_INVALID_ARG                          Either of the following conditions has occurred:
191         *                                                                                       - The specified width and height are invalid.
192         *                                                                                       - The size of the specified @c srcBuf or the specified @c destBuf is insufficient.
193         *   @exception   E_INVALID_DATA                         The source image data in the buffer is invalid.
194         *   @exception   E_OUT_OF_MEMORY                        The memory is insufficient.
195         *   @exception   E_SYSTEM                                       A system error has occurred.
196         *   @remarks
197         *                         - The supported pixel formats are @c MEDIA_PIXEL_FORMAT_RGB565LE, @c MEDIA_PIXEL_FORMAT_BGRA8888, and @c MEDIA_PIXEL_FORMAT_YUV420P.
198         *                         - The position and the limit of @c destBuf are set along with the size of the rotated data.
199         *                         - The dimension of the destination buffer must be calculated by the application.
200         *   @see        ImageRotationType
201         */
202         static result Rotate(const Tizen::Base::ByteBuffer& srcBuf, Tizen::Base::ByteBuffer& destBuf, const Tizen::Graphics::Dimension& dim, ImageRotationType rotate, MediaPixelFormat pixelFormat);
203
204         /**
205         *   Flips the image.
206         *
207         *   @since               2.0
208         *
209         *   @return              An error code
210         *   @param[in]   srcBuf                                         The source buffer
211         *   @param[out]  destBuf                                    The destination buffer
212         *   @param[in]   dim                                            The width and the height of the source and the destination images @n
213         *                                                                                       The values of the width and the height must be equal to or greater than @c 1.
214         *   @param[in]   flip                                           The flip type
215         *   @param[in]   pixelFormat                            The source and the destination pixel formats
216         *   @exception   E_SUCCESS                                      The method is successful.
217         *   @exception   E_UNSUPPORTED_FORMAT           The specified format is not supported.
218         *   @exception   E_INVALID_ARG                          Either of the following conditions has occurred:
219         *                                                                                       - The specified width and height are invalid.
220         *                                                                                       - The size of the specified @c srcBuf or the specified @c destBuf is insufficient.
221         *   @exception   E_INVALID_DATA                         The source image data in the buffer is invalid.
222         *   @exception   E_OUT_OF_MEMORY                        The memory is insufficient.
223         *   @exception   E_SYSTEM                                       A system error has occurred.
224         *   @remarks
225         *                         - The supported pixel formats are @c MEDIA_PIXEL_FORMAT_RGB565LE, @c MEDIA_PIXEL_FORMAT_BGRA8888, and @c MEDIA_PIXEL_FORMAT_YUV420P.
226         *                         - The position and the limit of @c destBuf are set along with the size of the flipped data.
227         *   @see                        ImageFlipType
228         */
229         static result Flip(const Tizen::Base::ByteBuffer& srcBuf, Tizen::Base::ByteBuffer& destBuf, const Tizen::Graphics::Dimension& dim, ImageFlipType flip, MediaPixelFormat pixelFormat);
230
231 private:
232
233         /**
234         * This default constructor is intentionally declared as private because
235         * this class cannot be constructed.
236         */
237         ImageUtil(void);
238
239         /**
240         * This destructor is intentionally declared as private because this class
241         * cannot be constructed.
242         */
243         virtual ~ImageUtil(void);
244
245         /**
246         * The implementation of this copy constructor is intentionally blank
247         * and declared as private to prohibit copying of objects.
248         */
249         ImageUtil(const ImageUtil& image);
250
251         /**
252         * The implementation of this copy assignment operator is intentionally blank
253         * and declared as private to prohibit copying of objects.
254         */
255         ImageUtil& operator =(const ImageUtil& image);
256
257 }; // class ImageUtil
258
259 }} // Tizen::Media
260
261 #endif // _FMEDIA_IMAGEUTIL_H_
262