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

WIP imbuf hacks

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

M       source/blender/imbuf/CMakeLists.txt
M       source/blender/imbuf/IMB_imbuf_types.h
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_types.h 
b/source/blender/imbuf/IMB_imbuf_types.h
index 44cb7f1..7b30f87 100644
--- a/source/blender/imbuf/IMB_imbuf_types.h
+++ b/source/blender/imbuf/IMB_imbuf_types.h
@@ -138,6 +138,10 @@ typedef struct ImBuf {
 
        /* information for compressed textures */
        struct DDSData dds_data;
+
+       /* TODO */
+       float (*ptex_coords)[4];
+       unsigned int num_ptex_coords;
 } ImBuf;
 
 /* Moved from BKE_bmfont_types.h because it is a userflag bit mask. */
@@ -185,6 +189,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/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..7b66ec8 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_coords) {
+                               MEM_freeN(ibuf->ptex_coords);
+                       }
                        MEM_freeN(ibuf);
                }
        }
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..e1d54c5
--- /dev/null
+++ b/source/blender/imbuf/intern/imb_ptex.c
@@ -0,0 +1,101 @@
+#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 "bl_ptex.h"
+
+/* TODO(nicholasbishop): could also look at using OIIO here */
+
+/* TODO(nicholasbishop): color space stuff */
+
+/* TODO(nicholasbishop): not sure if PtexCache is thread safe to use
+ * like this */
+static struct PtexCacheHandle *imb_ptex_cache = NULL;
+
+void imb_init_ptex(void)
+{
+       imb_ptex_cache = ptex_cache_new();
+}
+
+void imb_exit_ptex(void)
+{
+       ptex_cache_delete(imb_ptex_cache);
+}
+
+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");
+}
+
+struct ImBuf *imb_load_ptex_filepath(const char *path, const int flags,
+                                                                        char 
colorspace[IM_MAX_SPACE])
+{
+       ImBuf *ibuf = NULL;
+
+       if (imb_is_a_ptex_filepath(path)) {
+               struct PtexPackedTexture *ppt = ptex_packed_texture_new();
+       
+               if (ppt) {
+                       // TODO
+                       const int width = 1024;
+                       int x, y;
+                       unsigned char *dst;
+                       const unsigned char *src;
+                       int c;
+                       ptex_packed_texture_fill(ppt, path, width, 
imb_ptex_cache);
+
+                       ibuf = IMB_allocImBuf(ptex_packed_texture_width(ppt),
+                                                                 
ptex_packed_texture_height(ppt),
+                                                                 
ptex_packed_texture_bytes_per_texel(ppt),
+                                                                 IB_rect);
+
+                       // TODO: unnecessary copying, also channels and stuff
+                       // TODO: only 8 bit ints for now
+
+                       dst = (unsigned char*)ibuf->rect;
+                       src = ptex_packed_texture_texels(ppt);
+                       for (y = 0; y < ibuf->y; y++) {
+                               for (x = 0; x < ibuf->x; x++) {
+                                       for (c = 0; c < 3; c++) {
+                                               *dst = *src;
+                                               src++;
+                                               dst++;
+                                       }
+                                       *dst = 0xff;
+                                       dst++;
+                               }
+                       }
+
+                       ibuf->ptex_coords =
+                               MEM_mallocN(sizeof(*ibuf->ptex_coords) *
+                                                       
ptex_packed_texture_table_len(ppt),
+                                                       "ptex_coords");
+                       for (c = 0; c < ptex_packed_texture_table_len(ppt); 
c++) {
+                               const struct PtexTableElement *elem =
+                                       ptex_packed_texture_table_elem(ppt, c);
+                               ibuf->ptex_coords[c][0] = elem->co[0];
+                               ibuf->ptex_coords[c][1] = elem->co[1];
+                               ibuf->ptex_coords[c][2] = elem->res[0];
+                               ibuf->ptex_coords[c][3] = elem->res[1];
+                       }
+                       ibuf->num_ptex_coords = 
ptex_packed_texture_table_len(ppt);
+
+                       // Test
+                       IMB_saveiff(ibuf, "/tmp/packed.png", 0);
+
+                       ptex_packed_texture_delete(ppt);
+               }
+       }
+
+       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