diff options
author | Campbell Barton <ideasman42@gmail.com> | 2006-03-26 13:25:30 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2006-03-26 13:25:30 +0400 |
commit | 915cd5b80b888c0eb70160ca6be11d7d39d43e8d (patch) | |
tree | 9873d1cdaabcc580f76ed6072be327f3d08d5706 /source | |
parent | e2637de00fed1d46f9cdcce6c57431b9e623a340 (diff) |
Added python image pack/unpack per image.
Diffstat (limited to 'source')
-rw-r--r-- | source/blender/blenkernel/BKE_packedFile.h | 2 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/packedFile.c | 16 | ||||
-rw-r--r-- | source/blender/python/api2_2x/Image.c | 86 | ||||
-rw-r--r-- | source/blender/python/api2_2x/doc/Image.py | 90 |
4 files changed, 145 insertions, 49 deletions
diff --git a/source/blender/blenkernel/BKE_packedFile.h b/source/blender/blenkernel/BKE_packedFile.h index 270a0492f91..be9792322c4 100644 --- a/source/blender/blenkernel/BKE_packedFile.h +++ b/source/blender/blenkernel/BKE_packedFile.h @@ -52,7 +52,7 @@ int readPackedFile(struct PackedFile * pf, void * data, int size); int countPackedFiles(void); void freePackedFile(struct PackedFile * pf); void packAll(void); -int writePackedFile(char * filename, struct PackedFile *pf); +int writePackedFile(char * filename, struct PackedFile *pf, int guimode); int checkPackedFile(char * filename, struct PackedFile * pf); char * unpackFile(char * abs_name, char * local_name, struct PackedFile * pf, int how); int unpackVFont(struct VFont * vfont, int how); diff --git a/source/blender/blenkernel/intern/packedFile.c b/source/blender/blenkernel/intern/packedFile.c index cb47c43da06..49ad8d2ac67 100644 --- a/source/blender/blenkernel/intern/packedFile.c +++ b/source/blender/blenkernel/intern/packedFile.c @@ -278,7 +278,7 @@ char * find_new_name(char * name) */ -int writePackedFile(char * filename, PackedFile *pf) +int writePackedFile(char * filename, PackedFile *pf, int guimode) { int file, number, remove_tmp = FALSE; int ret_value = RET_OK; @@ -309,28 +309,28 @@ int writePackedFile(char * filename, PackedFile *pf) file = open(name, O_BINARY + O_WRONLY + O_CREAT + O_TRUNC, 0666); if (file >= 0) { if (write(file, pf->data, pf->size) != pf->size) { - error("Error writing file: %s", name); + if(guimode) error("Error writing file: %s", name); ret_value = RET_ERROR; } close(file); } else { - error("Error creating file: %s", name); + if(guimode) error("Error creating file: %s", name); ret_value = RET_ERROR; } if (remove_tmp) { if (ret_value == RET_ERROR) { if (BLI_rename(tempname, name) == RET_ERROR) { - error("Error restoring tempfile. Check files: '%s' '%s'", tempname, name); + if(guimode) error("Error restoring tempfile. Check files: '%s' '%s'", tempname, name); } } else { if (BLI_delete(tempname, 0, 0) == RET_ERROR) { - error("Error deleting '%s' (ignored)"); + if(guimode) error("Error deleting '%s' (ignored)"); } } } - waitcursor(0); + if(guimode) waitcursor(0); return (ret_value); } @@ -468,7 +468,7 @@ char * unpackFile(char * abs_name, char * local_name, PackedFile * pf, int how) } // else fall through and create it case PF_WRITE_LOCAL: - if (writePackedFile(local_name, pf) == RET_OK) { + if (writePackedFile(local_name, pf, 1) == RET_OK) { temp = local_name; } break; @@ -480,7 +480,7 @@ char * unpackFile(char * abs_name, char * local_name, PackedFile * pf, int how) } // else fall through and create it case PF_WRITE_ORIGINAL: - if (writePackedFile(abs_name, pf) == RET_OK) { + if (writePackedFile(abs_name, pf, 1) == RET_OK) { temp = abs_name; } break; diff --git a/source/blender/python/api2_2x/Image.c b/source/blender/python/api2_2x/Image.c index 1f76ff1c7a3..717e7f574f9 100644 --- a/source/blender/python/api2_2x/Image.c +++ b/source/blender/python/api2_2x/Image.c @@ -43,6 +43,9 @@ #include "IMB_imbuf_types.h" /* for the IB_rect define */ #include "BIF_gl.h" #include "gen_utils.h" +#include "BKE_packedFile.h" +#include "DNA_packedFile_types.h" +#include "BKE_icons.h" /* fixme @@ -406,7 +409,7 @@ static PyObject *Image_setPixelF( BPy_Image * self, PyObject * args ) || y > ( image->ibuf->y - 1 ) || x < image->ibuf->xorig || y < image->ibuf->yorig ) return EXPP_ReturnPyObjError( PyExc_RuntimeError, - "x or y is out of range" ); + "x or y is out of ruange" ); for( a = 0; a < 4; a++ ) { if( p[a] > 1.0 || p[a] < 0.0 ) @@ -541,6 +544,75 @@ static PyObject *Image_getMinXY( BPy_Image * self ) } +/* unpack +mode 0; never overwrite +mode 1; overwrite only if differs packed. +mode 2; always overwrite. +*/ + + +static PyObject *Image_unpack( BPy_Image * self, PyObject * args ) +{ + Image *image = self->image; + int mode, check, ret=RET_OK; /* offset into image data */ + char expandpath[FILE_MAXDIR + FILE_MAXFILE]; + + /*get the absolute path */ + if( !PyArg_ParseTuple( args, "i", &mode ) ) + return EXPP_ReturnPyObjError( PyExc_TypeError, + "expected 1 integer" ); + + if (image->packedfile==NULL) + return EXPP_ReturnPyObjError( PyExc_RuntimeError, + "image not packed" ); + + BLI_strncpy(expandpath, image->name, FILE_MAXDIR+FILE_MAXFILE); + BLI_convertstringcode(expandpath, G.sce, 1); + check= checkPackedFile(expandpath, image->packedfile); + + if (check==PF_NOFILE) { + ret= writePackedFile(expandpath, image->packedfile, 0); /* no guimode */ + } else if (check==PF_EQUAL){ + if (mode==2) /*always overwrite */ + ret= writePackedFile(expandpath, image->packedfile, 0); + } else if (check==PF_DIFFERS) { + if (mode!=0) + ret= writePackedFile(expandpath, image->packedfile, 0); /* no guimode */ + } + + if (ret==RET_ERROR) + return EXPP_ReturnPyObjError( PyExc_RuntimeError, + "internal unpacking error, could not write packed image, image still packed." ); + + /*free packed data*/ + freePackedFile(image->packedfile); + image->packedfile=NULL; + + /*free icon*/ + BKE_icon_delete(&image->id); + image->id.icon_id = 0; + + Py_RETURN_NONE; +} + +static PyObject *Image_pack( BPy_Image * self ) +{ + Image *image = self->image; + char expandpath[FILE_MAXDIR + FILE_MAXFILE]; + BLI_strncpy(expandpath, image->name, FILE_MAXDIR+FILE_MAXFILE); + + if (image->packedfile ) + return EXPP_ReturnPyObjError( PyExc_RuntimeError, + "image alredy packed" ); + if (!BLI_exists(expandpath)) + return EXPP_ReturnPyObjError( PyExc_RuntimeError, + "image path does not exist" ); + + image->packedfile = newPackedFile(image->name); + Py_RETURN_NONE; +} + + /* save image to file */ static PyObject *Image_save( BPy_Image * self ) @@ -606,6 +678,8 @@ static PyObject *Image_setPixelI( BPy_Image * self, PyObject * args ); static PyObject *Image_getMaxXY( BPy_Image * self ); static PyObject *Image_getMinXY( BPy_Image * self ); static PyObject *Image_save( BPy_Image * self ); +static PyObject *Image_unpack( BPy_Image * self, PyObject * args ); +static PyObject *Image_pack( BPy_Image * self ); /*****************************************************************************/ @@ -669,6 +743,10 @@ static PyMethodDef BPy_Image_methods[] = { "(int) - Change Image object animation speed (fps)"}, {"save", ( PyCFunction ) Image_save, METH_NOARGS, "() - Write image buffer to file"}, + {"unpack", ( PyCFunction ) Image_unpack, METH_VARARGS, + "(int) - Unpack image. [0,1,2], Never overwrite, Overwrite if different, Overwrite all."}, + {"pack", ( PyCFunction ) Image_pack, METH_NOARGS, + "() Pack the image"}, {NULL, NULL, 0, NULL} }; @@ -1102,11 +1180,11 @@ static PyObject *Image_getAttr( BPy_Image * self, char *name ) else if( strcmp( name, "speed" ) == 0 ) attr = PyInt_FromLong( self->image->animspeed ); else if( strcmp( name, "packed" ) == 0 ) { - if (self->image->packedfile) { + if (self->image->packedfile) attr = EXPP_incr_ret_True(); - } else { + else attr = EXPP_incr_ret_False(); - } + } else if( strcmp( name, "bindcode" ) == 0 ) attr = PyInt_FromLong( self->image->bindcode ); else if( strcmp( name, "users" ) == 0 ) diff --git a/source/blender/python/api2_2x/doc/Image.py b/source/blender/python/api2_2x/doc/Image.py index 7129075e10c..6ae215c9dcb 100644 --- a/source/blender/python/api2_2x/doc/Image.py +++ b/source/blender/python/api2_2x/doc/Image.py @@ -112,27 +112,27 @@ class Image: def getPixelF(x, y): """ - Get the the colors of the current pixel in the form [r,g,b,a]. - Returned values are floats normalized to 0.0 - 1.0. - Pixel coordinates are in the range from 0 to N-1. See L{getMaxXY} - @returns: [ r, g, b, a] - @rtype: list of 4 floats - @type x: int - @type y: int - @param x: the x coordinate of pixel. - @param y: the y coordinate of pixel. + Get the the colors of the current pixel in the form [r,g,b,a]. + Returned values are floats normalized to 0.0 - 1.0. + Pixel coordinates are in the range from 0 to N-1. See L{getMaxXY} + @returns: [ r, g, b, a] + @rtype: list of 4 floats + @type x: int + @type y: int + @param x: the x coordinate of pixel. + @param y: the y coordinate of pixel. """ def getPixelI(x, y): """ - Get the the colors of the current pixel in the form [r,g,b,a]. - Returned values are ints normalized to 0 - 255. - Pixel coordinates are in the range from 0 to N-1. See L{getMaxXY} - @returns: [ r, g, b, a] - @rtype: list of 4 ints - @type x: int - @type y: int - @param x: the x coordinate of pixel. - @param y: the y coordinate of pixel. + Get the the colors of the current pixel in the form [r,g,b,a]. + Returned values are ints normalized to 0 - 255. + Pixel coordinates are in the range from 0 to N-1. See L{getMaxXY} + @returns: [ r, g, b, a] + @rtype: list of 4 ints + @type x: int + @type y: int + @param x: the x coordinate of pixel. + @param y: the y coordinate of pixel. """ def getMaxXY(): @@ -276,30 +276,30 @@ class Image: """ Set the the colors of the current pixel in the form [r,g,b,a]. Color values must be floats in the range 0.0 - 1.0. - Pixel coordinates are in the range from 0 to N-1. See L{getMaxXY} - @type x: int - @type y: int - @type r: float - @type g: float - @type b: float - @type a: float - @returns: nothing - @rtype: none + Pixel coordinates are in the range from 0 to N-1. See L{getMaxXY} + @type x: int + @type y: int + @type r: float + @type g: float + @type b: float + @type a: float + @returns: nothing + @rtype: none """ def setPixelI(x, y, (r, g, b, a)): """ Set the the colors of the current pixel in the form [r,g,b,a]. Color values must be ints in the range 0 - 255. - Pixel coordinates are in the range from 0 to N-1. See L{getMaxXY} - @type x: int - @type y: int - @type r: int - @type g: int - @type b: int - @type a: int - @returns: nothing - @rtype: none + Pixel coordinates are in the range from 0 to N-1. See L{getMaxXY} + @type x: int + @type y: int + @type r: int + @type g: int + @type b: int + @type a: int + @returns: nothing + @rtype: none """ def save(): @@ -308,3 +308,21 @@ class Image: @returns: nothing @rtype: none """ + + def pack(): + """ + Packs the image into the current blend file. + @note: An error will be raised if the image is alredy packed or the filename path does not exist. + @returns: nothing + @rtype: none + """ + + def unpack(mode): + """ + Unpacks the image to the images filename. + @param mode: if 0, the existing file located at filename will be used. 1, The file will be overwritten if its different. 2, always overwrite the existing image file. + @note: An error will be raised if the image is not packed or the filename path does not exist. + @returns: nothing + @rtype: none + @type mode: int + """
\ No newline at end of file |