From 993e9d4c463406acf81abe8f17a0cac587afd180 Mon Sep 17 00:00:00 2001 From: Brecht Van Lommel Date: Sun, 3 Apr 2011 16:17:39 +0000 Subject: [PATCH] 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. --- source/blender/makesrna/intern/rna_image.c | 77 ++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/source/blender/makesrna/intern/rna_image.c b/source/blender/makesrna/intern/rna_image.c index 64b7e589ecb..2b6349a3877 100644 --- a/source/blender/makesrna/intern/rna_image.c +++ b/source/blender/makesrna/intern/rna_image.c @@ -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); }