Commit: bc5fa16ddbfb82e281b297e3589d215fca491a64
Author: Nicholas Bishop
Date:   Mon Jan 19 20:03:26 2015 +0100
Branches: cycles-ptex-49
https://developer.blender.org/rBbc5fa16ddbfb82e281b297e3589d215fca491a64

Add basic Ptex reading via OIIO to ImBuf

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

M       source/blender/imbuf/CMakeLists.txt
M       source/blender/imbuf/IMB_imbuf.h
M       source/blender/imbuf/IMB_imbuf_types.h
M       source/blender/imbuf/SConscript
M       source/blender/imbuf/intern/IMB_filetype.h
M       source/blender/imbuf/intern/allocimbuf.c
M       source/blender/imbuf/intern/filetype.c
A       source/blender/imbuf/intern/imb_ptex.c
M       source/blender/imbuf/intern/util.c

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

diff --git a/source/blender/imbuf/CMakeLists.txt 
b/source/blender/imbuf/CMakeLists.txt
index e897791..187c711 100644
--- a/source/blender/imbuf/CMakeLists.txt
+++ b/source/blender/imbuf/CMakeLists.txt
@@ -30,6 +30,7 @@ set(INC
        ../blenloader
        ../makesdna
        ../makesrna
+       ../../../extern/ptex
        ../../../intern/guardedalloc
        ../../../intern/memutil
 )
@@ -50,6 +51,7 @@ set(SRC
        intern/filetype.c
        intern/filter.c
        intern/imageprocess.c
+       intern/imb_ptex.c
        intern/indexer.c
        intern/indexer_dv.c
        intern/iris.c
diff --git a/source/blender/imbuf/IMB_imbuf.h b/source/blender/imbuf/IMB_imbuf.h
index 17bb873..5dce318 100644
--- a/source/blender/imbuf/IMB_imbuf.h
+++ b/source/blender/imbuf/IMB_imbuf.h
@@ -549,5 +549,24 @@ void IMB_processor_apply_threaded(int buffer_lines, int 
handle_size, void *init_
 void IMB_ffmpeg_init(void);
 const char *IMB_ffmpeg_last_error(void);
 
+/* ptex */
+
+struct BPXImageBuf;
+struct PtexPackedLayout;
+
+/* Create a new BPXImageBuf that wraps an ImBuf
+ *
+ * The BPXImageBuf will have access to the original ImBuf memory
+ * rather than allocating a copy.
+ *
+ * Return NULL on failure.
+ *
+ * The result should be freed with BPX_image_buf_free() when no longer
+ * needed. */
+struct BPXImageBuf *IMB_imbuf_as_bpx_image_buf(struct ImBuf *ibuf);
+
+/* Create an ImBuf from a finalized PtexPackedLayout */
+struct ImBuf *IMB_alloc_from_ptex_layout(const struct PtexPackedLayout 
*layout);
+
 #endif
 
diff --git a/source/blender/imbuf/IMB_imbuf_types.h 
b/source/blender/imbuf/IMB_imbuf_types.h
index 867c4a8..80d79ac 100644
--- a/source/blender/imbuf/IMB_imbuf_types.h
+++ b/source/blender/imbuf/IMB_imbuf_types.h
@@ -57,6 +57,14 @@ typedef struct DDSData {
        unsigned int size; /* The size of the compressed data */
 } DDSData;
 
+// TODO
+typedef struct {
+       int x;
+       int y;
+       int width;
+       int height;
+} ImPtexRegion;
+
 /**
  * \ingroup imbuf
  * This is the abstraction of an image.  ImBuf is the basic type used for all
@@ -136,6 +144,10 @@ typedef struct ImBuf {
 
        /* information for compressed textures */
        struct DDSData dds_data;
+
+       /* TODO */
+       ImPtexRegion *ptex_regions; /* array */
+       int num_ptex_regions;
 } ImBuf;
 
 /* Moved from BKE_bmfont_types.h because it is a userflag bit mask. */
@@ -183,6 +195,11 @@ typedef struct ImBuf {
 #define PSD                            (1 << 31)
 #endif
 
+/* TODO(nicholasbishop): this is a really confusing list of defines */
+//#ifdef WITH_PTEX
+#define IMB_PTEX               (1 << 29)
+//#endif
+
 #define PNG                            (1 << 30)
 #define TGA                            (1 << 28)
 #define JPG                            (1 << 27)
diff --git a/source/blender/imbuf/SConscript b/source/blender/imbuf/SConscript
index ccee3eb..1fb22c4 100644
--- a/source/blender/imbuf/SConscript
+++ b/source/blender/imbuf/SConscript
@@ -36,6 +36,7 @@ incs = [
     '#/intern/ffmpeg',
     '#/intern/guardedalloc',
     '#/intern/memutil',
+    '#/extern/ptex',
     '../avi',
     '../blenkernel',
     '../blenlib',
diff --git a/source/blender/imbuf/intern/IMB_filetype.h 
b/source/blender/imbuf/intern/IMB_filetype.h
index 9327c15..ba8bda0 100644
--- a/source/blender/imbuf/intern/IMB_filetype.h
+++ b/source/blender/imbuf/intern/IMB_filetype.h
@@ -121,5 +121,13 @@ void imb_loadtiletiff(struct ImBuf *ibuf, unsigned char 
*mem, size_t size,
        int tx, int ty, unsigned int *rect);
 int imb_savetiff(struct ImBuf *ibuf, const char *name, int flags);
 
+/* ptex */
+void imb_init_ptex(void);
+void imb_exit_ptex(void);
+int imb_is_a_ptex(unsigned char *buf);
+int imb_is_a_ptex_filepath(const char *name);
+struct ImBuf *imb_load_ptex_filepath(const char *name, int flags,
+                                                                        char 
colorspace[IM_MAX_SPACE]);
+
 #endif /* __IMB_FILETYPE_H__ */
 
diff --git a/source/blender/imbuf/intern/allocimbuf.c 
b/source/blender/imbuf/intern/allocimbuf.c
index 556c4be..9703966 100644
--- a/source/blender/imbuf/intern/allocimbuf.c
+++ b/source/blender/imbuf/intern/allocimbuf.c
@@ -190,6 +190,9 @@ void IMB_freeImBuf(ImBuf *ibuf)
                        if (ibuf->dds_data.data != NULL) {
                                free(ibuf->dds_data.data); /* dds_data.data is 
allocated by DirectDrawSurface::readData(), so don't use MEM_freeN! */
                        }
+                       if (ibuf->ptex_regions) {
+                               MEM_freeN(ibuf->ptex_regions);
+                       }
                        MEM_freeN(ibuf);
                }
        }
@@ -469,6 +472,10 @@ ImBuf *IMB_dupImBuf(ImBuf *ibuf1)
                tbuf.mipmap[a] = NULL;
        tbuf.dds_data.data = NULL;
        
+       if (ibuf1->ptex_regions) {
+               tbuf.ptex_regions = MEM_dupallocN(ibuf1->ptex_regions);
+       }
+
        /* set malloc flag */
        tbuf.mall               = ibuf2->mall;
        tbuf.c_handle           = NULL;
diff --git a/source/blender/imbuf/intern/filetype.c 
b/source/blender/imbuf/intern/filetype.c
index c6e358d..52fd512 100644
--- a/source/blender/imbuf/intern/filetype.c
+++ b/source/blender/imbuf/intern/filetype.c
@@ -89,6 +89,13 @@ ImFileType IMB_FILE_TYPES[] = {
 #ifdef WITH_OPENIMAGEIO
        {NULL, NULL, NULL, imb_is_a_photoshop, imb_ftype_default, NULL, 
imb_load_photoshop, NULL, NULL, IM_FTYPE_FLOAT, PSD, COLOR_ROLE_DEFAULT_FLOAT},
 #endif
+       /* TODO */
+       //#ifdef WITH_PTEX
+       {imb_init_ptex, imb_exit_ptex, imb_is_a_ptex, imb_is_a_ptex_filepath,
+        imb_ftype_default, NULL /* load */, imb_load_ptex_filepath,
+        NULL /* save */, NULL /* load_tile */, IM_FTYPE_FLOAT, IMB_PTEX,
+        COLOR_ROLE_DEFAULT_BYTE},
+       //#endif
        {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0}
 };
 
diff --git a/source/blender/imbuf/intern/imb_ptex.c 
b/source/blender/imbuf/intern/imb_ptex.c
new file mode 100644
index 0000000..caca591
--- /dev/null
+++ b/source/blender/imbuf/intern/imb_ptex.c
@@ -0,0 +1,114 @@
+#include "BLI_utildefines.h"
+
+#include "imbuf.h"
+#include "IMB_imbuf_types.h"
+#include "IMB_imbuf.h"
+#include "IMB_filetype.h"
+
+#include "BLI_string.h"
+#include "MEM_guardedalloc.h"
+
+#include "BPX_pack.h"
+
+/* TODO(nicholasbishop): color space stuff */
+
+void imb_init_ptex(void)
+{
+       // TODO
+}
+
+void imb_exit_ptex(void)
+{
+       // TODO
+}
+
+int imb_is_a_ptex(unsigned char *buf)
+{
+       unsigned char magic[4] = "Ptex";
+       return memcmp(buf, magic, sizeof(magic)) == 0;
+}
+
+int imb_is_a_ptex_filepath(const char *name)
+{
+       return BLI_str_endswith(name, ".ptx");
+}
+
+ImBuf *IMB_alloc_from_ptex_layout(const struct PtexPackedLayout *layout)
+{
+       ImBuf *ibuf = IMB_allocImBuf(ptex_packed_layout_width(layout),
+                                                                
ptex_packed_layout_height(layout),
+                                                                /* TODO? */
+                                                                32,
+                                                                /* TODO */
+                                                                IB_rect);
+       if (ibuf) {
+               int i;
+
+               /* Copy layout items into ImBuf.ptex_regions */
+               ibuf->num_ptex_regions = BPX_packed_layout_num_regions(layout);
+               ibuf->ptex_regions = MEM_mallocN(sizeof(*ibuf->ptex_regions) *
+                                                                               
 ibuf->num_ptex_regions,
+                                                                               
 "ImBuf ptex_regions");
+
+               for (i = 0; i < ibuf->num_ptex_regions; i++) {
+                       ImPtexRegion *dst_region = &ibuf->ptex_regions[i];
+                       if (!ptex_packed_layout_item(layout, i, &dst_region->x,
+                                                                               
 &dst_region->y, &dst_region->width,
+                                                                               
 &dst_region->height))
+                       {
+                               /* Error */
+                               IMB_freeImBuf(ibuf);
+                               return NULL;
+                       }
+               }
+       }
+
+       return ibuf;
+}
+
+static BPXImageBuf *imb_alloc_from_ptex_layout_cb(const struct 
PtexPackedLayout *layout,
+                                                                               
                  void *vcontext)
+{
+       ImBuf **ibuf = vcontext;
+       (*ibuf) = IMB_alloc_from_ptex_layout(layout);
+       return IMB_imbuf_as_bpx_image_buf(*ibuf);
+}
+
+BPXImageBuf *IMB_imbuf_as_bpx_image_buf(ImBuf *ibuf)
+{
+       /* TODO: channels and data type */
+       const int num_channels = 4;
+       return BPX_image_buf_wrap(ibuf->x, ibuf->y, num_channels, 
+                                                         BPX_TYPE_DESC_UINT8, 
ibuf->rect);
+}
+
+struct ImBuf *imb_load_ptex_filepath(const char *path,
+                                                                        const 
int UNUSED(flags),
+                                                                        char 
colorspace[IM_MAX_SPACE])
+{
+       ImBuf *ibuf = NULL;
+
+       if (imb_is_a_ptex_filepath(path)) {
+               BPXImageInput *bpx_src = BPX_image_input_from_filepath(path);
+
+               if (bpx_src) {
+                       BPXImageBuf *dst;
+                       dst = BPX_image_buf_ptex_pack(bpx_src,
+                                                                               
  imb_alloc_from_ptex_layout_cb,
+                                                                               
  &ibuf);
+                       if (!dst) {
+                               if (ibuf) {
+                                       IMB_freeImBuf(ibuf);
+                               }
+                               return NULL;
+                       }
+                       
+                       BPX_image_buf_free(dst);
+                                       
+                       // TODO
+                       IMB_rectfill_alpha(ibuf, 1);
+               }
+       }
+
+       return ibuf;
+}
diff --git a/source/blender/imbuf/intern/util.c 
b/source/blender/imbuf/intern/util.c
index 32100aa..848d572 100644
--- a/source/blender/imbuf/intern/util.c
+++ b/source/blender/imbuf/intern/util.c
@@ -104,6 +104,8 @@ const char *imb_ext_image_filepath_only[] = {
 #ifdef WITH_OPENIMAGEIO
        ".psd", ".pdd", ".psb",
 #endif
+       // TODO
+       ".ptx",
        NULL
 };

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to