style cleanup: follow style guide for formatting of if/for/while loops, and else...
[blender.git] / source / blender / editors / space_image / image_ops.c
index f96a8ea4d847d2f45a0ae6f65ae9341d7ae83a41..5de0dd3fccde060f76c8ec0b59d705b029ee72e8 100644 (file)
@@ -1,6 +1,4 @@
 /*
- * $Id$
- *
  * ***** BEGIN GPL LICENSE BLOCK *****
  *
  * This program is free software; you can redistribute it and/or
 
 /******************** view navigation utilities *********************/
 
-static void sima_zoom_set(SpaceImage *sima, ARegion *ar, float zoom)
+static void sima_zoom_set(SpaceImage *sima, ARegion *ar, float zoom, float location[2])
 {
        float oldzoom= sima->zoom;
        int width, height;
 
        sima->zoom= zoom;
 
-       if (sima->zoom > 0.1f && sima->zoom < 4.0f)
-               return;
+       if (sima->zoom < 0.1f || sima->zoom > 4.0f) {
+               /* check zoom limits */
+               ED_space_image_size(sima, &width, &height);
 
-       /* check zoom limits */
-       ED_space_image_size(sima, &width, &height);
+               width *= sima->zoom;
+               height *= sima->zoom;
+
+               if ((width < 4) && (height < 4))
+                       sima->zoom= oldzoom;
+               else if ((ar->winrct.xmax - ar->winrct.xmin) <= sima->zoom)
+                       sima->zoom= oldzoom;
+               else if ((ar->winrct.ymax - ar->winrct.ymin) <= sima->zoom)
+                       sima->zoom= oldzoom;
+       }
 
-       width *= sima->zoom;
-       height *= sima->zoom;
+       if ((U.uiflag & USER_ZOOM_TO_MOUSEPOS) && location) {
+               float aspx, aspy, w, h;
 
-       if((width < 4) && (height < 4))
-               sima->zoom= oldzoom;
-       else if((ar->winrct.xmax - ar->winrct.xmin) <= sima->zoom)
-               sima->zoom= oldzoom;
-       else if((ar->winrct.ymax - ar->winrct.ymin) <= sima->zoom)
-               sima->zoom= oldzoom;
+               ED_space_image_size(sima, &width, &height);
+               ED_space_image_aspect(sima, &aspx, &aspy);
+
+               w= width*aspx;
+               h= height*aspy;
+
+               sima->xof+= ((location[0]-0.5f)*w-sima->xof)*(sima->zoom-oldzoom)/sima->zoom;
+               sima->yof+= ((location[1]-0.5f)*h-sima->yof)*(sima->zoom-oldzoom)/sima->zoom;
+       }
 }
 
-static void sima_zoom_set_factor(SpaceImage *sima, ARegion *ar, float zoomfac)
+static void sima_zoom_set_factor(SpaceImage *sima, ARegion *ar, float zoomfac, float location[2])
 {
-       sima_zoom_set(sima, ar, sima->zoom*zoomfac);
+       sima_zoom_set(sima, ar, sima->zoom*zoomfac, location);
 }
 
 #if 0 // currently unused
@@ -123,31 +133,40 @@ static int image_poll(bContext *C)
 static int space_image_buffer_exists_poll(bContext *C)
 {
        SpaceImage *sima= CTX_wm_space_image(C);
-       if(sima && sima->spacetype==SPACE_IMAGE)
-               if(ED_space_image_has_buffer(sima))
+       if (sima && sima->spacetype==SPACE_IMAGE)
+               if (ED_space_image_has_buffer(sima))
                        return 1;
        return 0;
 }
 
 static int space_image_file_exists_poll(bContext *C)
 {
-       if(space_image_buffer_exists_poll(C)) {
+       if (space_image_buffer_exists_poll(C)) {
                Main *bmain= CTX_data_main(C);
                SpaceImage *sima= CTX_wm_space_image(C);
                ImBuf *ibuf;
                void *lock;
-               int poll= 0;
+               int ret= FALSE;
                char name[FILE_MAX];
 
                ibuf= ED_space_image_acquire_buffer(sima, &lock);
-               if(ibuf) {
+               if (ibuf) {
                        BLI_strncpy(name, ibuf->name, FILE_MAX);
                        BLI_path_abs(name, bmain->name);
-                       poll= (BLI_exists(name) && BLI_is_writable(name));
+
+                       if (BLI_exists(name) == FALSE) {
+                               CTX_wm_operator_poll_msg_set(C, "image file not found");
+                       }
+                       else if (BLI_file_is_writable(name) == FALSE) {
+                               CTX_wm_operator_poll_msg_set(C, "image path can't be written to");
+                       }
+                       else {
+                               ret= TRUE;
+                       }
                }
                ED_space_image_release_buffer(sima, lock);
 
-               return poll;
+               return ret;
        }
        return 0;
 }
@@ -155,7 +174,7 @@ static int space_image_file_exists_poll(bContext *C)
 static int space_image_poll(bContext *C)
 {
        SpaceImage *sima= CTX_wm_space_image(C);
-       if(sima && sima->spacetype==SPACE_IMAGE && sima->image)
+       if (sima && sima->spacetype==SPACE_IMAGE && sima->image)
                return 1;
        return 0;
 }
@@ -165,12 +184,36 @@ int space_image_main_area_poll(bContext *C)
        SpaceImage *sima= CTX_wm_space_image(C);
        // XXX ARegion *ar= CTX_wm_region(C);
 
-       if(sima)
+       if (sima)
                return 1; // XXX (ar && ar->type->regionid == RGN_TYPE_WINDOW);
        
        return 0;
 }
 
+/* For IMAGE_OT_curves_point_set to avoid sampling when in uv smooth mode */
+int space_image_main_area_not_uv_brush_poll(bContext *C)
+{
+       SpaceImage *sima= CTX_wm_space_image(C);
+
+       ToolSettings *toolsettings = CTX_data_scene(C)->toolsettings;
+       if (sima && !toolsettings->uvsculpt)
+               return 1;
+
+       return 0;
+}
+
+static int space_image_image_sample_poll(bContext *C)
+{
+       SpaceImage *sima= CTX_wm_space_image(C);
+       Object *obedit= CTX_data_edit_object(C);
+       ToolSettings *toolsettings = CTX_data_scene(C)->toolsettings;
+
+       if (obedit) {
+               if (ED_space_image_show_uvedit(sima, obedit) && (toolsettings->use_uv_sculpt))
+                       return 0;
+       }
+       return space_image_main_area_poll(C);
+}
 /********************** view pan operator *********************/
 
 typedef struct ViewPanData {
@@ -179,7 +222,7 @@ typedef struct ViewPanData {
        int event_type;
 } ViewPanData;
 
-static void view_pan_init(bContext *C, wmOperator *op, wmEvent *event)
+static void image_view_pan_init(bContext *C, wmOperator *op, wmEvent *event)
 {
        SpaceImage *sima= CTX_wm_space_image(C);
        ViewPanData *vpd;
@@ -196,12 +239,12 @@ static void view_pan_init(bContext *C, wmOperator *op, wmEvent *event)
        WM_event_add_modal_handler(C, op);
 }
 
-static void view_pan_exit(bContext *C, wmOperator *op, int cancel)
+static void image_view_pan_exit(bContext *C, wmOperator *op, int cancel)
 {
        SpaceImage *sima= CTX_wm_space_image(C);
        ViewPanData *vpd= op->customdata;
 
-       if(cancel) {
+       if (cancel) {
                sima->xof= vpd->xof;
                sima->yof= vpd->yof;
                ED_region_tag_redraw(CTX_wm_region(C));
@@ -211,7 +254,7 @@ static void view_pan_exit(bContext *C, wmOperator *op, int cancel)
        MEM_freeN(op->customdata);
 }
 
-static int view_pan_exec(bContext *C, wmOperator *op)
+static int image_view_pan_exec(bContext *C, wmOperator *op)
 {
        SpaceImage *sima= CTX_wm_space_image(C);
        float offset[2];
@@ -224,7 +267,7 @@ static int view_pan_exec(bContext *C, wmOperator *op)
 
        /* XXX notifier? */
 #if 0
-       if(image_preview_active(curarea, NULL, NULL)) {
+       if (image_preview_active(curarea, NULL, NULL)) {
                /* recalculates new preview rect */
                scrarea_do_windraw(curarea);
                image_preview_event(2);
@@ -234,7 +277,7 @@ static int view_pan_exec(bContext *C, wmOperator *op)
        return OPERATOR_FINISHED;
 }
 
-static int view_pan_invoke(bContext *C, wmOperator *op, wmEvent *event)
+static int image_view_pan_invoke(bContext *C, wmOperator *op, wmEvent *event)
 {
        if (event->type == MOUSEPAN) {
                SpaceImage *sima= CTX_wm_space_image(C);
@@ -244,16 +287,16 @@ static int view_pan_invoke(bContext *C, wmOperator *op, wmEvent *event)
                offset[1]= (event->y - event->prevy)/sima->zoom;
                RNA_float_set_array(op->ptr, "offset", offset);
 
-               view_pan_exec(C, op);
+               image_view_pan_exec(C, op);
                return OPERATOR_FINISHED;
        }
        else {
-               view_pan_init(C, op, event);
+               image_view_pan_init(C, op, event);
                return OPERATOR_RUNNING_MODAL;
        }
 }
 
-static int view_pan_modal(bContext *C, wmOperator *op, wmEvent *event)
+static int image_view_pan_modal(bContext *C, wmOperator *op, wmEvent *event)
 {
        SpaceImage *sima= CTX_wm_space_image(C);
        ViewPanData *vpd= op->customdata;
@@ -266,11 +309,11 @@ static int view_pan_modal(bContext *C, wmOperator *op, wmEvent *event)
                        offset[0]= (vpd->x - event->x)/sima->zoom;
                        offset[1]= (vpd->y - event->y)/sima->zoom;
                        RNA_float_set_array(op->ptr, "offset", offset);
-                       view_pan_exec(C, op);
+                       image_view_pan_exec(C, op);
                        break;
                default:
-                       if(event->type==vpd->event_type &&  event->val==KM_RELEASE) {
-                               view_pan_exit(C, op, 0);
+                       if (event->type==vpd->event_type &&  event->val==KM_RELEASE) {
+                               image_view_pan_exit(C, op, 0);
                                return OPERATOR_FINISHED;
                        }
                        break;
@@ -279,31 +322,31 @@ static int view_pan_modal(bContext *C, wmOperator *op, wmEvent *event)
        return OPERATOR_RUNNING_MODAL;
 }
 
-static int view_pan_cancel(bContext *C, wmOperator *op)
+static int image_view_pan_cancel(bContext *C, wmOperator *op)
 {
-       view_pan_exit(C, op, 1);
+       image_view_pan_exit(C, op, 1);
        return OPERATOR_CANCELLED;
 }
 
 void IMAGE_OT_view_pan(wmOperatorType *ot)
 {
        /* identifiers */
-       ot->name= "View Pan";
-       ot->idname= "IMAGE_OT_view_pan";
+       ot->name = "View Pan";
+       ot->idname = "IMAGE_OT_view_pan";
        
        /* api callbacks */
-       ot->execview_pan_exec;
-       ot->invokeview_pan_invoke;
-       ot->modalview_pan_modal;
-       ot->cancelview_pan_cancel;
-       ot->poll= space_image_main_area_poll;
+       ot->exec = image_view_pan_exec;
+       ot->invoke = image_view_pan_invoke;
+       ot->modal = image_view_pan_modal;
+       ot->cancel = image_view_pan_cancel;
+       ot->poll = space_image_main_area_poll;
 
        /* flags */
-       ot->flag= OPTYPE_BLOCKING;
+       ot->flag = OPTYPE_BLOCKING;
        
        /* properties */
        RNA_def_float_vector(ot->srna, "offset", 2, NULL, -FLT_MAX, FLT_MAX,
-               "Offset", "Offset in floating point units, 1.0 is the width and height of the image.", -FLT_MAX, FLT_MAX);
+               "Offset", "Offset in floating point units, 1.0 is the width and height of the image", -FLT_MAX, FLT_MAX);
 }
 
 /********************** view zoom operator *********************/
@@ -312,11 +355,13 @@ typedef struct ViewZoomData {
        float x, y;
        float zoom;
        int event_type;
+       float location[2];
 } ViewZoomData;
 
-static void view_zoom_init(bContext *C, wmOperator *op, wmEvent *event)
+static void image_view_zoom_init(bContext *C, wmOperator *op, wmEvent *event)
 {
        SpaceImage *sima= CTX_wm_space_image(C);
+       ARegion *ar= CTX_wm_region(C);
        ViewZoomData *vpd;
 
        op->customdata= vpd= MEM_callocN(sizeof(ViewZoomData), "ImageViewZoomData");
@@ -326,16 +371,18 @@ static void view_zoom_init(bContext *C, wmOperator *op, wmEvent *event)
        vpd->y= event->y;
        vpd->zoom= sima->zoom;
        vpd->event_type= event->type;
-       
+
+       UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1], &vpd->location[0], &vpd->location[1]);
+
        WM_event_add_modal_handler(C, op);
 }
 
-static void view_zoom_exit(bContext *C, wmOperator *op, int cancel)
+static void image_view_zoom_exit(bContext *C, wmOperator *op, int cancel)
 {
        SpaceImage *sima= CTX_wm_space_image(C);
        ViewZoomData *vpd= op->customdata;
 
-       if(cancel) {
+       if (cancel) {
                sima->zoom= vpd->zoom;
                ED_region_tag_redraw(CTX_wm_region(C));
        }
@@ -344,18 +391,18 @@ static void view_zoom_exit(bContext *C, wmOperator *op, int cancel)
        MEM_freeN(op->customdata);
 }
 
-static int view_zoom_exec(bContext *C, wmOperator *op)
+static int image_view_zoom_exec(bContext *C, wmOperator *op)
 {
        SpaceImage *sima= CTX_wm_space_image(C);
        ARegion *ar= CTX_wm_region(C);
 
-       sima_zoom_set_factor(sima, ar, RNA_float_get(op->ptr, "factor"));
+       sima_zoom_set_factor(sima, ar, RNA_float_get(op->ptr, "factor"), NULL);
 
        ED_region_tag_redraw(CTX_wm_region(C));
 
        /* XXX notifier? */
 #if 0
-       if(image_preview_active(curarea, NULL, NULL)) {
+       if (image_preview_active(curarea, NULL, NULL)) {
                /* recalculates new preview rect */
                scrarea_do_windraw(curarea);
                image_preview_event(2);
@@ -365,27 +412,29 @@ static int view_zoom_exec(bContext *C, wmOperator *op)
        return OPERATOR_FINISHED;
 }
 
-static int view_zoom_invoke(bContext *C, wmOperator *op, wmEvent *event)
+static int image_view_zoom_invoke(bContext *C, wmOperator *op, wmEvent *event)
 {
        if (event->type == MOUSEZOOM) {
                SpaceImage *sima= CTX_wm_space_image(C);
                ARegion *ar= CTX_wm_region(C);
-               float factor;
-               
+               float factor, location[2];
+
+               UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1], &location[0], &location[1]);
+
                factor= 1.0f + (event->x-event->prevx+event->y-event->prevy)/300.0f;
                RNA_float_set(op->ptr, "factor", factor);
-               sima_zoom_set(sima, ar, sima->zoom*factor);
+               sima_zoom_set(sima, ar, sima->zoom*factor, location);
                ED_region_tag_redraw(CTX_wm_region(C));
                
                return OPERATOR_FINISHED;
        }
        else {
-               view_zoom_init(C, op, event);
+               image_view_zoom_init(C, op, event);
                return OPERATOR_RUNNING_MODAL;
        }
 }
 
-static int view_zoom_modal(bContext *C, wmOperator *op, wmEvent *event)
+static int image_view_zoom_modal(bContext *C, wmOperator *op, wmEvent *event)
 {
        SpaceImage *sima= CTX_wm_space_image(C);
        ARegion *ar= CTX_wm_region(C);
@@ -396,12 +445,12 @@ static int view_zoom_modal(bContext *C, wmOperator *op, wmEvent *event)
                case MOUSEMOVE:
                        factor= 1.0f + (vpd->x-event->x+vpd->y-event->y)/300.0f;
                        RNA_float_set(op->ptr, "factor", factor);
-                       sima_zoom_set(sima, ar, vpd->zoom*factor);
+                       sima_zoom_set(sima, ar, vpd->zoom*factor, vpd->location);
                        ED_region_tag_redraw(CTX_wm_region(C));
                        break;
                default:
-                       if(event->type==vpd->event_type && event->val==KM_RELEASE) {
-                               view_zoom_exit(C, op, 0);
+                       if (event->type==vpd->event_type && event->val==KM_RELEASE) {
+                               image_view_zoom_exit(C, op, 0);
                                return OPERATOR_FINISHED;
                        }
                        break;
@@ -410,31 +459,31 @@ static int view_zoom_modal(bContext *C, wmOperator *op, wmEvent *event)
        return OPERATOR_RUNNING_MODAL;
 }
 
-static int view_zoom_cancel(bContext *C, wmOperator *op)
+static int image_view_zoom_cancel(bContext *C, wmOperator *op)
 {
-       view_zoom_exit(C, op, 1);
+       image_view_zoom_exit(C, op, 1);
        return OPERATOR_CANCELLED;
 }
 
 void IMAGE_OT_view_zoom(wmOperatorType *ot)
 {
        /* identifiers */
-       ot->name= "View Zoom";
-       ot->idname= "IMAGE_OT_view_zoom";
+       ot->name = "View Zoom";
+       ot->idname = "IMAGE_OT_view_zoom";
        
        /* api callbacks */
-       ot->execview_zoom_exec;
-       ot->invokeview_zoom_invoke;
-       ot->modalview_zoom_modal;
-       ot->cancelview_zoom_cancel;
-       ot->poll= space_image_main_area_poll;
+       ot->exec = image_view_zoom_exec;
+       ot->invoke = image_view_zoom_invoke;
+       ot->modal = image_view_zoom_modal;
+       ot->cancel = image_view_zoom_cancel;
+       ot->poll = space_image_main_area_poll;
 
        /* flags */
-       ot->flag= OPTYPE_BLOCKING;
+       ot->flag = OPTYPE_BLOCKING;
        
        /* properties */
        RNA_def_float(ot->srna, "factor", 0.0f, 0.0f, FLT_MAX,
-               "Factor", "Zoom factor, values higher than 1.0 zoom in, lower values zoom out.", -FLT_MAX, FLT_MAX);
+               "Factor", "Zoom factor, values higher than 1.0 zoom in, lower values zoom out", -FLT_MAX, FLT_MAX);
 }
 
 /********************** NDOF operator *********************/
@@ -445,7 +494,7 @@ void IMAGE_OT_view_zoom(wmOperatorType *ot)
  * that explains the negative signs in the code below
  */
 
-static int view_ndof_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *event)
+static int image_view_ndof_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *event)
 {
        if (event->type != NDOF_MOTION)
                return OPERATOR_CANCELLED;
@@ -474,7 +523,7 @@ static int view_ndof_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *event)
                if (U.ndof_flag & NDOF_ZOOM_INVERT)
                        zoom_factor = -zoom_factor;
 
-               sima_zoom_set_factor(sima, ar, zoom_factor);
+               sima_zoom_set_factor(sima, ar, zoom_factor, NULL);
                sima->xof += pan_x;
                sima->yof += pan_y;
 
@@ -487,11 +536,11 @@ static int view_ndof_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *event)
 void IMAGE_OT_view_ndof(wmOperatorType *ot)
 {
        /* identifiers */
-       ot->name= "NDOF Pan/Zoom";
-       ot->idname= "IMAGE_OT_view_ndof";
+       ot->name = "NDOF Pan/Zoom";
+       ot->idname = "IMAGE_OT_view_ndof";
        
        /* api callbacks */
-       ot->invokeview_ndof_invoke;
+       ot->invoke = image_view_ndof_invoke;
 }
 
 /********************** view all operator *********************/
@@ -500,7 +549,7 @@ void IMAGE_OT_view_ndof(wmOperatorType *ot)
  * Default behavior is to reset the position of the image and set the zoom to 1
  * If the image will not fit within the window rectangle, the zoom is adjusted */
 
-static int view_all_exec(bContext *C, wmOperator *UNUSED(op))
+static int image_view_all_exec(bContext *C, wmOperator *UNUSED(op))
 {
        SpaceImage *sima;
        ARegion *ar;
@@ -521,14 +570,14 @@ static int view_all_exec(bContext *C, wmOperator *UNUSED(op))
        width = ar->winrct.xmax - ar->winrct.xmin + 1;
        height = ar->winrct.ymax - ar->winrct.ymin + 1;
 
-       if((w >= width || h >= height) && (width > 0 && height > 0)) {
+       if ((w >= width || h >= height) && (width > 0 && height > 0)) {
                /* find the zoom value that will fit the image in the image space */
                zoomx= width/w;
                zoomy= height/h;
-               sima_zoom_set(sima, ar, 1.0f/power_of_2(1/MIN2(zoomx, zoomy)));
+               sima_zoom_set(sima, ar, 1.0f/power_of_2(1/MIN2(zoomx, zoomy)), NULL);
        }
        else
-               sima_zoom_set(sima, ar, 1.0f);
+               sima_zoom_set(sima, ar, 1.0f, NULL);
 
        sima->xof= sima->yof= 0.0f;
 
@@ -540,17 +589,17 @@ static int view_all_exec(bContext *C, wmOperator *UNUSED(op))
 void IMAGE_OT_view_all(wmOperatorType *ot)
 {
        /* identifiers */
-       ot->name= "View All";
-       ot->idname= "IMAGE_OT_view_all";
+       ot->name = "View All";
+       ot->idname = "IMAGE_OT_view_all";
        
        /* api callbacks */
-       ot->execview_all_exec;
-       ot->poll= space_image_main_area_poll;
+       ot->exec = image_view_all_exec;
+       ot->poll = space_image_main_area_poll;
 }
 
 /********************** view selected operator *********************/
 
-static int view_selected_exec(bContext *C, wmOperator *UNUSED(op))
+static int image_view_selected_exec(bContext *C, wmOperator *UNUSED(op))
 {
        SpaceImage *sima;
        ARegion *ar;
@@ -563,7 +612,7 @@ static int view_selected_exec(bContext *C, wmOperator *UNUSED(op))
        /* retrieve state */
        sima= CTX_wm_space_image(C);
        ar= CTX_wm_region(C);
-       scene= (Scene*)CTX_data_scene(C);
+       scene= CTX_data_scene(C);
        obedit= CTX_data_edit_object(C);
 
        ima= ED_space_image(sima);
@@ -574,7 +623,7 @@ static int view_selected_exec(bContext *C, wmOperator *UNUSED(op))
        height= height*aspy;
 
        /* get bounds */
-       if(!ED_uvedit_minmax(scene, ima, obedit, min, max))
+       if (!ED_uvedit_minmax(scene, ima, obedit, min, max))
                return OPERATOR_CANCELLED;
 
        /* adjust offset and zoom */
@@ -585,15 +634,15 @@ static int view_selected_exec(bContext *C, wmOperator *UNUSED(op))
        d[1]= max[1] - min[1];
        size= 0.5f*MAX2(d[0], d[1])*MAX2(width, height)/256.0f;
        
-       if(size<=0.01f) size= 0.01f;
-       sima_zoom_set(sima, ar, 0.7f/size);
+       if (size<=0.01f) size= 0.01f;
+       sima_zoom_set(sima, ar, 0.7f/size, NULL);
 
        ED_region_tag_redraw(CTX_wm_region(C));
        
        return OPERATOR_FINISHED;
 }
 
-static int view_selected_poll(bContext *C)
+static int image_view_selected_poll(bContext *C)
 {
        return (space_image_main_area_poll(C) && ED_operator_uvedit(C));
 }
@@ -601,70 +650,106 @@ static int view_selected_poll(bContext *C)
 void IMAGE_OT_view_selected(wmOperatorType *ot)
 {
        /* identifiers */
-       ot->name= "View Center";
-       ot->idname= "IMAGE_OT_view_selected";
+       ot->name = "View Center";
+       ot->idname = "IMAGE_OT_view_selected";
        
        /* api callbacks */
-       ot->execview_selected_exec;
-       ot->pollview_selected_poll;
+       ot->exec = image_view_selected_exec;
+       ot->poll = image_view_selected_poll;
 }
 
 /********************** view zoom in/out operator *********************/
 
-static int view_zoom_in_exec(bContext *C, wmOperator *UNUSED(op))
+static int image_view_zoom_in_exec(bContext *C, wmOperator *op)
 {
        SpaceImage *sima= CTX_wm_space_image(C);
        ARegion *ar= CTX_wm_region(C);
+       float location[2];
+
+       RNA_float_get_array(op->ptr, "location", location);
 
-       sima_zoom_set_factor(sima, ar, 1.25f);
+       sima_zoom_set_factor(sima, ar, 1.25f, location);
 
        ED_region_tag_redraw(CTX_wm_region(C));
        
        return OPERATOR_FINISHED;
 }
 
+static int image_view_zoom_in_invoke(bContext *C, wmOperator *op, wmEvent *event)
+{
+       ARegion *ar= CTX_wm_region(C);
+       float location[2];
+
+       UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1], &location[0], &location[1]);
+       RNA_float_set_array(op->ptr, "location", location);
+
+       return image_view_zoom_in_exec(C, op);
+}
+
 void IMAGE_OT_view_zoom_in(wmOperatorType *ot)
 {
        /* identifiers */
-       ot->name= "View Zoom In";
-       ot->idname= "IMAGE_OT_view_zoom_in";
+       ot->name = "View Zoom In";
+       ot->idname = "IMAGE_OT_view_zoom_in";
        
        /* api callbacks */
-       ot->exec= view_zoom_in_exec;
-       ot->poll= space_image_main_area_poll;
+       ot->invoke = image_view_zoom_in_invoke;
+       ot->exec = image_view_zoom_in_exec;
+       ot->poll = space_image_main_area_poll;
+
+       /* properties */
+       RNA_def_float_vector(ot->srna, "location", 2, NULL, -FLT_MAX, FLT_MAX, "Location", "Cursor location in screen coordinates", -10.0f, 10.0f);
 }
 
-static int view_zoom_out_exec(bContext *C, wmOperator *UNUSED(op))
+static int image_view_zoom_out_exec(bContext *C, wmOperator *op)
 {
        SpaceImage *sima= CTX_wm_space_image(C);
        ARegion *ar= CTX_wm_region(C);
+       float location[2];
 
-       sima_zoom_set_factor(sima, ar, 0.8f);
+       RNA_float_get_array(op->ptr, "location", location);
+
+       sima_zoom_set_factor(sima, ar, 0.8f, location);
 
        ED_region_tag_redraw(CTX_wm_region(C));
        
        return OPERATOR_FINISHED;
 }
 
+static int image_view_zoom_out_invoke(bContext *C, wmOperator *op, wmEvent *event)
+{
+       ARegion *ar= CTX_wm_region(C);
+       float location[2];
+
+       UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1], &location[0], &location[1]);
+       RNA_float_set_array(op->ptr, "location", location);
+
+       return image_view_zoom_out_exec(C, op);
+}
+
 void IMAGE_OT_view_zoom_out(wmOperatorType *ot)
 {
        /* identifiers */
-       ot->name= "View Zoom Out";
-       ot->idname= "IMAGE_OT_view_zoom_out";
+       ot->name = "View Zoom Out";
+       ot->idname = "IMAGE_OT_view_zoom_out";
        
        /* api callbacks */
-       ot->exec= view_zoom_out_exec;
-       ot->poll= space_image_main_area_poll;
+       ot->invoke = image_view_zoom_out_invoke;
+       ot->exec = image_view_zoom_out_exec;
+       ot->poll = space_image_main_area_poll;
+
+       /* properties */
+       RNA_def_float_vector(ot->srna, "location", 2, NULL, -FLT_MAX, FLT_MAX, "Location", "Cursor location in screen coordinates", -10.0f, 10.0f);
 }
 
 /********************** view zoom ratio operator *********************/
 
-static int view_zoom_ratio_exec(bContext *C, wmOperator *op)
+static int image_view_zoom_ratio_exec(bContext *C, wmOperator *op)
 {
        SpaceImage *sima= CTX_wm_space_image(C);
        ARegion *ar= CTX_wm_region(C);
 
-       sima_zoom_set(sima, ar, RNA_float_get(op->ptr, "ratio"));
+       sima_zoom_set(sima, ar, RNA_float_get(op->ptr, "ratio"), NULL);
        
        /* ensure pixel exact locations for draw */
        sima->xof= (int)sima->xof;
@@ -672,7 +757,7 @@ static int view_zoom_ratio_exec(bContext *C, wmOperator *op)
 
        /* XXX notifier? */
 #if 0
-       if(image_preview_active(curarea, NULL, NULL)) {
+       if (image_preview_active(curarea, NULL, NULL)) {
                /* recalculates new preview rect */
                scrarea_do_windraw(curarea);
                image_preview_event(2);
@@ -687,53 +772,19 @@ static int view_zoom_ratio_exec(bContext *C, wmOperator *op)
 void IMAGE_OT_view_zoom_ratio(wmOperatorType *ot)
 {
        /* identifiers */
-       ot->name= "View Zoom Ratio";
-       ot->idname= "IMAGE_OT_view_zoom_ratio";
+       ot->name = "View Zoom Ratio";
+       ot->idname = "IMAGE_OT_view_zoom_ratio";
        
        /* api callbacks */
-       ot->execview_zoom_ratio_exec;
-       ot->poll= space_image_main_area_poll;
+       ot->exec = image_view_zoom_ratio_exec;
+       ot->poll = space_image_main_area_poll;
        
        /* properties */
        RNA_def_float(ot->srna, "ratio", 0.0f, 0.0f, FLT_MAX,
-               "Ratio", "Zoom ratio, 1.0 is 1:1, higher is zoomed in, lower is zoomed out.", -FLT_MAX, FLT_MAX);
+               "Ratio", "Zoom ratio, 1.0 is 1:1, higher is zoomed in, lower is zoomed out", -FLT_MAX, FLT_MAX);
 }
 
 /**************** load/replace/save callbacks ******************/
-
-/* XXX make dynamic */
-static const EnumPropertyItem image_file_type_items[] = {
-               {R_TARGA, "TARGA", 0, "Targa", ""},
-               {R_RAWTGA, "TARGA RAW", 0, "Targa Raw", ""},
-               {R_PNG, "PNG", 0, "PNG", ""},
-#ifdef WITH_DDS
-               {R_DDS, "DDS", 0, "DirectDraw Surface", ""},
-#endif
-               {R_BMP, "BMP", 0, "BMP", ""},
-               {R_JPEG90, "JPEG", 0, "Jpeg", ""},
-#ifdef WITH_OPENJPEG
-               {R_JP2, "JPEG_2000", 0, "Jpeg 2000", ""},
-#endif
-               {R_IRIS, "IRIS", 0, "Iris", ""},
-#ifdef WITH_TIFF
-               {R_TIFF, "TIFF", 0, "Tiff", ""},
-#endif
-#ifdef WITH_DDS
-               {R_RADHDR, "RADIANCE_HDR", 0, "Radiance HDR", ""},
-#endif
-#ifdef WITH_CINEON
-               {R_CINEON, "CINEON", 0, "Cineon", ""},
-               {R_DPX, "DPX", 0, "DPX", ""},
-#endif
-#ifdef WITH_OPENEXR
-               {R_OPENEXR, "OPENEXR", 0, "OpenEXR", ""},
-       /* saving sequences of multilayer won't work, they copy buffers  */
-       /*if(ima->source==IMA_SRC_SEQUENCE && ima->type==IMA_TYPE_MULTILAYER);
-       else*/
-               {R_MULTILAYER, "MULTILAYER", 0, "MultiLayer", ""},
-#endif 
-               {0, NULL, 0, NULL, NULL}};
-
 static void image_filesel(bContext *C, wmOperator *op, const char *path)
 {
        RNA_string_set(op->ptr, "filepath", path);
@@ -742,7 +793,7 @@ static void image_filesel(bContext *C, wmOperator *op, const char *path)
 
 /******************** open image operator ********************/
 
-static void open_init(bContext *C, wmOperator *op)
+static void image_open_init(bContext *C, wmOperator *op)
 {
        PropertyPointerRNA *pprop;
 
@@ -750,14 +801,14 @@ static void open_init(bContext *C, wmOperator *op)
        uiIDContextProperty(C, &pprop->ptr, &pprop->prop);
 }
 
-static int open_cancel(bContext *UNUSED(C), wmOperator *op)
+static int image_open_cancel(bContext *UNUSED(C), wmOperator *op)
 {
        MEM_freeN(op->customdata);
        op->customdata= NULL;
        return OPERATOR_CANCELLED;
 }
 
-static int open_exec(bContext *C, wmOperator *op)
+static int image_open_exec(bContext *C, wmOperator *op)
 {
        SpaceImage *sima= CTX_wm_space_image(C); /* XXX other space types can call */
        Scene *scene= CTX_data_scene(C);
@@ -775,19 +826,19 @@ static int open_exec(bContext *C, wmOperator *op)
 
        ima= BKE_add_image_file(str);
 
-       if(!ima) {
-               if(op->customdata) MEM_freeN(op->customdata);
-               BKE_reportf(op->reports, RPT_ERROR, "Can't read: \"%s\", %s.", str, errno ? strerror(errno) : "Unsupported image format");
+       if (!ima) {
+               if (op->customdata) MEM_freeN(op->customdata);
+               BKE_reportf(op->reports, RPT_ERROR, "Can't read: \"%s\", %s", str, errno ? strerror(errno) : "Unsupported image format");
                return OPERATOR_CANCELLED;
        }
        
-       if(!op->customdata)
-               open_init(C, op);
+       if (!op->customdata)
+               image_open_init(C, op);
 
        /* hook into UI */
        pprop= op->customdata;
 
-       if(pprop->prop) {
+       if (pprop->prop) {
                /* when creating new ID blocks, use is already 1, but RNA
                 * pointer se also increases user, so this compensates it */
                ima->id.us--;
@@ -796,19 +847,19 @@ static int open_exec(bContext *C, wmOperator *op)
                RNA_property_pointer_set(&pprop->ptr, pprop->prop, idptr);
                RNA_property_update(C, &pprop->ptr, pprop->prop);
        }
-       else if(sima) {
-               ED_space_image_set(C, sima, scene, obedit, ima);
+       else if (sima) {
+               ED_space_image_set(sima, scene, obedit, ima);
                iuser= &sima->iuser;
        }
        else {
                Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
-               if(tex && tex->type==TEX_IMAGE)
+               if (tex && tex->type==TEX_IMAGE)
                        iuser= &tex->iuser;
                
        }
        
        /* initialize because of new image */
-       if(iuser) {
+       if (iuser) {
                iuser->sfra= 1;
                iuser->offset= 0;
                iuser->fie_ima= 2;
@@ -825,33 +876,29 @@ static int open_exec(bContext *C, wmOperator *op)
        return OPERATOR_FINISHED;
 }
 
-static int open_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
+static int image_open_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
 {
        SpaceImage *sima= CTX_wm_space_image(C); /* XXX other space types can call */
        char *path=U.textudir;
        Image *ima= NULL;
 
-       if(sima) {
+       if (sima) {
                ima= sima->image;
        }
 
        if (ima==NULL) {
                Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
-               if(tex && tex->type==TEX_IMAGE)
+               if (tex && tex->type==TEX_IMAGE)
                        ima= tex->ima;
        }
 
-       if(ima)
+       if (ima)
                path= ima->name;
-       
 
-       if(!RNA_property_is_set(op->ptr, "relative_path"))
-               RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS);
-
-       if(RNA_property_is_set(op->ptr, "filepath"))
-               return open_exec(C, op);
+       if (RNA_struct_property_is_set(op->ptr, "filepath"))
+               return image_open_exec(C, op);
        
-       open_init(C, op);
+       image_open_init(C, op);
 
        image_filesel(C, op, path);
 
@@ -862,34 +909,41 @@ static int open_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
 void IMAGE_OT_open(wmOperatorType *ot)
 {
        /* identifiers */
-       ot->name= "Open Image";
-       ot->description= "Open image";
-       ot->idname= "IMAGE_OT_open";
+       ot->name = "Open Image";
+       ot->description = "Open image";
+       ot->idname = "IMAGE_OT_open";
        
        /* api callbacks */
-       ot->execopen_exec;
-       ot->invokeopen_invoke;
-       ot->cancelopen_cancel;
+       ot->exec = image_open_exec;
+       ot->invoke = image_open_invoke;
+       ot->cancel = image_open_cancel;
 
        /* flags */
-       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+       ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
 
        /* properties */
-       WM_operator_properties_filesel(ot, FOLDERFILE|IMAGEFILE|MOVIEFILE, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_FILEPATH|WM_FILESEL_RELPATH);
+       WM_operator_properties_filesel(ot, FOLDERFILE|IMAGEFILE|MOVIEFILE, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_FILEPATH|WM_FILESEL_RELPATH, FILE_DEFAULTDISPLAY);
 }
 
 /******************** replace image operator ********************/
 
-static int replace_exec(bContext *C, wmOperator *op)
+static int image_replace_exec(bContext *C, wmOperator *op)
 {
        SpaceImage *sima= CTX_wm_space_image(C);
        char str[FILE_MAX];
 
-       if(!sima->image)
+       if (!sima->image)
                return OPERATOR_CANCELLED;
        
        RNA_string_get(op->ptr, "filepath", str);
-       BLI_strncpy(sima->image->name, str, sizeof(sima->image->name)); /* we cant do much if the str is longer then 240 :/ */
+
+       /* we cant do much if the str is longer then FILE_MAX :/ */
+       BLI_strncpy(sima->image->name, str, sizeof(sima->image->name));
+
+       if (BLI_testextensie_array(str, imb_ext_movie))
+               sima->image->source= IMA_SRC_MOVIE;
+       else
+               sima->image->source= IMA_SRC_FILE;
 
        /* XXX unpackImage frees image buffers */
        ED_preview_kill_jobs(C);
@@ -900,17 +954,17 @@ static int replace_exec(bContext *C, wmOperator *op)
        return OPERATOR_FINISHED;
 }
 
-static int replace_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
+static int image_replace_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
 {
        SpaceImage *sima= CTX_wm_space_image(C);
 
-       if(!sima->image)
+       if (!sima->image)
                return OPERATOR_CANCELLED;
 
-       if(RNA_property_is_set(op->ptr, "filepath"))
-               return replace_exec(C, op);
+       if (RNA_struct_property_is_set(op->ptr, "filepath"))
+               return image_replace_exec(C, op);
 
-       if(!RNA_property_is_set(op->ptr, "relative_path"))
+       if (!RNA_struct_property_is_set(op->ptr, "relative_path"))
                RNA_boolean_set(op->ptr, "relative_path", (strncmp(sima->image->name, "//", 2))==0);
 
        image_filesel(C, op, sima->image->name);
@@ -921,87 +975,115 @@ static int replace_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
 void IMAGE_OT_replace(wmOperatorType *ot)
 {
        /* identifiers */
-       ot->name= "Replace Image";
-       ot->idname= "IMAGE_OT_replace";
+       ot->name = "Replace Image";
+       ot->idname = "IMAGE_OT_replace";
        
        /* api callbacks */
-       ot->execreplace_exec;
-       ot->invokereplace_invoke;
-       ot->poll= space_image_poll;
+       ot->exec = image_replace_exec;
+       ot->invoke = image_replace_invoke;
+       ot->poll = space_image_poll;
 
        /* flags */
-       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+       ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
 
        /* properties */
-       WM_operator_properties_filesel(ot, FOLDERFILE|IMAGEFILE|MOVIEFILE, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_FILEPATH|WM_FILESEL_RELPATH);
+       WM_operator_properties_filesel(ot, FOLDERFILE|IMAGEFILE|MOVIEFILE, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_FILEPATH|WM_FILESEL_RELPATH, FILE_DEFAULTDISPLAY);
 }
 
 /******************** save image as operator ********************/
 
 typedef struct {
        /* matching scene->r settings */
-       short planes, imtype, subimtype, quality;
+       //short planes, imtype, subimtype, quality;
+       ImageFormatData im_format;
        char filepath[FILE_MAX]; /* keep absolute */
 } SaveImageOptions;
 
 static void save_image_options_defaults(SaveImageOptions *simopts)
 {
-       simopts->planes= R_PLANES24;
-       simopts->imtype= R_PNG;
-       simopts->subimtype= 0;
-       simopts->quality= 90;
+       memset(&simopts->im_format, 0, sizeof(simopts->im_format));
+       simopts->im_format.planes= R_IMF_PLANES_RGB;
+       simopts->im_format.imtype= R_IMF_IMTYPE_PNG;
+       simopts->im_format.quality= 90;
+       simopts->im_format.compress= 90;
        simopts->filepath[0]= '\0';
 }
 
+static char imtype_best_depth(ImBuf *ibuf, const char imtype)
+{
+       const char depth_ok= BKE_imtype_valid_depths(imtype);
+
+       if (ibuf->rect_float) {
+               if (depth_ok & R_IMF_CHAN_DEPTH_32) return R_IMF_CHAN_DEPTH_32;
+               if (depth_ok & R_IMF_CHAN_DEPTH_24) return R_IMF_CHAN_DEPTH_24;
+               if (depth_ok & R_IMF_CHAN_DEPTH_16) return R_IMF_CHAN_DEPTH_16;
+               if (depth_ok & R_IMF_CHAN_DEPTH_12) return R_IMF_CHAN_DEPTH_12;
+               return R_IMF_CHAN_DEPTH_8;
+       }
+       else {
+               if (depth_ok & R_IMF_CHAN_DEPTH_8) return R_IMF_CHAN_DEPTH_8;
+               if (depth_ok & R_IMF_CHAN_DEPTH_12) return R_IMF_CHAN_DEPTH_12;
+               if (depth_ok & R_IMF_CHAN_DEPTH_16) return R_IMF_CHAN_DEPTH_16;
+               if (depth_ok & R_IMF_CHAN_DEPTH_24) return R_IMF_CHAN_DEPTH_24;
+               if (depth_ok & R_IMF_CHAN_DEPTH_32) return R_IMF_CHAN_DEPTH_32;
+               return R_IMF_CHAN_DEPTH_8; /* fallback, should not get here */
+       }
+}
+
 static int save_image_options_init(SaveImageOptions *simopts, SpaceImage *sima, Scene *scene, const short guess_path)
 {
        void *lock;
        ImBuf *ibuf= ED_space_image_acquire_buffer(sima, &lock);
 
-       if(ibuf) {
+       if (ibuf) {
                Image *ima= sima->image;
-               RenderResult *rr= BKE_image_acquire_renderresult(scene, ima);
-
-               simopts->planes= ibuf->depth;
+               short is_depth_set= FALSE;
 
-               /* cant save multilayer sequence, ima->rr isn't valid for a specific frame */
-               if(rr && !(ima->source==IMA_SRC_SEQUENCE && ima->type==IMA_TYPE_MULTILAYER))
-                       simopts->imtype= R_MULTILAYER;
-               else if(ima->type==IMA_TYPE_R_RESULT)
-                       simopts->imtype= scene->r.imtype;
-               else if (ima->source == IMA_SRC_GENERATED)
-                       simopts->imtype= R_PNG;
-               else
-                       simopts->imtype= BKE_ftype_to_imtype(ibuf->ftype);
+               simopts->im_format.planes= ibuf->planes;
 
-               simopts->subimtype= scene->r.subimtype; /* XXX - this is lame, we need to make these available too! */
-               simopts->quality= ibuf->ftype & 0xff;
+               if (ELEM(ima->type, IMA_TYPE_R_RESULT, IMA_TYPE_COMPOSITE)) {
+                       /* imtype */
+                       simopts->im_format= scene->r.im_format;
+                       is_depth_set= TRUE;
+               }
+               else if (ima->source == IMA_SRC_GENERATED) {
+                       simopts->im_format.imtype= R_IMF_IMTYPE_PNG;
+               }
+               else {
+                       simopts->im_format.imtype= BKE_ftype_to_imtype(ibuf->ftype);
+               }
+               //simopts->subimtype= scene->r.subimtype; /* XXX - this is lame, we need to make these available too! */
+               simopts->im_format.quality= ibuf->ftype & 0xff;
 
                BLI_strncpy(simopts->filepath, ibuf->name, sizeof(simopts->filepath));
 
                /* sanitize all settings */
 
-               /* unlikely but just incase */
-               if (ELEM3(simopts->planes, R_PLANESBW, R_PLANES24, R_PLANES32) == 0) {
-                       simopts->planes= R_PLANES32;
+               /* unlikely but just in case */
+               if (ELEM3(simopts->im_format.planes, R_IMF_PLANES_BW, R_IMF_PLANES_RGB, R_IMF_PLANES_RGBA) == 0) {
+                       simopts->im_format.planes= R_IMF_PLANES_RGBA;
+               }
+
+               /* depth, account for float buffer and format support */
+               if (is_depth_set == FALSE) {
+                       simopts->im_format.depth= imtype_best_depth(ibuf, simopts->im_format.imtype);
                }
 
-               /* some formats dont use quality so fallback to scenes quality */
-               if (simopts->quality == 0) {
-                       simopts->quality= scene->r.quality;
+               /* some formats don't use quality so fallback to scenes quality */
+               if (simopts->im_format.quality == 0) {
+                       simopts->im_format.quality= scene->r.im_format.quality;
                }
 
                /* check for empty path */
-               if(guess_path && simopts->filepath[0]==0) {
+               if (guess_path && simopts->filepath[0]==0) {
                        if ( (G.ima[0] == '/') && (G.ima[1] == '/') && (G.ima[2] == '\0') ) {
                                BLI_strncpy(simopts->filepath, "//untitled", FILE_MAX);
-                       } else {
+                       }
+                       else {
                                BLI_strncpy(simopts->filepath, G.ima, FILE_MAX);
                        }
                        BLI_path_abs(simopts->filepath, G.main->name);
                }
-               /* cleanup */
-               BKE_image_release_renderresult(scene, ima);
        }
 
        ED_space_image_release_buffer(sima, lock);
@@ -1011,20 +1093,21 @@ static int save_image_options_init(SaveImageOptions *simopts, SpaceImage *sima,
 
 static void save_image_options_from_op(SaveImageOptions *simopts, wmOperator *op)
 {
-       if (RNA_property_is_set(op->ptr, "color_mode")) simopts->planes= RNA_enum_get(op->ptr, "color_mode");
-       if (RNA_property_is_set(op->ptr, "file_format")) simopts->imtype= RNA_enum_get(op->ptr, "file_format");
-       // if (RNA_property_is_set(op->ptr, "subimtype")) simopts->subimtype= RNA_enum_get(op->ptr, "subimtype"); // XXX
-       if (RNA_property_is_set(op->ptr, "file_quality")) simopts->quality= RNA_int_get(op->ptr, "file_quality");
+       if (op->customdata) {
+               simopts->im_format= *(ImageFormatData *)op->customdata;
+       }
 
-       if (RNA_property_is_set(op->ptr, "filepath")) RNA_string_get(op->ptr, "filepath", simopts->filepath);
+       if (RNA_struct_property_is_set(op->ptr, "filepath")) {
+               RNA_string_get(op->ptr, "filepath", simopts->filepath);
+               BLI_path_abs(simopts->filepath, G.main->name);
+       }
 }
 
 static void save_image_options_to_op(SaveImageOptions *simopts, wmOperator *op)
 {
-       RNA_enum_set(op->ptr, "color_mode", simopts->planes);
-       RNA_enum_set(op->ptr, "file_format", simopts->imtype);
-       // RNA_enum_set(op->ptr, "subimtype", simopts->subimtype);
-       RNA_int_set(op->ptr, "file_quality", simopts->quality);
+       if (op->customdata) {
+               *(ImageFormatData *)op->customdata= simopts->im_format;
+       }
 
        RNA_string_set(op->ptr, "filepath", simopts->filepath);
 }
@@ -1038,7 +1121,7 @@ static void save_image_doit(bContext *C, SpaceImage *sima, wmOperator *op, SaveI
        ImBuf *ibuf= ED_space_image_acquire_buffer(sima, &lock);
 
        if (ibuf) {
-               Main *bmain= CTX_data_main(C);
+               const char *relbase= ID_BLEND_PATH(CTX_data_main(C), &ima->id);
                const short relative= (RNA_struct_find_property(op->ptr, "relative_path") && RNA_boolean_get(op->ptr, "relative_path"));
                const short save_copy= (RNA_struct_find_property(op->ptr, "copy") && RNA_boolean_get(op->ptr, "copy"));
                short ok= FALSE;
@@ -1048,28 +1131,28 @@ static void save_image_doit(bContext *C, SpaceImage *sima, wmOperator *op, SaveI
 
                WM_cursor_wait(1);
 
-               if(ima->type == IMA_TYPE_R_RESULT) {
+               if (ima->type == IMA_TYPE_R_RESULT) {
                        /* enforce user setting for RGB or RGBA, but skip BW */
-                       if(simopts->planes==R_PLANES32) {
-                               ibuf->depth= 32;
+                       if (simopts->im_format.planes==R_IMF_PLANES_RGBA) {
+                               ibuf->planes= R_IMF_PLANES_RGBA;
                        }
-                       else if(simopts->planes==R_PLANES24) {
-                               ibuf->depth= 24;
+                       else if (simopts->im_format.planes==R_IMF_PLANES_RGB) {
+                               ibuf->planes= R_IMF_PLANES_RGB;
                        }
                }
                else {
                        /* TODO, better solution, if a 24bit image is painted onto it may contain alpha */
-                       if(ibuf->userflags & IB_BITMAPDIRTY) { /* it has been painted onto */
+                       if (ibuf->userflags & IB_BITMAPDIRTY) { /* it has been painted onto */
                                /* checks each pixel, not ideal */
-                               ibuf->depth= BKE_alphatest_ibuf(ibuf) ? 32 : 24;
+                               ibuf->planes= BKE_alphatest_ibuf(ibuf) ? 32 : 24;
                        }
                }
                
-               if(simopts->imtype==R_MULTILAYER) {
+               if (simopts->im_format.imtype==R_IMF_IMTYPE_MULTILAYER) {
                        Scene *scene= CTX_data_scene(C);
                        RenderResult *rr= BKE_image_acquire_renderresult(scene, ima);
-                       if(rr) {
-                               RE_WriteRenderResult(op->reports, rr, simopts->filepath, simopts->quality);
+                       if (rr) {
+                               RE_WriteRenderResult(op->reports, rr, simopts->filepath, simopts->im_format.quality);
                                ok= TRUE;
                        }
                        else {
@@ -1077,47 +1160,46 @@ static void save_image_doit(bContext *C, SpaceImage *sima, wmOperator *op, SaveI
                        }
                        BKE_image_release_renderresult(scene, ima);
                }
-               else if (BKE_write_ibuf(ibuf, simopts->filepath, simopts->imtype, simopts->subimtype, simopts->quality)) {
-                       ok= TRUE;
-               }
-
-               if(ok)  {
-                       if(relative)
-                               BLI_path_rel(simopts->filepath, bmain->name); /* only after saving */
-
-                       if(ibuf->name[0]==0) {
-                               BLI_strncpy(ibuf->name, simopts->filepath, sizeof(ibuf->name));
-                               BLI_strncpy(ima->name, simopts->filepath, sizeof(ima->name));
+               else {
+                       if (BKE_write_ibuf_as(ibuf, simopts->filepath, &simopts->im_format, save_copy)) {
+                               ok= TRUE;
                        }
+               }
 
-                       if(!save_copy) {
-                               if(do_newpath) {
-                                       BLI_strncpy(ima->name, simopts->filepath, sizeof(ima->name));
+               if (ok) {
+                       if (!save_copy) {
+                               if (do_newpath) {
                                        BLI_strncpy(ibuf->name, simopts->filepath, sizeof(ibuf->name));
+                                       BLI_strncpy(ima->name, simopts->filepath, sizeof(ima->name));
                                }
 
                                ibuf->userflags &= ~IB_BITMAPDIRTY;
 
                                /* change type? */
-                               if(ima->type==IMA_TYPE_R_RESULT) {
+                               if (ima->type==IMA_TYPE_R_RESULT) {
                                        ima->type= IMA_TYPE_IMAGE;
 
                                        /* workaround to ensure the render result buffer is no longer used
                                         * by this image, otherwise can crash when a new render result is
                                         * created. */
-                                       if(ibuf->rect && !(ibuf->mall & IB_rect))
+                                       if (ibuf->rect && !(ibuf->mall & IB_rect))
                                                imb_freerectImBuf(ibuf);
-                                       if(ibuf->rect_float && !(ibuf->mall & IB_rectfloat))
+                                       if (ibuf->rect_float && !(ibuf->mall & IB_rectfloat))
                                                imb_freerectfloatImBuf(ibuf);
-                                       if(ibuf->zbuf && !(ibuf->mall & IB_zbuf))
+                                       if (ibuf->zbuf && !(ibuf->mall & IB_zbuf))
                                                IMB_freezbufImBuf(ibuf);
-                                       if(ibuf->zbuf_float && !(ibuf->mall & IB_zbuffloat))
+                                       if (ibuf->zbuf_float && !(ibuf->mall & IB_zbuffloat))
                                                IMB_freezbuffloatImBuf(ibuf);
                                }
-                               if( ELEM(ima->source, IMA_SRC_GENERATED, IMA_SRC_VIEWER)) {
+                               if ( ELEM(ima->source, IMA_SRC_GENERATED, IMA_SRC_VIEWER)) {
                                        ima->source= IMA_SRC_FILE;
                                        ima->type= IMA_TYPE_IMAGE;
                                }
+
+                               /* only image path, never ibuf */
+                               if (relative) {
+                                       BLI_path_rel(ima->name, relbase); /* only after saving */
+                               }
                        }
                }
                else {
@@ -1133,94 +1215,135 @@ static void save_image_doit(bContext *C, SpaceImage *sima, wmOperator *op, SaveI
        ED_space_image_release_buffer(sima, lock);
 }
 
-static int save_as_exec(bContext *C, wmOperator *op)
+static void image_save_as_free(wmOperator *op)
+{
+       if (op->customdata) {
+               MEM_freeN(op->customdata);
+               op->customdata= NULL;
+       }
+}
+
+static int image_save_as_exec(bContext *C, wmOperator *op)
 {
        SpaceImage *sima= CTX_wm_space_image(C);
        SaveImageOptions simopts;
 
-       /* just incase to initialize values,
-        * these should be set on invoke or by the caller. */
        save_image_options_defaults(&simopts);
+
+       /* just in case to initialize values,
+        * these should be set on invoke or by the caller. */
+       save_image_options_init(&simopts, sima, CTX_data_scene(C), 0);
+
        save_image_options_from_op(&simopts, op);
 
        save_image_doit(C, sima, op, &simopts, TRUE);
 
+       image_save_as_free(op);
        return OPERATOR_FINISHED;
 }
 
 
-static int save_as_check(bContext *UNUSED(C), wmOperator *op)
+static int image_save_as_check(bContext *UNUSED(C), wmOperator *op)
 {
+       ImageFormatData *imf= op->customdata;
        char filepath[FILE_MAX];
        RNA_string_get(op->ptr, "filepath", filepath);
-       if(BKE_add_image_extension(filepath, RNA_enum_get(op->ptr, "file_format"))) {
+       if (BKE_add_image_extension(filepath, imf->imtype)) {
                RNA_string_set(op->ptr, "filepath", filepath);
                return TRUE;
        }
        return FALSE;
 }
 
-static int save_as_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
+static int image_save_as_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
 {
        SpaceImage *sima= CTX_wm_space_image(C);
        Image *ima = ED_space_image(sima);
        Scene *scene= CTX_data_scene(C);
        SaveImageOptions simopts;
 
-       if(!RNA_property_is_set(op->ptr, "relative_path"))
-               RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS);
-
-       if(RNA_property_is_set(op->ptr, "filepath"))
-               return save_as_exec(C, op);
+       if (RNA_struct_property_is_set(op->ptr, "filepath"))
+               return image_save_as_exec(C, op);
 
        if (save_image_options_init(&simopts, sima, scene, TRUE) == 0)
                return OPERATOR_CANCELLED;
        save_image_options_to_op(&simopts, op);
 
        /* enable save_copy by default for render results */
-       if(ELEM(ima->type, IMA_TYPE_R_RESULT, IMA_TYPE_COMPOSITE) && !RNA_property_is_set(op->ptr, "copy")) {
+       if (ELEM(ima->type, IMA_TYPE_R_RESULT, IMA_TYPE_COMPOSITE) && !RNA_struct_property_is_set(op->ptr, "copy")) {
                RNA_boolean_set(op->ptr, "copy", TRUE);
        }
 
-       // XXX note: we can give default menu enums to operator for this
+       op->customdata= MEM_mallocN(sizeof(simopts.im_format), __func__);
+       memcpy(op->customdata, &simopts.im_format, sizeof(simopts.im_format));
+
        image_filesel(C, op, simopts.filepath);
 
        return OPERATOR_RUNNING_MODAL;
 }
 
+static int image_save_as_cancel(bContext *UNUSED(C), wmOperator *op)
+{
+       image_save_as_free(op);
+
+       return OPERATOR_CANCELLED;
+}
+
+static int image_save_as_draw_check_prop(PointerRNA *ptr, PropertyRNA *prop)
+{
+       const char *prop_id= RNA_property_identifier(prop);
+
+       return !(strcmp(prop_id, "filepath") == 0 ||
+                strcmp(prop_id, "directory") == 0 ||
+                strcmp(prop_id, "filename") == 0 ||
+                /* when saving a copy, relative path has no effect */
+                ((strcmp(prop_id, "relative_path") == 0) && RNA_boolean_get(ptr, "copy"))
+                );
+}
+
+static void image_save_as_draw(bContext *UNUSED(C), wmOperator *op)
+{
+       uiLayout *layout= op->layout;
+       ImageFormatData *imf= op->customdata;
+       PointerRNA ptr;
+
+       /* image template */
+       RNA_pointer_create(NULL, &RNA_ImageFormatSettings, imf, &ptr);
+       uiTemplateImageSettings(layout, &ptr);
+
+       /* main draw call */
+       RNA_pointer_create(NULL, op->type->srna, op->properties, &ptr);
+       uiDefAutoButsRNA(layout, &ptr, image_save_as_draw_check_prop, '\0');
+}
+
 void IMAGE_OT_save_as(wmOperatorType *ot)
 {
-       PropertyRNA *prop;
+//     PropertyRNA *prop;
 
        /* identifiers */
-       ot->name= "Save As Image";
-       ot->idname= "IMAGE_OT_save_as";
+       ot->name = "Save As Image";
+       ot->idname = "IMAGE_OT_save_as";
        
        /* api callbacks */
-       ot->exec= save_as_exec;
-       ot->check= save_as_check;
-       ot->invoke= save_as_invoke;
-       ot->poll= space_image_buffer_exists_poll;
+       ot->exec = image_save_as_exec;
+       ot->check = image_save_as_check;
+       ot->invoke = image_save_as_invoke;
+       ot->cancel = image_save_as_cancel;
+       ot->ui = image_save_as_draw;
+       ot->poll = space_image_buffer_exists_poll;
 
        /* flags */
-       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+       ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
 
        /* properties */
-
-       /* format options */
-       RNA_def_enum(ot->srna, "file_format", image_file_type_items, R_PNG, "File Type", "File type to save image as.");
-       RNA_def_enum(ot->srna, "color_mode", image_color_mode_items, R_PLANES24, "Channels", "Image channels to save");
-       prop= RNA_def_int(ot->srna, "file_quality", 90, 0, 100, "Quality", "", 0, 100);
-       RNA_def_property_subtype(prop, PROP_PERCENTAGE);
-
-       WM_operator_properties_filesel(ot, FOLDERFILE|IMAGEFILE|MOVIEFILE, FILE_SPECIAL, FILE_SAVE, WM_FILESEL_FILEPATH|WM_FILESEL_RELPATH);
-
        RNA_def_boolean(ot->srna, "copy", 0, "Copy", "Create a new image file without modifying the current image in blender");
+
+       WM_operator_properties_filesel(ot, FOLDERFILE|IMAGEFILE|MOVIEFILE, FILE_SPECIAL, FILE_SAVE, WM_FILESEL_FILEPATH|WM_FILESEL_RELPATH, FILE_DEFAULTDISPLAY);
 }
 
 /******************** save image operator ********************/
 
-static int save_exec(bContext *C, wmOperator *op)
+static int image_save_exec(bContext *C, wmOperator *op)
 {
        SpaceImage *sima= CTX_wm_space_image(C);
        Scene *scene= CTX_data_scene(C);
@@ -1230,11 +1353,11 @@ static int save_exec(bContext *C, wmOperator *op)
                return OPERATOR_CANCELLED;
        save_image_options_from_op(&simopts, op);
 
-       if (BLI_exists(simopts.filepath) && BLI_is_writable(simopts.filepath)) {
+       if (BLI_exists(simopts.filepath) && BLI_file_is_writable(simopts.filepath)) {
                save_image_doit(C, sima, op, &simopts, FALSE);
        }
        else {
-               BKE_reportf(op->reports, RPT_ERROR, "Can not save image, path '%s' is not writable.", simopts.filepath);
+               BKE_reportf(op->reports, RPT_ERROR, "Can not save image, path '%s' is not writable", simopts.filepath);
                return OPERATOR_CANCELLED;
        }
 
@@ -1244,20 +1367,20 @@ static int save_exec(bContext *C, wmOperator *op)
 void IMAGE_OT_save(wmOperatorType *ot)
 {
        /* identifiers */
-       ot->name= "Save Image";
-       ot->idname= "IMAGE_OT_save";
+       ot->name = "Save Image";
+       ot->idname = "IMAGE_OT_save";
        
        /* api callbacks */
-       ot->execsave_exec;
-       ot->poll= space_image_file_exists_poll;
+       ot->exec = image_save_exec;
+       ot->poll = space_image_file_exists_poll;
 
        /* flags */
-       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+       ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
 }
 
 /******************* save sequence operator ********************/
 
-static int save_sequence_exec(bContext *C, wmOperator *op)
+static int image_save_sequence_exec(bContext *C, wmOperator *op)
 {
        Main *bmain= CTX_data_main(C);
        SpaceImage *sima= CTX_wm_space_image(C);
@@ -1265,32 +1388,32 @@ static int save_sequence_exec(bContext *C, wmOperator *op)
        int tot= 0;
        char di[FILE_MAX], fi[FILE_MAX];
        
-       if(sima->image==NULL)
+       if (sima->image==NULL)
                return OPERATOR_CANCELLED;
 
-       if(sima->image->source!=IMA_SRC_SEQUENCE) {
-               BKE_report(op->reports, RPT_ERROR, "Can only save sequence on image sequences.");
+       if (sima->image->source!=IMA_SRC_SEQUENCE) {
+               BKE_report(op->reports, RPT_ERROR, "Can only save sequence on image sequences");
                return OPERATOR_CANCELLED;
        }
 
-       if(sima->image->type==IMA_TYPE_MULTILAYER) {
-               BKE_report(op->reports, RPT_ERROR, "Can't save multilayer sequences.");
+       if (sima->image->type==IMA_TYPE_MULTILAYER) {
+               BKE_report(op->reports, RPT_ERROR, "Can't save multilayer sequences");
                return OPERATOR_CANCELLED;
        }
        
        /* get total */
-       for(ibuf= sima->image->ibufs.first; ibuf; ibuf= ibuf->next) 
-               if(ibuf->userflags & IB_BITMAPDIRTY)
+       for (ibuf= sima->image->ibufs.first; ibuf; ibuf= ibuf->next)
+               if (ibuf->userflags & IB_BITMAPDIRTY)
                        tot++;
        
-       if(tot==0) {
-               BKE_report(op->reports, RPT_WARNING, "No images have been changed.");
+       if (tot==0) {
+               BKE_report(op->reports, RPT_WARNING, "No images have been changed");
                return OPERATOR_CANCELLED;
        }
 
        /* get a filename for menu */
-       for(ibuf= sima->image->ibufs.first; ibuf; ibuf= ibuf->next) 
-               if(ibuf->userflags & IB_BITMAPDIRTY)
+       for (ibuf= sima->image->ibufs.first; ibuf; ibuf= ibuf->next)
+               if (ibuf->userflags & IB_BITMAPDIRTY)
                        break;
        
        BLI_strncpy(di, ibuf->name, FILE_MAX);
@@ -1298,15 +1421,15 @@ static int save_sequence_exec(bContext *C, wmOperator *op)
        
        BKE_reportf(op->reports, RPT_INFO, "%d Image(s) will be saved in %s", tot, di);
 
-       for(ibuf= sima->image->ibufs.first; ibuf; ibuf= ibuf->next) {
-               if(ibuf->userflags & IB_BITMAPDIRTY) {
+       for (ibuf= sima->image->ibufs.first; ibuf; ibuf= ibuf->next) {
+               if (ibuf->userflags & IB_BITMAPDIRTY) {
                        char name[FILE_MAX];
                        BLI_strncpy(name, ibuf->name, sizeof(name));
                        
                        BLI_path_abs(name, bmain->name);
 
-                       if(0 == IMB_saveiff(ibuf, name, IB_rect | IB_zbuf | IB_zbuffloat)) {
-                               BKE_reportf(op->reports, RPT_ERROR, "Could not write image %s.", name);
+                       if (0 == IMB_saveiff(ibuf, name, IB_rect | IB_zbuf | IB_zbuffloat)) {
+                               BKE_reportf(op->reports, RPT_ERROR, "Could not write image %s", name);
                                break;
                        }
 
@@ -1321,25 +1444,25 @@ static int save_sequence_exec(bContext *C, wmOperator *op)
 void IMAGE_OT_save_sequence(wmOperatorType *ot)
 {
        /* identifiers */
-       ot->name= "Save Sequence";
-       ot->idname= "IMAGE_OT_save_sequence";
+       ot->name = "Save Sequence";
+       ot->idname = "IMAGE_OT_save_sequence";
        
        /* api callbacks */
-       ot->execsave_sequence_exec;
-       ot->poll= space_image_buffer_exists_poll;
+       ot->exec = image_save_sequence_exec;
+       ot->poll = space_image_buffer_exists_poll;
 
        /* flags */
-       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+       ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
 }
 
 /******************** reload image operator ********************/
 
-static int reload_exec(bContext *C, wmOperator *UNUSED(op))
+static int image_reload_exec(bContext *C, wmOperator *UNUSED(op))
 {
        Image *ima= CTX_data_edit_image(C);
        SpaceImage *sima= CTX_wm_space_image(C);
 
-       if(!ima)
+       if (!ima)
                return OPERATOR_CANCELLED;
 
        /* XXX unpackImage frees image buffers */
@@ -1356,14 +1479,14 @@ static int reload_exec(bContext *C, wmOperator *UNUSED(op))
 void IMAGE_OT_reload(wmOperatorType *ot)
 {
        /* identifiers */
-       ot->name= "Reload Image";
-       ot->idname= "IMAGE_OT_reload";
+       ot->name = "Reload Image";
+       ot->idname = "IMAGE_OT_reload";
        
        /* api callbacks */
-       ot->execreload_exec;
+       ot->exec = image_reload_exec;
 
        /* flags */
-       ot->flag= OPTYPE_REGISTER; /* no undo, image buffer is not handled by undo */
+       ot->flag = OPTYPE_REGISTER; /* no undo, image buffer is not handled by undo */
 }
 
 /********************** new image operator *********************/
@@ -1382,7 +1505,7 @@ static int image_new_exec(bContext *C, wmOperator *op)
 
        /* retrieve state */
        sima= CTX_wm_space_image(C);
-       scene= (Scene*)CTX_data_scene(C);
+       scene= CTX_data_scene(C);
        obedit= CTX_data_edit_object(C);
 
        RNA_string_get(op->ptr, "name", name);
@@ -1396,18 +1519,18 @@ static int image_new_exec(bContext *C, wmOperator *op)
        if (!floatbuf && scene->r.color_mgt_flag & R_COLOR_MANAGEMENT)
                linearrgb_to_srgb_v3_v3(color, color);
 
-       if(!alpha)
+       if (!alpha)
                color[3]= 1.0f;
 
        ima = BKE_add_image_size(width, height, name, alpha ? 32 : 24, floatbuf, uvtestgrid, color);
 
-       if(!ima)
+       if (!ima)
                return OPERATOR_CANCELLED;
 
        /* hook into UI */
        uiIDContextProperty(C, &ptr, &prop);
 
-       if(prop) {
+       if (prop) {
                /* when creating new ID blocks, use is already 1, but RNA
                 * pointer se also increases user, so this compensates it */
                ima->id.us--;
@@ -1416,8 +1539,8 @@ static int image_new_exec(bContext *C, wmOperator *op)
                RNA_property_pointer_set(&ptr, prop, idptr);
                RNA_property_update(C, &ptr, prop);
        }
-       else if(sima)
-               ED_space_image_set(C, sima, scene, obedit, ima);
+       else if (sima)
+               ED_space_image_set(sima, scene, obedit, ima);
 
        // XXX other users?
        BKE_image_signal(ima, (sima)? &sima->iuser: NULL, IMA_SIGNAL_USER_NEW_IMAGE);
@@ -1438,26 +1561,26 @@ void IMAGE_OT_new(wmOperatorType *ot)
        static float default_color[4]= {0.0f, 0.0f, 0.0f, 1.0f};
        
        /* identifiers */
-       ot->name= "New Image";
-       ot->description= "Create a new image";
-       ot->idname= "IMAGE_OT_new";
+       ot->name = "New Image";
+       ot->description = "Create a new image";
+       ot->idname = "IMAGE_OT_new";
        
        /* api callbacks */
-       ot->exec= image_new_exec;
-       ot->invoke= image_new_invoke;
+       ot->exec = image_new_exec;
+       ot->invoke = image_new_invoke;
        
        /* flags */
-       ot->flag= OPTYPE_UNDO;
+       ot->flag = OPTYPE_UNDO;
 
        /* properties */
-       RNA_def_string(ot->srna, "name", "untitled", MAX_ID_NAME-2, "Name", "Image datablock name.");
-       RNA_def_int(ot->srna, "width", 1024, 1, INT_MAX, "Width", "Image width.", 1, 16384);
-       RNA_def_int(ot->srna, "height", 1024, 1, INT_MAX, "Height", "Image height.", 1, 16384);
-       prop= RNA_def_float_color(ot->srna, "color", 4, NULL, 0.0f, FLT_MAX, "Color", "Default fill color.", 0.0f, 1.0f);
+       RNA_def_string(ot->srna, "name", "untitled", MAX_ID_NAME-2, "Name", "Image datablock name");
+       RNA_def_int(ot->srna, "width", 1024, 1, INT_MAX, "Width", "Image width", 1, 16384);
+       RNA_def_int(ot->srna, "height", 1024, 1, INT_MAX, "Height", "Image height", 1, 16384);
+       prop= RNA_def_float_color(ot->srna, "color", 4, NULL, 0.0f, FLT_MAX, "Color", "Default fill color", 0.0f, 1.0f);
        RNA_def_property_float_array_default(prop, default_color);
-       RNA_def_boolean(ot->srna, "alpha", 1, "Alpha", "Create an image with an alpha channel.");
-       RNA_def_boolean(ot->srna, "uv_test_grid", 0, "UV Test Grid", "Fill the image with a grid for UV map testing.");
-       RNA_def_boolean(ot->srna, "float", 0, "32 bit Float", "Create image with 32 bit floating point bit depth.");
+       RNA_def_boolean(ot->srna, "alpha", 1, "Alpha", "Create an image with an alpha channel");
+       RNA_def_boolean(ot->srna, "uv_test_grid", 0, "UV Test Grid", "Fill the image with a grid for UV map testing");
+       RNA_def_boolean(ot->srna, "float", 0, "32 bit Float", "Create image with 32 bit floating point bit depth");
 }
 
 /********************* invert operators *********************/
@@ -1467,7 +1590,7 @@ static int image_invert_poll(bContext *C)
        Image *ima= CTX_data_edit_image(C);
        ImBuf *ibuf= BKE_image_get_ibuf(ima, NULL);
        
-       if( ibuf != NULL )
+       if ( ibuf != NULL )
                return 1;
        return 0;
 }
@@ -1485,32 +1608,32 @@ static int image_invert_exec(bContext *C, wmOperator *op)
 
        int i;
 
-       if( ibuf == NULL) // TODO: this should actually never happen, but does for render-results -> cleanup
+       if ( ibuf == NULL) // TODO: this should actually never happen, but does for render-results -> cleanup
                return OPERATOR_CANCELLED;
 
        /* TODO: make this into an IMB_invert_channels(ibuf,r,g,b,a) method!? */
        if (ibuf->rect_float) {
                
                float *fp = (float *) ibuf->rect_float;
-               for( i = ibuf->x * ibuf->y; i > 0; i--, fp+=4 ) {
-                       if( r ) fp[0] = 1.0f - fp[0];
-                       if( g ) fp[1] = 1.0f - fp[1];
-                       if( b ) fp[2] = 1.0f - fp[2];
-                       if( a ) fp[3] = 1.0f - fp[3];
+               for ( i = ibuf->x * ibuf->y; i > 0; i--, fp+=4 ) {
+                       if ( r ) fp[0] = 1.0f - fp[0];
+                       if ( g ) fp[1] = 1.0f - fp[1];
+                       if ( b ) fp[2] = 1.0f - fp[2];
+                       if ( a ) fp[3] = 1.0f - fp[3];
                }
 
-               if(ibuf->rect) {
+               if (ibuf->rect) {
                        IMB_rect_from_float(ibuf);
                }
        }
-       else if(ibuf->rect) {
+       else if (ibuf->rect) {
                
                char *cp = (char *) ibuf->rect;
-               for( i = ibuf->x * ibuf->y; i > 0; i--, cp+=4 ) {
-                       if( r ) cp[0] = 255 - cp[0];
-                       if( g ) cp[1] = 255 - cp[1];
-                       if( b ) cp[2] = 255 - cp[2];
-                       if( a ) cp[3] = 255 - cp[3];
+               for ( i = ibuf->x * ibuf->y; i > 0; i--, cp+=4 ) {
+                       if ( r ) cp[0] = 255 - cp[0];
+                       if ( g ) cp[1] = 255 - cp[1];
+                       if ( b ) cp[2] = 255 - cp[2];
+                       if ( a ) cp[3] = 255 - cp[3];
                }
        }
        else {
@@ -1518,7 +1641,7 @@ static int image_invert_exec(bContext *C, wmOperator *op)
        }
 
        ibuf->userflags |= IB_BITMAPDIRTY;
-       if(ibuf->mipmap[0])
+       if (ibuf->mipmap[0])
                ibuf->userflags |= IB_MIPMAP_INVALID;
 
        WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, ima);
@@ -1528,12 +1651,12 @@ static int image_invert_exec(bContext *C, wmOperator *op)
 void IMAGE_OT_invert(wmOperatorType *ot)
 {
        /* identifiers */
-       ot->name= "Invert Channels";
-       ot->idname= "IMAGE_OT_invert";
+       ot->name = "Invert Channels";
+       ot->idname = "IMAGE_OT_invert";
        
        /* api callbacks */
-       ot->exec= image_invert_exec;
-       ot->poll= image_invert_poll;
+       ot->exec = image_invert_exec;
+       ot->poll = image_invert_poll;
        
        /* properties */
        RNA_def_boolean(ot->srna, "invert_r", 0, "Red", "Invert Red Channel");
@@ -1542,54 +1665,55 @@ void IMAGE_OT_invert(wmOperatorType *ot)
        RNA_def_boolean(ot->srna, "invert_a", 0, "Alpha", "Invert Alpha Channel");
        
        /* flags */
-       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+       ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
 }
 
 /********************* pack operator *********************/
 
-static int pack_test(bContext *C, wmOperator *op)
+static int image_pack_test(bContext *C, wmOperator *op)
 {
        Image *ima= CTX_data_edit_image(C);
        int as_png= RNA_boolean_get(op->ptr, "as_png");
 
-       if(!ima)
+       if (!ima)
                return 0;
-       if(!as_png && ima->packedfile)
+       if (!as_png && ima->packedfile)
                return 0;
 
-       if(ima->source==IMA_SRC_SEQUENCE || ima->source==IMA_SRC_MOVIE) {
-               BKE_report(op->reports, RPT_ERROR, "Packing movies or image sequences not supported.");
+       if (ima->source==IMA_SRC_SEQUENCE || ima->source==IMA_SRC_MOVIE) {
+               BKE_report(op->reports, RPT_ERROR, "Packing movies or image sequences not supported");
                return 0;
        }
 
        return 1;
 }
 
-static int pack_exec(bContext *C, wmOperator *op)
+static int image_pack_exec(bContext *C, wmOperator *op)
 {
+       struct Main *bmain= CTX_data_main(C);
        Image *ima= CTX_data_edit_image(C);
        ImBuf *ibuf= BKE_image_get_ibuf(ima, NULL);
        int as_png= RNA_boolean_get(op->ptr, "as_png");
 
-       if(!pack_test(C, op))
+       if (!image_pack_test(C, op))
                return OPERATOR_CANCELLED;
        
-       if(!as_png && (ibuf && (ibuf->userflags & IB_BITMAPDIRTY))) {
-               BKE_report(op->reports, RPT_ERROR, "Can't pack edited image from disk, only as internal PNG.");
+       if (!as_png && (ibuf && (ibuf->userflags & IB_BITMAPDIRTY))) {
+               BKE_report(op->reports, RPT_ERROR, "Can't pack edited image from disk, only as internal PNG");
                return OPERATOR_CANCELLED;
        }
 
-       if(as_png)
+       if (as_png)
                BKE_image_memorypack(ima);
        else
-               ima->packedfile= newPackedFile(op->reports, ima->name);
+               ima->packedfile= newPackedFile(op->reports, ima->name, ID_BLEND_PATH(bmain, &ima->id));
 
        WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, ima);
        
        return OPERATOR_FINISHED;
 }
 
-static int pack_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
+static int image_pack_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
 {
        Image *ima= CTX_data_edit_image(C);
        ImBuf *ibuf= BKE_image_get_ibuf(ima, NULL);
@@ -1597,10 +1721,10 @@ static int pack_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
        uiLayout *layout;
        int as_png= RNA_boolean_get(op->ptr, "as_png");
 
-       if(!pack_test(C, op))
+       if (!image_pack_test(C, op))
                return OPERATOR_CANCELLED;
        
-       if(!as_png && (ibuf && (ibuf->userflags & IB_BITMAPDIRTY))) {
+       if (!as_png && (ibuf && (ibuf->userflags & IB_BITMAPDIRTY))) {
                pup= uiPupMenuBegin(C, "OK", ICON_QUESTION);
                layout= uiPupMenuLayout(pup);
                uiItemBooleanO(layout, "Can't pack edited image from disk. Pack as internal PNG?", ICON_NONE, op->idname, "as_png", 1);
@@ -1609,25 +1733,25 @@ static int pack_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
                return OPERATOR_CANCELLED;
        }
 
-       return pack_exec(C, op);
+       return image_pack_exec(C, op);
 }
 
 void IMAGE_OT_pack(wmOperatorType *ot)
 {
        /* identifiers */
-       ot->name= "Pack Image";
-       ot->description= "Pack an image as embedded data into the .blend file"; 
-       ot->idname= "IMAGE_OT_pack";
+       ot->name = "Pack Image";
+       ot->description = "Pack an image as embedded data into the .blend file"; 
+       ot->idname = "IMAGE_OT_pack";
        
        /* api callbacks */
-       ot->execpack_exec;
-       ot->invokepack_invoke;
+       ot->exec = image_pack_exec;
+       ot->invoke = image_pack_invoke;
 
        /* flags */
-       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+       ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
 
        /* properties */
-       RNA_def_boolean(ot->srna, "as_png", 0, "Pack As PNG", "Pack image as lossless PNG.");
+       RNA_def_boolean(ot->srna, "as_png", 0, "Pack As PNG", "Pack image as lossless PNG");
 }
 
 /********************* unpack operator *********************/
@@ -1638,23 +1762,23 @@ static int image_unpack_exec(bContext *C, wmOperator *op)
        int method= RNA_enum_get(op->ptr, "method");
 
        /* find the suppplied image by name */
-       if (RNA_property_is_set(op->ptr, "id")) {
+       if (RNA_struct_property_is_set(op->ptr, "id")) {
                char imaname[MAX_ID_NAME-2];
                RNA_string_get(op->ptr, "id", imaname);
                ima = BLI_findstring(&CTX_data_main(C)->image, imaname, offsetof(ID, name) + 2);
                if (!ima) ima = CTX_data_edit_image(C);
        }
        
-       if(!ima || !ima->packedfile)
+       if (!ima || !ima->packedfile)
                return OPERATOR_CANCELLED;
 
-       if(ima->source==IMA_SRC_SEQUENCE || ima->source==IMA_SRC_MOVIE) {
-               BKE_report(op->reports, RPT_ERROR, "Unpacking movies or image sequences not supported.");
+       if (ima->source==IMA_SRC_SEQUENCE || ima->source==IMA_SRC_MOVIE) {
+               BKE_report(op->reports, RPT_ERROR, "Unpacking movies or image sequences not supported");
                return OPERATOR_CANCELLED;
        }
 
-       if(G.fileflags & G_AUTOPACK)
-               BKE_report(op->reports, RPT_WARNING, "AutoPack is enabled, so image will be packed again on file save.");
+       if (G.fileflags & G_AUTOPACK)
+               BKE_report(op->reports, RPT_WARNING, "AutoPack is enabled, so image will be packed again on file save");
        
        /* XXX unpackImage frees image buffers */
        ED_preview_kill_jobs(C);
@@ -1670,19 +1794,19 @@ static int image_unpack_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(even
 {
        Image *ima= CTX_data_edit_image(C);
 
-       if(RNA_property_is_set(op->ptr, "id"))
+       if (RNA_struct_property_is_set(op->ptr, "id"))
                return image_unpack_exec(C, op);
                
-       if(!ima || !ima->packedfile)
+       if (!ima || !ima->packedfile)
                return OPERATOR_CANCELLED;
 
-       if(ima->source==IMA_SRC_SEQUENCE || ima->source==IMA_SRC_MOVIE) {
-               BKE_report(op->reports, RPT_ERROR, "Unpacking movies or image sequences not supported.");
+       if (ima->source==IMA_SRC_SEQUENCE || ima->source==IMA_SRC_MOVIE) {
+               BKE_report(op->reports, RPT_ERROR, "Unpacking movies or image sequences not supported");
                return OPERATOR_CANCELLED;
        }
 
-       if(G.fileflags & G_AUTOPACK)
-               BKE_report(op->reports, RPT_WARNING, "AutoPack is enabled, so image will be packed again on file save.");
+       if (G.fileflags & G_AUTOPACK)
+               BKE_report(op->reports, RPT_WARNING, "AutoPack is enabled, so image will be packed again on file save");
 
        unpack_menu(C, "IMAGE_OT_unpack", ima->id.name+2, ima->name, "textures", ima->packedfile);
 
@@ -1692,20 +1816,20 @@ static int image_unpack_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(even
 void IMAGE_OT_unpack(wmOperatorType *ot)
 {
        /* identifiers */
-       ot->name= "Unpack Image";
-       ot->description= "Save an image packed in the .blend file to disk"; 
-       ot->idname= "IMAGE_OT_unpack";
+       ot->name = "Unpack Image";
+       ot->description = "Save an image packed in the .blend file to disk"; 
+       ot->idname = "IMAGE_OT_unpack";
        
        /* api callbacks */
-       ot->exec= image_unpack_exec;
-       ot->invoke= image_unpack_invoke;
+       ot->exec = image_unpack_exec;
+       ot->invoke = image_unpack_invoke;
 
        /* flags */
-       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+       ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
        
        /* properties */
-       RNA_def_enum(ot->srna, "method", unpack_method_items, PF_USE_LOCAL, "Method", "How to unpack.");
-       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 */
+       RNA_def_enum(ot->srna, "method", unpack_method_items, PF_USE_LOCAL, "Method", "How to unpack");
+       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 */
 }
 
 /******************** sample image operator ********************/
@@ -1716,12 +1840,12 @@ typedef struct ImageSampleInfo {
        int x, y;
        int channels;
 
-       char col[4];
+       unsigned char col[4];
        float colf[4];
        int z;
        float zf;
 
-       char *colp;
+       unsigned char *colp;
        float *colfp;
        int *zp;
        float *zfp;
@@ -1729,16 +1853,16 @@ typedef struct ImageSampleInfo {
        int draw;
 } ImageSampleInfo;
 
-static void sample_draw(const bContext *UNUSED(C), ARegion *ar, void *arg_info)
+static void image_sample_draw(const bContext *UNUSED(C), ARegion *ar, void *arg_info)
 {
        ImageSampleInfo *info= arg_info;
-       if(info->draw) {
+       if (info->draw) {
                /* no color management needed for images (color_manage=0) */
-               draw_image_info(ar, 0, info->channels, info->x, info->y, info->colp, info->colfp, info->zp, info->zfp);
+               ED_image_draw_info(ar, 0, info->channels, info->x, info->y, info->colp, info->colfp, info->zp, info->zfp);
        }
 }
 
-static void sample_apply(bContext *C, wmOperator *op, wmEvent *event)
+static void image_sample_apply(bContext *C, wmOperator *op, wmEvent *event)
 {
        SpaceImage *sima= CTX_wm_space_image(C);
        ARegion *ar= CTX_wm_region(C);
@@ -1747,16 +1871,16 @@ static void sample_apply(bContext *C, wmOperator *op, wmEvent *event)
        ImageSampleInfo *info= op->customdata;
        float fx, fy;
        
-       if(ibuf == NULL) {
+       if (ibuf == NULL) {
                ED_space_image_release_buffer(sima, lock);
                return;
        }
 
        UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1], &fx, &fy);
 
-       if(fx>=0.0f && fy>=0.0f && fx<1.0f && fy<1.0f) {
+       if (fx>=0.0f && fy>=0.0f && fx<1.0f && fy<1.0f) {
                float *fp;
-               char *cp;
+               unsigned char *cp;
                int x= (int)(fx*ibuf->x), y= (int)(fy*ibuf->y);
 
                CLAMP(x, 0, ibuf->x-1);
@@ -1772,8 +1896,8 @@ static void sample_apply(bContext *C, wmOperator *op, wmEvent *event)
                info->zp= NULL;
                info->zfp= NULL;
                
-               if(ibuf->rect) {
-                       cp= (char *)(ibuf->rect + y*ibuf->x + x);
+               if (ibuf->rect) {
+                       cp= (unsigned char *)(ibuf->rect + y*ibuf->x + x);
 
                        info->col[0]= cp[0];
                        info->col[1]= cp[1];
@@ -1787,7 +1911,7 @@ static void sample_apply(bContext *C, wmOperator *op, wmEvent *event)
                        info->colf[3]= (float)cp[3]/255.0f;
                        info->colfp= info->colf;
                }
-               if(ibuf->rect_float) {
+               if (ibuf->rect_float) {
                        fp= (ibuf->rect_float + (ibuf->channels)*(y*ibuf->x + x));
 
                        info->colf[0]= fp[0];
@@ -1797,28 +1921,28 @@ static void sample_apply(bContext *C, wmOperator *op, wmEvent *event)
                        info->colfp= info->colf;
                }
 
-               if(ibuf->zbuf) {
+               if (ibuf->zbuf) {
                        info->z= ibuf->zbuf[y*ibuf->x + x];
                        info->zp= &info->z;
                }
-               if(ibuf->zbuf_float) {
+               if (ibuf->zbuf_float) {
                        info->zf= ibuf->zbuf_float[y*ibuf->x + x];
                        info->zfp= &info->zf;
                }
                
-               if(sima->cumap && ibuf->channels==4) {
+               if (sima->cumap && ibuf->channels==4) {
                        /* we reuse this callback for set curves point operators */
-                       if(RNA_struct_find_property(op->ptr, "point")) {
+                       if (RNA_struct_find_property(op->ptr, "point")) {
                                int point= RNA_enum_get(op->ptr, "point");
 
-                               if(point == 1) {
+                               if (point == 1) {
                                        curvemapping_set_black_white(sima->cumap, NULL, info->colfp);
-                                       if(ibuf->rect_float)
+                                       if (ibuf->rect_float)
                                                curvemapping_do_ibuf(sima->cumap, ibuf);
                                }
-                               else if(point == 0) {
+                               else if (point == 0) {
                                        curvemapping_set_black_white(sima->cumap, info->colfp, NULL);
-                                       if(ibuf->rect_float)
+                                       if (ibuf->rect_float)
                                                curvemapping_do_ibuf(sima->cumap, ibuf);
                                }
                        }
@@ -1830,8 +1954,8 @@ static void sample_apply(bContext *C, wmOperator *op, wmEvent *event)
                        ScrArea *sa, *cur= curarea;
                        
                        node_curvemap_sample(fp);       /* sends global to node editor */
-                       for(sa= G.curscreen->areabase.first; sa; sa= sa->next) {
-                               if(sa->spacetype==SPACE_NODE) {
+                       for (sa= G.curscreen->areabase.first; sa; sa= sa->next) {
+                               if (sa->spacetype==SPACE_NODE) {
                                        areawinset(sa->win);
                                        scrarea_do_windraw(sa);
                                }
@@ -1848,7 +1972,7 @@ static void sample_apply(bContext *C, wmOperator *op, wmEvent *event)
        ED_area_tag_redraw(CTX_wm_area(C));
 }
 
-static void sample_exit(bContext *C, wmOperator *op)
+static void image_sample_exit(bContext *C, wmOperator *op)
 {
        ImageSampleInfo *info= op->customdata;
 
@@ -1857,66 +1981,66 @@ static void sample_exit(bContext *C, wmOperator *op)
        MEM_freeN(info);
 }
 
-static int sample_invoke(bContext *C, wmOperator *op, wmEvent *event)
+static int image_sample_invoke(bContext *C, wmOperator *op, wmEvent *event)
 {
        SpaceImage *sima= CTX_wm_space_image(C);
        ARegion *ar= CTX_wm_region(C);
        ImageSampleInfo *info;
 
-       if(!ED_space_image_has_buffer(sima))
+       if (!ED_space_image_has_buffer(sima))
                return OPERATOR_CANCELLED;
        
        info= MEM_callocN(sizeof(ImageSampleInfo), "ImageSampleInfo");
        info->art= ar->type;
-       info->draw_handle = ED_region_draw_cb_activate(ar->type, sample_draw, info, REGION_DRAW_POST_PIXEL);
+       info->draw_handle = ED_region_draw_cb_activate(ar->type, image_sample_draw, info, REGION_DRAW_POST_PIXEL);
        op->customdata= info;
 
-       sample_apply(C, op, event);
+       image_sample_apply(C, op, event);
 
        WM_event_add_modal_handler(C, op);
 
        return OPERATOR_RUNNING_MODAL;
 }
 
-static int sample_modal(bContext *C, wmOperator *op, wmEvent *event)
+static int image_sample_modal(bContext *C, wmOperator *op, wmEvent *event)
 {
        switch(event->type) {
                case LEFTMOUSE:
                case RIGHTMOUSE: // XXX hardcoded
-                       sample_exit(C, op);
+                       image_sample_exit(C, op);
                        return OPERATOR_CANCELLED;
                case MOUSEMOVE:
-                       sample_apply(C, op, event);
+                       image_sample_apply(C, op, event);
                        break;
        }
 
        return OPERATOR_RUNNING_MODAL;
 }
 
-static int sample_cancel(bContext *C, wmOperator *op)
+static int image_sample_cancel(bContext *C, wmOperator *op)
 {
-       sample_exit(C, op);
+       image_sample_exit(C, op);
        return OPERATOR_CANCELLED;
 }
 
 void IMAGE_OT_sample(wmOperatorType *ot)
 {
        /* identifiers */
-       ot->name= "Sample Color";
-       ot->idname= "IMAGE_OT_sample";
+       ot->name = "Sample Color";
+       ot->idname = "IMAGE_OT_sample";
        
        /* api callbacks */
-       ot->invokesample_invoke;
-       ot->modalsample_modal;
-       ot->cancelsample_cancel;
-       ot->poll= space_image_main_area_poll;
+       ot->invoke = image_sample_invoke;
+       ot->modal = image_sample_modal;
+       ot->cancel = image_sample_cancel;
+       ot->poll = space_image_image_sample_poll;
 
        /* flags */
-       ot->flag= OPTYPE_BLOCKING;
+       ot->flag = OPTYPE_BLOCKING;
 }
 
 /******************** sample line operator ********************/
-static int sample_line_exec(bContext *C, wmOperator *op)
+static int image_sample_line_exec(bContext *C, wmOperator *op)
 {
        SpaceImage *sima= CTX_wm_space_image(C);
        ARegion *ar= CTX_wm_region(C);
@@ -1966,7 +2090,8 @@ static int sample_line_exec(bContext *C, wmOperator *op)
                
                if (x<0 || y<0 || x>=ibuf->x || y>=ibuf->y) {
                        hist->data_luma[i] = hist->data_r[i] = hist->data_g[i]= hist->data_b[i] = 0.0f;
-               } else {
+               }
+               else {
                        if (ibuf->rect_float) {
                                fp= (ibuf->rect_float + (ibuf->channels)*(y*ibuf->x + x));
 
@@ -1978,14 +2103,14 @@ static int sample_line_exec(bContext *C, wmOperator *op)
                                hist->data_r[i] = rgb[0];
                                hist->data_g[i] = rgb[1];
                                hist->data_b[i] = rgb[2];
-                               hist->data_luma[i] = (0.299f*rgb[0] + 0.587f*rgb[1] + 0.114f*rgb[2]);
+                               hist->data_luma[i] = rgb_to_luma(rgb);
                        }
                        else if (ibuf->rect) {
                                cp= (unsigned char *)(ibuf->rect + y*ibuf->x + x);
                                hist->data_r[i] = (float)cp[0]/255.0f;
                                hist->data_g[i] = (float)cp[1]/255.0f;
                                hist->data_b[i] = (float)cp[2]/255.0f;
-                               hist->data_luma[i] = (0.299f*cp[0] + 0.587f*cp[1] + 0.114f*cp[2])/255;
+                               hist->data_luma[i] = (float)rgb_to_luma_byte(cp)/255.0f;
                        }
                }
        }
@@ -1997,11 +2122,11 @@ static int sample_line_exec(bContext *C, wmOperator *op)
        return OPERATOR_FINISHED;
 }
 
-static int sample_line_invoke(bContext *C, wmOperator *op, wmEvent *event)
+static int image_sample_line_invoke(bContext *C, wmOperator *op, wmEvent *event)
 {
        SpaceImage *sima= CTX_wm_space_image(C);
        
-       if(!ED_space_image_has_buffer(sima))
+       if (!ED_space_image_has_buffer(sima))
                return OPERATOR_CANCELLED;
        
        return WM_gesture_straightline_invoke(C, op, event);
@@ -2010,18 +2135,18 @@ static int sample_line_invoke(bContext *C, wmOperator *op, wmEvent *event)
 void IMAGE_OT_sample_line(wmOperatorType *ot)
 {
        /* identifiers */
-       ot->name= "Sample Line";
-       ot->idname= "IMAGE_OT_sample_line";
+       ot->name = "Sample Line";
+       ot->idname = "IMAGE_OT_sample_line";
        
        /* api callbacks */
-       ot->invokesample_line_invoke;
-       ot->modal= WM_gesture_straightline_modal;
-       ot->execsample_line_exec;
-       ot->poll= space_image_main_area_poll;
-       ot->cancel= WM_gesture_straightline_cancel;
+       ot->invoke = image_sample_line_invoke;
+       ot->modal = WM_gesture_straightline_modal;
+       ot->exec = image_sample_line_exec;
+       ot->poll = space_image_main_area_poll;
+       ot->cancel = WM_gesture_straightline_cancel;
        
        /* flags */
-       ot->flag= 0; /* no undo/register since this operates on the space */
+       ot->flag = 0; /* no undo/register since this operates on the space */
        
        WM_operator_properties_gesture_straightline(ot, CURSOR_EDIT);
 }
@@ -2036,20 +2161,20 @@ void IMAGE_OT_curves_point_set(wmOperatorType *ot)
                {0, NULL, 0, NULL, NULL}};
 
        /* identifiers */
-       ot->name= "Set Curves Point";
-       ot->idname= "IMAGE_OT_curves_point_set";
+       ot->name = "Set Curves Point";
+       ot->idname = "IMAGE_OT_curves_point_set";
 
        /* flags */
-       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+       ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
        
        /* api callbacks */
-       ot->invokesample_invoke;
-       ot->modalsample_modal;
-       ot->cancelsample_cancel;
-       ot->poll= space_image_main_area_poll;
+       ot->invoke = image_sample_invoke;
+       ot->modal = image_sample_modal;
+       ot->cancel = image_sample_cancel;
+       ot->poll = space_image_main_area_not_uv_brush_poll;
 
        /* properties */
-       RNA_def_enum(ot->srna, "point", point_items, 0, "Point", "Set black point or white point for curves.");
+       RNA_def_enum(ot->srna, "point", point_items, 0, "Point", "Set black point or white point for curves");
 }
 
 /******************** record composite operator *********************/
@@ -2060,7 +2185,7 @@ typedef struct RecordCompositeData {
        int sfra, efra;
 } RecordCompositeData;
 
-static int record_composite_apply(bContext *C, wmOperator *op)
+static int image_record_composite_apply(bContext *C, wmOperator *op)
 {
        SpaceImage *sima= CTX_wm_space_image(C);
        RecordCompositeData *rcd= op->customdata;
@@ -2080,7 +2205,7 @@ static int record_composite_apply(bContext *C, wmOperator *op)
        
        ibuf= BKE_image_get_ibuf(sima->image, &sima->iuser);
        /* save memory in flipbooks */
-       if(ibuf)
+       if (ibuf)
                imb_freerectfloatImBuf(ibuf);
        
        scene->r.cfra++;
@@ -2088,15 +2213,15 @@ static int record_composite_apply(bContext *C, wmOperator *op)
        return (scene->r.cfra <= rcd->efra);
 }
 
-static int record_composite_init(bContext *C, wmOperator *op)
+static int image_record_composite_init(bContext *C, wmOperator *op)
 {
        SpaceImage *sima= CTX_wm_space_image(C);
        Scene *scene= CTX_data_scene(C);
        RecordCompositeData *rcd;
 
-       if(sima->iuser.frames < 2)
+       if (sima->iuser.frames < 2)
                return 0;
-       if(scene->nodetree == NULL)
+       if (scene->nodetree == NULL)
                return 0;
        
        op->customdata= rcd= MEM_callocN(sizeof(RecordCompositeData), "ImageRecordCompositeData");
@@ -2109,7 +2234,7 @@ static int record_composite_init(bContext *C, wmOperator *op)
        return 1;
 }
 
-static void record_composite_exit(bContext *C, wmOperator *op)
+static void image_record_composite_exit(bContext *C, wmOperator *op)
 {
        Scene *scene= CTX_data_scene(C);
        SpaceImage *sima= CTX_wm_space_image(C);
@@ -2119,7 +2244,7 @@ static void record_composite_exit(bContext *C, wmOperator *op)
 
        WM_cursor_restore(CTX_wm_window(C));
 
-       if(rcd->timer)
+       if (rcd->timer)
                WM_event_remove_timer(CTX_wm_manager(C), CTX_wm_window(C), rcd->timer);
 
        WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, sima->image);
@@ -2130,113 +2255,113 @@ static void record_composite_exit(bContext *C, wmOperator *op)
        MEM_freeN(rcd);
 }
 
-static int record_composite_exec(bContext *C, wmOperator *op)
+static int image_record_composite_exec(bContext *C, wmOperator *op)
 {
-       if(!record_composite_init(C, op))
+       if (!image_record_composite_init(C, op))
                return OPERATOR_CANCELLED;
        
-       while(record_composite_apply(C, op))
+       while(image_record_composite_apply(C, op))
                ;
        
-       record_composite_exit(C, op);
+       image_record_composite_exit(C, op);
        
        return OPERATOR_FINISHED;
 }
 
-static int record_composite_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
+static int image_record_composite_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
 {
        RecordCompositeData *rcd;
        
-       if(!record_composite_init(C, op))
+       if (!image_record_composite_init(C, op))
                return OPERATOR_CANCELLED;
 
        rcd= op->customdata;
        rcd->timer= WM_event_add_timer(CTX_wm_manager(C), CTX_wm_window(C), TIMER, 0.0f);
        WM_event_add_modal_handler(C, op);
 
-       if(!record_composite_apply(C, op))
+       if (!image_record_composite_apply(C, op))
                return OPERATOR_FINISHED;
 
        return OPERATOR_RUNNING_MODAL;
 }
 
-static int record_composite_modal(bContext *C, wmOperator *op, wmEvent *event)
+static int image_record_composite_modal(bContext *C, wmOperator *op, wmEvent *event)
 {
        RecordCompositeData *rcd= op->customdata;
 
        switch(event->type) {
                case TIMER:
-                       if(rcd->timer == event->customdata) {
-                               if(!record_composite_apply(C, op)) {
-                                       record_composite_exit(C, op);
+                       if (rcd->timer == event->customdata) {
+                               if (!image_record_composite_apply(C, op)) {
+                                       image_record_composite_exit(C, op);
                                        return OPERATOR_FINISHED;
                                }
                        }
                        break;
                case ESCKEY:
-                       record_composite_exit(C, op);
+                       image_record_composite_exit(C, op);
                        return OPERATOR_FINISHED;
        }
 
        return OPERATOR_RUNNING_MODAL;
 }
 
-static int record_composite_cancel(bContext *C, wmOperator *op)
+static int image_record_composite_cancel(bContext *C, wmOperator *op)
 {
-       record_composite_exit(C, op);
+       image_record_composite_exit(C, op);
        return OPERATOR_CANCELLED;
 }
 
 void IMAGE_OT_record_composite(wmOperatorType *ot)
 {
        /* identifiers */
-       ot->name= "Record Composite";
-       ot->idname= "IMAGE_OT_record_composite";
+       ot->name = "Record Composite";
+       ot->idname = "IMAGE_OT_record_composite";
        
        /* api callbacks */
-       ot->execrecord_composite_exec;
-       ot->invokerecord_composite_invoke;
-       ot->modalrecord_composite_modal;
-       ot->cancelrecord_composite_cancel;
-       ot->poll= space_image_buffer_exists_poll;
+       ot->exec = image_record_composite_exec;
+       ot->invoke = image_record_composite_invoke;
+       ot->modal = image_record_composite_modal;
+       ot->cancel = image_record_composite_cancel;
+       ot->poll = space_image_buffer_exists_poll;
 }
 
 /********************* cycle render slot operator *********************/
 
-static int cycle_render_slot_poll(bContext *C)
+static int image_cycle_render_slot_poll(bContext *C)
 {
        Image *ima= CTX_data_edit_image(C);
 
        return (ima && ima->type == IMA_TYPE_R_RESULT);
 }
 
-static int cycle_render_slot_exec(bContext *C, wmOperator *op)
+static int image_cycle_render_slot_exec(bContext *C, wmOperator *op)
 {
        Image *ima= CTX_data_edit_image(C);
        int a, slot, cur= ima->render_slot;
        const short use_reverse= RNA_boolean_get(op->ptr, "reverse");
 
-       for(a=1; a<IMA_MAX_RENDER_SLOT; a++) {
+       for (a=1; a<IMA_MAX_RENDER_SLOT; a++) {
                slot= (cur + (use_reverse ? -a:a))%IMA_MAX_RENDER_SLOT;
-               if(slot<0) slot+=IMA_MAX_RENDER_SLOT;
+               if (slot<0) slot+=IMA_MAX_RENDER_SLOT;
 
-               if(ima->renders[slot] || slot == ima->last_render_slot) {
+               if (ima->renders[slot] || slot == ima->last_render_slot) {
                        ima->render_slot= slot;
                        break;
                }
-               else if((slot - 1) == ima->last_render_slot && slot < IMA_MAX_RENDER_SLOT) {
+               else if ((slot - 1) == ima->last_render_slot && slot < IMA_MAX_RENDER_SLOT) {
                        ima->render_slot= slot;
                        break;
                }
        }
 
-       if(a == IMA_MAX_RENDER_SLOT)
+       if (a == IMA_MAX_RENDER_SLOT)
                ima->render_slot= ((cur == 1)? 0: 1);
        
        WM_event_add_notifier(C, NC_IMAGE|ND_DRAW, NULL);
 
        /* no undo push for browsing existing */
-       if(ima->renders[ima->render_slot] || ima->render_slot==ima->last_render_slot)
+       if (ima->renders[ima->render_slot] || ima->render_slot==ima->last_render_slot)
                return OPERATOR_CANCELLED;
        
        return OPERATOR_FINISHED;
@@ -2245,15 +2370,15 @@ static int cycle_render_slot_exec(bContext *C, wmOperator *op)
 void IMAGE_OT_cycle_render_slot(wmOperatorType *ot)
 {
        /* identifiers */
-       ot->name= "Cycle Render Slot";
-       ot->idname= "IMAGE_OT_cycle_render_slot";
+       ot->name = "Cycle Render Slot";
+       ot->idname = "IMAGE_OT_cycle_render_slot";
        
        /* api callbacks */
-       ot->execcycle_render_slot_exec;
-       ot->pollcycle_render_slot_poll;
+       ot->exec = image_cycle_render_slot_exec;
+       ot->poll = image_cycle_render_slot_poll;
 
        /* flags */
-       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+       ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
 
        RNA_def_boolean(ot->srna, "reverse", 0, "Cycle in Reverse", "");
 }
@@ -2271,42 +2396,42 @@ void ED_image_update_frame(const Main *mainp, int cfra)
        Tex *tex;
        
        /* texture users */
-       for(tex= mainp->tex.first; tex; tex= tex->id.next) {
-               if(tex->type==TEX_IMAGE && tex->ima) {
-                       if(ELEM(tex->ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE)) {
-                               if(tex->iuser.flag & IMA_ANIM_ALWAYS)
+       for (tex= mainp->tex.first; tex; tex= tex->id.next) {
+               if (tex->type==TEX_IMAGE && tex->ima) {
+                       if (ELEM(tex->ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE)) {
+                               if (tex->iuser.flag & IMA_ANIM_ALWAYS)
                                        BKE_image_user_calc_frame(&tex->iuser, cfra, 0);
                        }
                }
        }
        
        /* image window, compo node users */
-       for(wm=mainp->wm.first; wm; wm= wm->id.next) { /* only 1 wm */
-               for(win= wm->windows.first; win; win= win->next) {
+       for (wm=mainp->wm.first; wm; wm= wm->id.next) { /* only 1 wm */
+               for (win= wm->windows.first; win; win= win->next) {
                        ScrArea *sa;
-                       for(sa= win->screen->areabase.first; sa; sa= sa->next) {
-                               if(sa->spacetype==SPACE_VIEW3D) {
+                       for (sa= win->screen->areabase.first; sa; sa= sa->next) {
+                               if (sa->spacetype==SPACE_VIEW3D) {
                                        View3D *v3d= sa->spacedata.first;
                                        BGpic *bgpic;
-                                       for(bgpic= v3d->bgpicbase.first; bgpic; bgpic= bgpic->next)
-                                               if(bgpic->iuser.flag & IMA_ANIM_ALWAYS)
+                                       for (bgpic= v3d->bgpicbase.first; bgpic; bgpic= bgpic->next)
+                                               if (bgpic->iuser.flag & IMA_ANIM_ALWAYS)
                                                        BKE_image_user_calc_frame(&bgpic->iuser, cfra, 0);
                                }
-                               else if(sa->spacetype==SPACE_IMAGE) {
+                               else if (sa->spacetype==SPACE_IMAGE) {
                                        SpaceImage *sima= sa->spacedata.first;
-                                       if(sima->iuser.flag & IMA_ANIM_ALWAYS)
+                                       if (sima->iuser.flag & IMA_ANIM_ALWAYS)
                                                BKE_image_user_calc_frame(&sima->iuser, cfra, 0);
                                }
-                               else if(sa->spacetype==SPACE_NODE) {
+                               else if (sa->spacetype==SPACE_NODE) {
                                        SpaceNode *snode= sa->spacedata.first;
-                                       if((snode->treetype==NTREE_COMPOSIT) && (snode->nodetree)) {
+                                       if ((snode->treetype==NTREE_COMPOSIT) && (snode->nodetree)) {
                                                bNode *node;
-                                               for(node= snode->nodetree->nodes.first; node; node= node->next) {
-                                                       if(node->id && node->type==CMP_NODE_IMAGE) {
+                                               for (node= snode->nodetree->nodes.first; node; node= node->next) {
+                                                       if (node->id && node->type==CMP_NODE_IMAGE) {
                                                                Image *ima= (Image *)node->id;
                                                                ImageUser *iuser= node->storage;
-                                                               if(ELEM(ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE))
-                                                                       if(iuser->flag & IMA_ANIM_ALWAYS)
+                                                               if (ELEM(ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE))
+                                                                       if (iuser->flag & IMA_ANIM_ALWAYS)
                                                                                BKE_image_user_calc_frame(iuser, cfra, 0);
                                                        }
                                                }