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