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 that can be added to Material.
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 ** ( work in progress, will currently only work with no parameters ) **
57 * If no parameters are passed in, a single pixel white buffer image is created.
60 * For better performance and portability use power of two dimensions.
61 * The maximum size of the image is limited by GL_MAX_TEXTURE_SIZE.
63 * @note in case releasePolicy is "Unused", application has to call
64 * BufferImage::Update() whenever image is re-added to the stage
66 * Example of creating a Uint32Array buffer
68 * "var ab = new ArrayBuffer(256 x 256 );"
69 * var pixelBuffer = new Uint32Array(ab );
74 * @param {Object} options
75 * @param {Uint32Array} options.pixelBuffer Array of RGBA pixel data
76 * @param {Integer} options.width image width
77 * @param {Integer} options.height image height
78 * @param {Integer} options.pixelFormat pixel format ( see dali constants, e.g. dali.PIXEL_FORMAT_RGB8888)
79 * @param {Integer} [options.stride the] internal stride of the pixelbuffer in pixels (normally the width)
80 * @param {Integer} [options.releasePolicy] optionally release memory when image is not visible on screen.
81 * @return {Object} Image
83 Image BufferImageApi::New( const v8::FunctionCallbackInfo< v8::Value >& args )
85 v8::Isolate* isolate = args.GetIsolate();
86 v8::HandleScope handleScope( isolate );
87 v8::Local<v8::Value> options( args[0] );
88 if( !options->IsObject() )
90 return BufferImage::WHITE();
93 v8::Local<v8::Object> obj = options->ToObject();
95 v8::Local<v8::Value> widthValue = obj->Get( v8::String::NewFromUtf8( isolate, "width" ) );
96 v8::Local<v8::Value> heightValue= obj->Get( v8::String::NewFromUtf8( isolate, "height" ) );
98 unsigned int width = 0;
99 unsigned int height = 0;
101 if( widthValue->IsUint32() && heightValue->IsUint32() )
103 width = widthValue->ToUint32()->Value();
104 height = heightValue->ToUint32()->Value();
108 DALI_SCRIPT_EXCEPTION( isolate, "Missing valid width and height params");
109 return BufferImage();
112 Pixel::Format pixelFormat = Pixel::RGB8888;
113 v8::Local<v8::Value> pixelFormatValue = obj->Get( v8::String::NewFromUtf8( isolate, "pixelFormat" ) );
114 if( pixelFormatValue->IsUint32() )
116 pixelFormat = static_cast<Pixel::Format>( pixelFormatValue->ToUint32()->Value() );
120 DALI_SCRIPT_EXCEPTION( isolate, "Pixel format not specified");
121 return BufferImage();
124 v8::Local<v8::Value> pixelArray= obj->Get( v8::String::NewFromUtf8( isolate, "pixelBuffer" ) );
125 if( pixelArray->IsUint32Array() )
127 //v8::Local<v8::Uint32Array> v8Array = v8::Local<v8::Uint32Array>::Cast( pixelArray );
128 // uint32_t elementCount = v8Array->Length();
129 DALI_SCRIPT_EXCEPTION( isolate, "pixel buffer currently not supported \n");
130 return BufferImage::WHITE();
134 unsigned int stride = width;
135 v8::Local<v8::Value> strideValue = obj->Get( v8::String::NewFromUtf8( isolate, "stride" ) );
136 if( strideValue->IsUint32() )
138 stride = strideValue->ToUint32()->Value();
141 Image::ReleasePolicy releasePolicy = Dali::Image::NEVER;
142 v8::Local<v8::Value> releasePolicyValue = obj->Get( v8::String::NewFromUtf8( isolate, "releasePolicy" ) );
143 if( releasePolicyValue->IsUint32() )
145 releasePolicy = static_cast<Image::ReleasePolicy>( releasePolicyValue->ToUint32()->Value() );
148 DALI_SCRIPT_EXCEPTION( isolate, "pixel buffer currently not supported \n");
150 return BufferImage::New( NULL, width, height, pixelFormat, stride, releasePolicy);
154 * @brief Returns the pixel buffer of the Image **( currently not supported ) **
159 void BufferImageApi::GetBuffer( const v8::FunctionCallbackInfo< v8::Value >& args )
161 v8::Isolate* isolate = args.GetIsolate();
162 v8::HandleScope handleScope( isolate );
164 BufferImage image = GetBufferImage( isolate, args );
166 //@todo figure out what the best thing to do here is...
167 // we could copy the data into a javascript array
171 * @brief Returns buffer size in bytes.
172 * @method getBufferSize
174 * @return {Object} buffer
176 void BufferImageApi::GetBufferSize( const v8::FunctionCallbackInfo< v8::Value >& args )
178 v8::Isolate* isolate = args.GetIsolate();
179 v8::HandleScope handleScope( isolate );
181 BufferImage image = GetBufferImage( isolate, args );
183 args.GetReturnValue().Set( v8::Integer::New( isolate, image.GetBufferSize() ) );
187 * @brief Returns buffer stride in bytes.
188 * @method getBufferStride
192 void BufferImageApi::GetBufferStride( const v8::FunctionCallbackInfo< v8::Value >& args )
194 v8::Isolate* isolate = args.GetIsolate();
195 v8::HandleScope handleScope( isolate );
197 BufferImage image = GetBufferImage( isolate, args );
199 args.GetReturnValue().Set( v8::Integer::New( isolate, image.GetBufferStride() ) );
203 * @brief Returns pixel format
204 * @method getPixelFormat
206 * @return {Integer} pixel format
208 void BufferImageApi::GetPixelFormat( const v8::FunctionCallbackInfo< v8::Value >& args )
210 v8::Isolate* isolate = args.GetIsolate();
211 v8::HandleScope handleScope( isolate );
213 BufferImage image = GetBufferImage( isolate, args );
215 args.GetReturnValue().Set( v8::Integer::New( isolate, image.GetPixelFormat() ) );
220 * @brief Inform Dali that the contents of the buffer have changed
221 * @todo support update an area
225 void BufferImageApi::Update( const v8::FunctionCallbackInfo< v8::Value >& args )
227 v8::Isolate* isolate = args.GetIsolate();
228 v8::HandleScope handleScope( isolate );
230 BufferImage image = GetBufferImage( isolate, args );
236 * @brief returns whether BufferImage uses external data source or not.
237 * @method isDataExternal
239 * @return {Boolean} true if data is external
241 void BufferImageApi::IsDataExternal( const v8::FunctionCallbackInfo< v8::Value >& args )
243 v8::Isolate* isolate = args.GetIsolate();
244 v8::HandleScope handleScope( isolate );
246 BufferImage image = GetBufferImage( isolate, args );
248 args.GetReturnValue().Set( v8::Boolean::New( isolate, image.IsDataExternal() ) );
251 } // namespace V8Plugin