svn merge ^/trunk/blender -r49933:49939
[blender.git] / source / blender / editors / space_image / image_ops.c
index 1d8f63ce385248cf2d223a14f39dcc4dd28dd385..0fb2892cf7edf21698994e925f6f1bc08296ca96 100644 (file)
@@ -2000,6 +2000,54 @@ static void image_sample_draw(const bContext *UNUSED(C), ARegion *ar, void *arg_
        }
 }
 
+/* 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);