Support displaying metadata for images in sequencer preview windows (not
[blender.git] / source / blender / editors / space_image / image_draw.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * Contributor(s): Blender Foundation, 2002-2009
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/editors/space_image/image_draw.c
27  *  \ingroup spimage
28  */
29
30
31 #include <math.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include "MEM_guardedalloc.h"
36
37 #include "DNA_camera_types.h"
38 #include "DNA_object_types.h"
39 #include "DNA_space_types.h"
40 #include "DNA_scene_types.h"
41 #include "DNA_screen_types.h"
42 #include "DNA_brush_types.h"
43 #include "DNA_mask_types.h"
44
45 #include "PIL_time.h"
46
47 #include "BLI_math.h"
48 #include "BLI_rect.h"
49 #include "BLI_threads.h"
50 #include "BLI_string.h"
51 #include "BLI_utildefines.h"
52
53 #include "IMB_imbuf.h"
54 #include "IMB_imbuf_types.h"
55 #include "IMB_colormanagement.h"
56 #include "IMB_moviecache.h"
57
58 #include "BKE_context.h"
59 #include "BKE_global.h"
60 #include "BKE_image.h"
61 #include "BKE_paint.h"
62
63 #include "BIF_gl.h"
64 #include "BIF_glutil.h"
65
66 #include "BLF_api.h"
67
68 #include "ED_gpencil.h"
69 #include "ED_image.h"
70 #include "ED_mask.h"
71 #include "ED_render.h"
72 #include "ED_screen.h"
73
74 #include "UI_interface.h"
75 #include "UI_resources.h"
76 #include "UI_view2d.h"
77
78 #include "RE_pipeline.h"
79 #include "RE_engine.h"
80
81 #include "image_intern.h"
82
83 static void draw_render_info(const bContext *C,
84                              Scene *scene,
85                              Image *ima,
86                              ARegion *ar,
87                              float zoomx,
88                              float zoomy)
89 {
90         RenderResult *rr;
91         Render *re = RE_GetRender(scene->id.name);
92         RenderData *rd = RE_engine_get_render_data(re);
93         Scene *stats_scene = ED_render_job_get_scene(C);
94         if (stats_scene == NULL) {
95                 stats_scene = CTX_data_scene(C);
96         }
97
98         rr = BKE_image_acquire_renderresult(stats_scene, ima);
99
100         if (rr && rr->text) {
101                 float fill_color[4] = {0.0f, 0.0f, 0.0f, 0.25f};
102                 ED_region_info_draw(ar, rr->text, 1, fill_color);
103         }
104
105         BKE_image_release_renderresult(stats_scene, ima);
106
107         if (re) {
108                 int total_tiles;
109                 bool need_free_tiles;
110                 rcti *tiles;
111
112                 tiles = RE_engine_get_current_tiles(re, &total_tiles, &need_free_tiles);
113
114                 if (total_tiles) {
115                         int i, x, y;
116                         rcti *tile;
117
118                         /* find window pixel coordinates of origin */
119                         UI_view2d_view_to_region(&ar->v2d, 0.0f, 0.0f, &x, &y);
120
121                         glPushMatrix();
122                         glTranslatef(x, y, 0.0f);
123                         glScalef(zoomx, zoomy, 1.0f);
124
125                         if (rd->mode & R_BORDER) {
126                                 glTranslatef((int)(-rd->border.xmin * rd->xsch * rd->size / 100.0f),
127                                              (int)(-rd->border.ymin * rd->ysch * rd->size / 100.0f),
128                                              0.0f);
129                         }
130
131                         UI_ThemeColor(TH_FACE_SELECT);
132
133                         for (i = 0, tile = tiles; i < total_tiles; i++, tile++) {
134                                 glaDrawBorderCorners(tile, zoomx, zoomy);
135                         }
136
137                         if (need_free_tiles) {
138                                 MEM_freeN(tiles);
139                         }
140
141                         glPopMatrix();
142                 }
143         }
144 }
145
146 /* used by node view too */
147 void ED_image_draw_info(Scene *scene, ARegion *ar, bool color_manage, bool use_default_view, int channels, int x, int y,
148                         const unsigned char cp[4], const float fp[4], const float linearcol[4], int *zp, float *zpf)
149 {
150         rcti color_rect;
151         char str[256];
152         int dx = 6;
153         const int dy = 0.3f * UI_UNIT_Y;
154         /* text colors */
155         /* XXX colored text not allowed in Blender UI */
156 #if 0
157         unsigned char red[3] = {255, 50, 50};
158         unsigned char green[3] = {0, 255, 0};
159         unsigned char blue[3] = {100, 100, 255};
160 #else
161         unsigned char red[3] = {255, 255, 255};
162         unsigned char green[3] = {255, 255, 255};
163         unsigned char blue[3] = {255, 255, 255};
164 #endif
165         float hue = 0, sat = 0, val = 0, lum = 0, u = 0, v = 0;
166         float col[4], finalcol[4];
167
168         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
169         glEnable(GL_BLEND);
170
171         /* noisy, high contrast make impossible to read if lower alpha is used. */
172         glColor4ub(0, 0, 0, 190);
173         glRecti(0.0, 0.0, BLI_rcti_size_x(&ar->winrct) + 1, UI_UNIT_Y);
174         glDisable(GL_BLEND);
175
176         BLF_size(blf_mono_font, 11 * U.pixelsize, U.dpi);
177
178         glColor3ub(255, 255, 255);
179         BLI_snprintf(str, sizeof(str), "X:%-4d  Y:%-4d |", x, y);
180         BLF_position(blf_mono_font, dx, dy, 0);
181         BLF_draw_ascii(blf_mono_font, str, sizeof(str));
182         dx += BLF_width(blf_mono_font, str, sizeof(str));
183
184         if (zp) {
185                 glColor3ub(255, 255, 255);
186                 BLI_snprintf(str, sizeof(str), " Z:%-.4f |", 0.5f + 0.5f * (((float)*zp) / (float)0x7fffffff));
187                 BLF_position(blf_mono_font, dx, dy, 0);
188                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
189                 dx += BLF_width(blf_mono_font, str, sizeof(str));
190         }
191         if (zpf) {
192                 glColor3ub(255, 255, 255);
193                 BLI_snprintf(str, sizeof(str), " Z:%-.3f |", *zpf);
194                 BLF_position(blf_mono_font, dx, dy, 0);
195                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
196                 dx += BLF_width(blf_mono_font, str, sizeof(str));
197         }
198
199         if (channels >= 3) {
200                 glColor3ubv(red);
201                 if (fp)
202                         BLI_snprintf(str, sizeof(str), "  R:%-.5f", fp[0]);
203                 else if (cp)
204                         BLI_snprintf(str, sizeof(str), "  R:%-3d", cp[0]);
205                 else
206                         BLI_snprintf(str, sizeof(str), "  R:-");
207                 BLF_position(blf_mono_font, dx, dy, 0);
208                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
209                 dx += BLF_width(blf_mono_font, str, sizeof(str));
210                 
211                 glColor3ubv(green);
212                 if (fp)
213                         BLI_snprintf(str, sizeof(str), "  G:%-.5f", fp[1]);
214                 else if (cp)
215                         BLI_snprintf(str, sizeof(str), "  G:%-3d", cp[1]);
216                 else
217                         BLI_snprintf(str, sizeof(str), "  G:-");
218                 BLF_position(blf_mono_font, dx, dy, 0);
219                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
220                 dx += BLF_width(blf_mono_font, str, sizeof(str));
221                 
222                 glColor3ubv(blue);
223                 if (fp)
224                         BLI_snprintf(str, sizeof(str), "  B:%-.5f", fp[2]);
225                 else if (cp)
226                         BLI_snprintf(str, sizeof(str), "  B:%-3d", cp[2]);
227                 else
228                         BLI_snprintf(str, sizeof(str), "  B:-");
229                 BLF_position(blf_mono_font, dx, dy, 0);
230                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
231                 dx += BLF_width(blf_mono_font, str, sizeof(str));
232                 
233                 if (channels == 4) {
234                         glColor3ub(255, 255, 255);
235                         if (fp)
236                                 BLI_snprintf(str, sizeof(str), "  A:%-.4f", fp[3]);
237                         else if (cp)
238                                 BLI_snprintf(str, sizeof(str), "  A:%-3d", cp[3]);
239                         else
240                                 BLI_snprintf(str, sizeof(str), "- ");
241                         BLF_position(blf_mono_font, dx, dy, 0);
242                         BLF_draw_ascii(blf_mono_font, str, sizeof(str));
243                         dx += BLF_width(blf_mono_font, str, sizeof(str));
244                 }
245
246                 if (color_manage) {
247                         float rgba[4];
248
249                         copy_v3_v3(rgba, linearcol);
250                         if (channels == 3)
251                                 rgba[3] = 1.0f;
252                         else
253                                 rgba[3] = linearcol[3];
254
255                         if (use_default_view)
256                                 IMB_colormanagement_pixel_to_display_space_v4(rgba, rgba,  NULL, &scene->display_settings);
257                         else
258                                 IMB_colormanagement_pixel_to_display_space_v4(rgba, rgba,  &scene->view_settings, &scene->display_settings);
259
260                         BLI_snprintf(str, sizeof(str), "  |  CM  R:%-.4f  G:%-.4f  B:%-.4f", rgba[0], rgba[1], rgba[2]);
261                         BLF_position(blf_mono_font, dx, dy, 0);
262                         BLF_draw_ascii(blf_mono_font, str, sizeof(str));
263                         dx += BLF_width(blf_mono_font, str, sizeof(str));
264                 }
265         }
266         
267         /* color rectangle */
268         if (channels == 1) {
269                 if (fp) {
270                         col[0] = col[1] = col[2] = fp[0];
271                 }
272                 else if (cp) {
273                         col[0] = col[1] = col[2] = (float)cp[0] / 255.0f;
274                 }
275                 else {
276                         col[0] = col[1] = col[2] = 0.0f;
277                 }
278                 col[3] = 1.0f;
279         }
280         else if (channels == 3) {
281                 copy_v3_v3(col, linearcol);
282                 col[3] = 1.0f;
283         }
284         else if (channels == 4) {
285                 copy_v4_v4(col, linearcol);
286         }
287         else {
288                 BLI_assert(0);
289                 zero_v4(col);
290         }
291
292         if (color_manage) {
293                 if (use_default_view)
294                         IMB_colormanagement_pixel_to_display_space_v4(finalcol, col,  NULL, &scene->display_settings);
295                 else
296                         IMB_colormanagement_pixel_to_display_space_v4(finalcol, col,  &scene->view_settings, &scene->display_settings);
297         }
298         else {
299                 copy_v4_v4(finalcol, col);
300         }
301
302         glDisable(GL_BLEND);
303         dx += 0.25f * UI_UNIT_X;
304
305         BLI_rcti_init(&color_rect, dx, dx + (1.5f * UI_UNIT_X), 0.15f * UI_UNIT_Y, 0.85f * UI_UNIT_Y);
306
307         if (channels == 4) {
308                 rcti color_rect_half;
309                 int color_quater_x, color_quater_y;
310
311                 color_rect_half = color_rect;
312                 color_rect_half.xmax = BLI_rcti_cent_x(&color_rect);
313                 glRecti(color_rect.xmin, color_rect.ymin, color_rect.xmax, color_rect.ymax);
314
315                 color_rect_half = color_rect;
316                 color_rect_half.xmin = BLI_rcti_cent_x(&color_rect);
317
318                 color_quater_x = BLI_rcti_cent_x(&color_rect_half);
319                 color_quater_y = BLI_rcti_cent_y(&color_rect_half);
320
321                 glColor4ub(UI_ALPHA_CHECKER_DARK, UI_ALPHA_CHECKER_DARK, UI_ALPHA_CHECKER_DARK, 255);
322                 glRecti(color_rect_half.xmin, color_rect_half.ymin, color_rect_half.xmax, color_rect_half.ymax);
323
324                 glColor4ub(UI_ALPHA_CHECKER_LIGHT, UI_ALPHA_CHECKER_LIGHT, UI_ALPHA_CHECKER_LIGHT, 255);
325                 glRecti(color_quater_x, color_quater_y, color_rect_half.xmax, color_rect_half.ymax);
326                 glRecti(color_rect_half.xmin, color_rect_half.ymin, color_quater_x, color_quater_y);
327
328                 glEnable(GL_BLEND);
329                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
330                 glColor4f(UNPACK3(finalcol), fp ? fp[3] : (cp[3] / 255.0f));
331                 glRecti(color_rect.xmin, color_rect.ymin, color_rect.xmax, color_rect.ymax);
332                 glDisable(GL_BLEND);
333         }
334         else {
335                 glColor3fv(finalcol);
336                 glRecti(color_rect.xmin, color_rect.ymin, color_rect.xmax, color_rect.ymax);
337         }
338
339         /* draw outline */
340         glColor3ub(128, 128, 128);
341         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
342         glRecti(color_rect.xmin, color_rect.ymin, color_rect.xmax, color_rect.ymax);
343         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
344
345         dx += 1.75f * UI_UNIT_X;
346
347         glColor3ub(255, 255, 255);
348         if (channels == 1) {
349                 if (fp) {
350                         rgb_to_hsv(fp[0], fp[0], fp[0], &hue, &sat, &val);
351                         rgb_to_yuv(fp[0], fp[0], fp[0], &lum, &u, &v);
352                 }
353                 else if (cp) {
354                         rgb_to_hsv((float)cp[0] / 255.0f, (float)cp[0] / 255.0f, (float)cp[0] / 255.0f, &hue, &sat, &val);
355                         rgb_to_yuv((float)cp[0] / 255.0f, (float)cp[0] / 255.0f, (float)cp[0] / 255.0f, &lum, &u, &v);
356                 }
357                 
358                 BLI_snprintf(str, sizeof(str), "V:%-.4f", val);
359                 BLF_position(blf_mono_font, dx, dy, 0);
360                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
361                 dx += BLF_width(blf_mono_font, str, sizeof(str));
362
363                 BLI_snprintf(str, sizeof(str), "   L:%-.4f", lum);
364                 BLF_position(blf_mono_font, dx, dy, 0);
365                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
366                 dx += BLF_width(blf_mono_font, str, sizeof(str));
367         }
368         else if (channels >= 3) {
369                 rgb_to_hsv(finalcol[0], finalcol[1], finalcol[2], &hue, &sat, &val);
370                 rgb_to_yuv(finalcol[0], finalcol[1], finalcol[2], &lum, &u, &v);
371
372                 BLI_snprintf(str, sizeof(str), "H:%-.4f", hue);
373                 BLF_position(blf_mono_font, dx, dy, 0);
374                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
375                 dx += BLF_width(blf_mono_font, str, sizeof(str));
376
377                 BLI_snprintf(str, sizeof(str), "  S:%-.4f", sat);
378                 BLF_position(blf_mono_font, dx, dy, 0);
379                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
380                 dx += BLF_width(blf_mono_font, str, sizeof(str));
381
382                 BLI_snprintf(str, sizeof(str), "  V:%-.4f", val);
383                 BLF_position(blf_mono_font, dx, dy, 0);
384                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
385                 dx += BLF_width(blf_mono_font, str, sizeof(str));
386
387                 BLI_snprintf(str, sizeof(str), "   L:%-.4f", lum);
388                 BLF_position(blf_mono_font, dx, dy, 0);
389                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
390                 dx += BLF_width(blf_mono_font, str, sizeof(str));
391         }
392
393         (void)dx;
394 }
395
396 /* image drawing */
397
398 static void sima_draw_alpha_pixels(float x1, float y1, int rectx, int recty, unsigned int *recti)
399 {
400         
401         /* swap bytes, so alpha is most significant one, then just draw it as luminance int */
402         if (ENDIAN_ORDER == B_ENDIAN)
403                 glPixelStorei(GL_UNPACK_SWAP_BYTES, 1);
404
405         glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_LUMINANCE, GL_UNSIGNED_INT, recti);
406         glPixelStorei(GL_UNPACK_SWAP_BYTES, 0);
407 }
408
409 static void sima_draw_alpha_pixelsf(float x1, float y1, int rectx, int recty, float *rectf)
410 {
411         float *trectf = MEM_mallocN(rectx * recty * 4, "temp");
412         int a, b;
413         
414         for (a = rectx * recty - 1, b = 4 * a + 3; a >= 0; a--, b -= 4)
415                 trectf[a] = rectf[b];
416         
417         glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_LUMINANCE, GL_FLOAT, trectf);
418         MEM_freeN(trectf);
419         /* ogl trick below is slower... (on ATI 9600) */
420 //      glColorMask(1, 0, 0, 0);
421 //      glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_RGBA, GL_FLOAT, rectf + 3);
422 //      glColorMask(0, 1, 0, 0);
423 //      glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_RGBA, GL_FLOAT, rectf + 2);
424 //      glColorMask(0, 0, 1, 0);
425 //      glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_RGBA, GL_FLOAT, rectf + 1);
426 //      glColorMask(1, 1, 1, 1);
427 }
428
429 static void sima_draw_zbuf_pixels(float x1, float y1, int rectx, int recty, int *recti)
430 {
431         /* zbuffer values are signed, so we need to shift color range */
432         glPixelTransferf(GL_RED_SCALE, 0.5f);
433         glPixelTransferf(GL_GREEN_SCALE, 0.5f);
434         glPixelTransferf(GL_BLUE_SCALE, 0.5f);
435         glPixelTransferf(GL_RED_BIAS, 0.5f);
436         glPixelTransferf(GL_GREEN_BIAS, 0.5f);
437         glPixelTransferf(GL_BLUE_BIAS, 0.5f);
438         
439         glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_LUMINANCE, GL_INT, recti);
440         
441         glPixelTransferf(GL_RED_SCALE, 1.0f);
442         glPixelTransferf(GL_GREEN_SCALE, 1.0f);
443         glPixelTransferf(GL_BLUE_SCALE, 1.0f);
444         glPixelTransferf(GL_RED_BIAS, 0.0f);
445         glPixelTransferf(GL_GREEN_BIAS, 0.0f);
446         glPixelTransferf(GL_BLUE_BIAS, 0.0f);
447 }
448
449 static void sima_draw_zbuffloat_pixels(Scene *scene, float x1, float y1, int rectx, int recty, float *rect_float)
450 {
451         float bias, scale, *rectf, clipend;
452         int a;
453         
454         if (scene->camera && scene->camera->type == OB_CAMERA) {
455                 bias = ((Camera *)scene->camera->data)->clipsta;
456                 clipend = ((Camera *)scene->camera->data)->clipend;
457                 scale = 1.0f / (clipend - bias);
458         }
459         else {
460                 bias = 0.1f;
461                 scale = 0.01f;
462                 clipend = 100.0f;
463         }
464         
465         rectf = MEM_mallocN(rectx * recty * 4, "temp");
466         for (a = rectx * recty - 1; a >= 0; a--) {
467                 if (rect_float[a] > clipend)
468                         rectf[a] = 0.0f;
469                 else if (rect_float[a] < bias)
470                         rectf[a] = 1.0f;
471                 else {
472                         rectf[a] = 1.0f - (rect_float[a] - bias) * scale;
473                         rectf[a] *= rectf[a];
474                 }
475         }
476         glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_LUMINANCE, GL_FLOAT, rectf);
477         
478         MEM_freeN(rectf);
479 }
480
481 static void draw_image_buffer(const bContext *C, SpaceImage *sima, ARegion *ar, Scene *scene, ImBuf *ibuf, float fx, float fy, float zoomx, float zoomy)
482 {
483         int x, y;
484
485         /* set zoom */
486         glPixelZoom(zoomx, zoomy);
487
488         glaDefine2DArea(&ar->winrct);
489         
490         /* find window pixel coordinates of origin */
491         UI_view2d_view_to_region(&ar->v2d, fx, fy, &x, &y);
492
493         /* this part is generic image display */
494         if (sima->flag & SI_SHOW_ALPHA) {
495                 if (ibuf->rect)
496                         sima_draw_alpha_pixels(x, y, ibuf->x, ibuf->y, ibuf->rect);
497                 else if (ibuf->rect_float && ibuf->channels == 4)
498                         sima_draw_alpha_pixelsf(x, y, ibuf->x, ibuf->y, ibuf->rect_float);
499         }
500         else if (sima->flag & SI_SHOW_ZBUF && (ibuf->zbuf || ibuf->zbuf_float || (ibuf->channels == 1))) {
501                 if (ibuf->zbuf)
502                         sima_draw_zbuf_pixels(x, y, ibuf->x, ibuf->y, ibuf->zbuf);
503                 else if (ibuf->zbuf_float)
504                         sima_draw_zbuffloat_pixels(scene, x, y, ibuf->x, ibuf->y, ibuf->zbuf_float);
505                 else if (ibuf->channels == 1)
506                         sima_draw_zbuffloat_pixels(scene, x, y, ibuf->x, ibuf->y, ibuf->rect_float);
507         }
508         else {
509                 if (sima->flag & SI_USE_ALPHA) {
510                         glEnable(GL_BLEND);
511                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
512
513                         fdrawcheckerboard(x, y, x + ibuf->x * zoomx, y + ibuf->y * zoomy);
514                 }
515
516                 glaDrawImBuf_glsl_ctx(C, ibuf, x, y, GL_NEAREST);
517
518                 if (sima->flag & SI_USE_ALPHA)
519                         glDisable(GL_BLEND);
520         }
521
522         /* reset zoom */
523         glPixelZoom(1.0f, 1.0f);
524 }
525
526 static unsigned int *get_part_from_buffer(unsigned int *buffer, int width, short startx, short starty, short endx, short endy)
527 {
528         unsigned int *rt, *rp, *rectmain;
529         short y, heigth, len;
530
531         /* the right offset in rectot */
532
533         rt = buffer + (starty * width + startx);
534
535         len = (endx - startx);
536         heigth = (endy - starty);
537
538         rp = rectmain = MEM_mallocN(heigth * len * sizeof(int), "rect");
539         
540         for (y = 0; y < heigth; y++) {
541                 memcpy(rp, rt, len * 4);
542                 rt += width;
543                 rp += len;
544         }
545         return rectmain;
546 }
547
548 static void draw_image_buffer_tiled(SpaceImage *sima, ARegion *ar, Scene *scene, Image *ima, ImBuf *ibuf, float fx, float fy, float zoomx, float zoomy)
549 {
550         unsigned char *display_buffer;
551         unsigned int *rect;
552         int dx, dy, sx, sy, x, y;
553         void *cache_handle;
554
555         /* verify valid values, just leave this a while */
556         if (ima->xrep < 1) return;
557         if (ima->yrep < 1) return;
558
559         if (ima->flag & IMA_VIEW_AS_RENDER)
560                 display_buffer = IMB_display_buffer_acquire(ibuf, &scene->view_settings, &scene->display_settings, &cache_handle);
561         else
562                 display_buffer = IMB_display_buffer_acquire(ibuf, NULL, &scene->display_settings, &cache_handle);
563
564         if (!display_buffer)
565                 return;
566
567         glPixelZoom(zoomx, zoomy);
568
569         if (sima->curtile >= ima->xrep * ima->yrep)
570                 sima->curtile = ima->xrep * ima->yrep - 1;
571         
572         /* retrieve part of image buffer */
573         dx = max_ii(ibuf->x / ima->xrep, 1);
574         dy = max_ii(ibuf->y / ima->yrep, 1);
575         sx = (sima->curtile % ima->xrep) * dx;
576         sy = (sima->curtile / ima->xrep) * dy;
577         rect = get_part_from_buffer((unsigned int *)display_buffer, ibuf->x, sx, sy, sx + dx, sy + dy);
578         
579         /* draw repeated */
580         for (sy = 0; sy + dy <= ibuf->y; sy += dy) {
581                 for (sx = 0; sx + dx <= ibuf->x; sx += dx) {
582                         UI_view2d_view_to_region(&ar->v2d, fx + (float)sx / (float)ibuf->x, fy + (float)sy / (float)ibuf->y, &x, &y);
583
584                         glaDrawPixelsSafe(x, y, dx, dy, dx, GL_RGBA, GL_UNSIGNED_BYTE, rect);
585                 }
586         }
587
588         glPixelZoom(1.0f, 1.0f);
589
590         IMB_display_buffer_release(cache_handle);
591
592         MEM_freeN(rect);
593 }
594
595 static void draw_image_buffer_repeated(const bContext *C, SpaceImage *sima, ARegion *ar, Scene *scene, Image *ima, ImBuf *ibuf, float zoomx, float zoomy)
596 {
597         const double time_current = PIL_check_seconds_timer();
598
599         const int xmax = ceil(ar->v2d.cur.xmax);
600         const int ymax = ceil(ar->v2d.cur.ymax);
601         const int xmin = floor(ar->v2d.cur.xmin);
602         const int ymin = floor(ar->v2d.cur.ymin);
603
604         int x;
605
606         for (x = xmin; x < xmax; x++) {
607                 int y;
608                 for (y = ymin; y < ymax; y++) {
609                         if (ima && (ima->tpageflag & IMA_TILES))
610                                 draw_image_buffer_tiled(sima, ar, scene, ima, ibuf, x, y, zoomx, zoomy);
611                         else
612                                 draw_image_buffer(C, sima, ar, scene, ibuf, x, y, zoomx, zoomy);
613
614                         /* only draw until running out of time */
615                         if ((PIL_check_seconds_timer() - time_current) > 0.25)
616                                 return;
617                 }
618         }
619 }
620
621 /* draw uv edit */
622
623 /* draw grease pencil */
624 void draw_image_grease_pencil(bContext *C, bool onlyv2d)
625 {
626         /* draw in View2D space? */
627         if (onlyv2d) {
628                 /* draw grease-pencil ('image' strokes) */
629                 ED_gpencil_draw_2dimage(C);
630         }
631         else {
632                 /* assume that UI_view2d_restore(C) has been called... */
633                 //SpaceImage *sima = (SpaceImage *)CTX_wm_space_data(C);
634                 
635                 /* draw grease-pencil ('screen' strokes) */
636                 ED_gpencil_draw_view2d(C, 0);
637         }
638 }
639
640 void draw_image_sample_line(SpaceImage *sima)
641 {
642         if (sima->sample_line_hist.flag & HISTO_FLAG_SAMPLELINE) {
643                 Histogram *hist = &sima->sample_line_hist;
644
645                 glBegin(GL_LINES);
646                 glColor3ub(0, 0, 0);
647                 glVertex2fv(hist->co[0]);
648                 glVertex2fv(hist->co[1]);
649                 glEnd();
650
651                 setlinestyle(1);
652                 glBegin(GL_LINES);
653                 glColor3ub(255, 255, 255);
654                 glVertex2fv(hist->co[0]);
655                 glVertex2fv(hist->co[1]);
656                 glEnd();
657                 setlinestyle(0);
658
659         }
660 }
661
662 /* XXX becomes WM paint cursor */
663 #if 0
664 static void draw_image_view_tool(Scene *scene)
665 {
666         ToolSettings *settings = scene->toolsettings;
667         Brush *brush = settings->imapaint.brush;
668         int mval[2];
669         float radius;
670         int draw = 0;
671
672         if (brush) {
673                 if (settings->imapaint.flag & IMAGEPAINT_DRAWING) {
674                         if (settings->imapaint.flag & IMAGEPAINT_DRAW_TOOL_DRAWING)
675                                 draw = 1;
676                 }
677                 else if (settings->imapaint.flag & IMAGEPAINT_DRAW_TOOL)
678                         draw = 1;
679                 
680                 if (draw) {
681                         getmouseco_areawin(mval);
682
683                         radius = BKE_brush_size_get(brush) * G.sima->zoom;
684                         fdrawXORcirc(mval[0], mval[1], radius);
685
686                         if (brush->innerradius != 1.0) {
687                                 radius *= brush->innerradius;
688                                 fdrawXORcirc(mval[0], mval[1], radius);
689                         }
690                 }
691         }
692 }
693 #endif
694
695 static unsigned char *get_alpha_clone_image(const bContext *C, Scene *scene, int *width, int *height)
696 {
697         Brush *brush = BKE_paint_brush(&scene->toolsettings->imapaint.paint);
698         ImBuf *ibuf;
699         unsigned int size, alpha;
700         unsigned char *display_buffer;
701         unsigned char *rect, *cp;
702         void *cache_handle;
703
704         if (!brush || !brush->clone.image)
705                 return NULL;
706         
707         ibuf = BKE_image_acquire_ibuf(brush->clone.image, NULL, NULL);
708
709         if (!ibuf)
710                 return NULL;
711
712         display_buffer = IMB_display_buffer_acquire_ctx(C, ibuf, &cache_handle);
713
714         if (!display_buffer) {
715                 BKE_image_release_ibuf(brush->clone.image, ibuf, NULL);
716                 IMB_display_buffer_release(cache_handle);
717
718                 return NULL;
719         }
720
721         rect = MEM_dupallocN(display_buffer);
722
723         IMB_display_buffer_release(cache_handle);
724
725         if (!rect) {
726                 BKE_image_release_ibuf(brush->clone.image, ibuf, NULL);
727                 return NULL;
728         }
729
730         *width = ibuf->x;
731         *height = ibuf->y;
732
733         size = (*width) * (*height);
734         alpha = (unsigned char)255 * brush->clone.alpha;
735         cp = rect;
736
737         while (size-- > 0) {
738                 cp[3] = alpha;
739                 cp += 4;
740         }
741
742         BKE_image_release_ibuf(brush->clone.image, ibuf, NULL);
743
744         return rect;
745 }
746
747 static void draw_image_paint_helpers(const bContext *C, ARegion *ar, Scene *scene, float zoomx, float zoomy)
748 {
749         Brush *brush;
750         int x, y, w, h;
751         unsigned char *clonerect;
752
753         brush = BKE_paint_brush(&scene->toolsettings->imapaint.paint);
754
755         if (brush && (brush->imagepaint_tool == PAINT_TOOL_CLONE)) {
756                 /* this is not very efficient, but glDrawPixels doesn't allow
757                  * drawing with alpha */
758                 clonerect = get_alpha_clone_image(C, scene, &w, &h);
759
760                 if (clonerect) {
761                         UI_view2d_view_to_region(&ar->v2d, brush->clone.offset[0], brush->clone.offset[1], &x, &y);
762
763                         glPixelZoom(zoomx, zoomy);
764
765                         glEnable(GL_BLEND);
766                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
767                         glaDrawPixelsSafe(x, y, w, h, w, GL_RGBA, GL_UNSIGNED_BYTE, clonerect);
768                         glDisable(GL_BLEND);
769
770                         glPixelZoom(1.0, 1.0);
771
772                         MEM_freeN(clonerect);
773                 }
774         }
775 }
776
777 /* draw main image area */
778
779 void draw_image_main(const bContext *C, ARegion *ar)
780 {
781         SpaceImage *sima = CTX_wm_space_image(C);
782         Scene *scene = CTX_data_scene(C);
783         Image *ima;
784         ImBuf *ibuf;
785         float zoomx, zoomy;
786         bool show_viewer, show_render, show_paint, show_stereo3d, show_multilayer;
787         void *lock;
788
789         /* XXX can we do this in refresh? */
790 #if 0
791         what_image(sima);
792         
793         if (sima->image) {
794                 ED_image_get_aspect(sima->image, &xuser_asp, &yuser_asp);
795                 
796                 /* UGLY hack? until now iusers worked fine... but for flipbook viewer we need this */
797                 if (sima->image->type == IMA_TYPE_COMPOSITE) {
798                         ImageUser *iuser = ntree_get_active_iuser(scene->nodetree);
799                         if (iuser) {
800                                 BKE_image_user_calc_imanr(iuser, scene->r.cfra, 0);
801                                 sima->iuser = *iuser;
802                         }
803                 }
804                 /* and we check for spare */
805                 ibuf = ED_space_image_buffer(sima);
806         }
807 #endif
808
809         /* retrieve the image and information about it */
810         ima = ED_space_image(sima);
811         ED_space_image_get_zoom(sima, ar, &zoomx, &zoomy);
812
813         show_viewer = (ima && ima->source == IMA_SRC_VIEWER) != 0;
814         show_render = (show_viewer && ima->type == IMA_TYPE_R_RESULT) != 0;
815         show_paint = (ima && (sima->mode == SI_MODE_PAINT) && (show_viewer == false) && (show_render == false));
816         show_stereo3d = (ima && (ima->flag & IMA_IS_STEREO) && (sima->iuser.flag & IMA_SHOW_STEREO));
817         show_multilayer = ima && BKE_image_is_multilayer(ima);
818
819         if (show_viewer) {
820                 /* use locked draw for drawing viewer image buffer since the compositor
821                  * is running in separated thread and compositor could free this buffers.
822                  * other images are not modifying in such a way so they does not require
823                  * lock (sergey)
824                  */
825                 BLI_lock_thread(LOCK_DRAW_IMAGE);
826         }
827
828         if (show_stereo3d) {
829                 if (show_multilayer)
830                         /* update multiindex and pass for the current eye */
831                         BKE_image_multilayer_index(ima->rr, &sima->iuser);
832                 else
833                         BKE_image_multiview_index(ima, &sima->iuser);
834         }
835
836         ibuf = ED_space_image_acquire_buffer(sima, &lock);
837
838         /* draw the image or grid */
839         if (ibuf == NULL) {
840                 ED_region_grid_draw(ar, zoomx, zoomy);
841         }
842         else {
843
844                 if (sima->flag & SI_DRAW_TILE)
845                         draw_image_buffer_repeated(C, sima, ar, scene, ima, ibuf, zoomx, zoomy);
846                 else if (ima && (ima->tpageflag & IMA_TILES))
847                         draw_image_buffer_tiled(sima, ar, scene, ima, ibuf, 0.0f, 0.0, zoomx, zoomy);
848                 else
849                         draw_image_buffer(C, sima, ar, scene, ibuf, 0.0f, 0.0f, zoomx, zoomy);
850                 
851                 if (sima->flag & SI_DRAW_METADATA) {
852                         int x, y;
853                         rctf frame;
854
855                         BLI_rctf_init(&frame, 0.0f, ibuf->x, 0.0f, ibuf->y);
856                         UI_view2d_view_to_region(&ar->v2d, 0.0f, 0.0f, &x, &y);
857
858                         ED_region_image_metadata_draw(x, y, ibuf, frame, zoomx, zoomy);
859                 }
860         }
861
862         ED_space_image_release_buffer(sima, ibuf, lock);
863
864         /* paint helpers */
865         if (show_paint)
866                 draw_image_paint_helpers(C, ar, scene, zoomx, zoomy);
867
868         /* XXX integrate this code */
869 #if 0
870         if (ibuf) {
871                 float xoffs = 0.0f, yoffs = 0.0f;
872                 
873                 if (image_preview_active(sa, &xim, &yim)) {
874                         xoffs = scene->r.disprect.xmin;
875                         yoffs = scene->r.disprect.ymin;
876                         glColor3ub(0, 0, 0);
877                         calc_image_view(sima, 'f');
878                         myortho2(G.v2d->cur.xmin, G.v2d->cur.xmax, G.v2d->cur.ymin, G.v2d->cur.ymax);
879                         glRectf(0.0f, 0.0f, 1.0f, 1.0f);
880                         glLoadIdentity();
881                 }
882         }
883 #endif
884
885         if (show_viewer) {
886                 BLI_unlock_thread(LOCK_DRAW_IMAGE);
887         }
888
889         /* render info */
890         if (ima && show_render)
891                 draw_render_info(C, sima->iuser.scene, ima, ar, zoomx, zoomy);
892 }
893
894 bool ED_space_image_show_cache(SpaceImage *sima)
895 {
896         Image *image = ED_space_image(sima);
897         Mask *mask = NULL;
898         if (sima->mode == SI_MODE_MASK) {
899                 mask = ED_space_image_get_mask(sima);
900         }
901         if (image == NULL && mask == NULL) {
902                 return false;
903         }
904         if (mask == NULL) {
905                 return ELEM(image->source, IMA_SRC_SEQUENCE, IMA_SRC_MOVIE);
906         }
907         return true;
908 }
909
910 void draw_image_cache(const bContext *C, ARegion *ar)
911 {
912         SpaceImage *sima = CTX_wm_space_image(C);
913         Scene *scene = CTX_data_scene(C);
914         Image *image = ED_space_image(sima);
915         float x, cfra = CFRA, sfra = SFRA, efra = EFRA, framelen = ar->winx / (efra - sfra + 1);
916         Mask *mask = NULL;
917
918         if (!ED_space_image_show_cache(sima)) {
919                 return;
920         }
921
922         if (sima->mode == SI_MODE_MASK) {
923                 mask = ED_space_image_get_mask(sima);
924         }
925
926         glEnable(GL_BLEND);
927         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
928
929         /* Draw cache background. */
930         ED_region_cache_draw_background(ar);
931
932         /* Draw cached segments. */
933         if (image != NULL && image->cache != NULL && ELEM(image->source, IMA_SRC_SEQUENCE, IMA_SRC_MOVIE)) {
934                 int num_segments = 0;
935                 int *points = NULL;
936
937                 IMB_moviecache_get_cache_segments(image->cache, IMB_PROXY_NONE, 0, &num_segments, &points);
938                 ED_region_cache_draw_cached_segments(ar, num_segments, points, sfra + sima->iuser.offset, efra + sima->iuser.offset);
939         }
940
941         glDisable(GL_BLEND);
942
943         /* Draw current frame. */
944         x = (cfra - sfra) / (efra - sfra + 1) * ar->winx;
945
946         UI_ThemeColor(TH_CFRAME);
947         glRecti(x, 0, x + ceilf(framelen), 8 * UI_DPI_FAC);
948         ED_region_cache_draw_curfra_label(cfra, x, 8.0f * UI_DPI_FAC);
949
950         if (mask != NULL) {
951                 ED_mask_draw_frames(mask, ar, cfra, sfra, efra);
952         }
953 }