Skip to content
Snippets Groups Projects
Commit b0916aa7 authored by Lars Kanis's avatar Lars Kanis
Browse files

Add proper FXImage#setData

previous swig's #setData implementation wasn't really useable
parent 09e3c0eb
No related branches found
No related tags found
No related merge requests found
......@@ -65,6 +65,24 @@ module Fox
def initialize(a, pixels=nil, opts=0, width=1, height=1) # :yields: theImage
end
#
# Populate the image with new pixel data of the same size or of a new size
#
# Pixel data is copied and IMAGE_OWNED option is set.
# If called with +width+ and +height+, the size of the serverside representation
# of the image, if it exists, is adjusted but the contents are not updated yet.
# This can be done by calling render().
#
# ==== Parameters:
#
# +pix+:: the array of FXColor values.
# +opts+:: image options [Integer]
# +width+:: image width [Integer]
# +height+:: image height [Integer]
#
def setData(pix, opts=0, width=nil, height=nil)
end
#
# Return the color of the pixel at (_x_, _y_).
#
......
......@@ -86,21 +86,33 @@ public:
/// Change options
void setOptions(FXuint opts);
/**
* Populate the image with new pixel data of the same size; it will assume
* ownership of the pixel data if image IMAGE_OWNED option is passed.
* The server-side representation of the image, if it exists, is not updated.
* This can be done by calling render().
*/
virtual void setData(FXColor *pix,FXuint opts=0);
/**
* Populate the image with new pixel data of a new size; it will assume ownership
* of the pixel data if image IMAGE_OWNED option is passed. The size of the server-
* side representation of the image, if it exists, is adjusted but the contents are
* not updated yet. This can be done by calling render().
*/
virtual void setData(FXColor *pix,FXuint opts,FXint w,FXint h);
%extend {
/**
* Populate the image with new pixel data of the same size; it will assume
* ownership of the pixel data if image IMAGE_OWNED option is passed.
* The server-side representation of the image, if it exists, is not updated.
* This can be done by calling render().
*/
void setData(VALUE ary,FXuint opts=0,VALUE w=Qnil,VALUE h=Qnil){
FXColor* pix=0;
Check_Type(ary,T_ARRAY);
if( ( (NIL_P(w) || NIL_P(h)) && self->getWidth()*self->getHeight() != RARRAY_LEN(ary)) ||
(!(NIL_P(w) || NIL_P(h)) && NUM2UINT(w)*NUM2UINT(h) != RARRAY_LEN(ary))){
rb_raise( rb_eArgError, "array size does not match image size" );
}
if(FXMALLOC(&pix,FXColor,RARRAY_LEN(ary))){
for(long i=0; i<RARRAY_LEN(ary); i++){
pix[i]=static_cast<FXColor>(NUM2UINT(rb_ary_entry(ary,i)));
}
}
opts|=IMAGE_OWNED;
if( NIL_P(w) || NIL_P(h) ){
self->setData(pix,opts);
}else{
self->setData(pix,opts,NUM2UINT(w),NUM2UINT(h));
}
}
}
/// Get pixel at x,y
FXColor getPixel(FXint x,FXint y) const;
......
......@@ -59,6 +59,22 @@ class TC_FXImage < Fox::TestCase
assert_equal(IMAGE_OWNED, img.options)
end
def test_setData
img = FXImage.new(app, nil, 0, 2, 2)
img.setData([0x12345678, 2, 3, 4])
assert_equal(IMAGE_OWNED, img.options)
assert_equal(2*2, img.data.size)
assert_equal([0x12345678, 2, 3, 4], img.data.to_a)
end
def test_setData2
img = FXImage.new(app)
img.setData([0x12345678, 2], 0, 2, 1)
assert_equal(IMAGE_OWNED, img.options)
assert_equal(2*1, img.data.size)
assert_equal([0x12345678, 2], img.data.to_a)
end
def test_create
#
# If the image owns its pixel data and IMAGE_KEEP was not specified,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment