Tizen 2.1 base
[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 converting the color space of the image, flipping, rotating, or resizing.
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 height of the source and destination images @n
130         *                                                                                        The value of the width and 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                          The specified width and height are invalid, or the size of @c srcBuf or @c destBuf is insufficient.
134         *   @exception   E_INVALID_DATA                    A source image data in the buffer is invalid.
135         *   @exception   E_OUT_OF_MEMORY                          The memory is insufficient.
136         *   @exception   E_SYSTEM                                        A system error has occurred.
137         *   @remarks            The supported pixel formats are @c MEDIA_PIXEL_FORMAT_RGB565LE, @c MEDIA_PIXEL_FORMAT_BGRA8888, and @c MEDIA_PIXEL_FORMAT_YUV420P. @n
138         *                                  The conversion is possible between: @n
139         *                                  - @c MEDIA_PIXEL_FORMAT_YUV420P and @c MEDIA_PIXEL_FORMAT_RGB565LE. @n
140         *                                  - @c MEDIA_PIXEL_FORMAT_YUV420P and @c MEDIA_PIXEL_FORMAT_BGRA8888. @n
141         *                               The position and limit of @c destBuf are set along with the size of the converted data.
142         */
143         static result ConvertPixelFormat(const Tizen::Base::ByteBuffer& srcBuf, Tizen::Base::ByteBuffer& destBuf, MediaPixelFormat srcPixelFormat, MediaPixelFormat destPixelFormat, const Tizen::Graphics::Dimension& dim);
144
145         /**
146         *   Resizes the image.
147         *
148         *   @since         2.0
149         *
150         *   @return      An error code
151         *   @param[in]   srcBuf                                         The source buffer
152         *   @param[out]  destBuf                                                The destination buffer
153         *   @param[in]   srcDim                                         The width and height of the source image @n
154         *                                                                                       The value of the width and height must be greater than or equal to @c 1.
155         *   @param[in]   destDim                                                The width and height of the destination image @n
156         *                                                                                       The value of the width and height must be greater than or equal to @c 1.
157         *   @param[in]   pixelFormat                                    The source and destination pixel format
158         *   @exception   E_SUCCESS                                 The method is successful.
159         *   @exception   E_UNSUPPORTED_FORMAT           The specified format is not supported.
160         *   @exception   E_INVALID_ARG                     The specified width and height are invalid, or the size of @c srcBuf or @c destBuf is insufficient.
161         *   @exception   E_INVALID_DATA                   A source image data in the buffer is invalid.
162         *   @exception   E_OUT_OF_MEMORY                         The memory is insufficient.
163         *   @exception   E_SYSTEM                                       A system error has occurred.
164         *   @remarks            The supported pixel formats are @c MEDIA_PIXEL_FORMAT_RGB565LE, @c MEDIA_PIXEL_FORMAT_BGRA8888, and @c MEDIA_PIXEL_FORMAT_YUV420P. @n
165         *                               The position and limit of @c destBuf are set along with the size of the resized data. @n
166         *                               The minimum resolution is 16x16.
167         */
168         static result Resize(const Tizen::Base::ByteBuffer& srcBuf, Tizen::Base::ByteBuffer& destBuf, const Tizen::Graphics::Dimension& srcDim, const Tizen::Graphics::Dimension& destDim, MediaPixelFormat pixelFormat);
169
170         /**
171         *   Rotates the image.
172         *
173         *   @since         2.0
174         *
175         *   @return      An error code
176         *   @param[in]   srcBuf                                         The source buffer
177         *   @param[out]  destBuf                                                The destination buffer
178         *   @param[in]   dim                                                    The width and height of the source image @n
179         *                                                                                       The value of the width and height must be equal to or greater than @c 1.
180         *   @param[in]   rotate                                         The rotation type
181         *   @param[in]   pixelFormat                                    The source and destination pixel formats
182         *   @exception   E_SUCCESS                                      The method is successful.
183         *   @exception  E_UNSUPPORTED_FORMAT                    The specified format is not supported.
184         *   @exception   E_INVALID_ARG                          The specified width and height are invalid, or the size of @c srcBuf or @c destBuf is insufficient.
185         *   @exception   E_INVALID_DATA                         A source image data in the buffer is invalid.
186         *   @exception   E_OUT_OF_MEMORY                                The memory is insufficient.
187         *   @exception   E_SYSTEM                                       A system error has occurred.
188         *   @remarks            The supported pixel formats are @c MEDIA_PIXEL_FORMAT_RGB565LE, @c MEDIA_PIXEL_FORMAT_BGRA8888, and @c MEDIA_PIXEL_FORMAT_YUV420P. @n
189         *                               The position and limit of @c destBuf are set along with the size of the rotated data. @n
190         *                               The dimension of the destination buffer must be calculated by an application.
191         *   @see                        ImageRotationType
192         */
193         static result Rotate(const Tizen::Base::ByteBuffer& srcBuf, Tizen::Base::ByteBuffer& destBuf, const Tizen::Graphics::Dimension& dim, ImageRotationType rotate, MediaPixelFormat pixelFormat);
194
195         /**
196         *   Flips the image.
197         *
198         *   @since         2.0
199         *
200         *   @return      An error code
201         *   @param[in]   srcBuf                                         The source buffer
202         *   @param[out]  destBuf                                                The destination buffer
203         *   @param[in]   dim                                                    The width and height of the source and destination images @n
204         *                                                                                       The value of the width and height must be equal to or greater than @c 1.
205         *   @param[in]   flip                                           The flip type
206         *   @param[in]   pixelFormat                                    The source and destination pixel formats
207         *   @exception   E_SUCCESS                                      The method is successful.
208         *   @exception   E_UNSUPPORTED_FORMAT           The specified format is not supported.
209         *   @exception   E_INVALID_ARG                          The specified width and height are invalid, or the size of @c srcBuf or @c destBuf is insufficient.
210         *   @exception   E_INVALID_DATA                         A source image data in the buffer is invalid.
211         *   @exception   E_OUT_OF_MEMORY                                The memory is insufficient.
212         *   @exception   E_SYSTEM                                       A system error has occurred.
213         *   @remarks            The supported pixel formats are @c MEDIA_PIXEL_FORMAT_RGB565LE, @c MEDIA_PIXEL_FORMAT_BGRA8888, and @c MEDIA_PIXEL_FORMAT_YUV420P. @n
214         *                               The position and limit of @c destBuf are set along with the size of the flipped data.
215         *   @see                        ImageFlipType
216         */
217         static result Flip(const Tizen::Base::ByteBuffer& srcBuf, Tizen::Base::ByteBuffer& destBuf, const Tizen::Graphics::Dimension& dim, ImageFlipType flip, MediaPixelFormat pixelFormat);
218
219 private:
220
221         /**
222         * This default constructor is intentionally declared as private because
223         * this class cannot be constructed.
224         */
225         ImageUtil(void);
226
227         /**
228         * This destructor is intentionally declared as private because this class
229         * cannot be constructed.
230         */
231         virtual ~ImageUtil(void);
232
233         /**
234         * The implementation of this copy constructor is intentionally blank
235         * and declared as private to prohibit copying of objects.
236         */
237         ImageUtil(const ImageUtil& image);
238
239         /**
240         * The implementation of this copy assignment operator is intentionally blank
241         * and declared as private to prohibit copying of objects.
242         */
243         ImageUtil& operator =(const ImageUtil& image);
244
245 }; // class ImageUtil
246
247 }} // Tizen::Media
248
249 #endif // _FMEDIA_IMAGEUTIL_H_
250