Blender V2.61 - r43446

rna_image_api.c

Go to the documentation of this file.
00001 /*
00002  * 
00003  * ***** BEGIN GPL LICENSE BLOCK *****
00004  *
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * as published by the Free Software Foundation; either version 2
00008  * of the License, or (at your option) any later version. 
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software Foundation,
00017  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00018  *
00019  * The Original Code is Copyright (C) 2009 Blender Foundation.
00020  * All rights reserved.
00021  *
00022  * 
00023  * Contributor(s): Arystanbek Dyussenov
00024  *
00025  * ***** END GPL LICENSE BLOCK *****
00026  */
00027 
00033 #include <stdlib.h>
00034 #include <stdio.h>
00035 #include <string.h>
00036 #include <time.h>
00037 
00038 #include "RNA_define.h"
00039 #include "RNA_enum_types.h"
00040 
00041 #include "DNA_packedFile_types.h"
00042 
00043 #include "BIF_gl.h"
00044 
00045 #ifdef RNA_RUNTIME
00046 
00047 #include "BKE_image.h"
00048 #include "BKE_packedFile.h"
00049 #include "BKE_main.h"
00050 
00051 #include "BKE_global.h" /* grr: G.main->name */
00052 
00053 #include "IMB_imbuf.h"
00054 
00055 #include "BIF_gl.h"
00056 #include "GPU_draw.h"
00057 
00058 #include "DNA_image_types.h"
00059 #include "DNA_scene_types.h"
00060 
00061 #include "MEM_guardedalloc.h"
00062 
00063 static void rna_Image_save_render(Image *image, bContext *C, ReportList *reports, const char *path, Scene *scene)
00064 {
00065     ImBuf *ibuf;
00066 
00067     if (scene == NULL) {
00068         scene = CTX_data_scene(C);
00069     }
00070 
00071     if (scene) {
00072         ImageUser iuser;
00073         void *lock;
00074 
00075         iuser.scene = scene;
00076         iuser.ok = 1;
00077 
00078         ibuf = BKE_image_acquire_ibuf(image, &iuser, &lock);
00079 
00080         if (ibuf == NULL) {
00081             BKE_reportf(reports, RPT_ERROR, "Couldn't acquire buffer from image");
00082         }
00083         else {
00084             /* temp swap out the color */
00085             const unsigned char imb_planes_back= ibuf->planes;
00086             const float dither_back= ibuf->dither; 
00087             ibuf->planes= scene->r.im_format.planes;
00088             ibuf->dither= scene->r.dither_intensity;
00089             if (!BKE_write_ibuf(ibuf, path, &scene->r.im_format)) {
00090                 BKE_reportf(reports, RPT_ERROR, "Couldn't write image: %s", path);
00091             }
00092             ibuf->planes= imb_planes_back;
00093             ibuf->dither= dither_back;
00094         }
00095 
00096         BKE_image_release_ibuf(image, lock);
00097     } else {
00098         BKE_reportf(reports, RPT_ERROR, "Scene not in context, couldn't get save parameters");
00099     }
00100 }
00101 
00102 static void rna_Image_save(Image *image, ReportList *reports)
00103 {
00104     ImBuf *ibuf= BKE_image_get_ibuf(image, NULL);
00105     if(ibuf) {
00106         char filename[FILE_MAX];
00107         BLI_strncpy(filename, image->name, sizeof(filename));
00108         BLI_path_abs(filename, G.main->name);
00109 
00110         if(image->packedfile) {
00111             if (writePackedFile(reports, image->name, image->packedfile, 0) != RET_OK) {
00112                 BKE_reportf(reports, RPT_ERROR, "Image \"%s\" could saved packed file to \"%s\"", image->id.name+2, image->name);
00113             }
00114         }
00115         else if (IMB_saveiff(ibuf, filename, ibuf->flags)) {
00116             image->type= IMA_TYPE_IMAGE;
00117 
00118             if(image->source==IMA_SRC_GENERATED)
00119                 image->source= IMA_SRC_FILE;
00120 
00121             ibuf->userflags &= ~IB_BITMAPDIRTY;
00122         }
00123         else {
00124             BKE_reportf(reports, RPT_ERROR, "Image \"%s\" could not be saved to \"%s\"", image->id.name+2, image->name);
00125         }
00126     }
00127     else {
00128         BKE_reportf(reports, RPT_ERROR, "Image \"%s\" does not have any image data", image->id.name+2);
00129     }
00130 }
00131 
00132 static void rna_Image_pack(Image *image, ReportList *reports, int as_png)
00133 {
00134     ImBuf *ibuf = BKE_image_get_ibuf(image, NULL);
00135 
00136     if (!as_png && (ibuf && (ibuf->userflags & IB_BITMAPDIRTY))) {
00137         BKE_reportf(reports, RPT_ERROR, "Can't pack edited image from disk, only as internal PNG");
00138     }
00139     else {
00140         if(as_png) {
00141             BKE_image_memorypack(image);
00142         }
00143         else {
00144             image->packedfile= newPackedFile(reports, image->name, ID_BLEND_PATH(G.main, &image->id));
00145         }
00146     }
00147 }
00148 
00149 static void rna_Image_unpack(Image *image, ReportList *reports, int method)
00150 {
00151     if (!image->packedfile) {
00152         BKE_report(reports, RPT_ERROR, "Image not packed");
00153     }
00154     else if (image->source==IMA_SRC_SEQUENCE || image->source==IMA_SRC_MOVIE) {
00155         BKE_report(reports, RPT_ERROR, "Unpacking movies or image sequences not supported");
00156         return;
00157     }
00158     else {
00159         /* reports its own error on failure */
00160         unpackImage (reports, image, method);
00161     }
00162 }
00163 
00164 static void rna_Image_reload(Image *image)
00165 {
00166     BKE_image_signal(image, NULL, IMA_SIGNAL_RELOAD);
00167 }
00168 
00169 static void rna_Image_update(Image *image, ReportList *reports)
00170 {
00171     ImBuf *ibuf= BKE_image_get_ibuf(image, NULL);
00172 
00173     if(ibuf == NULL) {
00174         BKE_reportf(reports, RPT_ERROR, "Image \"%s\" does not have any image data", image->id.name+2);
00175         return;
00176     }
00177 
00178     IMB_rect_from_float(ibuf);
00179 }
00180 
00181 static int rna_Image_gl_load(Image *image, ReportList *reports, int filter, int mag)
00182 {
00183     ImBuf *ibuf;
00184     unsigned int *bind = &image->bindcode;
00185     int error = GL_NO_ERROR;
00186 
00187     if(*bind)
00188         return error;
00189 
00190     ibuf= BKE_image_get_ibuf(image, NULL);
00191 
00192     if(ibuf == NULL || ibuf->rect == NULL ) {
00193         BKE_reportf(reports, RPT_ERROR, "Image \"%s\" does not have any image data", image->id.name+2);
00194         return (int)GL_INVALID_OPERATION;
00195     }
00196 
00197     /* could be made into a function? */
00198     glGenTextures(1, (GLuint*)bind);
00199     glBindTexture(GL_TEXTURE_2D, *bind);
00200 
00201     if (filter != GL_NEAREST && filter != GL_LINEAR)
00202         error = (int)gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, ibuf->x, ibuf->y, GL_RGBA, GL_UNSIGNED_INT, ibuf->rect);
00203 
00204     if (!error) {
00205         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, image->tpageflag & IMA_CLAMP_U ? GL_CLAMP : GL_REPEAT);
00206         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, image->tpageflag & IMA_CLAMP_V ? GL_CLAMP : GL_REPEAT);
00207         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (GLint)filter);
00208         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, (GLint)mag);
00209         glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
00210         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ibuf->x, ibuf->y, 0, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect); 
00211         error = (int)glGetError();
00212     }
00213 
00214     if (error) {
00215         glDeleteTextures(1, (GLuint*)bind);
00216         image->bindcode = 0;
00217     }
00218 
00219     return error;
00220 }
00221 
00222 static void rna_Image_gl_free(Image *image)
00223 {
00224     GPU_free_image(image);
00225 
00226     /* remove the nocollect flag, image is available for garbage collection again */
00227     image->flag &= ~IMA_NOCOLLECT;
00228 }
00229 
00230 #else
00231 
00232 void RNA_api_image(StructRNA *srna)
00233 {
00234     FunctionRNA *func;
00235     PropertyRNA *parm;
00236 
00237     func= RNA_def_function(srna, "save_render", "rna_Image_save_render");
00238     RNA_def_function_ui_description(func, "Save image to a specific path using a scenes render settings");
00239     RNA_def_function_flag(func, FUNC_USE_CONTEXT|FUNC_USE_REPORTS);
00240     parm= RNA_def_string_file_path(func, "filepath", "", 0, "", "Save path");
00241     RNA_def_property_flag(parm, PROP_REQUIRED);
00242     RNA_def_pointer(func, "scene", "Scene", "", "Scene to take image parameters from");
00243 
00244     func= RNA_def_function(srna, "save", "rna_Image_save");
00245     RNA_def_function_ui_description(func, "Save image to its source path");
00246     RNA_def_function_flag(func, FUNC_USE_REPORTS);
00247 
00248     func= RNA_def_function(srna, "pack", "rna_Image_pack");
00249     RNA_def_function_ui_description(func, "Pack an image as embedded data into the .blend file");
00250     RNA_def_function_flag(func, FUNC_USE_REPORTS);
00251     RNA_def_boolean(func, "as_png", 0, "as_png", "Pack the image as PNG (needed for generated/dirty images)");
00252 
00253     func= RNA_def_function(srna, "unpack", "rna_Image_unpack");
00254     RNA_def_function_ui_description(func, "Save an image packed in the .blend file to disk");
00255     RNA_def_function_flag(func, FUNC_USE_REPORTS);
00256     RNA_def_enum(func, "method", unpack_method_items, PF_USE_LOCAL, "method", "How to unpack");
00257 
00258     func= RNA_def_function(srna, "reload", "rna_Image_reload");
00259     RNA_def_function_ui_description(func, "Reload the image from its source path");
00260 
00261     func= RNA_def_function(srna, "update", "rna_Image_update");
00262     RNA_def_function_ui_description(func, "Update the display image from the floating point buffer");
00263     RNA_def_function_flag(func, FUNC_USE_REPORTS);
00264 
00265     func= RNA_def_function(srna, "gl_load", "rna_Image_gl_load");
00266     RNA_def_function_ui_description(func, "Load the image into OpenGL graphics memory");
00267     RNA_def_function_flag(func, FUNC_USE_REPORTS);
00268     RNA_def_int(func, "filter", GL_LINEAR_MIPMAP_NEAREST, -INT_MAX, INT_MAX, "Filter",
00269                 "The texture minifying function", -INT_MAX, INT_MAX);
00270     RNA_def_int(func, "mag", GL_LINEAR, -INT_MAX, INT_MAX, "Magnification",
00271                 "The texture magnification function", -INT_MAX, INT_MAX);
00272     /* return value */
00273     parm= RNA_def_int(func, "error", 0, -INT_MAX, INT_MAX, "Error", "OpenGL error value", -INT_MAX, INT_MAX);
00274     RNA_def_function_return(func, parm);
00275 
00276     func= RNA_def_function(srna, "gl_free", "rna_Image_gl_free");
00277     RNA_def_function_ui_description(func, "Free the image from OpenGL graphics memory");
00278 
00279     /* TODO, pack/unpack, maybe should be generic functions? */
00280 }
00281 
00282 #endif
00283