Blender  V2.59
image_ops.c
Go to the documentation of this file.
00001 /*
00002  * $Id: image_ops.c 39108 2011-08-06 22:31:16Z merwin $
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) 2001-2002 by NaN Holding BV.
00021  * All rights reserved.
00022  *
00023  * Contributor(s): Blender Foundation, 2002-2009, Xavier Thomas
00024  *
00025  * ***** END GPL LICENSE BLOCK *****
00026  */
00027 
00033 #include <stddef.h>
00034 #include <string.h>
00035 #include <stdlib.h>
00036 #include <errno.h>
00037 
00038 #include "MEM_guardedalloc.h"
00039 
00040 #include "BLI_math.h"
00041 #include "BLI_blenlib.h"
00042 #include "BLI_utildefines.h"
00043 
00044 #include "DNA_object_types.h"
00045 #include "DNA_node_types.h"
00046 #include "DNA_packedFile_types.h"
00047 #include "DNA_scene_types.h"
00048 
00049 #include "BKE_colortools.h"
00050 #include "BKE_context.h"
00051 #include "BKE_image.h"
00052 #include "BKE_global.h"
00053 #include "BKE_library.h"
00054 #include "BKE_main.h"
00055 #include "BKE_node.h"
00056 #include "BKE_packedFile.h"
00057 #include "BKE_report.h"
00058 #include "BKE_screen.h"
00059 
00060 #include "IMB_imbuf.h"
00061 #include "IMB_imbuf_types.h"
00062 
00063 #include "RE_pipeline.h"
00064 
00065 #include "RNA_access.h"
00066 #include "RNA_define.h"
00067 #include "RNA_enum_types.h"
00068 
00069 #include "ED_image.h"
00070 #include "ED_render.h"
00071 #include "ED_screen.h"
00072 #include "ED_space_api.h"
00073 #include "ED_uvedit.h"
00074 #include "ED_util.h"
00075 
00076 #include "UI_interface.h"
00077 #include "UI_resources.h"
00078 #include "UI_view2d.h"
00079 
00080 #include "WM_api.h"
00081 #include "WM_types.h"
00082 
00083 #include "image_intern.h"
00084 
00085 /******************** view navigation utilities *********************/
00086 
00087 static void sima_zoom_set(SpaceImage *sima, ARegion *ar, float zoom)
00088 {
00089         float oldzoom= sima->zoom;
00090         int width, height;
00091 
00092         sima->zoom= zoom;
00093 
00094         if (sima->zoom > 0.1f && sima->zoom < 4.0f)
00095                 return;
00096 
00097         /* check zoom limits */
00098         ED_space_image_size(sima, &width, &height);
00099 
00100         width *= sima->zoom;
00101         height *= sima->zoom;
00102 
00103         if((width < 4) && (height < 4))
00104                 sima->zoom= oldzoom;
00105         else if((ar->winrct.xmax - ar->winrct.xmin) <= sima->zoom)
00106                 sima->zoom= oldzoom;
00107         else if((ar->winrct.ymax - ar->winrct.ymin) <= sima->zoom)
00108                 sima->zoom= oldzoom;
00109 }
00110 
00111 static void sima_zoom_set_factor(SpaceImage *sima, ARegion *ar, float zoomfac)
00112 {
00113         sima_zoom_set(sima, ar, sima->zoom*zoomfac);
00114 }
00115 
00116 #if 0 // currently unused
00117 static int image_poll(bContext *C)
00118 {
00119         return (CTX_data_edit_image(C) != NULL);
00120 }
00121 #endif
00122 
00123 static int space_image_buffer_exists_poll(bContext *C)
00124 {
00125         SpaceImage *sima= CTX_wm_space_image(C);
00126         if(sima && sima->spacetype==SPACE_IMAGE)
00127                 if(ED_space_image_has_buffer(sima))
00128                         return 1;
00129         return 0;
00130 }
00131 
00132 static int space_image_file_exists_poll(bContext *C)
00133 {
00134         if(space_image_buffer_exists_poll(C)) {
00135                 Main *bmain= CTX_data_main(C);
00136                 SpaceImage *sima= CTX_wm_space_image(C);
00137                 ImBuf *ibuf;
00138                 void *lock;
00139                 int poll= 0;
00140                 char name[FILE_MAX];
00141 
00142                 ibuf= ED_space_image_acquire_buffer(sima, &lock);
00143                 if(ibuf) {
00144                         BLI_strncpy(name, ibuf->name, FILE_MAX);
00145                         BLI_path_abs(name, bmain->name);
00146                         poll= (BLI_exists(name) && BLI_is_writable(name));
00147                 }
00148                 ED_space_image_release_buffer(sima, lock);
00149 
00150                 return poll;
00151         }
00152         return 0;
00153 }
00154 
00155 static int space_image_poll(bContext *C)
00156 {
00157         SpaceImage *sima= CTX_wm_space_image(C);
00158         if(sima && sima->spacetype==SPACE_IMAGE && sima->image)
00159                 return 1;
00160         return 0;
00161 }
00162 
00163 int space_image_main_area_poll(bContext *C)
00164 {
00165         SpaceImage *sima= CTX_wm_space_image(C);
00166         // XXX ARegion *ar= CTX_wm_region(C);
00167 
00168         if(sima)
00169                 return 1; // XXX (ar && ar->type->regionid == RGN_TYPE_WINDOW);
00170         
00171         return 0;
00172 }
00173 
00174 /********************** view pan operator *********************/
00175 
00176 typedef struct ViewPanData {
00177         float x, y;
00178         float xof, yof;
00179         int event_type;
00180 } ViewPanData;
00181 
00182 static void view_pan_init(bContext *C, wmOperator *op, wmEvent *event)
00183 {
00184         SpaceImage *sima= CTX_wm_space_image(C);
00185         ViewPanData *vpd;
00186 
00187         op->customdata= vpd= MEM_callocN(sizeof(ViewPanData), "ImageViewPanData");
00188         WM_cursor_modal(CTX_wm_window(C), BC_NSEW_SCROLLCURSOR);
00189 
00190         vpd->x= event->x;
00191         vpd->y= event->y;
00192         vpd->xof= sima->xof;
00193         vpd->yof= sima->yof;
00194         vpd->event_type= event->type;
00195 
00196         WM_event_add_modal_handler(C, op);
00197 }
00198 
00199 static void view_pan_exit(bContext *C, wmOperator *op, int cancel)
00200 {
00201         SpaceImage *sima= CTX_wm_space_image(C);
00202         ViewPanData *vpd= op->customdata;
00203 
00204         if(cancel) {
00205                 sima->xof= vpd->xof;
00206                 sima->yof= vpd->yof;
00207                 ED_region_tag_redraw(CTX_wm_region(C));
00208         }
00209 
00210         WM_cursor_restore(CTX_wm_window(C));
00211         MEM_freeN(op->customdata);
00212 }
00213 
00214 static int view_pan_exec(bContext *C, wmOperator *op)
00215 {
00216         SpaceImage *sima= CTX_wm_space_image(C);
00217         float offset[2];
00218 
00219         RNA_float_get_array(op->ptr, "offset", offset);
00220         sima->xof += offset[0];
00221         sima->yof += offset[1];
00222 
00223         ED_region_tag_redraw(CTX_wm_region(C));
00224 
00225         /* XXX notifier? */
00226 #if 0
00227         if(image_preview_active(curarea, NULL, NULL)) {
00228                 /* recalculates new preview rect */
00229                 scrarea_do_windraw(curarea);
00230                 image_preview_event(2);
00231         }
00232 #endif
00233         
00234         return OPERATOR_FINISHED;
00235 }
00236 
00237 static int view_pan_invoke(bContext *C, wmOperator *op, wmEvent *event)
00238 {
00239         if (event->type == MOUSEPAN) {
00240                 SpaceImage *sima= CTX_wm_space_image(C);
00241                 float offset[2];
00242                 
00243                 offset[0]= (event->x - event->prevx)/sima->zoom;
00244                 offset[1]= (event->y - event->prevy)/sima->zoom;
00245                 RNA_float_set_array(op->ptr, "offset", offset);
00246 
00247                 view_pan_exec(C, op);
00248                 return OPERATOR_FINISHED;
00249         }
00250         else {
00251                 view_pan_init(C, op, event);
00252                 return OPERATOR_RUNNING_MODAL;
00253         }
00254 }
00255 
00256 static int view_pan_modal(bContext *C, wmOperator *op, wmEvent *event)
00257 {
00258         SpaceImage *sima= CTX_wm_space_image(C);
00259         ViewPanData *vpd= op->customdata;
00260         float offset[2];
00261 
00262         switch(event->type) {
00263                 case MOUSEMOVE:
00264                         sima->xof= vpd->xof;
00265                         sima->yof= vpd->yof;
00266                         offset[0]= (vpd->x - event->x)/sima->zoom;
00267                         offset[1]= (vpd->y - event->y)/sima->zoom;
00268                         RNA_float_set_array(op->ptr, "offset", offset);
00269                         view_pan_exec(C, op);
00270                         break;
00271                 default:
00272                         if(event->type==vpd->event_type &&  event->val==KM_RELEASE) {
00273                                 view_pan_exit(C, op, 0);
00274                                 return OPERATOR_FINISHED;
00275                         }
00276                         break;
00277         }
00278 
00279         return OPERATOR_RUNNING_MODAL;
00280 }
00281 
00282 static int view_pan_cancel(bContext *C, wmOperator *op)
00283 {
00284         view_pan_exit(C, op, 1);
00285         return OPERATOR_CANCELLED;
00286 }
00287 
00288 void IMAGE_OT_view_pan(wmOperatorType *ot)
00289 {
00290         /* identifiers */
00291         ot->name= "View Pan";
00292         ot->idname= "IMAGE_OT_view_pan";
00293         
00294         /* api callbacks */
00295         ot->exec= view_pan_exec;
00296         ot->invoke= view_pan_invoke;
00297         ot->modal= view_pan_modal;
00298         ot->cancel= view_pan_cancel;
00299         ot->poll= space_image_main_area_poll;
00300 
00301         /* flags */
00302         ot->flag= OPTYPE_BLOCKING;
00303         
00304         /* properties */
00305         RNA_def_float_vector(ot->srna, "offset", 2, NULL, -FLT_MAX, FLT_MAX,
00306                 "Offset", "Offset in floating point units, 1.0 is the width and height of the image.", -FLT_MAX, FLT_MAX);
00307 }
00308 
00309 /********************** view zoom operator *********************/
00310 
00311 typedef struct ViewZoomData {
00312         float x, y;
00313         float zoom;
00314         int event_type;
00315 } ViewZoomData;
00316 
00317 static void view_zoom_init(bContext *C, wmOperator *op, wmEvent *event)
00318 {
00319         SpaceImage *sima= CTX_wm_space_image(C);
00320         ViewZoomData *vpd;
00321 
00322         op->customdata= vpd= MEM_callocN(sizeof(ViewZoomData), "ImageViewZoomData");
00323         WM_cursor_modal(CTX_wm_window(C), BC_NSEW_SCROLLCURSOR);
00324 
00325         vpd->x= event->x;
00326         vpd->y= event->y;
00327         vpd->zoom= sima->zoom;
00328         vpd->event_type= event->type;
00329         
00330         WM_event_add_modal_handler(C, op);
00331 }
00332 
00333 static void view_zoom_exit(bContext *C, wmOperator *op, int cancel)
00334 {
00335         SpaceImage *sima= CTX_wm_space_image(C);
00336         ViewZoomData *vpd= op->customdata;
00337 
00338         if(cancel) {
00339                 sima->zoom= vpd->zoom;
00340                 ED_region_tag_redraw(CTX_wm_region(C));
00341         }
00342 
00343         WM_cursor_restore(CTX_wm_window(C));
00344         MEM_freeN(op->customdata);
00345 }
00346 
00347 static int view_zoom_exec(bContext *C, wmOperator *op)
00348 {
00349         SpaceImage *sima= CTX_wm_space_image(C);
00350         ARegion *ar= CTX_wm_region(C);
00351 
00352         sima_zoom_set_factor(sima, ar, RNA_float_get(op->ptr, "factor"));
00353 
00354         ED_region_tag_redraw(CTX_wm_region(C));
00355 
00356         /* XXX notifier? */
00357 #if 0
00358         if(image_preview_active(curarea, NULL, NULL)) {
00359                 /* recalculates new preview rect */
00360                 scrarea_do_windraw(curarea);
00361                 image_preview_event(2);
00362         }
00363 #endif
00364         
00365         return OPERATOR_FINISHED;
00366 }
00367 
00368 static int view_zoom_invoke(bContext *C, wmOperator *op, wmEvent *event)
00369 {
00370         if (event->type == MOUSEZOOM) {
00371                 SpaceImage *sima= CTX_wm_space_image(C);
00372                 ARegion *ar= CTX_wm_region(C);
00373                 float factor;
00374                 
00375                 factor= 1.0f + (event->x-event->prevx+event->y-event->prevy)/300.0f;
00376                 RNA_float_set(op->ptr, "factor", factor);
00377                 sima_zoom_set(sima, ar, sima->zoom*factor);
00378                 ED_region_tag_redraw(CTX_wm_region(C));
00379                 
00380                 return OPERATOR_FINISHED;
00381         }
00382         else {
00383                 view_zoom_init(C, op, event);
00384                 return OPERATOR_RUNNING_MODAL;
00385         }
00386 }
00387 
00388 static int view_zoom_modal(bContext *C, wmOperator *op, wmEvent *event)
00389 {
00390         SpaceImage *sima= CTX_wm_space_image(C);
00391         ARegion *ar= CTX_wm_region(C);
00392         ViewZoomData *vpd= op->customdata;
00393         float factor;
00394 
00395         switch(event->type) {
00396                 case MOUSEMOVE:
00397                         factor= 1.0f + (vpd->x-event->x+vpd->y-event->y)/300.0f;
00398                         RNA_float_set(op->ptr, "factor", factor);
00399                         sima_zoom_set(sima, ar, vpd->zoom*factor);
00400                         ED_region_tag_redraw(CTX_wm_region(C));
00401                         break;
00402                 default:
00403                         if(event->type==vpd->event_type && event->val==KM_RELEASE) {
00404                                 view_zoom_exit(C, op, 0);
00405                                 return OPERATOR_FINISHED;
00406                         }
00407                         break;
00408         }
00409 
00410         return OPERATOR_RUNNING_MODAL;
00411 }
00412 
00413 static int view_zoom_cancel(bContext *C, wmOperator *op)
00414 {
00415         view_zoom_exit(C, op, 1);
00416         return OPERATOR_CANCELLED;
00417 }
00418 
00419 void IMAGE_OT_view_zoom(wmOperatorType *ot)
00420 {
00421         /* identifiers */
00422         ot->name= "View Zoom";
00423         ot->idname= "IMAGE_OT_view_zoom";
00424         
00425         /* api callbacks */
00426         ot->exec= view_zoom_exec;
00427         ot->invoke= view_zoom_invoke;
00428         ot->modal= view_zoom_modal;
00429         ot->cancel= view_zoom_cancel;
00430         ot->poll= space_image_main_area_poll;
00431 
00432         /* flags */
00433         ot->flag= OPTYPE_BLOCKING;
00434         
00435         /* properties */
00436         RNA_def_float(ot->srna, "factor", 0.0f, 0.0f, FLT_MAX,
00437                 "Factor", "Zoom factor, values higher than 1.0 zoom in, lower values zoom out.", -FLT_MAX, FLT_MAX);
00438 }
00439 
00440 /********************** NDOF operator *********************/
00441 
00442 /* Combined pan/zoom from a 3D mouse device.
00443  * Z zooms, XY pans
00444  * "view" (not "paper") control -- user moves the viewpoint, not the image being viewed
00445  * that explains the negative signs in the code below
00446  */
00447 
00448 static int view_ndof_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *event)
00449 {
00450         if (event->type != NDOF_MOTION)
00451                 return OPERATOR_CANCELLED;
00452         else {
00453                 SpaceImage *sima= CTX_wm_space_image(C);
00454                 ARegion *ar= CTX_wm_region(C);
00455 
00456                 wmNDOFMotionData* ndof = (wmNDOFMotionData*) event->customdata;
00457 
00458                 float dt = ndof->dt;
00459                 /* tune these until it feels right */
00460                 const float zoom_sensitivity = 0.5f; // 50% per second (I think)
00461                 const float pan_sensitivity = 300.f; // screen pixels per second
00462 
00463                 float pan_x = pan_sensitivity * dt * ndof->tvec[0] / sima->zoom;
00464                 float pan_y = pan_sensitivity * dt * ndof->tvec[1] / sima->zoom;
00465 
00466                 /* "mouse zoom" factor = 1 + (dx + dy) / 300
00467                  * what about "ndof zoom" factor? should behave like this:
00468                  * at rest -> factor = 1
00469                  * move forward -> factor > 1
00470                  * move backward -> factor < 1
00471                  */
00472                 float zoom_factor = 1.f + zoom_sensitivity * dt * -ndof->tvec[2];
00473 
00474                 if (U.ndof_flag & NDOF_ZOOM_INVERT)
00475                         zoom_factor = -zoom_factor;
00476 
00477                 sima_zoom_set_factor(sima, ar, zoom_factor);
00478                 sima->xof += pan_x;
00479                 sima->yof += pan_y;
00480 
00481                 ED_region_tag_redraw(ar);       
00482 
00483                 return OPERATOR_FINISHED;
00484         }
00485 }
00486 
00487 void IMAGE_OT_view_ndof(wmOperatorType *ot)
00488 {
00489         /* identifiers */
00490         ot->name= "NDOF Pan/Zoom";
00491         ot->idname= "IMAGE_OT_view_ndof";
00492         
00493         /* api callbacks */
00494         ot->invoke= view_ndof_invoke;
00495 }
00496 
00497 /********************** view all operator *********************/
00498 
00499 /* Updates the fields of the View2D member of the SpaceImage struct.
00500  * Default behavior is to reset the position of the image and set the zoom to 1
00501  * If the image will not fit within the window rectangle, the zoom is adjusted */
00502 
00503 static int view_all_exec(bContext *C, wmOperator *UNUSED(op))
00504 {
00505         SpaceImage *sima;
00506         ARegion *ar;
00507         float aspx, aspy, zoomx, zoomy, w, h;
00508         int width, height;
00509 
00510         /* retrieve state */
00511         sima= CTX_wm_space_image(C);
00512         ar= CTX_wm_region(C);
00513 
00514         ED_space_image_size(sima, &width, &height);
00515         ED_space_image_aspect(sima, &aspx, &aspy);
00516 
00517         w= width*aspx;
00518         h= height*aspy;
00519         
00520         /* check if the image will fit in the image with zoom==1 */
00521         width = ar->winrct.xmax - ar->winrct.xmin + 1;
00522         height = ar->winrct.ymax - ar->winrct.ymin + 1;
00523 
00524         if((w >= width || h >= height) && (width > 0 && height > 0)) {
00525                 /* find the zoom value that will fit the image in the image space */
00526                 zoomx= width/w;
00527                 zoomy= height/h;
00528                 sima_zoom_set(sima, ar, 1.0f/power_of_2(1/MIN2(zoomx, zoomy)));
00529         }
00530         else
00531                 sima_zoom_set(sima, ar, 1.0f);
00532 
00533         sima->xof= sima->yof= 0.0f;
00534 
00535         ED_region_tag_redraw(CTX_wm_region(C));
00536         
00537         return OPERATOR_FINISHED;
00538 }
00539 
00540 void IMAGE_OT_view_all(wmOperatorType *ot)
00541 {
00542         /* identifiers */
00543         ot->name= "View All";
00544         ot->idname= "IMAGE_OT_view_all";
00545         
00546         /* api callbacks */
00547         ot->exec= view_all_exec;
00548         ot->poll= space_image_main_area_poll;
00549 }
00550 
00551 /********************** view selected operator *********************/
00552 
00553 static int view_selected_exec(bContext *C, wmOperator *UNUSED(op))
00554 {
00555         SpaceImage *sima;
00556         ARegion *ar;
00557         Scene *scene;
00558         Object *obedit;
00559         Image *ima;
00560         float size, min[2], max[2], d[2], aspx, aspy;
00561         int width, height;
00562 
00563         /* retrieve state */
00564         sima= CTX_wm_space_image(C);
00565         ar= CTX_wm_region(C);
00566         scene= (Scene*)CTX_data_scene(C);
00567         obedit= CTX_data_edit_object(C);
00568 
00569         ima= ED_space_image(sima);
00570         ED_space_image_size(sima, &width, &height);
00571         ED_image_aspect(ima, &aspx, &aspy);
00572 
00573         width= width*aspx;
00574         height= height*aspy;
00575 
00576         /* get bounds */
00577         if(!ED_uvedit_minmax(scene, ima, obedit, min, max))
00578                 return OPERATOR_CANCELLED;
00579 
00580         /* adjust offset and zoom */
00581         sima->xof= (int)(((min[0] + max[0])*0.5f - 0.5f)*width);
00582         sima->yof= (int)(((min[1] + max[1])*0.5f - 0.5f)*height);
00583 
00584         d[0]= max[0] - min[0];
00585         d[1]= max[1] - min[1];
00586         size= 0.5f*MAX2(d[0], d[1])*MAX2(width, height)/256.0f;
00587         
00588         if(size<=0.01f) size= 0.01f;
00589         sima_zoom_set(sima, ar, 0.7f/size);
00590 
00591         ED_region_tag_redraw(CTX_wm_region(C));
00592         
00593         return OPERATOR_FINISHED;
00594 }
00595 
00596 static int view_selected_poll(bContext *C)
00597 {
00598         return (space_image_main_area_poll(C) && ED_operator_uvedit(C));
00599 }
00600 
00601 void IMAGE_OT_view_selected(wmOperatorType *ot)
00602 {
00603         /* identifiers */
00604         ot->name= "View Center";
00605         ot->idname= "IMAGE_OT_view_selected";
00606         
00607         /* api callbacks */
00608         ot->exec= view_selected_exec;
00609         ot->poll= view_selected_poll;
00610 }
00611 
00612 /********************** view zoom in/out operator *********************/
00613 
00614 static int view_zoom_in_exec(bContext *C, wmOperator *UNUSED(op))
00615 {
00616         SpaceImage *sima= CTX_wm_space_image(C);
00617         ARegion *ar= CTX_wm_region(C);
00618 
00619         sima_zoom_set_factor(sima, ar, 1.25f);
00620 
00621         ED_region_tag_redraw(CTX_wm_region(C));
00622         
00623         return OPERATOR_FINISHED;
00624 }
00625 
00626 void IMAGE_OT_view_zoom_in(wmOperatorType *ot)
00627 {
00628         /* identifiers */
00629         ot->name= "View Zoom In";
00630         ot->idname= "IMAGE_OT_view_zoom_in";
00631         
00632         /* api callbacks */
00633         ot->exec= view_zoom_in_exec;
00634         ot->poll= space_image_main_area_poll;
00635 }
00636 
00637 static int view_zoom_out_exec(bContext *C, wmOperator *UNUSED(op))
00638 {
00639         SpaceImage *sima= CTX_wm_space_image(C);
00640         ARegion *ar= CTX_wm_region(C);
00641 
00642         sima_zoom_set_factor(sima, ar, 0.8f);
00643 
00644         ED_region_tag_redraw(CTX_wm_region(C));
00645         
00646         return OPERATOR_FINISHED;
00647 }
00648 
00649 void IMAGE_OT_view_zoom_out(wmOperatorType *ot)
00650 {
00651         /* identifiers */
00652         ot->name= "View Zoom Out";
00653         ot->idname= "IMAGE_OT_view_zoom_out";
00654         
00655         /* api callbacks */
00656         ot->exec= view_zoom_out_exec;
00657         ot->poll= space_image_main_area_poll;
00658 }
00659 
00660 /********************** view zoom ratio operator *********************/
00661 
00662 static int view_zoom_ratio_exec(bContext *C, wmOperator *op)
00663 {
00664         SpaceImage *sima= CTX_wm_space_image(C);
00665         ARegion *ar= CTX_wm_region(C);
00666 
00667         sima_zoom_set(sima, ar, RNA_float_get(op->ptr, "ratio"));
00668         
00669         /* ensure pixel exact locations for draw */
00670         sima->xof= (int)sima->xof;
00671         sima->yof= (int)sima->yof;
00672 
00673         /* XXX notifier? */
00674 #if 0
00675         if(image_preview_active(curarea, NULL, NULL)) {
00676                 /* recalculates new preview rect */
00677                 scrarea_do_windraw(curarea);
00678                 image_preview_event(2);
00679         }
00680 #endif
00681 
00682         ED_region_tag_redraw(CTX_wm_region(C));
00683         
00684         return OPERATOR_FINISHED;
00685 }
00686 
00687 void IMAGE_OT_view_zoom_ratio(wmOperatorType *ot)
00688 {
00689         /* identifiers */
00690         ot->name= "View Zoom Ratio";
00691         ot->idname= "IMAGE_OT_view_zoom_ratio";
00692         
00693         /* api callbacks */
00694         ot->exec= view_zoom_ratio_exec;
00695         ot->poll= space_image_main_area_poll;
00696         
00697         /* properties */
00698         RNA_def_float(ot->srna, "ratio", 0.0f, 0.0f, FLT_MAX,
00699                 "Ratio", "Zoom ratio, 1.0 is 1:1, higher is zoomed in, lower is zoomed out.", -FLT_MAX, FLT_MAX);
00700 }
00701 
00702 /**************** load/replace/save callbacks ******************/
00703 
00704 /* XXX make dynamic */
00705 static const EnumPropertyItem image_file_type_items[] = {
00706                 {R_TARGA, "TARGA", 0, "Targa", ""},
00707                 {R_RAWTGA, "TARGA RAW", 0, "Targa Raw", ""},
00708                 {R_PNG, "PNG", 0, "PNG", ""},
00709 #ifdef WITH_DDS
00710                 {R_DDS, "DDS", 0, "DirectDraw Surface", ""},
00711 #endif
00712                 {R_BMP, "BMP", 0, "BMP", ""},
00713                 {R_JPEG90, "JPEG", 0, "Jpeg", ""},
00714 #ifdef WITH_OPENJPEG
00715                 {R_JP2, "JPEG_2000", 0, "Jpeg 2000", ""},
00716 #endif
00717                 {R_IRIS, "IRIS", 0, "Iris", ""},
00718 #ifdef WITH_TIFF
00719                 {R_TIFF, "TIFF", 0, "Tiff", ""},
00720 #endif
00721 #ifdef WITH_DDS
00722                 {R_RADHDR, "RADIANCE_HDR", 0, "Radiance HDR", ""},
00723 #endif
00724 #ifdef WITH_CINEON
00725                 {R_CINEON, "CINEON", 0, "Cineon", ""},
00726                 {R_DPX, "DPX", 0, "DPX", ""},
00727 #endif
00728 #ifdef WITH_OPENEXR
00729                 {R_OPENEXR, "OPENEXR", 0, "OpenEXR", ""},
00730         /* saving sequences of multilayer won't work, they copy buffers  */
00731         /*if(ima->source==IMA_SRC_SEQUENCE && ima->type==IMA_TYPE_MULTILAYER);
00732         else*/
00733                 {R_MULTILAYER, "MULTILAYER", 0, "MultiLayer", ""},
00734 #endif  
00735                 {0, NULL, 0, NULL, NULL}};
00736 
00737 static void image_filesel(bContext *C, wmOperator *op, const char *path)
00738 {
00739         RNA_string_set(op->ptr, "filepath", path);
00740         WM_event_add_fileselect(C, op); 
00741 }
00742 
00743 /******************** open image operator ********************/
00744 
00745 static void open_init(bContext *C, wmOperator *op)
00746 {
00747         PropertyPointerRNA *pprop;
00748 
00749         op->customdata= pprop= MEM_callocN(sizeof(PropertyPointerRNA), "OpenPropertyPointerRNA");
00750         uiIDContextProperty(C, &pprop->ptr, &pprop->prop);
00751 }
00752 
00753 static int open_cancel(bContext *UNUSED(C), wmOperator *op)
00754 {
00755         MEM_freeN(op->customdata);
00756         op->customdata= NULL;
00757         return OPERATOR_CANCELLED;
00758 }
00759 
00760 static int open_exec(bContext *C, wmOperator *op)
00761 {
00762         SpaceImage *sima= CTX_wm_space_image(C); /* XXX other space types can call */
00763         Scene *scene= CTX_data_scene(C);
00764         Object *obedit= CTX_data_edit_object(C);
00765         ImageUser *iuser= NULL;
00766         PropertyPointerRNA *pprop;
00767         PointerRNA idptr;
00768         Image *ima= NULL;
00769         char str[FILE_MAX];
00770 
00771         RNA_string_get(op->ptr, "filepath", str);
00772         /* default to frame 1 if there's no scene in context */
00773 
00774         errno= 0;
00775 
00776         ima= BKE_add_image_file(str);
00777 
00778         if(!ima) {
00779                 if(op->customdata) MEM_freeN(op->customdata);
00780                 BKE_reportf(op->reports, RPT_ERROR, "Can't read: \"%s\", %s.", str, errno ? strerror(errno) : "Unsupported image format");
00781                 return OPERATOR_CANCELLED;
00782         }
00783         
00784         if(!op->customdata)
00785                 open_init(C, op);
00786 
00787         /* hook into UI */
00788         pprop= op->customdata;
00789 
00790         if(pprop->prop) {
00791                 /* when creating new ID blocks, use is already 1, but RNA
00792                  * pointer se also increases user, so this compensates it */
00793                 ima->id.us--;
00794 
00795                 RNA_id_pointer_create(&ima->id, &idptr);
00796                 RNA_property_pointer_set(&pprop->ptr, pprop->prop, idptr);
00797                 RNA_property_update(C, &pprop->ptr, pprop->prop);
00798         }
00799         else if(sima) {
00800                 ED_space_image_set(C, sima, scene, obedit, ima);
00801                 iuser= &sima->iuser;
00802         }
00803         else {
00804                 Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
00805                 if(tex && tex->type==TEX_IMAGE)
00806                         iuser= &tex->iuser;
00807                 
00808         }
00809         
00810         /* initialize because of new image */
00811         if(iuser) {
00812                 iuser->sfra= 1;
00813                 iuser->offset= 0;
00814                 iuser->fie_ima= 2;
00815         }
00816 
00817         /* XXX unpackImage frees image buffers */
00818         ED_preview_kill_jobs(C);
00819         
00820         BKE_image_signal(ima, iuser, IMA_SIGNAL_RELOAD);
00821         WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, ima);
00822         
00823         MEM_freeN(op->customdata);
00824 
00825         return OPERATOR_FINISHED;
00826 }
00827 
00828 static int open_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
00829 {
00830         SpaceImage *sima= CTX_wm_space_image(C); /* XXX other space types can call */
00831         char *path=U.textudir;
00832         Image *ima= NULL;
00833 
00834         if(sima) {
00835                 ima= sima->image;
00836         }
00837 
00838         if (ima==NULL) {
00839                 Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
00840                 if(tex && tex->type==TEX_IMAGE)
00841                         ima= tex->ima;
00842         }
00843 
00844         if(ima)
00845                 path= ima->name;
00846         
00847 
00848         if(!RNA_property_is_set(op->ptr, "relative_path"))
00849                 RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS);
00850 
00851         if(RNA_property_is_set(op->ptr, "filepath"))
00852                 return open_exec(C, op);
00853         
00854         open_init(C, op);
00855 
00856         image_filesel(C, op, path);
00857 
00858         return OPERATOR_RUNNING_MODAL;
00859 }
00860 
00861 /* called by other space types too */
00862 void IMAGE_OT_open(wmOperatorType *ot)
00863 {
00864         /* identifiers */
00865         ot->name= "Open Image";
00866         ot->description= "Open image";
00867         ot->idname= "IMAGE_OT_open";
00868         
00869         /* api callbacks */
00870         ot->exec= open_exec;
00871         ot->invoke= open_invoke;
00872         ot->cancel= open_cancel;
00873 
00874         /* flags */
00875         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
00876 
00877         /* properties */
00878         WM_operator_properties_filesel(ot, FOLDERFILE|IMAGEFILE|MOVIEFILE, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_FILEPATH|WM_FILESEL_RELPATH);
00879 }
00880 
00881 /******************** replace image operator ********************/
00882 
00883 static int replace_exec(bContext *C, wmOperator *op)
00884 {
00885         SpaceImage *sima= CTX_wm_space_image(C);
00886         char str[FILE_MAX];
00887 
00888         if(!sima->image)
00889                 return OPERATOR_CANCELLED;
00890         
00891         RNA_string_get(op->ptr, "filepath", str);
00892         BLI_strncpy(sima->image->name, str, sizeof(sima->image->name)); /* we cant do much if the str is longer then 240 :/ */
00893 
00894         /* XXX unpackImage frees image buffers */
00895         ED_preview_kill_jobs(C);
00896         
00897         BKE_image_signal(sima->image, &sima->iuser, IMA_SIGNAL_RELOAD);
00898         WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, sima->image);
00899 
00900         return OPERATOR_FINISHED;
00901 }
00902 
00903 static int replace_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
00904 {
00905         SpaceImage *sima= CTX_wm_space_image(C);
00906 
00907         if(!sima->image)
00908                 return OPERATOR_CANCELLED;
00909 
00910         if(RNA_property_is_set(op->ptr, "filepath"))
00911                 return replace_exec(C, op);
00912 
00913         if(!RNA_property_is_set(op->ptr, "relative_path"))
00914                 RNA_boolean_set(op->ptr, "relative_path", (strncmp(sima->image->name, "//", 2))==0);
00915 
00916         image_filesel(C, op, sima->image->name);
00917 
00918         return OPERATOR_RUNNING_MODAL;
00919 }
00920 
00921 void IMAGE_OT_replace(wmOperatorType *ot)
00922 {
00923         /* identifiers */
00924         ot->name= "Replace Image";
00925         ot->idname= "IMAGE_OT_replace";
00926         
00927         /* api callbacks */
00928         ot->exec= replace_exec;
00929         ot->invoke= replace_invoke;
00930         ot->poll= space_image_poll;
00931 
00932         /* flags */
00933         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
00934 
00935         /* properties */
00936         WM_operator_properties_filesel(ot, FOLDERFILE|IMAGEFILE|MOVIEFILE, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_FILEPATH|WM_FILESEL_RELPATH);
00937 }
00938 
00939 /******************** save image as operator ********************/
00940 
00941 /* assumes name is FILE_MAX */
00942 /* ima->name and ibuf->name should end up the same */
00943 static void save_image_doit(bContext *C, SpaceImage *sima, Scene *scene, wmOperator *op, char *path, int do_newpath)
00944 {
00945         Image *ima= ED_space_image(sima);
00946         void *lock;
00947         ImBuf *ibuf= ED_space_image_acquire_buffer(sima, &lock);
00948 
00949         if (ibuf) {
00950                 Main *bmain= CTX_data_main(C);
00951                 const short relative= (RNA_struct_find_property(op->ptr, "relative_path") && RNA_boolean_get(op->ptr, "relative_path"));
00952                 const short save_copy= (RNA_struct_find_property(op->ptr, "copy") && RNA_boolean_get(op->ptr, "copy"));
00953                 short ok= FALSE;
00954 
00955                 BLI_path_abs(path, bmain->name);
00956                 /* old global to ensure a 2nd save goes to same dir */
00957                 BLI_strncpy(G.ima, path, sizeof(G.ima));
00958 
00959                 WM_cursor_wait(1);
00960 
00961                 if(ima->type == IMA_TYPE_R_RESULT) {
00962                         /* enforce user setting for RGB or RGBA, but skip BW */
00963                         if(scene->r.planes==32) {
00964                                 ibuf->depth= 32;
00965                         }
00966                         else if(scene->r.planes==24) {
00967                                 ibuf->depth= 24;
00968                         }
00969                 }
00970                 else {
00971                         /* TODO, better solution, if a 24bit image is painted onto it may contain alpha */
00972                         if(ibuf->userflags & IB_BITMAPDIRTY) { /* it has been painted onto */
00973                                 /* checks each pixel, not ideal */
00974                                 ibuf->depth= BKE_alphatest_ibuf(ibuf) ? 32 : 24;
00975                         }
00976                 }
00977 
00978                 if(scene->r.scemode & R_EXTENSION)  {
00979                         BKE_add_image_extension(path, sima->imtypenr);
00980                 }
00981                 
00982                 if(sima->imtypenr==R_MULTILAYER) {
00983                         RenderResult *rr= BKE_image_acquire_renderresult(scene, ima);
00984                         if(rr) {
00985                                 RE_WriteRenderResult(rr, path, scene->r.quality);
00986                                 ok= TRUE;
00987                         }
00988                         else {
00989                                 BKE_report(op->reports, RPT_ERROR, "Did not write, no Multilayer Image");
00990                         }
00991                         BKE_image_release_renderresult(scene, ima);
00992                 }
00993                 else if (BKE_write_ibuf(ibuf, path, sima->imtypenr, scene->r.subimtype, scene->r.quality)) {
00994                         ok= TRUE;
00995                 }
00996 
00997                 if(ok)  {
00998                         if(relative)
00999                                 BLI_path_rel(path, bmain->name); /* only after saving */
01000 
01001                         if(ibuf->name[0]==0) {
01002                                 BLI_strncpy(ibuf->name, path, sizeof(ibuf->name));
01003                                 BLI_strncpy(ima->name, path, sizeof(ima->name));
01004                         }
01005 
01006                         if(!save_copy) {
01007                                 if(do_newpath) {
01008                                         BLI_strncpy(ima->name, path, sizeof(ima->name));
01009                                         BLI_strncpy(ibuf->name, path, sizeof(ibuf->name));
01010                                 }
01011 
01012                                 ibuf->userflags &= ~IB_BITMAPDIRTY;
01013 
01014                                 /* change type? */
01015                                 if(ima->type==IMA_TYPE_R_RESULT) {
01016                                         ima->type= IMA_TYPE_IMAGE;
01017 
01018                                         /* workaround to ensure the render result buffer is no longer used
01019                                          * by this image, otherwise can crash when a new render result is
01020                                          * created. */
01021                                         if(ibuf->rect && !(ibuf->mall & IB_rect))
01022                                                 imb_freerectImBuf(ibuf);
01023                                         if(ibuf->rect_float && !(ibuf->mall & IB_rectfloat))
01024                                                 imb_freerectfloatImBuf(ibuf);
01025                                         if(ibuf->zbuf && !(ibuf->mall & IB_zbuf))
01026                                                 IMB_freezbufImBuf(ibuf);
01027                                         if(ibuf->zbuf_float && !(ibuf->mall & IB_zbuffloat))
01028                                                 IMB_freezbuffloatImBuf(ibuf);
01029                                 }
01030                                 if( ELEM(ima->source, IMA_SRC_GENERATED, IMA_SRC_VIEWER)) {
01031                                         ima->source= IMA_SRC_FILE;
01032                                         ima->type= IMA_TYPE_IMAGE;
01033                                 }
01034                         }
01035                 }
01036                 else {
01037                         BKE_reportf(op->reports, RPT_ERROR, "Couldn't write image: %s", path);
01038                 }
01039 
01040 
01041                 WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, sima->image);
01042 
01043                 WM_cursor_wait(0);
01044         }
01045 
01046         ED_space_image_release_buffer(sima, lock);
01047 }
01048 
01049 static int save_as_exec(bContext *C, wmOperator *op)
01050 {
01051         SpaceImage *sima= CTX_wm_space_image(C);
01052         Scene *scene= CTX_data_scene(C);
01053         Image *ima = ED_space_image(sima);
01054         char str[FILE_MAX];
01055 
01056         if(!ima)
01057                 return OPERATOR_CANCELLED;
01058 
01059         sima->imtypenr= RNA_enum_get(op->ptr, "file_type");
01060         RNA_string_get(op->ptr, "filepath", str);
01061 
01062         save_image_doit(C, sima, scene, op, str, TRUE);
01063 
01064         return OPERATOR_FINISHED;
01065 }
01066 
01067 
01068 static int save_as_check(bContext *UNUSED(C), wmOperator *op)
01069 {
01070         char filepath[FILE_MAX];
01071         RNA_string_get(op->ptr, "filepath", filepath);
01072         if(BKE_add_image_extension(filepath, RNA_enum_get(op->ptr, "file_type"))) {
01073                 RNA_string_set(op->ptr, "filepath", filepath);
01074                 return TRUE;
01075         }
01076         return FALSE;
01077 }
01078 
01079 static int save_as_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
01080 {
01081         SpaceImage *sima= CTX_wm_space_image(C);
01082         Image *ima = ED_space_image(sima);
01083         Scene *scene= CTX_data_scene(C);
01084         ImBuf *ibuf;
01085         char filename[FILE_MAX];
01086         
01087         void *lock;
01088 
01089         if(!RNA_property_is_set(op->ptr, "relative_path"))
01090                 RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS);
01091 
01092         if(RNA_property_is_set(op->ptr, "filepath"))
01093                 return save_as_exec(C, op);
01094         
01095         if(!ima)
01096                 return OPERATOR_CANCELLED;
01097 
01098         /* always opens fileselect */
01099         ibuf= ED_space_image_acquire_buffer(sima, &lock);
01100 
01101         if(ibuf) {
01102                 /* cant save multilayer sequence, ima->rr isn't valid for a specific frame */
01103                 if(ima->rr && !(ima->source==IMA_SRC_SEQUENCE && ima->type==IMA_TYPE_MULTILAYER))
01104                         sima->imtypenr= R_MULTILAYER;
01105                 else if(ima->type==IMA_TYPE_R_RESULT)
01106                         sima->imtypenr= scene->r.imtype;
01107                 else if (ima->source == IMA_SRC_GENERATED)
01108                         sima->imtypenr= R_PNG;
01109                 else
01110                         sima->imtypenr= BKE_ftype_to_imtype(ibuf->ftype);
01111 
01112                 RNA_enum_set(op->ptr, "file_type", sima->imtypenr);
01113                 
01114                 if(ibuf->name[0]==0)
01115                         if ( (G.ima[0] == '/') && (G.ima[1] == '/') && (G.ima[2] == '\0') ) {
01116                                 BLI_strncpy(filename, "//untitled", FILE_MAX);
01117                         } else {
01118                                 BLI_strncpy(filename, G.ima, FILE_MAX);
01119                         }
01120                 else
01121                         BLI_strncpy(filename, ibuf->name, FILE_MAX);
01122                 
01123                 /* enable save_copy by default for render results */
01124                 if(ELEM(ima->type, IMA_TYPE_R_RESULT, IMA_TYPE_COMPOSITE) && !RNA_property_is_set(op->ptr, "copy")) {
01125                         RNA_boolean_set(op->ptr, "copy", TRUE);
01126                 }
01127 
01128                 // XXX note: we can give default menu enums to operator for this 
01129                 image_filesel(C, op, filename);
01130 
01131                 ED_space_image_release_buffer(sima, lock);
01132                 
01133                 return OPERATOR_RUNNING_MODAL;
01134         }
01135 
01136         ED_space_image_release_buffer(sima, lock);
01137 
01138         return OPERATOR_CANCELLED;
01139 }
01140 
01141 void IMAGE_OT_save_as(wmOperatorType *ot)
01142 {
01143         /* identifiers */
01144         ot->name= "Save As Image";
01145         ot->idname= "IMAGE_OT_save_as";
01146         
01147         /* api callbacks */
01148         ot->exec= save_as_exec;
01149         ot->check= save_as_check;
01150         ot->invoke= save_as_invoke;
01151         ot->poll= space_image_buffer_exists_poll;
01152 
01153         /* flags */
01154         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
01155 
01156         /* properties */
01157         RNA_def_enum(ot->srna, "file_type", image_file_type_items, R_PNG, "File Type", "File type to save image as.");
01158         WM_operator_properties_filesel(ot, FOLDERFILE|IMAGEFILE|MOVIEFILE, FILE_SPECIAL, FILE_SAVE, WM_FILESEL_FILEPATH|WM_FILESEL_RELPATH);
01159 
01160         RNA_def_boolean(ot->srna, "copy", 0, "Copy", "Create a new image file without modifying the current image in blender");
01161 }
01162 
01163 /******************** save image operator ********************/
01164 
01165 static int save_exec(bContext *C, wmOperator *op)
01166 {
01167         Main *bmain= CTX_data_main(C);
01168         SpaceImage *sima= CTX_wm_space_image(C);
01169         Image *ima = ED_space_image(sima);
01170         void *lock;
01171         ImBuf *ibuf= ED_space_image_acquire_buffer(sima, &lock);
01172         Scene *scene= CTX_data_scene(C);
01173         RenderResult *rr;
01174         char name[FILE_MAX];
01175 
01176         if(!ima || !ibuf) {
01177                 ED_space_image_release_buffer(sima, lock);
01178                 return OPERATOR_CANCELLED;
01179         }
01180 
01181         /* if exists, saves over without fileselect */
01182         
01183         BLI_strncpy(name, ima->name, FILE_MAX);
01184         if(name[0]==0)
01185                 BLI_strncpy(name, G.ima, FILE_MAX);
01186         else
01187                 BLI_path_abs(name, bmain->name);
01188         
01189         if(BLI_exists(name) && BLI_is_writable(name)) {
01190                 rr= BKE_image_acquire_renderresult(scene, ima);
01191 
01192                 if(rr)
01193                         sima->imtypenr= R_MULTILAYER;
01194                 else 
01195                         sima->imtypenr= BKE_ftype_to_imtype(ibuf->ftype);
01196 
01197                 BKE_image_release_renderresult(scene, ima);
01198                 ED_space_image_release_buffer(sima, lock);
01199                 
01200                 save_image_doit(C, sima, scene, op, name, FALSE);
01201         }
01202         else {
01203                 ED_space_image_release_buffer(sima, lock);
01204 
01205                 BKE_report(op->reports, RPT_ERROR, "Can not save image.");
01206                 return OPERATOR_CANCELLED;
01207         }
01208 
01209         return OPERATOR_FINISHED;
01210 }
01211 
01212 void IMAGE_OT_save(wmOperatorType *ot)
01213 {
01214         /* identifiers */
01215         ot->name= "Save Image";
01216         ot->idname= "IMAGE_OT_save";
01217         
01218         /* api callbacks */
01219         ot->exec= save_exec;
01220         ot->poll= space_image_file_exists_poll;
01221 
01222         /* flags */
01223         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
01224 }
01225 
01226 /******************* save sequence operator ********************/
01227 
01228 static int save_sequence_exec(bContext *C, wmOperator *op)
01229 {
01230         Main *bmain= CTX_data_main(C);
01231         SpaceImage *sima= CTX_wm_space_image(C);
01232         ImBuf *ibuf;
01233         int tot= 0;
01234         char di[FILE_MAX], fi[FILE_MAX];
01235         
01236         if(sima->image==NULL)
01237                 return OPERATOR_CANCELLED;
01238 
01239         if(sima->image->source!=IMA_SRC_SEQUENCE) {
01240                 BKE_report(op->reports, RPT_ERROR, "Can only save sequence on image sequences.");
01241                 return OPERATOR_CANCELLED;
01242         }
01243 
01244         if(sima->image->type==IMA_TYPE_MULTILAYER) {
01245                 BKE_report(op->reports, RPT_ERROR, "Can't save multilayer sequences.");
01246                 return OPERATOR_CANCELLED;
01247         }
01248         
01249         /* get total */
01250         for(ibuf= sima->image->ibufs.first; ibuf; ibuf= ibuf->next) 
01251                 if(ibuf->userflags & IB_BITMAPDIRTY)
01252                         tot++;
01253         
01254         if(tot==0) {
01255                 BKE_report(op->reports, RPT_WARNING, "No images have been changed.");
01256                 return OPERATOR_CANCELLED;
01257         }
01258 
01259         /* get a filename for menu */
01260         for(ibuf= sima->image->ibufs.first; ibuf; ibuf= ibuf->next) 
01261                 if(ibuf->userflags & IB_BITMAPDIRTY)
01262                         break;
01263         
01264         BLI_strncpy(di, ibuf->name, FILE_MAX);
01265         BLI_splitdirstring(di, fi);
01266         
01267         BKE_reportf(op->reports, RPT_INFO, "%d Image(s) will be saved in %s", tot, di);
01268 
01269         for(ibuf= sima->image->ibufs.first; ibuf; ibuf= ibuf->next) {
01270                 if(ibuf->userflags & IB_BITMAPDIRTY) {
01271                         char name[FILE_MAX];
01272                         BLI_strncpy(name, ibuf->name, sizeof(name));
01273                         
01274                         BLI_path_abs(name, bmain->name);
01275 
01276                         if(0 == IMB_saveiff(ibuf, name, IB_rect | IB_zbuf | IB_zbuffloat)) {
01277                                 BKE_reportf(op->reports, RPT_ERROR, "Could not write image %s.", name);
01278                                 break;
01279                         }
01280 
01281                         BKE_reportf(op->reports, RPT_INFO, "Saved: %s\n", ibuf->name);
01282                         ibuf->userflags &= ~IB_BITMAPDIRTY;
01283                 }
01284         }
01285 
01286         return OPERATOR_FINISHED;
01287 }
01288 
01289 void IMAGE_OT_save_sequence(wmOperatorType *ot)
01290 {
01291         /* identifiers */
01292         ot->name= "Save Sequence";
01293         ot->idname= "IMAGE_OT_save_sequence";
01294         
01295         /* api callbacks */
01296         ot->exec= save_sequence_exec;
01297         ot->poll= space_image_buffer_exists_poll;
01298 
01299         /* flags */
01300         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
01301 }
01302 
01303 /******************** reload image operator ********************/
01304 
01305 static int reload_exec(bContext *C, wmOperator *UNUSED(op))
01306 {
01307         Image *ima= CTX_data_edit_image(C);
01308         SpaceImage *sima= CTX_wm_space_image(C);
01309 
01310         if(!ima)
01311                 return OPERATOR_CANCELLED;
01312 
01313         /* XXX unpackImage frees image buffers */
01314         ED_preview_kill_jobs(C);
01315         
01316         // XXX other users?
01317         BKE_image_signal(ima, (sima)? &sima->iuser: NULL, IMA_SIGNAL_RELOAD);
01318 
01319         WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, ima);
01320         
01321         return OPERATOR_FINISHED;
01322 }
01323 
01324 void IMAGE_OT_reload(wmOperatorType *ot)
01325 {
01326         /* identifiers */
01327         ot->name= "Reload Image";
01328         ot->idname= "IMAGE_OT_reload";
01329         
01330         /* api callbacks */
01331         ot->exec= reload_exec;
01332 
01333         /* flags */
01334         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
01335 }
01336 
01337 /********************** new image operator *********************/
01338 
01339 static int image_new_exec(bContext *C, wmOperator *op)
01340 {
01341         SpaceImage *sima;
01342         Scene *scene;
01343         Object *obedit;
01344         Image *ima;
01345         PointerRNA ptr, idptr;
01346         PropertyRNA *prop;
01347         char name[MAX_ID_NAME-2];
01348         float color[4];
01349         int width, height, floatbuf, uvtestgrid, alpha;
01350 
01351         /* retrieve state */
01352         sima= CTX_wm_space_image(C);
01353         scene= (Scene*)CTX_data_scene(C);
01354         obedit= CTX_data_edit_object(C);
01355 
01356         RNA_string_get(op->ptr, "name", name);
01357         width= RNA_int_get(op->ptr, "width");
01358         height= RNA_int_get(op->ptr, "height");
01359         floatbuf= RNA_boolean_get(op->ptr, "float");
01360         uvtestgrid= RNA_boolean_get(op->ptr, "uv_test_grid");
01361         RNA_float_get_array(op->ptr, "color", color);
01362         alpha= RNA_boolean_get(op->ptr, "alpha");
01363         
01364         if (!floatbuf && scene->r.color_mgt_flag & R_COLOR_MANAGEMENT)
01365                 linearrgb_to_srgb_v3_v3(color, color);
01366 
01367         if(!alpha)
01368                 color[3]= 1.0f;
01369 
01370         ima = BKE_add_image_size(width, height, name, alpha ? 32 : 24, floatbuf, uvtestgrid, color);
01371 
01372         if(!ima)
01373                 return OPERATOR_CANCELLED;
01374 
01375         /* hook into UI */
01376         uiIDContextProperty(C, &ptr, &prop);
01377 
01378         if(prop) {
01379                 /* when creating new ID blocks, use is already 1, but RNA
01380                  * pointer se also increases user, so this compensates it */
01381                 ima->id.us--;
01382 
01383                 RNA_id_pointer_create(&ima->id, &idptr);
01384                 RNA_property_pointer_set(&ptr, prop, idptr);
01385                 RNA_property_update(C, &ptr, prop);
01386         }
01387         else if(sima)
01388                 ED_space_image_set(C, sima, scene, obedit, ima);
01389 
01390         // XXX other users?
01391         BKE_image_signal(ima, (sima)? &sima->iuser: NULL, IMA_SIGNAL_USER_NEW_IMAGE);
01392         
01393         return OPERATOR_FINISHED;
01394 }
01395 
01396 /* XXX, Ton is not a fan of OK buttons but using this function to avoid undo/redo bug while in mesh-editmode, - campbell */
01397 static int image_new_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
01398 {
01399         return WM_operator_props_dialog_popup(C, op, 300, 100);
01400 
01401 }
01402 
01403 void IMAGE_OT_new(wmOperatorType *ot)
01404 {
01405         PropertyRNA *prop;
01406         static float default_color[4]= {0.0f, 0.0f, 0.0f, 1.0f};
01407         
01408         /* identifiers */
01409         ot->name= "New Image";
01410         ot->description= "Create a new image";
01411         ot->idname= "IMAGE_OT_new";
01412         
01413         /* api callbacks */
01414         ot->exec= image_new_exec;
01415         ot->invoke= image_new_invoke;
01416         
01417         /* flags */
01418         ot->flag= OPTYPE_UNDO;
01419 
01420         /* properties */
01421         RNA_def_string(ot->srna, "name", "untitled", MAX_ID_NAME-2, "Name", "Image datablock name.");
01422         RNA_def_int(ot->srna, "width", 1024, 1, INT_MAX, "Width", "Image width.", 1, 16384);
01423         RNA_def_int(ot->srna, "height", 1024, 1, INT_MAX, "Height", "Image height.", 1, 16384);
01424         prop= RNA_def_float_color(ot->srna, "color", 4, NULL, 0.0f, FLT_MAX, "Color", "Default fill color.", 0.0f, 1.0f);
01425         RNA_def_property_float_array_default(prop, default_color);
01426         RNA_def_boolean(ot->srna, "alpha", 1, "Alpha", "Create an image with an alpha channel.");
01427         RNA_def_boolean(ot->srna, "uv_test_grid", 0, "UV Test Grid", "Fill the image with a grid for UV map testing.");
01428         RNA_def_boolean(ot->srna, "float", 0, "32 bit Float", "Create image with 32 bit floating point bit depth.");
01429 }
01430 
01431 /********************* invert operators *********************/
01432 
01433 static int image_invert_poll(bContext *C)
01434 {
01435         Image *ima= CTX_data_edit_image(C);
01436         ImBuf *ibuf= BKE_image_get_ibuf(ima, NULL);
01437         
01438         if( ibuf != NULL )
01439                 return 1;
01440         return 0;
01441 }
01442 
01443 static int image_invert_exec(bContext *C, wmOperator *op)
01444 {
01445         Image *ima= CTX_data_edit_image(C);
01446         ImBuf *ibuf= BKE_image_get_ibuf(ima, NULL);
01447 
01448         // flags indicate if this channel should be inverted
01449         const short r= RNA_boolean_get(op->ptr, "invert_r");
01450         const short g= RNA_boolean_get(op->ptr, "invert_g");
01451         const short b= RNA_boolean_get(op->ptr, "invert_b");
01452         const short a= RNA_boolean_get(op->ptr, "invert_a");
01453 
01454         int i;
01455 
01456         if( ibuf == NULL) // TODO: this should actually never happen, but does for render-results -> cleanup
01457                 return OPERATOR_CANCELLED;
01458 
01459         /* TODO: make this into an IMB_invert_channels(ibuf,r,g,b,a) method!? */
01460         if (ibuf->rect_float) {
01461                 
01462                 float *fp = (float *) ibuf->rect_float;
01463                 for( i = ibuf->x * ibuf->y; i > 0; i--, fp+=4 ) {
01464                         if( r ) fp[0] = 1.0f - fp[0];
01465                         if( g ) fp[1] = 1.0f - fp[1];
01466                         if( b ) fp[2] = 1.0f - fp[2];
01467                         if( a ) fp[3] = 1.0f - fp[3];
01468                 }
01469 
01470                 if(ibuf->rect) {
01471                         IMB_rect_from_float(ibuf);
01472                 }
01473         }
01474         else if(ibuf->rect) {
01475                 
01476                 char *cp = (char *) ibuf->rect;
01477                 for( i = ibuf->x * ibuf->y; i > 0; i--, cp+=4 ) {
01478                         if( r ) cp[0] = 255 - cp[0];
01479                         if( g ) cp[1] = 255 - cp[1];
01480                         if( b ) cp[2] = 255 - cp[2];
01481                         if( a ) cp[3] = 255 - cp[3];
01482                 }
01483         }
01484         else {
01485                 return OPERATOR_CANCELLED;
01486         }
01487 
01488         ibuf->userflags |= IB_BITMAPDIRTY;
01489         if(ibuf->mipmap[0])
01490                 ibuf->userflags |= IB_MIPMAP_INVALID;
01491 
01492         WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, ima);
01493         return OPERATOR_FINISHED;
01494 }
01495 
01496 void IMAGE_OT_invert(wmOperatorType *ot)
01497 {
01498         /* identifiers */
01499         ot->name= "Invert Channels";
01500         ot->idname= "IMAGE_OT_invert";
01501         
01502         /* api callbacks */
01503         ot->exec= image_invert_exec;
01504         ot->poll= image_invert_poll;
01505         
01506         /* properties */
01507         RNA_def_boolean(ot->srna, "invert_r", 0, "Red", "Invert Red Channel");
01508         RNA_def_boolean(ot->srna, "invert_g", 0, "Green", "Invert Green Channel");
01509         RNA_def_boolean(ot->srna, "invert_b", 0, "Blue", "Invert Blue Channel");
01510         RNA_def_boolean(ot->srna, "invert_a", 0, "Alpha", "Invert Alpha Channel");
01511         
01512         /* flags */
01513         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
01514 }
01515 
01516 /********************* pack operator *********************/
01517 
01518 static int pack_test(bContext *C, wmOperator *op)
01519 {
01520         Image *ima= CTX_data_edit_image(C);
01521         int as_png= RNA_boolean_get(op->ptr, "as_png");
01522 
01523         if(!ima)
01524                 return 0;
01525         if(!as_png && ima->packedfile)
01526                 return 0;
01527 
01528         if(ima->source==IMA_SRC_SEQUENCE || ima->source==IMA_SRC_MOVIE) {
01529                 BKE_report(op->reports, RPT_ERROR, "Packing movies or image sequences not supported.");
01530                 return 0;
01531         }
01532 
01533         return 1;
01534 }
01535 
01536 static int pack_exec(bContext *C, wmOperator *op)
01537 {
01538         Image *ima= CTX_data_edit_image(C);
01539         ImBuf *ibuf= BKE_image_get_ibuf(ima, NULL);
01540         int as_png= RNA_boolean_get(op->ptr, "as_png");
01541 
01542         if(!pack_test(C, op))
01543                 return OPERATOR_CANCELLED;
01544         
01545         if(!as_png && (ibuf && (ibuf->userflags & IB_BITMAPDIRTY))) {
01546                 BKE_report(op->reports, RPT_ERROR, "Can't pack edited image from disk, only as internal PNG.");
01547                 return OPERATOR_CANCELLED;
01548         }
01549 
01550         if(as_png)
01551                 BKE_image_memorypack(ima);
01552         else
01553                 ima->packedfile= newPackedFile(op->reports, ima->name);
01554 
01555         WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, ima);
01556         
01557         return OPERATOR_FINISHED;
01558 }
01559 
01560 static int pack_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
01561 {
01562         Image *ima= CTX_data_edit_image(C);
01563         ImBuf *ibuf= BKE_image_get_ibuf(ima, NULL);
01564         uiPopupMenu *pup;
01565         uiLayout *layout;
01566         int as_png= RNA_boolean_get(op->ptr, "as_png");
01567 
01568         if(!pack_test(C, op))
01569                 return OPERATOR_CANCELLED;
01570         
01571         if(!as_png && (ibuf && (ibuf->userflags & IB_BITMAPDIRTY))) {
01572                 pup= uiPupMenuBegin(C, "OK", ICON_QUESTION);
01573                 layout= uiPupMenuLayout(pup);
01574                 uiItemBooleanO(layout, "Can't pack edited image from disk. Pack as internal PNG?", ICON_NONE, op->idname, "as_png", 1);
01575                 uiPupMenuEnd(C, pup);
01576 
01577                 return OPERATOR_CANCELLED;
01578         }
01579 
01580         return pack_exec(C, op);
01581 }
01582 
01583 void IMAGE_OT_pack(wmOperatorType *ot)
01584 {
01585         /* identifiers */
01586         ot->name= "Pack Image";
01587         ot->description= "Pack an image as embedded data into the .blend file"; 
01588         ot->idname= "IMAGE_OT_pack";
01589         
01590         /* api callbacks */
01591         ot->exec= pack_exec;
01592         ot->invoke= pack_invoke;
01593 
01594         /* flags */
01595         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
01596 
01597         /* properties */
01598         RNA_def_boolean(ot->srna, "as_png", 0, "Pack As PNG", "Pack image as lossless PNG.");
01599 }
01600 
01601 /********************* unpack operator *********************/
01602 
01603 static int image_unpack_exec(bContext *C, wmOperator *op)
01604 {
01605         Image *ima= CTX_data_edit_image(C);
01606         int method= RNA_enum_get(op->ptr, "method");
01607 
01608         /* find the suppplied image by name */
01609         if (RNA_property_is_set(op->ptr, "id")) {
01610                 char imaname[MAX_ID_NAME-2];
01611                 RNA_string_get(op->ptr, "id", imaname);
01612                 ima = BLI_findstring(&CTX_data_main(C)->image, imaname, offsetof(ID, name) + 2);
01613                 if (!ima) ima = CTX_data_edit_image(C);
01614         }
01615         
01616         if(!ima || !ima->packedfile)
01617                 return OPERATOR_CANCELLED;
01618 
01619         if(ima->source==IMA_SRC_SEQUENCE || ima->source==IMA_SRC_MOVIE) {
01620                 BKE_report(op->reports, RPT_ERROR, "Unpacking movies or image sequences not supported.");
01621                 return OPERATOR_CANCELLED;
01622         }
01623 
01624         if(G.fileflags & G_AUTOPACK)
01625                 BKE_report(op->reports, RPT_WARNING, "AutoPack is enabled, so image will be packed again on file save.");
01626         
01627         /* XXX unpackImage frees image buffers */
01628         ED_preview_kill_jobs(C);
01629         
01630         unpackImage(op->reports, ima, method);
01631         
01632         WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, ima);
01633 
01634         return OPERATOR_FINISHED;
01635 }
01636 
01637 static int image_unpack_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
01638 {
01639         Image *ima= CTX_data_edit_image(C);
01640 
01641         if(RNA_property_is_set(op->ptr, "id"))
01642                 return image_unpack_exec(C, op);
01643                 
01644         if(!ima || !ima->packedfile)
01645                 return OPERATOR_CANCELLED;
01646 
01647         if(ima->source==IMA_SRC_SEQUENCE || ima->source==IMA_SRC_MOVIE) {
01648                 BKE_report(op->reports, RPT_ERROR, "Unpacking movies or image sequences not supported.");
01649                 return OPERATOR_CANCELLED;
01650         }
01651 
01652         if(G.fileflags & G_AUTOPACK)
01653                 BKE_report(op->reports, RPT_WARNING, "AutoPack is enabled, so image will be packed again on file save.");
01654 
01655         unpack_menu(C, "IMAGE_OT_unpack", ima->id.name+2, ima->name, "textures", ima->packedfile);
01656 
01657         return OPERATOR_FINISHED;
01658 }
01659 
01660 void IMAGE_OT_unpack(wmOperatorType *ot)
01661 {
01662         /* identifiers */
01663         ot->name= "Unpack Image";
01664         ot->description= "Save an image packed in the .blend file to disk"; 
01665         ot->idname= "IMAGE_OT_unpack";
01666         
01667         /* api callbacks */
01668         ot->exec= image_unpack_exec;
01669         ot->invoke= image_unpack_invoke;
01670 
01671         /* flags */
01672         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
01673         
01674         /* properties */
01675         RNA_def_enum(ot->srna, "method", unpack_method_items, PF_USE_LOCAL, "Method", "How to unpack.");
01676         RNA_def_string(ot->srna, "id", "", MAX_ID_NAME-2, "Image Name", "Image datablock name to unpack."); /* XXX, weark!, will fail with library, name collisions */
01677 }
01678 
01679 /******************** sample image operator ********************/
01680 
01681 typedef struct ImageSampleInfo {
01682         ARegionType *art;
01683         void *draw_handle;
01684         int x, y;
01685         int channels;
01686 
01687         char col[4];
01688         float colf[4];
01689         int z;
01690         float zf;
01691 
01692         char *colp;
01693         float *colfp;
01694         int *zp;
01695         float *zfp;
01696 
01697         int draw;
01698 } ImageSampleInfo;
01699 
01700 static void sample_draw(const bContext *UNUSED(C), ARegion *ar, void *arg_info)
01701 {
01702         ImageSampleInfo *info= arg_info;
01703         if(info->draw) {
01704                 /* no color management needed for images (color_manage=0) */
01705                 draw_image_info(ar, 0, info->channels, info->x, info->y, info->colp, info->colfp, info->zp, info->zfp);
01706         }
01707 }
01708 
01709 static void sample_apply(bContext *C, wmOperator *op, wmEvent *event)
01710 {
01711         SpaceImage *sima= CTX_wm_space_image(C);
01712         ARegion *ar= CTX_wm_region(C);
01713         void *lock;
01714         ImBuf *ibuf= ED_space_image_acquire_buffer(sima, &lock);
01715         ImageSampleInfo *info= op->customdata;
01716         float fx, fy;
01717         
01718         if(ibuf == NULL) {
01719                 ED_space_image_release_buffer(sima, lock);
01720                 return;
01721         }
01722 
01723         UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1], &fx, &fy);
01724 
01725         if(fx>=0.0f && fy>=0.0f && fx<1.0f && fy<1.0f) {
01726                 float *fp;
01727                 char *cp;
01728                 int x= (int)(fx*ibuf->x), y= (int)(fy*ibuf->y);
01729 
01730                 CLAMP(x, 0, ibuf->x-1);
01731                 CLAMP(y, 0, ibuf->y-1);
01732 
01733                 info->x= x;
01734                 info->y= y;
01735                 info->draw= 1;
01736                 info->channels= ibuf->channels;
01737 
01738                 info->colp= NULL;
01739                 info->colfp= NULL;
01740                 info->zp= NULL;
01741                 info->zfp= NULL;
01742                 
01743                 if(ibuf->rect) {
01744                         cp= (char *)(ibuf->rect + y*ibuf->x + x);
01745 
01746                         info->col[0]= cp[0];
01747                         info->col[1]= cp[1];
01748                         info->col[2]= cp[2];
01749                         info->col[3]= cp[3];
01750                         info->colp= info->col;
01751 
01752                         info->colf[0]= (float)cp[0]/255.0f;
01753                         info->colf[1]= (float)cp[1]/255.0f;
01754                         info->colf[2]= (float)cp[2]/255.0f;
01755                         info->colf[3]= (float)cp[3]/255.0f;
01756                         info->colfp= info->colf;
01757                 }
01758                 if(ibuf->rect_float) {
01759                         fp= (ibuf->rect_float + (ibuf->channels)*(y*ibuf->x + x));
01760 
01761                         info->colf[0]= fp[0];
01762                         info->colf[1]= fp[1];
01763                         info->colf[2]= fp[2];
01764                         info->colf[3]= fp[3];
01765                         info->colfp= info->colf;
01766                 }
01767 
01768                 if(ibuf->zbuf) {
01769                         info->z= ibuf->zbuf[y*ibuf->x + x];
01770                         info->zp= &info->z;
01771                 }
01772                 if(ibuf->zbuf_float) {
01773                         info->zf= ibuf->zbuf_float[y*ibuf->x + x];
01774                         info->zfp= &info->zf;
01775                 }
01776                 
01777                 if(sima->cumap && ibuf->channels==4) {
01778                         /* we reuse this callback for set curves point operators */
01779                         if(RNA_struct_find_property(op->ptr, "point")) {
01780                                 int point= RNA_enum_get(op->ptr, "point");
01781 
01782                                 if(point == 1) {
01783                                         curvemapping_set_black_white(sima->cumap, NULL, info->colfp);
01784                                         if(ibuf->rect_float)
01785                                                 curvemapping_do_ibuf(sima->cumap, ibuf);
01786                                 }
01787                                 else if(point == 0) {
01788                                         curvemapping_set_black_white(sima->cumap, info->colfp, NULL);
01789                                         if(ibuf->rect_float)
01790                                                 curvemapping_do_ibuf(sima->cumap, ibuf);
01791                                 }
01792                         }
01793                 }
01794                                 
01795                 // XXX node curve integration ..
01796 #if 0
01797                 {
01798                         ScrArea *sa, *cur= curarea;
01799                         
01800                         node_curvemap_sample(fp);       /* sends global to node editor */
01801                         for(sa= G.curscreen->areabase.first; sa; sa= sa->next) {
01802                                 if(sa->spacetype==SPACE_NODE) {
01803                                         areawinset(sa->win);
01804                                         scrarea_do_windraw(sa);
01805                                 }
01806                         }
01807                         node_curvemap_sample(NULL);             /* clears global in node editor */
01808                         curarea= cur;
01809                 }
01810 #endif
01811         }
01812         else
01813                 info->draw= 0;
01814 
01815         ED_space_image_release_buffer(sima, lock);
01816         ED_area_tag_redraw(CTX_wm_area(C));
01817 }
01818 
01819 static void sample_exit(bContext *C, wmOperator *op)
01820 {
01821         ImageSampleInfo *info= op->customdata;
01822 
01823         ED_region_draw_cb_exit(info->art, info->draw_handle);
01824         ED_area_tag_redraw(CTX_wm_area(C));
01825         MEM_freeN(info);
01826 }
01827 
01828 static int sample_invoke(bContext *C, wmOperator *op, wmEvent *event)
01829 {
01830         SpaceImage *sima= CTX_wm_space_image(C);
01831         ARegion *ar= CTX_wm_region(C);
01832         ImageSampleInfo *info;
01833 
01834         if(!ED_space_image_has_buffer(sima))
01835                 return OPERATOR_CANCELLED;
01836         
01837         info= MEM_callocN(sizeof(ImageSampleInfo), "ImageSampleInfo");
01838         info->art= ar->type;
01839         info->draw_handle = ED_region_draw_cb_activate(ar->type, sample_draw, info, REGION_DRAW_POST_PIXEL);
01840         op->customdata= info;
01841 
01842         sample_apply(C, op, event);
01843 
01844         WM_event_add_modal_handler(C, op);
01845 
01846         return OPERATOR_RUNNING_MODAL;
01847 }
01848 
01849 static int sample_modal(bContext *C, wmOperator *op, wmEvent *event)
01850 {
01851         switch(event->type) {
01852                 case LEFTMOUSE:
01853                 case RIGHTMOUSE: // XXX hardcoded
01854                         sample_exit(C, op);
01855                         return OPERATOR_CANCELLED;
01856                 case MOUSEMOVE:
01857                         sample_apply(C, op, event);
01858                         break;
01859         }
01860 
01861         return OPERATOR_RUNNING_MODAL;
01862 }
01863 
01864 static int sample_cancel(bContext *C, wmOperator *op)
01865 {
01866         sample_exit(C, op);
01867         return OPERATOR_CANCELLED;
01868 }
01869 
01870 void IMAGE_OT_sample(wmOperatorType *ot)
01871 {
01872         /* identifiers */
01873         ot->name= "Sample Color";
01874         ot->idname= "IMAGE_OT_sample";
01875         
01876         /* api callbacks */
01877         ot->invoke= sample_invoke;
01878         ot->modal= sample_modal;
01879         ot->cancel= sample_cancel;
01880         ot->poll= space_image_main_area_poll;
01881 
01882         /* flags */
01883         ot->flag= OPTYPE_BLOCKING;
01884 }
01885 
01886 /******************** sample line operator ********************/
01887 static int sample_line_exec(bContext *C, wmOperator *op)
01888 {
01889         SpaceImage *sima= CTX_wm_space_image(C);
01890         ARegion *ar= CTX_wm_region(C);
01891         Scene *scene= CTX_data_scene(C);
01892         
01893         int x_start= RNA_int_get(op->ptr, "xstart");
01894         int y_start= RNA_int_get(op->ptr, "ystart");
01895         int x_end= RNA_int_get(op->ptr, "xend");
01896         int y_end= RNA_int_get(op->ptr, "yend");
01897         
01898         void *lock;
01899         ImBuf *ibuf= ED_space_image_acquire_buffer(sima, &lock);
01900         Histogram *hist= &sima->sample_line_hist;
01901         
01902         float x1f, y1f, x2f, y2f;
01903         int x1, y1, x2, y2;
01904         int i, x, y;
01905         float *fp;
01906         float rgb[3];
01907         unsigned char *cp;
01908         
01909         if (ibuf == NULL) {
01910                 ED_space_image_release_buffer(sima, lock);
01911                 return OPERATOR_CANCELLED;
01912         }
01913         /* hmmmm */
01914         if (ibuf->channels < 3) {
01915                 ED_space_image_release_buffer(sima, lock);
01916                 return OPERATOR_CANCELLED;
01917         }
01918         
01919         UI_view2d_region_to_view(&ar->v2d, x_start, y_start, &x1f, &y1f);
01920         UI_view2d_region_to_view(&ar->v2d, x_end, y_end, &x2f, &y2f);
01921         x1= 0.5f+ x1f*ibuf->x;
01922         x2= 0.5f+ x2f*ibuf->x;
01923         y1= 0.5f+ y1f*ibuf->y;
01924         y2= 0.5f+ y2f*ibuf->y;
01925         
01926         hist->channels = 3;
01927         hist->x_resolution = 256;
01928         hist->xmax = 1.0f;
01929         hist->ymax = 1.0f;
01930         
01931         for (i=0; i<256; i++) {
01932                 x= (int)(0.5f + x1 + (float)i*(x2-x1)/255.0f);
01933                 y= (int)(0.5f + y1 + (float)i*(y2-y1)/255.0f);
01934                 
01935                 if (x<0 || y<0 || x>=ibuf->x || y>=ibuf->y) {
01936                         hist->data_luma[i] = hist->data_r[i] = hist->data_g[i]= hist->data_b[i] = 0.0f;
01937                 } else {
01938                         if (ibuf->rect_float) {
01939                                 fp= (ibuf->rect_float + (ibuf->channels)*(y*ibuf->x + x));
01940 
01941                                 if (scene->r.color_mgt_flag & R_COLOR_MANAGEMENT)
01942                                         linearrgb_to_srgb_v3_v3(rgb, fp);
01943                                 else
01944                                         copy_v3_v3(rgb, fp);
01945 
01946                                 hist->data_r[i] = rgb[0];
01947                                 hist->data_g[i] = rgb[1];
01948                                 hist->data_b[i] = rgb[2];
01949                                 hist->data_luma[i] = (0.299f*rgb[0] + 0.587f*rgb[1] + 0.114f*rgb[2]);
01950                         }
01951                         else if (ibuf->rect) {
01952                                 cp= (unsigned char *)(ibuf->rect + y*ibuf->x + x);
01953                                 hist->data_r[i] = (float)cp[0]/255.0f;
01954                                 hist->data_g[i] = (float)cp[1]/255.0f;
01955                                 hist->data_b[i] = (float)cp[2]/255.0f;
01956                                 hist->data_luma[i] = (0.299f*cp[0] + 0.587f*cp[1] + 0.114f*cp[2])/255;
01957                         }
01958                 }
01959         }
01960         
01961         ED_space_image_release_buffer(sima, lock);
01962         
01963         ED_area_tag_redraw(CTX_wm_area(C));
01964         
01965         return OPERATOR_FINISHED;
01966 }
01967 
01968 static int sample_line_invoke(bContext *C, wmOperator *op, wmEvent *event)
01969 {
01970         SpaceImage *sima= CTX_wm_space_image(C);
01971         
01972         if(!ED_space_image_has_buffer(sima))
01973                 return OPERATOR_CANCELLED;
01974         
01975         return WM_gesture_straightline_invoke(C, op, event);
01976 }
01977 
01978 void IMAGE_OT_sample_line(wmOperatorType *ot)
01979 {
01980         /* identifiers */
01981         ot->name= "Sample Line";
01982         ot->idname= "IMAGE_OT_sample_line";
01983         
01984         /* api callbacks */
01985         ot->invoke= sample_line_invoke;
01986         ot->modal= WM_gesture_straightline_modal;
01987         ot->exec= sample_line_exec;
01988         ot->poll= space_image_main_area_poll;
01989         ot->cancel= WM_gesture_straightline_cancel;
01990         
01991         /* flags */
01992         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
01993         
01994         WM_operator_properties_gesture_straightline(ot, CURSOR_EDIT);
01995 }
01996 
01997 /******************** set curve point operator ********************/
01998 
01999 void IMAGE_OT_curves_point_set(wmOperatorType *ot)
02000 {
02001         static EnumPropertyItem point_items[]= {
02002                 {0, "BLACK_POINT", 0, "Black Point", ""},
02003                 {1, "WHITE_POINT", 0, "White Point", ""},
02004                 {0, NULL, 0, NULL, NULL}};
02005 
02006         /* identifiers */
02007         ot->name= "Set Curves Point";
02008         ot->idname= "IMAGE_OT_curves_point_set";
02009 
02010         /* flags */
02011         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
02012         
02013         /* api callbacks */
02014         ot->invoke= sample_invoke;
02015         ot->modal= sample_modal;
02016         ot->cancel= sample_cancel;
02017         ot->poll= space_image_main_area_poll;
02018 
02019         /* properties */
02020         RNA_def_enum(ot->srna, "point", point_items, 0, "Point", "Set black point or white point for curves.");
02021 }
02022 
02023 /******************** record composite operator *********************/
02024 
02025 typedef struct RecordCompositeData {
02026         wmTimer *timer;
02027         int old_cfra;
02028         int sfra, efra;
02029 } RecordCompositeData;
02030 
02031 static int record_composite_apply(bContext *C, wmOperator *op)
02032 {
02033         SpaceImage *sima= CTX_wm_space_image(C);
02034         RecordCompositeData *rcd= op->customdata;
02035         Scene *scene= CTX_data_scene(C);
02036         ImBuf *ibuf;
02037         
02038         WM_timecursor(CTX_wm_window(C), scene->r.cfra);
02039 
02040         // XXX scene->nodetree->test_break= blender_test_break;
02041         // XXX scene->nodetree->test_break= NULL;
02042         
02043         BKE_image_all_free_anim_ibufs(scene->r.cfra);
02044         ntreeCompositTagAnimated(scene->nodetree);
02045         ntreeCompositExecTree(scene->nodetree, &scene->r, scene->r.cfra != rcd->old_cfra);      /* 1 is no previews */
02046 
02047         ED_area_tag_redraw(CTX_wm_area(C));
02048         
02049         ibuf= BKE_image_get_ibuf(sima->image, &sima->iuser);
02050         /* save memory in flipbooks */
02051         if(ibuf)
02052                 imb_freerectfloatImBuf(ibuf);
02053         
02054         scene->r.cfra++;
02055 
02056         return (scene->r.cfra <= rcd->efra);
02057 }
02058 
02059 static int record_composite_init(bContext *C, wmOperator *op)
02060 {
02061         SpaceImage *sima= CTX_wm_space_image(C);
02062         Scene *scene= CTX_data_scene(C);
02063         RecordCompositeData *rcd;
02064 
02065         if(sima->iuser.frames < 2)
02066                 return 0;
02067         if(scene->nodetree == NULL)
02068                 return 0;
02069         
02070         op->customdata= rcd= MEM_callocN(sizeof(RecordCompositeData), "ImageRecordCompositeData");
02071 
02072         rcd->old_cfra= scene->r.cfra;
02073         rcd->sfra= sima->iuser.sfra;
02074         rcd->efra= sima->iuser.sfra + sima->iuser.frames-1;
02075         scene->r.cfra= rcd->sfra;
02076 
02077         return 1;
02078 }
02079 
02080 static void record_composite_exit(bContext *C, wmOperator *op)
02081 {
02082         Scene *scene= CTX_data_scene(C);
02083         SpaceImage *sima= CTX_wm_space_image(C);
02084         RecordCompositeData *rcd= op->customdata;
02085 
02086         scene->r.cfra= rcd->old_cfra;
02087 
02088         WM_cursor_restore(CTX_wm_window(C));
02089 
02090         if(rcd->timer)
02091                 WM_event_remove_timer(CTX_wm_manager(C), CTX_wm_window(C), rcd->timer);
02092 
02093         WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, sima->image);
02094 
02095         // XXX play_anim(0);
02096         // XXX allqueue(REDRAWNODE, 1);
02097 
02098         MEM_freeN(rcd);
02099 }
02100 
02101 static int record_composite_exec(bContext *C, wmOperator *op)
02102 {
02103         if(!record_composite_init(C, op))
02104                 return OPERATOR_CANCELLED;
02105         
02106         while(record_composite_apply(C, op))
02107                 ;
02108         
02109         record_composite_exit(C, op);
02110         
02111         return OPERATOR_FINISHED;
02112 }
02113 
02114 static int record_composite_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
02115 {
02116         RecordCompositeData *rcd;
02117         
02118         if(!record_composite_init(C, op))
02119                 return OPERATOR_CANCELLED;
02120 
02121         rcd= op->customdata;
02122         rcd->timer= WM_event_add_timer(CTX_wm_manager(C), CTX_wm_window(C), TIMER, 0.0f);
02123         WM_event_add_modal_handler(C, op);
02124 
02125         if(!record_composite_apply(C, op))
02126                 return OPERATOR_FINISHED;
02127 
02128         return OPERATOR_RUNNING_MODAL;
02129 }
02130 
02131 static int record_composite_modal(bContext *C, wmOperator *op, wmEvent *event)
02132 {
02133         RecordCompositeData *rcd= op->customdata;
02134 
02135         switch(event->type) {
02136                 case TIMER:
02137                         if(rcd->timer == event->customdata) {
02138                                 if(!record_composite_apply(C, op)) {
02139                                         record_composite_exit(C, op);
02140                                         return OPERATOR_FINISHED;
02141                                 }
02142                         }
02143                         break;
02144                 case ESCKEY:
02145                         record_composite_exit(C, op);
02146                         return OPERATOR_FINISHED;
02147         }
02148 
02149         return OPERATOR_RUNNING_MODAL;
02150 }
02151 
02152 static int record_composite_cancel(bContext *C, wmOperator *op)
02153 {
02154         record_composite_exit(C, op);
02155         return OPERATOR_CANCELLED;
02156 }
02157 
02158 void IMAGE_OT_record_composite(wmOperatorType *ot)
02159 {
02160         /* identifiers */
02161         ot->name= "Record Composite";
02162         ot->idname= "IMAGE_OT_record_composite";
02163         
02164         /* api callbacks */
02165         ot->exec= record_composite_exec;
02166         ot->invoke= record_composite_invoke;
02167         ot->modal= record_composite_modal;
02168         ot->cancel= record_composite_cancel;
02169         ot->poll= space_image_buffer_exists_poll;
02170 }
02171 
02172 /********************* cycle render slot operator *********************/
02173 
02174 static int cycle_render_slot_poll(bContext *C)
02175 {
02176         Image *ima= CTX_data_edit_image(C);
02177 
02178         return (ima && ima->type == IMA_TYPE_R_RESULT);
02179 }
02180 
02181 static int cycle_render_slot_exec(bContext *C, wmOperator *op)
02182 {
02183         Image *ima= CTX_data_edit_image(C);
02184         int a, slot, cur= ima->render_slot;
02185         const short use_reverse= RNA_boolean_get(op->ptr, "reverse");
02186 
02187         for(a=1; a<IMA_MAX_RENDER_SLOT; a++) {
02188                 slot= (cur + (use_reverse ? -a:a))%IMA_MAX_RENDER_SLOT;
02189                 if(slot<0) slot+=IMA_MAX_RENDER_SLOT;
02190 
02191                 if(ima->renders[slot] || slot == ima->last_render_slot) {
02192                         ima->render_slot= slot;
02193                         break;
02194                 }
02195                 else if((slot - 1) == ima->last_render_slot && slot < IMA_MAX_RENDER_SLOT) {
02196                         ima->render_slot= slot;
02197                         break;
02198                 }
02199         }
02200 
02201         if(a == IMA_MAX_RENDER_SLOT)
02202                 ima->render_slot= ((cur == 1)? 0: 1);
02203         
02204         WM_event_add_notifier(C, NC_IMAGE|ND_DRAW, NULL);
02205 
02206         /* no undo push for browsing existing */
02207         if(ima->renders[ima->render_slot] || ima->render_slot==ima->last_render_slot)
02208                 return OPERATOR_CANCELLED;
02209         
02210         return OPERATOR_FINISHED;
02211 }
02212 
02213 void IMAGE_OT_cycle_render_slot(wmOperatorType *ot)
02214 {
02215         /* identifiers */
02216         ot->name= "Cycle Render Slot";
02217         ot->idname= "IMAGE_OT_cycle_render_slot";
02218         
02219         /* api callbacks */
02220         ot->exec= cycle_render_slot_exec;
02221         ot->poll= cycle_render_slot_poll;
02222 
02223         /* flags */
02224         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
02225 
02226         RNA_def_boolean(ot->srna, "reverse", 0, "Cycle in Reverse", "");
02227 }
02228 
02229 /******************** TODO ********************/
02230 
02231 /* XXX notifier? */
02232 
02233 /* goes over all ImageUsers, and sets frame numbers if auto-refresh is set */
02234 
02235 void ED_image_update_frame(const Main *mainp, int cfra)
02236 {
02237         wmWindowManager *wm;
02238         wmWindow *win;
02239         Tex *tex;
02240         
02241         /* texture users */
02242         for(tex= mainp->tex.first; tex; tex= tex->id.next) {
02243                 if(tex->type==TEX_IMAGE && tex->ima) {
02244                         if(ELEM(tex->ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE)) {
02245                                 if(tex->iuser.flag & IMA_ANIM_ALWAYS)
02246                                         BKE_image_user_calc_frame(&tex->iuser, cfra, 0);
02247                         }
02248                 }
02249         }
02250         
02251         /* image window, compo node users */
02252         for(wm=mainp->wm.first; wm; wm= wm->id.next) { /* only 1 wm */
02253                 for(win= wm->windows.first; win; win= win->next) {
02254                         ScrArea *sa;
02255                         for(sa= win->screen->areabase.first; sa; sa= sa->next) {
02256                                 if(sa->spacetype==SPACE_VIEW3D) {
02257                                         View3D *v3d= sa->spacedata.first;
02258                                         BGpic *bgpic;
02259                                         for(bgpic= v3d->bgpicbase.first; bgpic; bgpic= bgpic->next)
02260                                                 if(bgpic->iuser.flag & IMA_ANIM_ALWAYS)
02261                                                         BKE_image_user_calc_frame(&bgpic->iuser, cfra, 0);
02262                                 }
02263                                 else if(sa->spacetype==SPACE_IMAGE) {
02264                                         SpaceImage *sima= sa->spacedata.first;
02265                                         if(sima->iuser.flag & IMA_ANIM_ALWAYS)
02266                                                 BKE_image_user_calc_frame(&sima->iuser, cfra, 0);
02267                                 }
02268                                 else if(sa->spacetype==SPACE_NODE) {
02269                                         SpaceNode *snode= sa->spacedata.first;
02270                                         if((snode->treetype==NTREE_COMPOSIT) && (snode->nodetree)) {
02271                                                 bNode *node;
02272                                                 for(node= snode->nodetree->nodes.first; node; node= node->next) {
02273                                                         if(node->id && node->type==CMP_NODE_IMAGE) {
02274                                                                 Image *ima= (Image *)node->id;
02275                                                                 ImageUser *iuser= node->storage;
02276                                                                 if(ELEM(ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE))
02277                                                                         if(iuser->flag & IMA_ANIM_ALWAYS)
02278                                                                                 BKE_image_user_calc_frame(iuser, cfra, 0);
02279                                                         }
02280                                                 }
02281                                         }
02282                                 }
02283                         }
02284                 }
02285         }
02286 }
02287