Code cleanup: warning fixes.
[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         (void)dx;
337 }
338
339 /* image drawing */
340
341 static void draw_image_grid(ARegion *ar, float zoomx, float zoomy)
342 {
343         float gridsize, gridstep= 1.0f/32.0f;
344         float fac, blendfac;
345         int x1, y1, x2, y2;
346         
347         /* the image is located inside (0,0),(1, 1) as set by view2d */
348         UI_ThemeColorShade(TH_BACK, 20);
349
350         UI_view2d_to_region_no_clip(&ar->v2d, 0.0f, 0.0f, &x1, &y1);
351         UI_view2d_to_region_no_clip(&ar->v2d, 1.0f, 1.0f, &x2, &y2);
352         glRectf(x1, y1, x2, y2);
353
354         /* gridsize adapted to zoom level */
355         gridsize= 0.5f*(zoomx+zoomy);
356         if(gridsize<=0.0f) return;
357         
358         if(gridsize<1.0f) {
359                 while(gridsize<1.0f) {
360                         gridsize*= 4.0f;
361                         gridstep*= 4.0f;
362                 }
363         }
364         else {
365                 while(gridsize>=4.0f) {
366                         gridsize/= 4.0f;
367                         gridstep/= 4.0f;
368                 }
369         }
370         
371         /* the fine resolution level */
372         blendfac= 0.25f*gridsize - floorf(0.25f*gridsize);
373         CLAMP(blendfac, 0.0f, 1.0f);
374         UI_ThemeColorShade(TH_BACK, (int)(20.0f*(1.0f-blendfac)));
375         
376         fac= 0.0f;
377         glBegin(GL_LINES);
378         while(fac<1.0f) {
379                 glVertex2f(x1, y1*(1.0f-fac) + y2*fac);
380                 glVertex2f(x2, y1*(1.0f-fac) + y2*fac);
381                 glVertex2f(x1*(1.0f-fac) + x2*fac, y1);
382                 glVertex2f(x1*(1.0f-fac) + x2*fac, y2);
383                 fac+= gridstep;
384         }
385         
386         /* the large resolution level */
387         UI_ThemeColor(TH_BACK);
388         
389         fac= 0.0f;
390         while(fac<1.0f) {
391                 glVertex2f(x1, y1*(1.0f-fac) + y2*fac);
392                 glVertex2f(x2, y1*(1.0f-fac) + y2*fac);
393                 glVertex2f(x1*(1.0f-fac) + x2*fac, y1);
394                 glVertex2f(x1*(1.0f-fac) + x2*fac, y2);
395                 fac+= 4.0f*gridstep;
396         }
397         glEnd();
398 }
399
400 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])
401 {
402         GLubyte checker_stipple[32*32/8] =
403         {
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                 255,255,0,0,255,255,0,0,255,255,0,0,255,255,0,0, \
407                 255,255,0,0,255,255,0,0,255,255,0,0,255,255,0,0, \
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                 0,0,255,255,0,0,255,255,0,0,255,255,0,0,255,255, \
411                 0,0,255,255,0,0,255,255,0,0,255,255,0,0,255,255, \
412         };
413         
414         glColor3ubv(col1);
415         glRectf(x1, y1, x1 + zoomx*xsize, y1 + zoomy*ysize);
416         glColor3ubv(col2);
417
418         glEnable(GL_POLYGON_STIPPLE);
419         glPolygonStipple(checker_stipple);
420         glRectf(x1, y1, x1 + zoomx*xsize, y1 + zoomy*ysize);
421         glDisable(GL_POLYGON_STIPPLE);
422 }
423
424 static void sima_draw_alpha_pixels(float x1, float y1, int rectx, int recty, unsigned int *recti)
425 {
426         
427         /* swap bytes, so alpha is most significant one, then just draw it as luminance int */
428         if(ENDIAN_ORDER == B_ENDIAN)
429                 glPixelStorei(GL_UNPACK_SWAP_BYTES, 1);
430
431         glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_LUMINANCE, GL_UNSIGNED_INT, recti);
432         glPixelStorei(GL_UNPACK_SWAP_BYTES, 0);
433 }
434
435 static void sima_draw_alpha_pixelsf(float x1, float y1, int rectx, int recty, float *rectf)
436 {
437         float *trectf= MEM_mallocN(rectx*recty*4, "temp");
438         int a, b;
439         
440         for(a= rectx*recty -1, b= 4*a+3; a>=0; a--, b-=4)
441                 trectf[a]= rectf[b];
442         
443         glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_LUMINANCE, GL_FLOAT, trectf);
444         MEM_freeN(trectf);
445         /* ogl trick below is slower... (on ATI 9600) */
446 //      glColorMask(1, 0, 0, 0);
447 //      glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_RGBA, GL_FLOAT, rectf+3);
448 //      glColorMask(0, 1, 0, 0);
449 //      glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_RGBA, GL_FLOAT, rectf+2);
450 //      glColorMask(0, 0, 1, 0);
451 //      glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_RGBA, GL_FLOAT, rectf+1);
452 //      glColorMask(1, 1, 1, 1);
453 }
454
455 #ifdef WITH_LCMS
456 static int sima_draw_colorcorrected_pixels(float x1, float y1, ImBuf *ibuf)
457 {
458         colorcorrection_do_ibuf(ibuf, "MONOSCNR.ICM"); /* path is hardcoded here, find some place better */
459
460         if(ibuf->crect) {
461                 glaDrawPixelsSafe(x1, y1, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->crect);
462                 return 1;
463         }
464
465         return 0;
466 }
467 #endif
468
469 static void sima_draw_zbuf_pixels(float x1, float y1, int rectx, int recty, int *recti)
470 {
471         /* zbuffer values are signed, so we need to shift color range */
472         glPixelTransferf(GL_RED_SCALE, 0.5f);
473         glPixelTransferf(GL_GREEN_SCALE, 0.5f);
474         glPixelTransferf(GL_BLUE_SCALE, 0.5f);
475         glPixelTransferf(GL_RED_BIAS, 0.5f);
476         glPixelTransferf(GL_GREEN_BIAS, 0.5f);
477         glPixelTransferf(GL_BLUE_BIAS, 0.5f);
478         
479         glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_LUMINANCE, GL_INT, recti);
480         
481         glPixelTransferf(GL_RED_SCALE, 1.0f);
482         glPixelTransferf(GL_GREEN_SCALE, 1.0f);
483         glPixelTransferf(GL_BLUE_SCALE, 1.0f);
484         glPixelTransferf(GL_RED_BIAS, 0.0f);
485         glPixelTransferf(GL_GREEN_BIAS, 0.0f);
486         glPixelTransferf(GL_BLUE_BIAS, 0.0f);
487 }
488
489 static void sima_draw_zbuffloat_pixels(Scene *scene, float x1, float y1, int rectx, int recty, float *rect_float)
490 {
491         float bias, scale, *rectf, clipend;
492         int a;
493         
494         if(scene->camera && scene->camera->type==OB_CAMERA) {
495                 bias= ((Camera *)scene->camera->data)->clipsta;
496                 clipend= ((Camera *)scene->camera->data)->clipend;
497                 scale= 1.0f/(clipend-bias);
498         }
499         else {
500                 bias= 0.1f;
501                 scale= 0.01f;
502                 clipend= 100.0f;
503         }
504         
505         rectf= MEM_mallocN(rectx*recty*4, "temp");
506         for(a= rectx*recty -1; a>=0; a--) {
507                 if(rect_float[a]>clipend)
508                         rectf[a]= 0.0f;
509                 else if(rect_float[a]<bias)
510                         rectf[a]= 1.0f;
511                 else {
512                         rectf[a]= 1.0f - (rect_float[a]-bias)*scale;
513                         rectf[a]*= rectf[a];
514                 }
515         }
516         glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_LUMINANCE, GL_FLOAT, rectf);
517         
518         MEM_freeN(rectf);
519 }
520
521 static void draw_image_buffer(SpaceImage *sima, ARegion *ar, Scene *scene, Image *ima, ImBuf *ibuf, float fx, float fy, float zoomx, float zoomy)
522 {
523         int x, y;
524         int color_manage = scene->r.color_mgt_flag & R_COLOR_MANAGEMENT;
525
526         /* set zoom */
527         glPixelZoom(zoomx, zoomy);
528
529         /* find window pixel coordinates of origin */
530         UI_view2d_to_region_no_clip(&ar->v2d, fx, fy, &x, &y);
531
532         /* this part is generic image display */
533         if(sima->flag & SI_SHOW_ALPHA) {
534                 if(ibuf->rect)
535                         sima_draw_alpha_pixels(x, y, ibuf->x, ibuf->y, ibuf->rect);
536                 else if(ibuf->rect_float && ibuf->channels==4)
537                         sima_draw_alpha_pixelsf(x, y, ibuf->x, ibuf->y, ibuf->rect_float);
538         }
539         else if(sima->flag & SI_SHOW_ZBUF && (ibuf->zbuf || ibuf->zbuf_float || (ibuf->channels==1))) {
540                 if(ibuf->zbuf)
541                         sima_draw_zbuf_pixels(x, y, ibuf->x, ibuf->y, ibuf->zbuf);
542                 else if(ibuf->zbuf_float)
543                         sima_draw_zbuffloat_pixels(scene, x, y, ibuf->x, ibuf->y, ibuf->zbuf_float);
544                 else if(ibuf->channels==1)
545                         sima_draw_zbuffloat_pixels(scene, x, y, ibuf->x, ibuf->y, ibuf->rect_float);
546         }
547 #ifdef WITH_LCMS
548         else if(sima->flag & SI_COLOR_CORRECTION) {
549                 image_verify_buffer_float(ima, ibuf, color_manage);
550                 
551                 if(sima_draw_colorcorrected_pixels(x, y, ibuf)==0) {
552                         unsigned char col1[3]= {100, 0, 100}, col2[3]= {160, 0, 160}; /* pink says 'warning' in blender land */
553                         sima_draw_alpha_backdrop(x, y, ibuf->x, ibuf->y, zoomx, zoomy, col1, col2);
554                 }
555
556         }
557 #endif
558         else {
559                 if(sima->flag & SI_USE_ALPHA) {
560                         unsigned char col1[3]= {100, 100, 100}, col2[3]= {160, 160, 160};
561                         sima_draw_alpha_backdrop(x, y, ibuf->x, ibuf->y, zoomx, zoomy, col1, col2);
562
563                         glEnable(GL_BLEND);
564                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
565                 }
566
567                 /* we don't draw floats buffers directly but
568                  * convert them, and optionally apply curves */
569                 image_verify_buffer_float(ima, ibuf, color_manage);
570
571                 if(ibuf->rect)
572                         glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
573                 /*else
574                         glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_FLOAT, ibuf->rect_float);*/
575                 
576                 if(sima->flag & SI_USE_ALPHA)
577                         glDisable(GL_BLEND);
578         }
579
580         /* reset zoom */
581         glPixelZoom(1.0f, 1.0f);
582 }
583
584 static unsigned int *get_part_from_ibuf(ImBuf *ibuf, short startx, short starty, short endx, short endy)
585 {
586         unsigned int *rt, *rp, *rectmain;
587         short y, heigth, len;
588
589         /* the right offset in rectot */
590
591         rt= ibuf->rect+ (starty*ibuf->x+ startx);
592
593         len= (endx-startx);
594         heigth= (endy-starty);
595
596         rp=rectmain= MEM_mallocN(heigth*len*sizeof(int), "rect");
597         
598         for(y=0; y<heigth; y++) {
599                 memcpy(rp, rt, len*4);
600                 rt+= ibuf->x;
601                 rp+= len;
602         }
603         return rectmain;
604 }
605
606 static void draw_image_buffer_tiled(SpaceImage *sima, ARegion *ar, Scene *scene, Image *ima, ImBuf *ibuf, float fx, float fy, float zoomx, float zoomy)
607 {
608         unsigned int *rect;
609         int dx, dy, sx, sy, x, y;
610         int color_manage = scene->r.color_mgt_flag & R_COLOR_MANAGEMENT;
611
612         /* verify valid values, just leave this a while */
613         if(ima->xrep<1) return;
614         if(ima->yrep<1) return;
615         
616         glPixelZoom(zoomx, zoomy);
617
618         if(sima->curtile >= ima->xrep*ima->yrep) 
619                 sima->curtile = ima->xrep*ima->yrep - 1; 
620         
621         /* create char buffer from float if needed */
622         image_verify_buffer_float(ima, ibuf, color_manage);
623
624         /* retrieve part of image buffer */
625         dx= ibuf->x/ima->xrep;
626         dy= ibuf->y/ima->yrep;
627         sx= (sima->curtile % ima->xrep)*dx;
628         sy= (sima->curtile / ima->xrep)*dy;
629         rect= get_part_from_ibuf(ibuf, sx, sy, sx+dx, sy+dy);
630         
631         /* draw repeated */
632         for(sy=0; sy+dy<=ibuf->y; sy+= dy) {
633                 for(sx=0; sx+dx<=ibuf->x; sx+= dx) {
634                         UI_view2d_to_region_no_clip(&ar->v2d, fx + (float)sx/(float)ibuf->x, fy + (float)sy/(float)ibuf->y, &x, &y);
635
636                         glaDrawPixelsSafe(x, y, dx, dy, dx, GL_RGBA, GL_UNSIGNED_BYTE, rect);
637                 }
638         }
639
640         glPixelZoom(1.0f, 1.0f);
641
642         MEM_freeN(rect);
643 }
644
645 static void draw_image_buffer_repeated(SpaceImage *sima, ARegion *ar, Scene *scene, Image *ima, ImBuf *ibuf, float zoomx, float zoomy)
646 {
647         const double time_current= PIL_check_seconds_timer();
648
649         const int xmax= ceil(ar->v2d.cur.xmax);
650         const int ymax= ceil(ar->v2d.cur.ymax);
651         const int xmin= floor(ar->v2d.cur.xmin);
652         const int ymin= floor(ar->v2d.cur.ymin);
653
654         int x;
655
656         for(x=xmin; x<xmax; x++) {
657                 int y;
658                 for(y=ymin; y<ymax; y++) { 
659                         if(ima && (ima->tpageflag & IMA_TILES))
660                                 draw_image_buffer_tiled(sima, ar, scene, ima, ibuf, x, y, zoomx, zoomy);
661                         else
662                                 draw_image_buffer(sima, ar, scene, ima, ibuf, x, y, zoomx, zoomy);
663
664                         /* only draw until running out of time */
665                         if((PIL_check_seconds_timer() - time_current) > 0.25)
666                                 return;
667                 }
668         }
669 }
670
671 /* draw uv edit */
672
673 /* draw grease pencil */
674 void draw_image_grease_pencil(bContext *C, short onlyv2d)
675 {
676         /* draw in View2D space? */
677         if (onlyv2d) {
678                 /* assume that UI_view2d_ortho(C) has been called... */
679                 SpaceImage *sima= (SpaceImage *)CTX_wm_space_data(C);
680                 void *lock;
681                 ImBuf *ibuf= ED_space_image_acquire_buffer(sima, &lock);
682                 
683                 /* draw grease-pencil ('image' strokes) */
684                 //if (sima->flag & SI_DISPGP)
685                         draw_gpencil_2dimage(C, ibuf);
686
687                 ED_space_image_release_buffer(sima, lock);
688         }
689         else {
690                 /* assume that UI_view2d_restore(C) has been called... */
691                 //SpaceImage *sima= (SpaceImage *)CTX_wm_space_data(C);
692                 
693                 /* draw grease-pencil ('screen' strokes) */
694                 //if (sima->flag & SI_DISPGP)
695                         draw_gpencil_view2d(C, 0);
696         }
697 }
698
699 /* XXX becomes WM paint cursor */
700 #if 0
701 static void draw_image_view_tool(Scene *scene)
702 {
703         ToolSettings *settings= scene->toolsettings;
704         Brush *brush= settings->imapaint.brush;
705         short mval[2];
706         float radius;
707         int draw= 0;
708
709         if(brush) {
710                 if(settings->imapaint.flag & IMAGEPAINT_DRAWING) {
711                         if(settings->imapaint.flag & IMAGEPAINT_DRAW_TOOL_DRAWING)
712                                 draw= 1;
713                 }
714                 else if(settings->imapaint.flag & IMAGEPAINT_DRAW_TOOL)
715                         draw= 1;
716                 
717                 if(draw) {
718                         getmouseco_areawin(mval);
719
720                         radius= brush_size(brush)*G.sima->zoom;
721                         fdrawXORcirc(mval[0], mval[1], radius);
722
723                         if (brush->innerradius != 1.0) {
724                                 radius *= brush->innerradius;
725                                 fdrawXORcirc(mval[0], mval[1], radius);
726                         }
727                 }
728         }
729 }
730 #endif
731
732 static unsigned char *get_alpha_clone_image(Scene *scene, int *width, int *height)
733 {
734         Brush *brush = paint_brush(&scene->toolsettings->imapaint.paint);
735         ImBuf *ibuf;
736         unsigned int size, alpha;
737         unsigned char *rect, *cp;
738
739         if(!brush || !brush->clone.image)
740                 return NULL;
741         
742         ibuf= BKE_image_get_ibuf(brush->clone.image, NULL);
743
744         if(!ibuf || !ibuf->rect)
745                 return NULL;
746
747         rect= MEM_dupallocN(ibuf->rect);
748         if(!rect)
749                 return NULL;
750
751         *width= ibuf->x;
752         *height= ibuf->y;
753
754         size= (*width)*(*height);
755         alpha= (unsigned char)255*brush->clone.alpha;
756         cp= rect;
757
758         while(size-- > 0) {
759                 cp[3]= alpha;
760                 cp += 4;
761         }
762
763         return rect;
764 }
765
766 static void draw_image_paint_helpers(ARegion *ar, Scene *scene, float zoomx, float zoomy)
767 {
768         Brush *brush;
769         int x, y, w, h;
770         unsigned char *clonerect;
771
772         brush= paint_brush(&scene->toolsettings->imapaint.paint);
773
774         if(brush && (brush->imagepaint_tool == PAINT_TOOL_CLONE)) {
775                 /* this is not very efficient, but glDrawPixels doesn't allow
776                    drawing with alpha */
777                 clonerect= get_alpha_clone_image(scene, &w, &h);
778
779                 if(clonerect) {
780                         UI_view2d_to_region_no_clip(&ar->v2d, brush->clone.offset[0], brush->clone.offset[1], &x, &y);
781
782                         glPixelZoom(zoomx, zoomy);
783
784                         glEnable(GL_BLEND);
785                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
786                         glaDrawPixelsSafe(x, y, w, h, w, GL_RGBA, GL_UNSIGNED_BYTE, clonerect);
787                         glDisable(GL_BLEND);
788
789                         glPixelZoom(1.0, 1.0);
790
791                         MEM_freeN(clonerect);
792                 }
793         }
794 }
795
796 /* draw main image area */
797
798 void draw_image_main(SpaceImage *sima, ARegion *ar, Scene *scene)
799 {
800         Image *ima;
801         ImBuf *ibuf;
802         float zoomx, zoomy;
803         int show_viewer, show_render;
804         void *lock;
805
806         /* XXX can we do this in refresh? */
807 #if 0
808         what_image(sima);
809         
810         if(sima->image) {
811                 ED_image_aspect(sima->image, &xuser_asp, &yuser_asp);
812                 
813                 /* UGLY hack? until now iusers worked fine... but for flipbook viewer we need this */
814                 if(sima->image->type==IMA_TYPE_COMPOSITE) {
815                         ImageUser *iuser= ntree_get_active_iuser(scene->nodetree);
816                         if(iuser) {
817                                 BKE_image_user_calc_imanr(iuser, scene->r.cfra, 0);
818                                 sima->iuser= *iuser;
819                         }
820                 }
821                 /* and we check for spare */
822                 ibuf= ED_space_image_buffer(sima);
823         }
824 #endif
825
826         /* retrieve the image and information about it */
827         ima= ED_space_image(sima);
828         ED_space_image_zoom(sima, ar, &zoomx, &zoomy);
829         ibuf= ED_space_image_acquire_buffer(sima, &lock);
830
831         show_viewer= (ima && ima->source == IMA_SRC_VIEWER);
832         show_render= (show_viewer && ima->type == IMA_TYPE_R_RESULT);
833
834         /* draw the image or grid */
835         if(ibuf==NULL)
836                 draw_image_grid(ar, zoomx, zoomy);
837         else if(sima->flag & SI_DRAW_TILE)
838                 draw_image_buffer_repeated(sima, ar, scene, ima, ibuf, zoomx, zoomy);
839         else if(ima && (ima->tpageflag & IMA_TILES))
840                 draw_image_buffer_tiled(sima, ar, scene, ima, ibuf, 0.0f, 0.0, zoomx, zoomy);
841         else
842                 draw_image_buffer(sima, ar, scene, ima, ibuf, 0.0f, 0.0f, zoomx, zoomy);
843
844         /* paint helpers */
845         if(sima->flag & SI_DRAWTOOL)
846                 draw_image_paint_helpers(ar, scene, zoomx, zoomy);
847
848
849         /* XXX integrate this code */
850 #if 0
851         if(ibuf) {
852                 float xoffs=0.0f, yoffs= 0.0f;
853                 
854                 if(image_preview_active(sa, &xim, &yim)) {
855                         xoffs= scene->r.disprect.xmin;
856                         yoffs= scene->r.disprect.ymin;
857                         glColor3ub(0,0,0);
858                         calc_image_view(sima, 'f');     
859                         myortho2(G.v2d->cur.xmin, G.v2d->cur.xmax, G.v2d->cur.ymin, G.v2d->cur.ymax);
860                         glRectf(0.0f, 0.0f, 1.0f, 1.0f);
861                         glLoadIdentity();
862                 }
863         }
864 #endif
865
866         ED_space_image_release_buffer(sima, lock);
867
868         /* render info */
869         if(ima && show_render)
870                 draw_render_info(scene, ima, ar);
871 }
872