2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "buffer-image-api.h"
23 #include <image/image-wrapper.h>
34 * Bitmap represents an image resource as a pixel data buffer.
35 * Its pixel buffer data is provided by the application developer.
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.
43 BufferImage BufferImageApi::GetBufferImage( v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
45 v8::HandleScope handleScope( isolate );
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();
51 ImageWrapper* wrapper = static_cast< ImageWrapper *>(ptr);
52 return BufferImage::DownCast( wrapper->GetImage() );
56 * Create a new buffer image object using an external data source.
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.
61 * Example of creating a buffer image from a pixel buffer
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
68 * var pixelBuffer = new Uint8Array(pixelBufferData.length);
69 * pixelBuffer.set(pixelBufferData, 0);
74 * pixelFormat : dali.PIXEL_FORMAT_RGBA888, // optional
75 * stride : 2 // optional
78 * var bufferImage = new dali.BufferImage(pixelBuffer, option);
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
91 Image BufferImageApi::New( const v8::FunctionCallbackInfo< v8::Value >& args )
93 v8::Isolate* isolate = args.GetIsolate();
94 v8::HandleScope handleScope( isolate );
98 PixelBuffer* pixelBuffer = static_cast<PixelBuffer*>(V8Utils::GetArrayBufferViewParameter( PARAMETER_0, found, isolate, args));
101 DALI_SCRIPT_EXCEPTION( isolate, "invalid pixelBuffer parameter" );
102 return BufferImage();
105 v8::Local<v8::Value> options( args[1] );
106 if( !options->IsObject() )
108 DALI_SCRIPT_EXCEPTION( isolate, "invalid option parameters" );
109 return BufferImage();
112 v8::Local<v8::Object> obj = options->ToObject();
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" ) );
117 unsigned int width = 0;
118 unsigned int height = 0;
120 if( widthValue->IsUint32() && heightValue->IsUint32() )
122 width = widthValue->ToUint32()->Value();
123 height = heightValue->ToUint32()->Value();
127 DALI_SCRIPT_EXCEPTION( isolate, "Missing valid width and height params");
128 return BufferImage();
131 Pixel::Format pixelFormat = Pixel::RGBA8888;
132 v8::Local<v8::Value> pixelFormatValue = obj->Get( v8::String::NewFromUtf8( isolate, "pixelFormat" ) );
133 if( pixelFormatValue->IsUint32() )
135 pixelFormat = static_cast<Pixel::Format>( pixelFormatValue->ToUint32()->Value() );
138 unsigned int stride = width;
139 v8::Local<v8::Value> strideValue = obj->Get( v8::String::NewFromUtf8( isolate, "stride" ) );
140 if( strideValue->IsUint32() )
142 stride = strideValue->ToUint32()->Value();
145 return BufferImage::New( pixelBuffer, width, height, pixelFormat, stride);
148 * Returns the pixel buffer of the Image
149 * The application can write to the buffer to modify its contents.
153 * @return {Object} The pixel buffer
155 void BufferImageApi::GetBuffer( const v8::FunctionCallbackInfo< v8::Value >& args )
157 v8::Isolate* isolate = args.GetIsolate();
158 v8::HandleScope handleScope( isolate );
160 BufferImage image = GetBufferImage( isolate, args );
162 args.GetReturnValue().Set( v8::ArrayBuffer::New( isolate, static_cast<void*>( image.GetBuffer() ), image.GetBufferSize() ) );
166 * Returns buffer size in bytes.
167 * @method getBufferSize
169 * @return {Integer} buffer size
171 void BufferImageApi::GetBufferSize( const v8::FunctionCallbackInfo< v8::Value >& args )
173 v8::Isolate* isolate = args.GetIsolate();
174 v8::HandleScope handleScope( isolate );
176 BufferImage image = GetBufferImage( isolate, args );
178 args.GetReturnValue().Set( v8::Integer::New( isolate, image.GetBufferSize() ) );
182 * Returns buffer stride in bytes.
183 * @method getBufferStride
187 void BufferImageApi::GetBufferStride( const v8::FunctionCallbackInfo< v8::Value >& args )
189 v8::Isolate* isolate = args.GetIsolate();
190 v8::HandleScope handleScope( isolate );
192 BufferImage image = GetBufferImage( isolate, args );
194 args.GetReturnValue().Set( v8::Integer::New( isolate, image.GetBufferStride() ) );
198 * Returns pixel format
199 * @method getPixelFormat
201 * @return {Integer} pixel format
203 void BufferImageApi::GetPixelFormat( const v8::FunctionCallbackInfo< v8::Value >& args )
205 v8::Isolate* isolate = args.GetIsolate();
206 v8::HandleScope handleScope( isolate );
208 BufferImage image = GetBufferImage( isolate, args );
210 args.GetReturnValue().Set( v8::Integer::New( isolate, image.GetPixelFormat() ) );
214 * Inform Dali that the contents of the buffer have changed
216 * Example of updating the pixel buffer in the buffer image
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
223 * var pixelBuffer = bufferImage.getBuffer();
224 * var pixelBufferDataArray = new Uint8Array(pixelBuffer);
225 * pixelBufferDataArray.set(newPixelBufferData, 0);
227 * bufferImage.update();
232 void BufferImageApi::Update( const v8::FunctionCallbackInfo< v8::Value >& args )
234 v8::Isolate* isolate = args.GetIsolate();
235 v8::HandleScope handleScope( isolate );
237 BufferImage image = GetBufferImage( isolate, args );
243 * Return whether BufferImage uses external data source or not.
244 * @method isDataExternal
246 * @return {Boolean} true if data is external
248 void BufferImageApi::IsDataExternal( const v8::FunctionCallbackInfo< v8::Value >& args )
250 v8::Isolate* isolate = args.GetIsolate();
251 v8::HandleScope handleScope( isolate );
253 BufferImage image = GetBufferImage( isolate, args );
255 args.GetReturnValue().Set( v8::Boolean::New( isolate, image.IsDataExternal() ) );
257 } // namespace V8Plugin