[dali_1.4.26] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / plugins / dali-script-v8 / src / image / buffer-image-api.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include "buffer-image-api.h"
20
21 // INTERNAL INCLUDES
22 #include <v8-utils.h>
23 #include <image/image-wrapper.h>
24
25
26 namespace Dali
27 {
28
29 namespace V8Plugin
30 {
31
32 /**
33  * ## BufferImage
34  * Bitmap represents an image resource as a pixel data buffer.
35  * Its pixel buffer data is provided by the application developer.
36  *
37  * If the pixel format of the pixel buffer contains an alpha channel,
38  * then the image is considered to be have transparent pixels without
39  * regard for the actual content of the channel, and will be blended.
40  * @class BufferImage
41  * @extends Image
42  */
43 BufferImage BufferImageApi::GetBufferImage( v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
44 {
45   v8::HandleScope handleScope( isolate );
46
47   v8::Local<v8::Object> object = args.This();
48   v8::Local<v8::External> field = v8::Local<v8::External>::Cast( object->GetInternalField(0) );
49   void* ptr = field->Value();
50
51   ImageWrapper* wrapper = static_cast< ImageWrapper *>(ptr);
52   return BufferImage::DownCast( wrapper->GetImage() );
53 }
54
55 /**
56  * Create a new buffer image object using an external data source.
57  *
58  * For better performance and portability use power of two dimensions.
59  * The maximum size of the image is limited by GL_MAX_TEXTURE_SIZE.
60  *
61  * Example of creating a buffer image from a pixel buffer
62  * ```
63  *  var pixelBufferData = [255, 0, 0, 255,   // red
64  *                         0, 255, 0, 255,   // green
65  *                         0, 0, 255, 255,   // blue
66  *                         255, 0, 0, 255];  // red
67  *
68  *  var pixelBuffer = new Uint8Array(pixelBufferData.length);
69  *  pixelBuffer.set(pixelBufferData, 0);
70  *
71  *  var option = {
72  *                 width       : 2,
73  *                 height      : 2,
74  *                 pixelFormat : dali.PIXEL_FORMAT_RGBA888,  // optional
75  *                 stride      : 2                           // optional
76  *               }
77  *
78  *  var bufferImage = new dali.BufferImage(pixelBuffer, option);
79  * ```
80  * @constructor
81  * @method BufferImage
82  * @for BufferImage
83  * @param {Uint8Array} pixelBuffer Array of RGBA pixel data
84  * @param {Object} options
85  * @param {Integer} options.width image width
86  * @param {Integer} options.height image height
87  * @param {Integer} [options.pixelFormat] pixel format (see dali constants, the default value is dali.PIXEL_FORMAT_RGBA8888)
88  * @param {Integer} [options.stride] the internal stride of the pixelbuffer in pixels (the default value is the image width)
89  * @return {Object} Image
90  */
91 Image BufferImageApi::New( const v8::FunctionCallbackInfo< v8::Value >& args )
92 {
93   v8::Isolate* isolate = args.GetIsolate();
94   v8::HandleScope handleScope( isolate );
95
96   bool found( false );
97
98   PixelBuffer* pixelBuffer = static_cast<PixelBuffer*>(V8Utils::GetArrayBufferViewParameter( PARAMETER_0, found, isolate, args));
99   if( !found )
100   {
101     DALI_SCRIPT_EXCEPTION( isolate, "invalid pixelBuffer parameter" );
102     return BufferImage();
103   }
104
105   v8::Local<v8::Value> options( args[1] );
106   if( !options->IsObject() )
107   {
108     DALI_SCRIPT_EXCEPTION( isolate, "invalid option parameters" );
109     return BufferImage();
110   }
111
112   v8::Local<v8::Object> obj = options->ToObject();
113
114   v8::Local<v8::Value> widthValue = obj->Get( v8::String::NewFromUtf8( isolate, "width" ) );
115   v8::Local<v8::Value> heightValue= obj->Get( v8::String::NewFromUtf8( isolate, "height" ) );
116
117   unsigned int width = 0;
118   unsigned int height = 0;
119
120   if( widthValue->IsUint32() && heightValue->IsUint32() )
121   {
122     width = widthValue->ToUint32()->Value();
123     height = heightValue->ToUint32()->Value();
124   }
125   else
126   {
127     DALI_SCRIPT_EXCEPTION( isolate, "Missing valid width and height params");
128     return BufferImage();
129   }
130
131   Pixel::Format  pixelFormat = Pixel::RGBA8888;
132   v8::Local<v8::Value> pixelFormatValue = obj->Get( v8::String::NewFromUtf8( isolate, "pixelFormat" ) );
133   if( pixelFormatValue->IsUint32() )
134   {
135      pixelFormat = static_cast<Pixel::Format>( pixelFormatValue->ToUint32()->Value() );
136   }
137
138   unsigned int stride = width;
139   v8::Local<v8::Value> strideValue = obj->Get( v8::String::NewFromUtf8( isolate, "stride" ) );
140   if( strideValue->IsUint32() )
141   {
142     stride = strideValue->ToUint32()->Value();
143   }
144
145   return BufferImage::New( pixelBuffer, width, height, pixelFormat, stride);
146 }
147 /**
148  * Returns the pixel buffer of the Image
149  * The application can write to the buffer to modify its contents.
150  *
151  * @method getBuffer
152  * @for BufferImage
153  * @return {Object} The pixel buffer
154  */
155 void BufferImageApi::GetBuffer( const v8::FunctionCallbackInfo< v8::Value >& args )
156 {
157   v8::Isolate* isolate = args.GetIsolate();
158   v8::HandleScope handleScope( isolate );
159
160   BufferImage image = GetBufferImage( isolate, args );
161
162   args.GetReturnValue().Set( v8::ArrayBuffer::New( isolate, static_cast<void*>( image.GetBuffer() ), image.GetBufferSize() ) );
163 }
164
165 /**
166  * Returns buffer size in bytes.
167  * @method getBufferSize
168  * @for BufferImage
169  * @return {Integer} buffer size
170  */
171 void BufferImageApi::GetBufferSize( const v8::FunctionCallbackInfo< v8::Value >& args )
172 {
173   v8::Isolate* isolate = args.GetIsolate();
174   v8::HandleScope handleScope( isolate );
175
176   BufferImage image = GetBufferImage( isolate, args );
177
178   args.GetReturnValue().Set( v8::Integer::New( isolate, image.GetBufferSize() ) );
179 }
180
181 /**
182  * Returns buffer stride in bytes.
183  * @method getBufferStride
184  * @for BufferImage
185  * @return {Object}
186  */
187 void BufferImageApi::GetBufferStride( const v8::FunctionCallbackInfo< v8::Value >& args )
188 {
189   v8::Isolate* isolate = args.GetIsolate();
190   v8::HandleScope handleScope( isolate );
191
192   BufferImage image = GetBufferImage( isolate, args );
193
194   args.GetReturnValue().Set( v8::Integer::New( isolate, image.GetBufferStride() ) );
195 }
196
197 /**
198  * Returns pixel format
199  * @method getPixelFormat
200  * @for BufferImage
201  * @return {Integer} pixel format
202  */
203 void BufferImageApi::GetPixelFormat( const v8::FunctionCallbackInfo< v8::Value >& args )
204 {
205   v8::Isolate* isolate = args.GetIsolate();
206   v8::HandleScope handleScope( isolate );
207
208   BufferImage image = GetBufferImage( isolate, args );
209
210   args.GetReturnValue().Set( v8::Integer::New( isolate, image.GetPixelFormat() ) );
211 }
212
213 /**
214  * Inform Dali that the contents of the buffer have changed
215  *
216  * Example of updating the pixel buffer in the buffer image
217  * ```
218  *  var newPixelBufferData = [0, 255, 0, 255,   // green
219  *                            255, 0, 0, 255,   // red
220  *                            255, 0, 0, 255,   // red
221  *                            0, 0, 255, 255];  // blue
222  *
223  *  var pixelBuffer = bufferImage.getBuffer();
224  *  var pixelBufferDataArray = new Uint8Array(pixelBuffer);
225  *  pixelBufferDataArray.set(newPixelBufferData, 0);
226  *
227  *  bufferImage.update();
228  * ```
229  * @method update
230  * @for BufferImage
231  */
232 void BufferImageApi::Update( const v8::FunctionCallbackInfo< v8::Value >& args )
233 {
234   v8::Isolate* isolate = args.GetIsolate();
235   v8::HandleScope handleScope( isolate );
236
237   BufferImage image = GetBufferImage( isolate, args );
238
239   image.Update();
240 }
241
242 /**
243  * Return whether BufferImage uses external data source or not.
244  * @method isDataExternal
245  * @for BufferImage
246  * @return {Boolean} true if data is external
247  */
248 void BufferImageApi::IsDataExternal( const v8::FunctionCallbackInfo< v8::Value >& args )
249 {
250   v8::Isolate* isolate = args.GetIsolate();
251   v8::HandleScope handleScope( isolate );
252
253   BufferImage image = GetBufferImage( isolate, args );
254
255   args.GetReturnValue().Set( v8::Boolean::New( isolate, image.IsDataExternal() ) );
256 }
257 } // namespace V8Plugin
258
259 } // namespace Dali