svn merge ^/trunk/blender -r49933:49939
authorCampbell Barton <ideasman42@gmail.com>
Thu, 16 Aug 2012 14:48:43 +0000 (14:48 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Thu, 16 Aug 2012 14:48:43 +0000 (14:48 +0000)
1  2 
source/blender/compositor/operations/COM_InpaintOperation.cpp
source/blender/editors/space_image/image_ops.c

index b0e9c601e077ddd6c3ea29bfa79d3ccc65d4df5f,bfc0ecd1c00e57113eabed688299227e13920fef..6ae42b36a7ca705a3ab79f69d0c4568787cb828b
@@@ -121,7 -121,7 +121,7 @@@ void InpaintSimpleOperation::calc_manha
  {
        int width = this->getWidth();
        int height = this->getHeight();
-       short *m = this->m_manhatten_distance = new short[width * height];
+       short *m = this->m_manhatten_distance = (short *)MEM_mallocN(sizeof(short) * width * height, __func__);
        int *offsets;
  
        offsets = (int *)MEM_callocN(sizeof(int) * (width + height + 1), "InpaintSimpleOperation offsets");
                        m[j * width + i] = r;
                }
        }
-       
        for (int j = height - 1; j >= 0; j--) {
                for (int i = width - 1; i >= 0; i--) {
                        int r = m[j * width + i];
        }
        
        this->m_area_size = offsets[width + height];
-       this->m_pixelorder = new int[this->m_area_size];
+       this->m_pixelorder = (int *)MEM_mallocN(sizeof(int) * this->m_area_size, __func__);
        
        for (int i = 0; i < width * height; i++) {
                if (m[i] > 0) {
@@@ -182,7 -182,8 +182,7 @@@ void InpaintSimpleOperation::pix_step(i
  
        for (int dx = -1; dx <= 1; dx++) {
                for (int dy = -1; dy <= 1; dy++) {
 -                      if (dx != 0 && dy != 0) {
 -
 +                      if (dx != 0 || dy != 0) {
                                int x_ofs = x + dx;
                                int y_ofs = y + dy;
  
                interp_v3_v3v3(output, pix, output, output[3]);
                output[3] = 1.0f;
        }
 +
 +      this->get_pixel(x, y)[3] = 1.0f;
  }
  
  void *InpaintSimpleOperation::initializeTileData(rcti *rect)
        lockMutex();
        if (!this->m_cached_buffer_ready) {
                MemoryBuffer *buf = (MemoryBuffer *)this->m_inputImageProgram->initializeTileData(rect);
-               this->m_cached_buffer = new float[this->getWidth() * this->getHeight() * COM_NUMBER_OF_CHANNELS];
-               memcpy(this->m_cached_buffer, buf->getBuffer(), this->getWidth() * this->getHeight() * COM_NUMBER_OF_CHANNELS * sizeof(float));
+               this->m_cached_buffer = (float *)MEM_dupallocN(buf->getBuffer());
  
                this->calc_manhatten_distance();
  
@@@ -256,17 -253,17 +254,17 @@@ void InpaintSimpleOperation::deinitExec
        this->m_inputImageProgram = NULL;
        this->deinitMutex();
        if (this->m_cached_buffer) {
-               delete [] this->m_cached_buffer;
+               MEM_freeN(this->m_cached_buffer);
                this->m_cached_buffer = NULL;
        }
  
        if (this->m_pixelorder) {
-               delete [] this->m_pixelorder;
+               MEM_freeN(this->m_pixelorder);
                this->m_pixelorder = NULL;
        }
  
        if (this->m_manhatten_distance) {
-               delete [] this->m_manhatten_distance;
+               MEM_freeN(this->m_manhatten_distance);
                this->m_manhatten_distance = NULL;
        }
        this->m_cached_buffer_ready = false;
index 1d8f63ce385248cf2d223a14f39dcc4dd28dd385,30480bd095f879f9d0c3e61f654f6c127480b771..0fb2892cf7edf21698994e925f6f1bc08296ca96
@@@ -55,7 -55,6 +55,7 @@@
  #include "BKE_report.h"
  #include "BKE_screen.h"
  
 +#include "IMB_colormanagement.h"
  #include "IMB_imbuf.h"
  #include "IMB_imbuf_types.h"
  
@@@ -1115,14 -1114,12 +1115,14 @@@ static char imtype_best_depth(ImBuf *ib
        }
  }
  
 -static int save_image_options_init(SaveImageOptions *simopts, SpaceImage *sima, Scene *scene, const short guess_path)
 +static int save_image_options_init(bContext *C, SaveImageOptions *simopts, SpaceImage *sima, Scene *scene, const short guess_path)
  {
        void *lock;
        ImBuf *ibuf = ED_space_image_acquire_buffer(sima, &lock);
  
        if (ibuf) {
 +              wmWindow *win = CTX_wm_window(C);
 +              const ColorManagedViewSettings *view_settings;
                Image *ima = sima->image;
                short is_depth_set = FALSE;
  
                        }
                        BLI_path_abs(simopts->filepath, G.main->name);
                }
 +
 +              /* color management */
 +              view_settings = IMB_view_settings_get_effective(win, &sima->view_settings);
 +
 +              BKE_color_managed_display_settings_copy(&simopts->im_format.display_settings, &win->display_settings);
 +              BKE_color_managed_view_settings_copy(&simopts->im_format.view_settings, view_settings);
        }
  
        ED_space_image_release_buffer(sima, lock);
@@@ -1205,38 -1196,6 +1205,38 @@@ static void save_image_options_to_op(Sa
        RNA_string_set(op->ptr, "filepath", simopts->filepath);
  }
  
 +static ImBuf *save_image_colormanaged_imbuf_acquire(ImBuf *ibuf, SaveImageOptions *simopts, void **cache_handle)
 +{
 +      ImageFormatData *imf = &simopts->im_format;
 +      ImBuf *colormanaged_ibuf;
 +      int do_colormanagement;
 +
 +      *cache_handle = NULL;
 +      do_colormanagement = !BKE_imtype_supports_float(imf->imtype);
 +
 +      if (do_colormanagement) {
 +              unsigned char *display_buffer =
 +                      IMB_display_buffer_acquire(ibuf, &imf->view_settings, &imf->display_settings, cache_handle);
 +
 +              if (*cache_handle) {
 +                      colormanaged_ibuf = IMB_allocImBuf(ibuf->x, ibuf->y, ibuf->planes, 0);
 +                      colormanaged_ibuf->rect = (unsigned int *) display_buffer;
 +              }
 +              else {
 +                      /* no cache handle means color management didn't run transformation
 +                       * or performed transformation to image's byte buffer which doesn't
 +                       * require allocating new image buffer
 +                       */
 +                      colormanaged_ibuf = ibuf;
 +              }
 +      }
 +      else {
 +              colormanaged_ibuf = ibuf;
 +      }
 +
 +      return colormanaged_ibuf;
 +}
 +
  /* assumes name is FILE_MAX */
  /* ima->name and ibuf->name should end up the same */
  static void save_image_doit(bContext *C, SpaceImage *sima, wmOperator *op, SaveImageOptions *simopts, int do_newpath)
        ImBuf *ibuf = ED_space_image_acquire_buffer(sima, &lock);
  
        if (ibuf) {
 +              void *cache_handle;
 +              ImBuf *colormanaged_ibuf;
                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"));
                                ibuf->planes = BKE_imbuf_alpha_test(ibuf) ? 32 : 24;
                        }
                }
 -              
 +
 +              colormanaged_ibuf = save_image_colormanaged_imbuf_acquire(ibuf, simopts, &cache_handle);
 +
                if (simopts->im_format.imtype == R_IMF_IMTYPE_MULTILAYER) {
                        Scene *scene = CTX_data_scene(C);
                        RenderResult *rr = BKE_image_acquire_renderresult(scene, ima);
                        BKE_image_release_renderresult(scene, ima);
                }
                else {
 -                      if (BKE_imbuf_write_as(ibuf, simopts->filepath, &simopts->im_format, save_copy)) {
 +                      if (BKE_imbuf_write_as(colormanaged_ibuf, simopts->filepath, &simopts->im_format, save_copy)) {
                                ok = TRUE;
                        }
                }
                WM_event_add_notifier(C, NC_IMAGE | NA_EDITED, sima->image);
  
                WM_cursor_wait(0);
 +
 +              if (cache_handle) {
 +                      colormanaged_ibuf->rect = NULL;
 +                      IMB_freeImBuf(colormanaged_ibuf);
 +
 +                      IMB_display_buffer_release(cache_handle);
 +              }
        }
  
        ED_space_image_release_buffer(sima, lock);
@@@ -1368,7 -1316,7 +1368,7 @@@ static int image_save_as_exec(bContext 
  
        /* 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_init(C, &simopts, sima, CTX_data_scene(C), 0);
  
        save_image_options_from_op(&simopts, op);
  
@@@ -1397,7 -1345,7 +1397,7 @@@ static int image_save_as_invoke(bContex
  
        save_image_options_defaults(&simopts);
  
 -      if (save_image_options_init(&simopts, sima, scene, TRUE) == 0)
 +      if (save_image_options_init(C, &simopts, sima, scene, TRUE) == 0)
                return OPERATOR_CANCELLED;
        save_image_options_to_op(&simopts, op);
  
@@@ -1483,7 -1431,7 +1483,7 @@@ static int image_save_exec(bContext *C
        Scene *scene = CTX_data_scene(C);
        SaveImageOptions simopts;
  
 -      if (save_image_options_init(&simopts, sima, scene, FALSE) == 0)
 +      if (save_image_options_init(C, &simopts, sima, scene, FALSE) == 0)
                return OPERATOR_CANCELLED;
        save_image_options_from_op(&simopts, op);
  
@@@ -2000,6 -1948,54 +2000,54 @@@ static void image_sample_draw(const bCo
        }
  }
  
+ /* returns color in SRGB */
+ /* matching ED_space_node_color_sample() */
+ int ED_space_image_color_sample(SpaceImage *sima, ARegion *ar, int mval[2], float r_col[3])
+ {
+       void *lock;
+       ImBuf *ibuf = ED_space_image_acquire_buffer(sima, &lock);
+       float fx, fy;
+       int ret = FALSE;
+       if (ibuf == NULL) {
+               ED_space_image_release_buffer(sima, lock);
+               return FALSE;
+       }
+       UI_view2d_region_to_view(&ar->v2d, mval[0], mval[1], &fx, &fy);
+       if (fx >= 0.0f && fy >= 0.0f && fx < 1.0f && fy < 1.0f) {
+               float *fp;
+               unsigned char *cp;
+               int x = (int)(fx * ibuf->x), y = (int)(fy * ibuf->y);
+               CLAMP(x, 0, ibuf->x - 1);
+               CLAMP(y, 0, ibuf->y - 1);
+               if (ibuf->rect_float) {
+                       fp = (ibuf->rect_float + (ibuf->channels) * (y * ibuf->x + x));
+                       if (ibuf->profile == IB_PROFILE_LINEAR_RGB) {
+                               linearrgb_to_srgb_v3_v3(r_col, fp);
+                       }
+                       else {
+                               copy_v3_v3(r_col, fp);
+                       }
+                       ret = TRUE;
+               }
+               else if (ibuf->rect) {
+                       cp = (unsigned char *)(ibuf->rect + y * ibuf->x + x);
+                       r_col[0] = cp[0] / 255.0f;
+                       r_col[1] = cp[1] / 255.0f;
+                       r_col[2] = cp[2] / 255.0f;
+                       ret = TRUE;
+               }
+       }
+       ED_space_image_release_buffer(sima, lock);
+       return ret;
+ }
  static void image_sample_apply(bContext *C, wmOperator *op, wmEvent *event)
  {
        SpaceImage *sima = CTX_wm_space_image(C);