[Bf-blender-cvs] [65b4a9f] multi_previews_id: Add copy_from_render to Image RNA API, allows to get 'real' usable image out of render_result one.

Bastien Montagne noreply at git.blender.org
Mon Nov 14 18:18:00 CET 2016


Commit: 65b4a9f40339bbba1c8846e67e51b4a1df712d2e
Author: Bastien Montagne
Date:   Mon Nov 14 17:49:19 2016 +0100
Branches: multi_previews_id
https://developer.blender.org/rB65b4a9f40339bbba1c8846e67e51b4a1df712d2e

Add copy_from_render to Image RNA API, allows to get 'real' usable image out of render_result one.

===================================================================

M	source/blender/makesrna/intern/rna_image_api.c

===================================================================

diff --git a/source/blender/makesrna/intern/rna_image_api.c b/source/blender/makesrna/intern/rna_image_api.c
index 6530e09..2889b5f 100644
--- a/source/blender/makesrna/intern/rna_image_api.c
+++ b/source/blender/makesrna/intern/rna_image_api.c
@@ -72,48 +72,95 @@ static void rna_ImagePackedFile_save(ImagePackedFile *imapf, ReportList *reports
 	}
 }
 
-static void rna_Image_save_render(Image *image, bContext *C, ReportList *reports, const char *path, Scene *scene)
+static ImBuf *get_render_ibuf(Image *image, ReportList *reports, Scene *scene)
 {
-	ImBuf *ibuf;
+	ImBuf *ibuf, *ret_ibuf = NULL;
+	ImageUser iuser = {NULL};
+	void *lock;
+
+	iuser.scene = scene;
+	iuser.ok = 1;
+
+	ibuf = BKE_image_acquire_ibuf(image, &iuser, &lock);
+
+	if (ibuf == NULL) {
+		BKE_report(reports, RPT_ERROR, "Could not acquire buffer from image");
+	}
+	else {
+		ret_ibuf = IMB_colormanagement_imbuf_for_write(ibuf, true, true, &scene->view_settings,
+		                                               &scene->display_settings, &scene->r.im_format);
+
+		/* We do not want to change ibuf acquired from image in any way, so always ensure we are working on a copy. */
+		if (ret_ibuf == ibuf) {
+			ret_ibuf = IMB_dupImBuf(ibuf);
+		}
+		ret_ibuf->planes = scene->r.im_format.planes;
+		ret_ibuf->dither = scene->r.dither_intensity;
 
+		BKE_image_release_ibuf(image, ibuf, lock);
+	}
+
+	return ret_ibuf;
+}
+
+static void rna_Image_save_render(Image *image, bContext *C, ReportList *reports, const char *path, Scene *scene)
+{
 	if (scene == NULL) {
 		scene = CTX_data_scene(C);
 	}
 
 	if (scene) {
-		ImageUser iuser = {NULL};
-		void *lock;
+		ImBuf *write_ibuf = get_render_ibuf(image, reports, scene);
 
-		iuser.scene = scene;
-		iuser.ok = 1;
-
-		ibuf = BKE_image_acquire_ibuf(image, &iuser, &lock);
-
-		if (ibuf == NULL) {
+		if (write_ibuf == NULL) {
 			BKE_report(reports, RPT_ERROR, "Could not acquire buffer from image");
 		}
 		else {
-			ImBuf *write_ibuf;
-
-			write_ibuf = IMB_colormanagement_imbuf_for_write(ibuf, true, true, &scene->view_settings,
-			                                                 &scene->display_settings, &scene->r.im_format);
-
-			write_ibuf->planes = scene->r.im_format.planes;
-			write_ibuf->dither = scene->r.dither_intensity;
-
 			if (!BKE_imbuf_write(write_ibuf, path, &scene->r.im_format)) {
 				BKE_reportf(reports, RPT_ERROR, "Could not write image: %s, '%s'", strerror(errno), path);
 			}
 
-			if (write_ibuf != ibuf)
-				IMB_freeImBuf(write_ibuf);
+			IMB_freeImBuf(write_ibuf);
+		}
+	}
+	else {
+		BKE_report(reports, RPT_ERROR, "Scene not in context, could not get save parameters");
+	}
+}
+
+static Image *rna_Image_copy_from_render(Image *image, bContext *C, ReportList *reports, Scene *scene)
+{
+	Image *ret_image = NULL;
+
+	if (scene == NULL) {
+		scene = CTX_data_scene(C);
+	}
+
+	if (scene) {
+		ImBuf *new_ibuf = get_render_ibuf(image, reports, scene);
+
+		if (new_ibuf == NULL) {
+			BKE_report(reports, RPT_ERROR, "Could not acquire buffer from image");
 		}
+		else {
+			ret_image = BKE_image_add_from_imbuf(new_ibuf, image->id.name + 2);
 
-		BKE_image_release_ibuf(image, ibuf, lock);
+			if (!ret_image) {
+				BKE_reportf(reports, RPT_ERROR, "Could not create copy image.");
+				IMB_freeImBuf(new_ibuf);
+			}
+			else {
+				id_us_min(&ret_image->id);
+			}
+
+			IMB_freeImBuf(new_ibuf);
+		}
 	}
 	else {
 		BKE_report(reports, RPT_ERROR, "Scene not in context, could not get save parameters");
 	}
+
+	return ret_image;
 }
 
 static void rna_Image_save(Image *image, Main *bmain, bContext *C, ReportList *reports)
@@ -317,6 +364,14 @@ void RNA_api_image(StructRNA *srna)
 	RNA_def_property_flag(parm, PROP_REQUIRED);
 	RNA_def_pointer(func, "scene", "Scene", "", "Scene to take image parameters from");
 
+	func = RNA_def_function(srna, "copy_from_render", "rna_Image_copy_from_render");
+	RNA_def_function_ui_description(func, "Return a new, regular image, copy of given render result one");
+	RNA_def_function_flag(func, FUNC_USE_CONTEXT | FUNC_USE_REPORTS);
+	RNA_def_pointer(func, "scene", "Scene", "", "Scene to take image parameters from");
+	parm = RNA_def_pointer(func, "image", "Image", "",
+	                       "Copied image from this render result one");
+	RNA_def_function_return(func, parm);
+
 	func = RNA_def_function(srna, "save", "rna_Image_save");
 	RNA_def_function_ui_description(func, "Save image to its source path");
 	RNA_def_function_flag(func, FUNC_USE_MAIN | FUNC_USE_CONTEXT | FUNC_USE_REPORTS);




More information about the Bf-blender-cvs mailing list