Color info in node editor backdrop now supports color management.
[blender.git] / source / blender / editors / space_image / image_draw.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * Contributor(s): Blender Foundation, 2002-2009
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/space_image/image_draw.c
29  *  \ingroup spimage
30  */
31
32
33 #include <math.h>
34 #include <stdlib.h>
35 #include <string.h>
36
37 #include "MEM_guardedalloc.h"
38
39 #include "DNA_camera_types.h"
40 #include "DNA_object_types.h"
41 #include "DNA_space_types.h"
42 #include "DNA_scene_types.h"
43 #include "DNA_screen_types.h"
44 #include "DNA_brush_types.h"
45
46 #include "PIL_time.h"
47
48 #include "BLI_math.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
56 #include "BKE_context.h"
57 #include "BKE_global.h"
58 #include "BKE_image.h"
59 #include "BKE_paint.h"
60
61 #ifdef WITH_LCMS
62 #include "BKE_colortools.h"
63 #endif
64
65 #include "BIF_gl.h"
66 #include "BIF_glutil.h"
67
68 #include "BLF_api.h"
69
70 #include "ED_gpencil.h"
71 #include "ED_image.h"
72
73 #include "UI_interface.h"
74 #include "UI_resources.h"
75 #include "UI_view2d.h"
76
77
78 #include "RE_pipeline.h"
79
80 #include "image_intern.h"
81
82 #define HEADER_HEIGHT 18
83
84 static void image_verify_buffer_float(Image *ima, ImBuf *ibuf, int color_manage)
85 {
86         /* detect if we need to redo the curve map.
87            ibuf->rect is zero for compositor and render results after change 
88            convert to 32 bits always... drawing float rects isnt supported well (atis)
89         
90            NOTE: if float buffer changes, we have to manually remove the rect
91         */
92
93         if(ibuf->rect_float && (ibuf->rect==NULL || (ibuf->userflags & IB_RECT_INVALID)) ) {
94                 if(color_manage) {
95                         if(ima && ima->source == IMA_SRC_VIEWER)
96                                 ibuf->profile = IB_PROFILE_LINEAR_RGB;
97                 }
98                 else
99                         ibuf->profile = IB_PROFILE_NONE;
100
101                 IMB_rect_from_float(ibuf);
102         }
103 }
104
105 static void draw_render_info(Scene *scene, Image *ima, ARegion *ar)
106 {
107         RenderResult *rr;
108         rcti rect;
109         float colf[3];
110         
111         rr= BKE_image_acquire_renderresult(scene, ima);
112
113         if(rr && rr->text) {
114                 rect= ar->winrct;
115                 rect.xmin= 0;
116                 rect.ymin= ar->winrct.ymax - ar->winrct.ymin - HEADER_HEIGHT;
117                 rect.xmax= ar->winrct.xmax - ar->winrct.xmin;
118                 rect.ymax= ar->winrct.ymax - ar->winrct.ymin;
119                 
120                 /* clear header rect */
121                 UI_GetThemeColor3fv(TH_BACK, colf);
122                 glEnable(GL_BLEND);
123                 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
124                 glColor4f(colf[0]+0.1f, colf[1]+0.1f, colf[2]+0.1f, 0.5f);
125                 glRecti(rect.xmin, rect.ymin, rect.xmax, rect.ymax+1);
126                 glDisable(GL_BLEND);
127                 
128                 UI_ThemeColor(TH_TEXT_HI);
129
130                 UI_DrawString(12, rect.ymin + 5, rr->text);
131         }
132
133         BKE_image_release_renderresult(scene, ima);
134 }
135
136 void draw_image_info(ARegion *ar, int color_manage, int channels, int x, int y, char *cp, float *fp, int *zp, float *zpf)
137 {
138         char str[256];
139         float dx= 6;
140         /* text colors */
141         /* XXX colored text not allowed in Blender UI */
142         #if 0
143         unsigned char red[3] = {255, 50, 50};
144         unsigned char green[3] = {0, 255, 0};
145         unsigned char blue[3] = {100, 100, 255};
146         #else
147         unsigned char red[3] = {255, 255, 255};
148         unsigned char green[3] = {255, 255, 255};
149         unsigned char blue[3] = {255, 255, 255};
150         #endif
151         float hue=0, sat=0, val=0, lum=0, u=0, v=0;
152         float col[4], finalcol[4];
153
154         glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
155         glEnable(GL_BLEND);
156
157         /* noisy, high contrast make impossible to read if lower alpha is used. */
158         glColor4ub(0, 0, 0, 190);
159         glRecti(0.0, 0.0, ar->winrct.xmax - ar->winrct.xmin + 1, 20);
160         glDisable(GL_BLEND);
161
162         BLF_size(blf_mono_font, 11, 72);
163
164         glColor3ub(255, 255, 255);
165         sprintf(str, "X:%-4d  Y:%-4d |", x, y);
166         // UI_DrawString(6, 6, str); // works ok but fixed width is nicer.
167         BLF_position(blf_mono_font, dx, 6, 0);
168         BLF_draw_ascii(blf_mono_font, str, sizeof(str));
169         dx += BLF_width(blf_mono_font, str);
170
171         if(zp) {
172                 glColor3ub(255, 255, 255);
173                 sprintf(str, " Z:%-.4f |", 0.5f+0.5f*(((float)*zp)/(float)0x7fffffff));
174                 BLF_position(blf_mono_font, dx, 6, 0);
175                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
176                 dx += BLF_width(blf_mono_font, str);
177         }
178         if(zpf) {
179                 glColor3ub(255, 255, 255);
180                 sprintf(str, " Z:%-.3f |", *zpf);
181                 BLF_position(blf_mono_font, dx, 6, 0);
182                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
183                 dx += BLF_width(blf_mono_font, str);
184         }
185
186         if(channels >= 3) {
187                 glColor3ubv(red);
188                 if (fp)
189                         sprintf(str, "  R:%-.4f", fp[0]);
190                 else if (cp)
191                         sprintf(str, "  R:%-3d", cp[0]);
192                 else
193                         sprintf(str, "  R:-");
194                 BLF_position(blf_mono_font, dx, 6, 0);
195                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
196                 dx += BLF_width(blf_mono_font, str);
197                 
198                 glColor3ubv(green);
199                 if (fp)
200                         sprintf(str, "  G:%-.4f", fp[1]);
201                 else if (cp)
202                         sprintf(str, "  G:%-3d", cp[1]);
203                 else
204                         sprintf(str, "  G:-");
205                 BLF_position(blf_mono_font, dx, 6, 0);
206                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
207                 dx += BLF_width(blf_mono_font, str);
208                 
209                 glColor3ubv(blue);
210                 if (fp)
211                         sprintf(str, "  B:%-.4f", fp[2]);
212                 else if (cp)
213                         sprintf(str, "  B:%-3d", cp[2]);
214                 else
215                         sprintf(str, "  B:-");
216                 BLF_position(blf_mono_font, dx, 6, 0);
217                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
218                 dx += BLF_width(blf_mono_font, str);
219                 
220                 if(channels == 4) {
221                         glColor3ub(255, 255, 255);
222                         if (fp)
223                                 sprintf(str, "  A:%-.4f", fp[3]);
224                         else if (cp)
225                                 sprintf(str, "  A:%-3d", cp[3]);
226                         else
227                                 sprintf(str, "- ");
228                         BLF_position(blf_mono_font, dx, 6, 0);
229                         BLF_draw_ascii(blf_mono_font, str, sizeof(str));
230                         dx += BLF_width(blf_mono_font, str);
231                 }
232         }
233         
234         /* color rectangle */
235         if (channels==1) {
236                 if (fp)
237                         col[0] = col[1] = col[2] = fp[0];
238                 else if (cp)
239                         col[0] = col[1] = col[2] = (float)cp[0]/255.0f;
240                 else
241                         col[0] = col[1] = col[2] = 0.0f;
242         }
243         else if (channels==3) {
244                 if (fp)
245                         copy_v3_v3(col, fp);
246                 else if (cp) {
247                         col[0] = (float)cp[0]/255.0f;
248                         col[1] = (float)cp[1]/255.0f;
249                         col[2] = (float)cp[2]/255.0f;
250                 }
251                 else
252                         zero_v3(col);
253         }
254         else if (channels==4) {
255                 if (fp)
256                         copy_v4_v4(col, fp);
257                 else if (cp) {
258                         col[0] = (float)cp[0]/255.0f;
259                         col[1] = (float)cp[1]/255.0f;
260                         col[2] = (float)cp[2]/255.0f;
261                         col[3] = (float)cp[3]/255.0f;
262                 }
263                 else
264                         zero_v4(col);
265         }
266         if (color_manage) {
267                 linearrgb_to_srgb_v3_v3(finalcol, col);
268                 finalcol[3] = col[3];
269         }
270         else {
271                 copy_v4_v4(finalcol, col);
272         }
273         glDisable(GL_BLEND);
274         glColor3fv(finalcol);
275         dx += 5;
276         glBegin(GL_QUADS);
277         glVertex2f(dx, 3);
278         glVertex2f(dx, 17);
279         glVertex2f(dx+30, 17);
280         glVertex2f(dx+30, 3);
281         glEnd();
282         dx += 35;
283
284         glColor3ub(255, 255, 255);
285         if(channels == 1) {
286                 if (fp) {
287                         rgb_to_hsv(fp[0], fp[0], fp[0], &hue, &sat, &val);
288                         rgb_to_yuv(fp[0], fp[0], fp[0], &lum, &u, &v);
289                 }
290                 else if (cp) {
291                         rgb_to_hsv((float)cp[0]/255.0f, (float)cp[0]/255.0f, (float)cp[0]/255.0f, &hue, &sat, &val);
292                         rgb_to_yuv((float)cp[0]/255.0f, (float)cp[0]/255.0f, (float)cp[0]/255.0f, &lum, &u, &v);
293                 }
294                 
295                 sprintf(str, "V:%-.4f", val);
296                 BLF_position(blf_mono_font, dx, 6, 0);
297                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
298                 dx += BLF_width(blf_mono_font, str);
299
300                 sprintf(str, "   L:%-.4f", lum);
301                 BLF_position(blf_mono_font, dx, 6, 0);
302                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
303                 dx += BLF_width(blf_mono_font, str);
304         }
305         else if(channels >= 3) {
306                 if (fp) {
307                         rgb_to_hsv(fp[0], fp[1], fp[2], &hue, &sat, &val);
308                         rgb_to_yuv(fp[0], fp[1], fp[2], &lum, &u, &v);
309                 }
310                 else if (cp) {
311                         rgb_to_hsv((float)cp[0]/255.0f, (float)cp[1]/255.0f, (float)cp[2]/255.0f, &hue, &sat, &val);
312                         rgb_to_yuv((float)cp[0]/255.0f, (float)cp[1]/255.0f, (float)cp[2]/255.0f, &lum, &u, &v);
313                 }
314
315                 sprintf(str, "H:%-.4f", hue);
316                 BLF_position(blf_mono_font, dx, 6, 0);
317                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
318                 dx += BLF_width(blf_mono_font, str);
319
320                 sprintf(str, "  S:%-.4f", sat);
321                 BLF_position(blf_mono_font, dx, 6, 0);
322                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
323                 dx += BLF_width(blf_mono_font, str);
324
325                 sprintf(str, "  V:%-.4f", val);
326                 BLF_position(blf_mono_font, dx, 6, 0);
327                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
328                 dx += BLF_width(blf_mono_font, str);
329
330                 sprintf(str, "   L:%-.4f", lum);
331                 BLF_position(blf_mono_font, dx, 6, 0);
332                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
333                 dx += BLF_width(blf_mono_font, str);
334         }
335 }
336
337 /* image drawing */
338
339 static void draw_image_grid(ARegion *ar, float zoomx, float zoomy)
340 {
341         float gridsize, gridstep= 1.0f/32.0f;
342         float fac, blendfac;
343         int x1, y1, x2, y2;
344         
345         /* the image is located inside (0,0),(1, 1) as set by view2d */
346         UI_ThemeColorShade(TH_BACK, 20);
347
348         UI_view2d_to_region_no_clip(&ar->v2d, 0.0f, 0.0f, &x1, &y1);
349         UI_view2d_to_region_no_clip(&ar->v2d, 1.0f, 1.0f, &x2, &y2);
350         glRectf(x1, y1, x2, y2);
351
352         /* gridsize adapted to zoom level */
353         gridsize= 0.5f*(zoomx+zoomy);
354         if(gridsize<=0.0f) return;
355         
356         if(gridsize<1.0f) {
357                 while(gridsize<1.0f) {
358                         gridsize*= 4.0f;
359                         gridstep*= 4.0f;
360                 }
361         }
362         else {
363                 while(gridsize>=4.0f) {
364                         gridsize/= 4.0f;
365                         gridstep/= 4.0f;
366                 }
367         }
368         
369         /* the fine resolution level */
370         blendfac= 0.25f*gridsize - floorf(0.25f*gridsize);
371         CLAMP(blendfac, 0.0f, 1.0f);
372         UI_ThemeColorShade(TH_BACK, (int)(20.0f*(1.0f-blendfac)));
373         
374         fac= 0.0f;
375         glBegin(GL_LINES);
376         while(fac<1.0f) {
377                 glVertex2f(x1, y1*(1.0f-fac) + y2*fac);
378                 glVertex2f(x2, y1*(1.0f-fac) + y2*fac);
379                 glVertex2f(x1*(1.0f-fac) + x2*fac, y1);
380                 glVertex2f(x1*(1.0f-fac) + x2*fac, y2);
381                 fac+= gridstep;
382         }
383         
384         /* the large resolution level */
385         UI_ThemeColor(TH_BACK);
386         
387         fac= 0.0f;
388         while(fac<1.0f) {
389                 glVertex2f(x1, y1*(1.0f-fac) + y2*fac);
390                 glVertex2f(x2, y1*(1.0f-fac) + y2*fac);
391                 glVertex2f(x1*(1.0f-fac) + x2*fac, y1);
392                 glVertex2f(x1*(1.0f-fac) + x2*fac, y2);
393                 fac+= 4.0f*gridstep;
394         }
395         glEnd();
396 }
397
398 static void sima_draw_alpha_backdrop(float x1, float y1, float xsize, float ysize, float zoomx, float zoomy, unsigned char col1[3], unsigned char col2[3])
399 {
400         GLubyte checker_stipple[32*32/8] =
401         {
402                 255,255,0,0,255,255,0,0,255,255,0,0,255,255,0,0, \
403                 255,255,0,0,255,255,0,0,255,255,0,0,255,255,0,0, \
404                 255,255,0,0,255,255,0,0,255,255,0,0,255,255,0,0, \
405                 255,255,0,0,255,255,0,0,255,255,0,0,255,255,0,0, \
406                 0,0,255,255,0,0,255,255,0,0,255,255,0,0,255,255, \
407                 0,0,255,255,0,0,255,255,0,0,255,255,0,0,255,255, \
408                 0,0,255,255,0,0,255,255,0,0,255,255,0,0,255,255, \
409                 0,0,255,255,0,0,255,255,0,0,255,255,0,0,255,255, \
410         };
411         
412         glColor3ubv(col1);
413         glRectf(x1, y1, x1 + zoomx*xsize, y1 + zoomy*ysize);
414         glColor3ubv(col2);
415
416         glEnable(GL_POLYGON_STIPPLE);
417         glPolygonStipple(checker_stipple);
418         glRectf(x1, y1, x1 + zoomx*xsize, y1 + zoomy*ysize);
419         glDisable(GL_POLYGON_STIPPLE);
420 }
421
422 static void sima_draw_alpha_pixels(float x1, float y1, int rectx, int recty, unsigned int *recti)
423 {
424         
425         /* swap bytes, so alpha is most significant one, then just draw it as luminance int */
426         if(ENDIAN_ORDER == B_ENDIAN)
427                 glPixelStorei(GL_UNPACK_SWAP_BYTES, 1);
428
429         glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_LUMINANCE, GL_UNSIGNED_INT, recti);
430         glPixelStorei(GL_UNPACK_SWAP_BYTES, 0);
431 }
432
433 static void sima_draw_alpha_pixelsf(float x1, float y1, int rectx, int recty, float *rectf)
434 {
435         float *trectf= MEM_mallocN(rectx*recty*4, "temp");
436         int a, b;
437         
438         for(a= rectx*recty -1, b= 4*a+3; a>=0; a--, b-=4)
439                 trectf[a]= rectf[b];
440         
441         glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_LUMINANCE, GL_FLOAT, trectf);
442         MEM_freeN(trectf);
443         /* ogl trick below is slower... (on ATI 9600) */
444 //      glColorMask(1, 0, 0, 0);
445 //      glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_RGBA, GL_FLOAT, rectf+3);
446 //      glColorMask(0, 1, 0, 0);
447 //      glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_RGBA, GL_FLOAT, rectf+2);
448 //      glColorMask(0, 0, 1, 0);
449 //      glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_RGBA, GL_FLOAT, rectf+1);
450 //      glColorMask(1, 1, 1, 1);
451 }
452
453 #ifdef WITH_LCMS
454 static int sima_draw_colorcorrected_pixels(float x1, float y1, ImBuf *ibuf)
455 {
456         colorcorrection_do_ibuf(ibuf, "MONOSCNR.ICM"); /* path is hardcoded here, find some place better */
457
458         if(ibuf->crect) {
459                 glaDrawPixelsSafe(x1, y1, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->crect);
460                 return 1;
461         }
462
463         return 0;
464 }
465 #endif
466
467 static void sima_draw_zbuf_pixels(float x1, float y1, int rectx, int recty, int *recti)
468 {
469         /* zbuffer values are signed, so we need to shift color range */
470         glPixelTransferf(GL_RED_SCALE, 0.5f);
471         glPixelTransferf(GL_GREEN_SCALE, 0.5f);
472         glPixelTransferf(GL_BLUE_SCALE, 0.5f);
473         glPixelTransferf(GL_RED_BIAS, 0.5f);
474         glPixelTransferf(GL_GREEN_BIAS, 0.5f);
475         glPixelTransferf(GL_BLUE_BIAS, 0.5f);
476         
477         glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_LUMINANCE, GL_INT, recti);
478         
479         glPixelTransferf(GL_RED_SCALE, 1.0f);
480         glPixelTransferf(GL_GREEN_SCALE, 1.0f);
481         glPixelTransferf(GL_BLUE_SCALE, 1.0f);
482         glPixelTransferf(GL_RED_BIAS, 0.0f);
483         glPixelTransferf(GL_GREEN_BIAS, 0.0f);
484         glPixelTransferf(GL_BLUE_BIAS, 0.0f);
485 }
486
487 static void sima_draw_zbuffloat_pixels(Scene *scene, float x1, float y1, int rectx, int recty, float *rect_float)
488 {
489         float bias, scale, *rectf, clipend;
490         int a;
491         
492         if(scene->camera && scene->camera->type==OB_CAMERA) {
493                 bias= ((Camera *)scene->camera->data)->clipsta;
494                 clipend= ((Camera *)scene->camera->data)->clipend;
495                 scale= 1.0f/(clipend-bias);
496         }
497         else {
498                 bias= 0.1f;
499                 scale= 0.01f;
500                 clipend= 100.0f;
501         }
502         
503         rectf= MEM_mallocN(rectx*recty*4, "temp");
504         for(a= rectx*recty -1; a>=0; a--) {
505                 if(rect_float[a]>clipend)
506                         rectf[a]= 0.0f;
507                 else if(rect_float[a]<bias)
508                         rectf[a]= 1.0f;
509                 else {
510                         rectf[a]= 1.0f - (rect_float[a]-bias)*scale;
511                         rectf[a]*= rectf[a];
512                 }
513         }
514         glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_LUMINANCE, GL_FLOAT, rectf);
515         
516         MEM_freeN(rectf);
517 }
518
519 static void draw_image_buffer(SpaceImage *sima, ARegion *ar, Scene *scene, Image *ima, ImBuf *ibuf, float fx, float fy, float zoomx, float zoomy)
520 {
521         int x, y;
522         int color_manage = scene->r.color_mgt_flag & R_COLOR_MANAGEMENT;
523
524         /* set zoom */
525         glPixelZoom(zoomx, zoomy);
526
527         /* find window pixel coordinates of origin */
528         UI_view2d_to_region_no_clip(&ar->v2d, fx, fy, &x, &y);
529
530         /* this part is generic image display */
531         if(sima->flag & SI_SHOW_ALPHA) {
532                 if(ibuf->rect)
533                         sima_draw_alpha_pixels(x, y, ibuf->x, ibuf->y, ibuf->rect);
534                 else if(ibuf->rect_float && ibuf->channels==4)
535                         sima_draw_alpha_pixelsf(x, y, ibuf->x, ibuf->y, ibuf->rect_float);
536         }
537         else if(sima->flag & SI_SHOW_ZBUF && (ibuf->zbuf || ibuf->zbuf_float || (ibuf->channels==1))) {
538                 if(ibuf->zbuf)
539                         sima_draw_zbuf_pixels(x, y, ibuf->x, ibuf->y, ibuf->zbuf);
540                 else if(ibuf->zbuf_float)
541                         sima_draw_zbuffloat_pixels(scene, x, y, ibuf->x, ibuf->y, ibuf->zbuf_float);
542                 else if(ibuf->channels==1)
543                         sima_draw_zbuffloat_pixels(scene, x, y, ibuf->x, ibuf->y, ibuf->rect_float);
544         }
545 #ifdef WITH_LCMS
546         else if(sima->flag & SI_COLOR_CORRECTION) {
547                 image_verify_buffer_float(ima, ibuf, color_manage);
548                 
549                 if(sima_draw_colorcorrected_pixels(x, y, ibuf)==0) {
550                         unsigned char col1[3]= {100, 0, 100}, col2[3]= {160, 0, 160}; /* pink says 'warning' in blender land */
551                         sima_draw_alpha_backdrop(x, y, ibuf->x, ibuf->y, zoomx, zoomy, col1, col2);
552                 }
553
554         }
555 #endif
556         else {
557                 if(sima->flag & SI_USE_ALPHA) {
558                         unsigned char col1[3]= {100, 100, 100}, col2[3]= {160, 160, 160};
559                         sima_draw_alpha_backdrop(x, y, ibuf->x, ibuf->y, zoomx, zoomy, col1, col2);
560
561                         glEnable(GL_BLEND);
562                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
563                 }
564
565                 /* we don't draw floats buffers directly but
566                  * convert them, and optionally apply curves */
567                 image_verify_buffer_float(ima, ibuf, color_manage);
568
569                 if(ibuf->rect)
570                         glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
571                 /*else
572                         glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_FLOAT, ibuf->rect_float);*/
573                 
574                 if(sima->flag & SI_USE_ALPHA)
575                         glDisable(GL_BLEND);
576         }
577
578         /* reset zoom */
579         glPixelZoom(1.0f, 1.0f);
580 }
581
582 static unsigned int *get_part_from_ibuf(ImBuf *ibuf, short startx, short starty, short endx, short endy)
583 {
584         unsigned int *rt, *rp, *rectmain;
585         short y, heigth, len;
586
587         /* the right offset in rectot */
588
589         rt= ibuf->rect+ (starty*ibuf->x+ startx);
590
591         len= (endx-startx);
592         heigth= (endy-starty);
593
594         rp=rectmain= MEM_mallocN(heigth*len*sizeof(int), "rect");
595         
596         for(y=0; y<heigth; y++) {
597                 memcpy(rp, rt, len*4);
598                 rt+= ibuf->x;
599                 rp+= len;
600         }
601         return rectmain;
602 }
603
604 static void draw_image_buffer_tiled(SpaceImage *sima, ARegion *ar, Scene *scene, Image *ima, ImBuf *ibuf, float fx, float fy, float zoomx, float zoomy)
605 {
606         unsigned int *rect;
607         int dx, dy, sx, sy, x, y;
608         int color_manage = scene->r.color_mgt_flag & R_COLOR_MANAGEMENT;
609
610         /* verify valid values, just leave this a while */
611         if(ima->xrep<1) return;
612         if(ima->yrep<1) return;
613         
614         glPixelZoom(zoomx, zoomy);
615
616         if(sima->curtile >= ima->xrep*ima->yrep) 
617                 sima->curtile = ima->xrep*ima->yrep - 1; 
618         
619         /* create char buffer from float if needed */
620         image_verify_buffer_float(ima, ibuf, color_manage);
621
622         /* retrieve part of image buffer */
623         dx= ibuf->x/ima->xrep;
624         dy= ibuf->y/ima->yrep;
625         sx= (sima->curtile % ima->xrep)*dx;
626         sy= (sima->curtile / ima->xrep)*dy;
627         rect= get_part_from_ibuf(ibuf, sx, sy, sx+dx, sy+dy);
628         
629         /* draw repeated */
630         for(sy=0; sy+dy<=ibuf->y; sy+= dy) {
631                 for(sx=0; sx+dx<=ibuf->x; sx+= dx) {
632                         UI_view2d_to_region_no_clip(&ar->v2d, fx + (float)sx/(float)ibuf->x, fy + (float)sy/(float)ibuf->y, &x, &y);
633
634                         glaDrawPixelsSafe(x, y, dx, dy, dx, GL_RGBA, GL_UNSIGNED_BYTE, rect);
635                 }
636         }
637
638         glPixelZoom(1.0f, 1.0f);
639
640         MEM_freeN(rect);
641 }
642
643 static void draw_image_buffer_repeated(SpaceImage *sima, ARegion *ar, Scene *scene, Image *ima, ImBuf *ibuf, float zoomx, float zoomy)
644 {
645         const double time_current= PIL_check_seconds_timer();
646
647         const int xmax= ceil(ar->v2d.cur.xmax);
648         const int ymax= ceil(ar->v2d.cur.ymax);
649         const int xmin= floor(ar->v2d.cur.xmin);
650         const int ymin= floor(ar->v2d.cur.ymin);
651
652         int x;
653
654         for(x=xmin; x<xmax; x++) {
655                 int y;
656                 for(y=ymin; y<ymax; y++) { 
657                         if(ima && (ima->tpageflag & IMA_TILES))
658                                 draw_image_buffer_tiled(sima, ar, scene, ima, ibuf, x, y, zoomx, zoomy);
659                         else
660                                 draw_image_buffer(sima, ar, scene, ima, ibuf, x, y, zoomx, zoomy);
661
662                         /* only draw until running out of time */
663                         if((PIL_check_seconds_timer() - time_current) > 0.25)
664                                 return;
665                 }
666         }
667 }
668
669 /* draw uv edit */
670
671 /* draw grease pencil */
672 void draw_image_grease_pencil(bContext *C, short onlyv2d)
673 {
674         /* draw in View2D space? */
675         if (onlyv2d) {
676                 /* assume that UI_view2d_ortho(C) has been called... */
677                 SpaceImage *sima= (SpaceImage *)CTX_wm_space_data(C);
678                 void *lock;
679                 ImBuf *ibuf= ED_space_image_acquire_buffer(sima, &lock);
680                 
681                 /* draw grease-pencil ('image' strokes) */
682                 //if (sima->flag & SI_DISPGP)
683                         draw_gpencil_2dimage(C, ibuf);
684
685                 ED_space_image_release_buffer(sima, lock);
686         }
687         else {
688                 /* assume that UI_view2d_restore(C) has been called... */
689                 //SpaceImage *sima= (SpaceImage *)CTX_wm_space_data(C);
690                 
691                 /* draw grease-pencil ('screen' strokes) */
692                 //if (sima->flag & SI_DISPGP)
693                         draw_gpencil_view2d(C, 0);
694         }
695 }
696
697 /* XXX becomes WM paint cursor */
698 #if 0
699 static void draw_image_view_tool(Scene *scene)
700 {
701         ToolSettings *settings= scene->toolsettings;
702         Brush *brush= settings->imapaint.brush;
703         short mval[2];
704         float radius;
705         int draw= 0;
706
707         if(brush) {
708                 if(settings->imapaint.flag & IMAGEPAINT_DRAWING) {
709                         if(settings->imapaint.flag & IMAGEPAINT_DRAW_TOOL_DRAWING)
710                                 draw= 1;
711                 }
712                 else if(settings->imapaint.flag & IMAGEPAINT_DRAW_TOOL)
713                         draw= 1;
714                 
715                 if(draw) {
716                         getmouseco_areawin(mval);
717
718                         radius= brush_size(brush)*G.sima->zoom;
719                         fdrawXORcirc(mval[0], mval[1], radius);
720
721                         if (brush->innerradius != 1.0) {
722                                 radius *= brush->innerradius;
723                                 fdrawXORcirc(mval[0], mval[1], radius);
724                         }
725                 }
726         }
727 }
728 #endif
729
730 static unsigned char *get_alpha_clone_image(Scene *scene, int *width, int *height)
731 {
732         Brush *brush = paint_brush(&scene->toolsettings->imapaint.paint);
733         ImBuf *ibuf;
734         unsigned int size, alpha;
735         unsigned char *rect, *cp;
736
737         if(!brush || !brush->clone.image)
738                 return NULL;
739         
740         ibuf= BKE_image_get_ibuf(brush->clone.image, NULL);
741
742         if(!ibuf || !ibuf->rect)
743                 return NULL;
744
745         rect= MEM_dupallocN(ibuf->rect);
746         if(!rect)
747                 return NULL;
748
749         *width= ibuf->x;
750         *height= ibuf->y;
751
752         size= (*width)*(*height);
753         alpha= (unsigned char)255*brush->clone.alpha;
754         cp= rect;
755
756         while(size-- > 0) {
757                 cp[3]= alpha;
758                 cp += 4;
759         }
760
761         return rect;
762 }
763
764 static void draw_image_paint_helpers(ARegion *ar, Scene *scene, float zoomx, float zoomy)
765 {
766         Brush *brush;
767         int x, y, w, h;
768         unsigned char *clonerect;
769
770         brush= paint_brush(&scene->toolsettings->imapaint.paint);
771
772         if(brush && (brush->imagepaint_tool == PAINT_TOOL_CLONE)) {
773                 /* this is not very efficient, but glDrawPixels doesn't allow
774                    drawing with alpha */
775                 clonerect= get_alpha_clone_image(scene, &w, &h);
776
777                 if(clonerect) {
778                         UI_view2d_to_region_no_clip(&ar->v2d, brush->clone.offset[0], brush->clone.offset[1], &x, &y);
779
780                         glPixelZoom(zoomx, zoomy);
781
782                         glEnable(GL_BLEND);
783                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
784                         glaDrawPixelsSafe(x, y, w, h, w, GL_RGBA, GL_UNSIGNED_BYTE, clonerect);
785                         glDisable(GL_BLEND);
786
787                         glPixelZoom(1.0, 1.0);
788
789                         MEM_freeN(clonerect);
790                 }
791         }
792 }
793
794 /* draw main image area */
795
796 void draw_image_main(SpaceImage *sima, ARegion *ar, Scene *scene)
797 {
798         Image *ima;
799         ImBuf *ibuf;
800         float zoomx, zoomy;
801         int show_viewer, show_render;
802         void *lock;
803
804         /* XXX can we do this in refresh? */
805 #if 0
806         what_image(sima);
807         
808         if(sima->image) {
809                 ED_image_aspect(sima->image, &xuser_asp, &yuser_asp);
810                 
811                 /* UGLY hack? until now iusers worked fine... but for flipbook viewer we need this */
812                 if(sima->image->type==IMA_TYPE_COMPOSITE) {
813                         ImageUser *iuser= ntree_get_active_iuser(scene->nodetree);
814                         if(iuser) {
815                                 BKE_image_user_calc_imanr(iuser, scene->r.cfra, 0);
816                                 sima->iuser= *iuser;
817                         }
818                 }
819                 /* and we check for spare */
820                 ibuf= ED_space_image_buffer(sima);
821         }
822 #endif
823
824         /* retrieve the image and information about it */
825         ima= ED_space_image(sima);
826         ED_space_image_zoom(sima, ar, &zoomx, &zoomy);
827         ibuf= ED_space_image_acquire_buffer(sima, &lock);
828
829         show_viewer= (ima && ima->source == IMA_SRC_VIEWER);
830         show_render= (show_viewer && ima->type == IMA_TYPE_R_RESULT);
831
832         /* draw the image or grid */
833         if(ibuf==NULL)
834                 draw_image_grid(ar, zoomx, zoomy);
835         else if(sima->flag & SI_DRAW_TILE)
836                 draw_image_buffer_repeated(sima, ar, scene, ima, ibuf, zoomx, zoomy);
837         else if(ima && (ima->tpageflag & IMA_TILES))
838                 draw_image_buffer_tiled(sima, ar, scene, ima, ibuf, 0.0f, 0.0, zoomx, zoomy);
839         else
840                 draw_image_buffer(sima, ar, scene, ima, ibuf, 0.0f, 0.0f, zoomx, zoomy);
841
842         /* paint helpers */
843         if(sima->flag & SI_DRAWTOOL)
844                 draw_image_paint_helpers(ar, scene, zoomx, zoomy);
845
846
847         /* XXX integrate this code */
848 #if 0
849         if(ibuf) {
850                 float xoffs=0.0f, yoffs= 0.0f;
851                 
852                 if(image_preview_active(sa, &xim, &yim)) {
853                         xoffs= scene->r.disprect.xmin;
854                         yoffs= scene->r.disprect.ymin;
855                         glColor3ub(0,0,0);
856                         calc_image_view(sima, 'f');     
857                         myortho2(G.v2d->cur.xmin, G.v2d->cur.xmax, G.v2d->cur.ymin, G.v2d->cur.ymax);
858                         glRectf(0.0f, 0.0f, 1.0f, 1.0f);
859                         glLoadIdentity();
860                 }
861         }
862 #endif
863
864         ED_space_image_release_buffer(sima, lock);
865
866         /* render info */
867         if(ima && show_render)
868                 draw_render_info(scene, ima, ar);
869 }
870