[Bf-blender-cvs] [60690ac] cycles-ptex-49: Use BPX for BKE_ptex_update_from_image

Nicholas Bishop noreply at git.blender.org
Mon Feb 9 17:31:52 CET 2015


Commit: 60690acf6a26b3babe011eaab4438703cddc3b7b
Author: Nicholas Bishop
Date:   Mon Feb 9 16:24:08 2015 +0100
Branches: cycles-ptex-49
https://developer.blender.org/rB60690acf6a26b3babe011eaab4438703cddc3b7b

Use BPX for BKE_ptex_update_from_image

That should make it correct for different data types, channels, etc.

Also added API comment.

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

M	extern/ptex/bpx_c_api.cpp
M	source/blender/blenkernel/BKE_ptex.h
M	source/blender/blenkernel/intern/bke_ptex.c

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

diff --git a/extern/ptex/bpx_c_api.cpp b/extern/ptex/bpx_c_api.cpp
index 7a2a41a..0042adc 100644
--- a/extern/ptex/bpx_c_api.cpp
+++ b/extern/ptex/bpx_c_api.cpp
@@ -143,24 +143,41 @@ void BPX_image_buf_free(BPXImageBuf * const buf)
 
 bool BPX_image_buf_pixels_copy_partial(BPXImageBuf *bpx_dst,
 									   const BPXImageBuf *bpx_src,
-									   int xbegin, int ybegin,
+									   const int xbegin, const int ybegin,
 									   const BPXRect *src_rect)
 {
 	if (bpx_dst && bpx_src) {
 		ImageBuf *dst = bpx_image_buf_to_oiio_image_buf(bpx_dst);
+		const ImageSpec &dst_spec = dst->spec();
 		const ImageBuf *src = bpx_image_buf_to_oiio_image_buf(bpx_src);
 		ROI src_roi = src->roi();
-		const int zbegin = 0;
 		const int chbegin = 0;
 
 		if (src_rect) {
 			src_roi = bpx_rect_to_oiio_roi(*src_rect);
 		}
-		
+
+#if 0
+		const int zbegin = 0;
 		return ImageBufAlgo::paste(*dst, xbegin, ybegin, zbegin, chbegin,
 								   *src, src_roi);
-
-		return dst->copy(*src);
+#else
+		/* TODO(nicholasbishop): for some reason this code is much
+		 * faster than paste, but obviously a lot uglier too... would
+		 * like to figure out why, maybe just doing something silly */
+		const stride_t xstride = dst_spec.pixel_bytes();
+		const stride_t ystride = dst_spec.scanline_bytes();;
+		const stride_t zstride = 1;
+		const int chend = std::min(dst_spec.nchannels,
+								   src->spec().nchannels);
+		return src->get_pixel_channels(src_roi.xbegin, src_roi.xend,
+									   src_roi.ybegin, src_roi.yend,
+									   src_roi.zbegin, src_roi.zend,
+									   chbegin, chend,
+									   dst_spec.format,
+									   dst->pixeladdr(xbegin, ybegin),
+									   xstride, ystride, zstride);
+#endif
 	}
 	return false;
 }
diff --git a/source/blender/blenkernel/BKE_ptex.h b/source/blender/blenkernel/BKE_ptex.h
index 0bbba66..fa36b48 100644
--- a/source/blender/blenkernel/BKE_ptex.h
+++ b/source/blender/blenkernel/BKE_ptex.h
@@ -54,6 +54,13 @@ MPtexDataType BKE_loop_ptex_texel_data_type(const MLoopPtex *loop_ptex);
 
 void BKE_loop_ptex_resize(MLoopPtex *loop_ptex, const MPtexLogRes logres);
 
+/* Copy regions from the image into loop_ptex
+ *
+ * TODO(nicholasbishop): should have some way to set which loops need
+ * updates, probably new flag in MLoopPtex
+ *
+ * Return true if successful (even if some loops have an error), false
+ * otherwise. */
 bool BKE_ptex_update_from_image(MLoopPtex *loop_ptex, const int totloop);
 
 /* Add CD_LOOP_PTEX layer to DerivedMesh. The 'id' field will be just
diff --git a/source/blender/blenkernel/intern/bke_ptex.c b/source/blender/blenkernel/intern/bke_ptex.c
index f6d1fd0..71b2a92 100644
--- a/source/blender/blenkernel/intern/bke_ptex.c
+++ b/source/blender/blenkernel/intern/bke_ptex.c
@@ -132,64 +132,6 @@ void BKE_ptex_tess_face_interp(MTessFacePtex *tess_face_ptex,
 	}
 }
 
-bool BKE_ptex_update_from_image(MLoopPtex *loop_ptex, const int totloop)
-{
-	int i;
-
-	// TODO
-	Image *image;
-	ImBuf *ibuf;
-
-	if (!loop_ptex) {
-		return false;
-	}
-
-	image = loop_ptex->image;
-	if (!image) {
-		return false;
-	}
-
-	ibuf = BKE_image_acquire_ibuf(image, NULL, NULL);
-
-	if (!ibuf) {
-		return false;
-	}
-
-	// TODO
-	BLI_assert(ibuf->rect);
-	BLI_assert(ibuf->num_ptex_regions == totloop);
-	BLI_assert(loop_ptex->texel_info.num_channels == 4);
-
-	BLI_assert(ibuf->ptex_regions);
-	for (i = 0; i < totloop; i++) {
-		// TODO
-		MLoopPtex *pt = &loop_ptex[i];
-		const MPtexRes dst_res = bke_ptex_res_from_logres(pt->logres);
-		const size_t bytes_per_texel = BKE_ptex_bytes_per_texel(pt->texel_info);
-		const size_t dst_bytes_per_row = dst_res.u * bytes_per_texel;
-		const size_t src_bytes_per_row = ibuf->x * bytes_per_texel;
-		unsigned char *dst = pt->rect;
-		unsigned char *src = (unsigned char*)ibuf->rect;
-		ImPtexRegion *ptex_regions = &ibuf->ptex_regions[i];
-		int v;
-
-		BLI_assert(dst_res.u == ptex_regions->width);
-		BLI_assert(dst_res.v == ptex_regions->height);
-
-		src += (size_t)(src_bytes_per_row * ptex_regions->y + ptex_regions->x * bytes_per_texel);
-
-		for (v = 0; v < dst_res.v; v++) {
-			memcpy(dst, src, dst_bytes_per_row);
-			dst += dst_bytes_per_row;
-			src += src_bytes_per_row;
-		}
-	}
-
-	BKE_image_release_ibuf(image, ibuf, NULL);
-
-	return true;
-}
-
 static void *bke_ptex_texels_malloc(const MPtexTexelInfo texel_info,
 									const MPtexLogRes logres)
 {
@@ -593,7 +535,7 @@ static BPXImageBuf *bpx_image_buf_wrap_loop_ptex(MLoopPtex *loop_ptex)
 	if (loop_ptex && loop_ptex->rect) {
 		const MPtexRes res = bke_ptex_res_from_logres(loop_ptex->logres);
 		const MPtexTexelInfo texel_info = loop_ptex->texel_info;
-		BPXTypeDesc type_desc = BPX_TYPE_DESC_UINT8;
+		BPXTypeDesc type_desc;
 		
 		if (bpx_type_desc_from_mptex_data_type(texel_info.data_type,
 											   &type_desc)) {
@@ -1067,6 +1009,68 @@ bool BKE_ptex_import(Mesh *me, const char *filepath)
 
 	return true;
 }
+
+bool BKE_ptex_update_from_image(MLoopPtex *loop_ptex, const int totloop)
+{
+	BPXImageBuf *bpx_src;
+	int i;
+
+	// TODO
+	Image *image;
+	ImBuf *ibuf;
+
+	if (!loop_ptex) {
+		return false;
+	}
+
+	image = loop_ptex->image;
+	if (!image) {
+		return false;
+	}
+
+	ibuf = BKE_image_acquire_ibuf(image, NULL, NULL);
+
+	if (!ibuf) {
+		return false;
+	}
+
+	// TODO
+	BLI_assert(ibuf->rect);
+	BLI_assert(ibuf->num_ptex_regions == totloop);
+	BLI_assert(ibuf->ptex_regions);
+
+	bpx_src = IMB_imbuf_as_bpx_image_buf(ibuf);
+	if (!bpx_src) {
+		return false;
+	}
+
+	for (i = 0; i < totloop; i++) {
+		MLoopPtex *lp = &loop_ptex[i];
+		BPXImageBuf *bpx_dst = bpx_image_buf_wrap_loop_ptex(lp);
+		BPXRect src_rect;
+		const ImPtexRegion *region = &ibuf->ptex_regions[i];
+
+		BLI_assert(bpx_dst);
+		if (!bpx_dst) {
+			continue;
+		}
+
+		src_rect.xbegin = region->x;
+		src_rect.xend = region->x + region->width;
+		src_rect.ybegin = region->y;
+		src_rect.yend = region->y + region->height;
+
+		if (!BPX_image_buf_pixels_copy_partial(bpx_dst, bpx_src,
+											   0, 0, &src_rect)) {
+			BLI_assert(!"copy from image to MLoopPtex failed");
+		}
+	}
+
+	BKE_image_release_ibuf(image, ibuf, NULL);
+
+	return true;
+}
+
 #else
 /* Stubs if WITH_PTEX is not defined */
 
@@ -1098,4 +1102,10 @@ bool BKE_ptex_import(struct Mesh *UNUSED(me), const char *UNUSED(filepath))
 	return false;
 }
 
+bool BKE_ptex_update_from_image(MLoopPtex *UNUSED(loop_ptex),
+								const int UNUSED(totloop))
+{
+	return false;
+}
+
 #endif




More information about the Bf-blender-cvs mailing list