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