Image pixel acces, through Image.pixels as floating point values.

It's not the most efficient solution, but this can be optimized later. It's
best to copy out all the pixels at once into a list, rather than accessing
them one by one.
This commit is contained in:
Brecht Van Lommel 2011-04-03 16:17:39 +00:00
parent 480b9dca64
commit 993e9d4c46

@ -243,6 +243,76 @@ static int rna_Image_depth_get(PointerRNA *ptr)
return depth;
}
static int rna_Image_pixels_get_length(PointerRNA *ptr, int length[RNA_MAX_ARRAY_DIMENSION])
{
Image *ima= ptr->id.data;
ImBuf *ibuf;
void *lock;
ibuf= BKE_image_acquire_ibuf(ima, NULL, &lock);
if(ibuf) {
length[0]= ibuf->x*ibuf->y;
length[1]= ibuf->channels;
}
else {
length[0]= 0;
length[1]= 0;
}
BKE_image_release_ibuf(ima, lock);
return length[0]*length[1];
}
static void rna_Image_pixels_get(PointerRNA *ptr, float *values)
{
Image *ima= ptr->id.data;
ImBuf *ibuf;
void *lock;
int i, size;
ibuf= BKE_image_acquire_ibuf(ima, NULL, &lock);
if(ibuf) {
size= ibuf->x*ibuf->y*ibuf->channels;
if(ibuf->rect_float) {
memcpy(values, ibuf->rect_float, sizeof(float)*size);
}
else {
for(i = 0; i < size; i++)
values[i] = ((unsigned char*)ibuf->rect)[i]*(1.0f/255.0f);
}
}
BKE_image_release_ibuf(ima, lock);
}
static void rna_Image_pixels_set(PointerRNA *ptr, const float *values)
{
Image *ima= ptr->id.data;
ImBuf *ibuf;
void *lock;
int i, size;
ibuf= BKE_image_acquire_ibuf(ima, NULL, &lock);
if(ibuf) {
size= ibuf->x*ibuf->y*ibuf->channels;
if(ibuf->rect_float) {
memcpy(ibuf->rect_float, values, sizeof(float)*size);
}
else {
for(i = 0; i < size; i++)
((unsigned char*)ibuf->rect)[i] = FTOCHAR(values[i]);
}
}
BKE_image_release_ibuf(ima, lock);
}
#else
static void rna_def_imageuser(BlenderRNA *brna)
@ -489,6 +559,13 @@ static void rna_def_image(BlenderRNA *brna)
RNA_def_property_int_funcs(prop, "rna_Image_size_get" , NULL, NULL);
RNA_def_property_clear_flag(prop, PROP_EDITABLE);
prop= RNA_def_property(srna, "pixels", PROP_FLOAT, PROP_NONE);
RNA_def_property_flag(prop, PROP_DYNAMIC);
RNA_def_property_multi_array(prop, 2, NULL);
RNA_def_property_ui_text(prop, "Pixels", "Image pixels in floating point values");
RNA_def_property_dynamic_array_funcs(prop, "rna_Image_pixels_get_length");
RNA_def_property_float_funcs(prop, "rna_Image_pixels_get", "rna_Image_pixels_set", NULL);
RNA_api_image(srna);
}