style cleanup: follow style guide for formatting of if/for/while loops, and else...
[blender.git] / source / blender / editors / space_image / image_draw.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * Contributor(s): Blender Foundation, 2002-2009
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/editors/space_image/image_draw.c
27  *  \ingroup spimage
28  */
29
30
31 #include <math.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include "MEM_guardedalloc.h"
36
37 #include "DNA_camera_types.h"
38 #include "DNA_object_types.h"
39 #include "DNA_space_types.h"
40 #include "DNA_scene_types.h"
41 #include "DNA_screen_types.h"
42 #include "DNA_brush_types.h"
43
44 #include "PIL_time.h"
45
46 #include "BLI_math.h"
47 #include "BLI_threads.h"
48 #include "BLI_string.h"
49 #include "BLI_utildefines.h"
50
51 #include "IMB_imbuf.h"
52 #include "IMB_imbuf_types.h"
53
54 #include "BKE_context.h"
55 #include "BKE_global.h"
56 #include "BKE_image.h"
57 #include "BKE_paint.h"
58
59 #include "BIF_gl.h"
60 #include "BIF_glutil.h"
61
62 #include "BLF_api.h"
63
64 #include "ED_gpencil.h"
65 #include "ED_image.h"
66 #include "ED_screen.h"
67
68 #include "UI_interface.h"
69 #include "UI_resources.h"
70 #include "UI_view2d.h"
71
72
73 #include "RE_pipeline.h"
74
75 #include "image_intern.h"
76
77 #define HEADER_HEIGHT 18
78
79 static void image_verify_buffer_float(Image *ima, ImBuf *ibuf, int color_manage)
80 {
81         /* detect if we need to redo the curve map.
82          * ibuf->rect is zero for compositor and render results after change 
83          * convert to 32 bits always... drawing float rects isn't supported well (atis)
84          *
85          * NOTE: if float buffer changes, we have to manually remove the rect
86          */
87
88         if (ibuf->rect_float && (ibuf->rect==NULL || (ibuf->userflags & IB_RECT_INVALID)) ) {
89                 if (color_manage) {
90                         if (ima && ima->source == IMA_SRC_VIEWER)
91                                 ibuf->profile = IB_PROFILE_LINEAR_RGB;
92                 }
93                 else
94                         ibuf->profile = IB_PROFILE_NONE;
95
96                 IMB_rect_from_float(ibuf);
97         }
98 }
99
100 static void draw_render_info(Scene *scene, Image *ima, ARegion *ar)
101 {
102         RenderResult *rr;
103         
104         rr= BKE_image_acquire_renderresult(scene, ima);
105
106         if (rr && rr->text) {
107                 ED_region_info_draw(ar, rr->text, 1, 0.25);
108         }
109
110         BKE_image_release_renderresult(scene, ima);
111 }
112
113 /* used by node view too */
114 void ED_image_draw_info(ARegion *ar, int color_manage, int channels, int x, int y,
115                         const unsigned char cp[4], const float fp[4], int *zp, float *zpf)
116 {
117         char str[256];
118         float dx= 6;
119         /* text colors */
120         /* XXX colored text not allowed in Blender UI */
121         #if 0
122         unsigned char red[3] = {255, 50, 50};
123         unsigned char green[3] = {0, 255, 0};
124         unsigned char blue[3] = {100, 100, 255};
125         #else
126         unsigned char red[3] = {255, 255, 255};
127         unsigned char green[3] = {255, 255, 255};
128         unsigned char blue[3] = {255, 255, 255};
129         #endif
130         float hue=0, sat=0, val=0, lum=0, u=0, v=0;
131         float col[4], finalcol[4];
132
133         glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
134         glEnable(GL_BLEND);
135
136         /* noisy, high contrast make impossible to read if lower alpha is used. */
137         glColor4ub(0, 0, 0, 190);
138         glRecti(0.0, 0.0, ar->winrct.xmax - ar->winrct.xmin + 1, 20);
139         glDisable(GL_BLEND);
140
141         BLF_size(blf_mono_font, 11, 72);
142
143         glColor3ub(255, 255, 255);
144         BLI_snprintf(str, sizeof(str), "X:%-4d  Y:%-4d |", x, y);
145         // UI_DrawString(6, 6, str); // works ok but fixed width is nicer.
146         BLF_position(blf_mono_font, dx, 6, 0);
147         BLF_draw_ascii(blf_mono_font, str, sizeof(str));
148         dx += BLF_width(blf_mono_font, str);
149
150         if (zp) {
151                 glColor3ub(255, 255, 255);
152                 BLI_snprintf(str, sizeof(str), " Z:%-.4f |", 0.5f+0.5f*(((float)*zp)/(float)0x7fffffff));
153                 BLF_position(blf_mono_font, dx, 6, 0);
154                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
155                 dx += BLF_width(blf_mono_font, str);
156         }
157         if (zpf) {
158                 glColor3ub(255, 255, 255);
159                 BLI_snprintf(str, sizeof(str), " Z:%-.3f |", *zpf);
160                 BLF_position(blf_mono_font, dx, 6, 0);
161                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
162                 dx += BLF_width(blf_mono_font, str);
163         }
164
165         if (channels >= 3) {
166                 glColor3ubv(red);
167                 if (fp)
168                         BLI_snprintf(str, sizeof(str), "  R:%-.4f", fp[0]);
169                 else if (cp)
170                         BLI_snprintf(str, sizeof(str), "  R:%-3d", cp[0]);
171                 else
172                         BLI_snprintf(str, sizeof(str), "  R:-");
173                 BLF_position(blf_mono_font, dx, 6, 0);
174                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
175                 dx += BLF_width(blf_mono_font, str);
176                 
177                 glColor3ubv(green);
178                 if (fp)
179                         BLI_snprintf(str, sizeof(str), "  G:%-.4f", fp[1]);
180                 else if (cp)
181                         BLI_snprintf(str, sizeof(str), "  G:%-3d", cp[1]);
182                 else
183                         BLI_snprintf(str, sizeof(str), "  G:-");
184                 BLF_position(blf_mono_font, dx, 6, 0);
185                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
186                 dx += BLF_width(blf_mono_font, str);
187                 
188                 glColor3ubv(blue);
189                 if (fp)
190                         BLI_snprintf(str, sizeof(str), "  B:%-.4f", fp[2]);
191                 else if (cp)
192                         BLI_snprintf(str, sizeof(str), "  B:%-3d", cp[2]);
193                 else
194                         BLI_snprintf(str, sizeof(str), "  B:-");
195                 BLF_position(blf_mono_font, dx, 6, 0);
196                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
197                 dx += BLF_width(blf_mono_font, str);
198                 
199                 if (channels == 4) {
200                         glColor3ub(255, 255, 255);
201                         if (fp)
202                                 BLI_snprintf(str, sizeof(str), "  A:%-.4f", fp[3]);
203                         else if (cp)
204                                 BLI_snprintf(str, sizeof(str), "  A:%-3d", cp[3]);
205                         else
206                                 BLI_snprintf(str, sizeof(str), "- ");
207                         BLF_position(blf_mono_font, dx, 6, 0);
208                         BLF_draw_ascii(blf_mono_font, str, sizeof(str));
209                         dx += BLF_width(blf_mono_font, str);
210                 }
211         }
212         
213         /* color rectangle */
214         if (channels==1) {
215                 if (fp) {
216                         col[0] = col[1] = col[2] = fp[0];
217                 }
218                 else if (cp) {
219                         col[0] = col[1] = col[2] = (float)cp[0]/255.0f;
220                 }
221                 else {
222                         col[0] = col[1] = col[2] = 0.0f;
223                 }
224                 col[3] = 1.0f;
225         }
226         else if (channels==3) {
227                 if (fp) {
228                         copy_v3_v3(col, fp);
229                 }
230                 else if (cp) {
231                         rgb_uchar_to_float(col, cp);
232                 }
233                 else {
234                         zero_v3(col);
235                 }
236                 col[3] = 1.0f;
237         }
238         else if (channels==4) {
239                 if (fp)
240                         copy_v4_v4(col, fp);
241                 else if (cp) {
242                         rgba_uchar_to_float(col, cp);
243                 }
244                 else {
245                         zero_v4(col);
246                 }
247         }
248         else {
249                 BLI_assert(0);
250                 zero_v4(col);
251         }
252
253         if (color_manage) {
254                 linearrgb_to_srgb_v4(finalcol, col);
255         }
256         else {
257                 copy_v4_v4(finalcol, col);
258         }
259         glDisable(GL_BLEND);
260         glColor3fv(finalcol);
261         dx += 5;
262         glBegin(GL_QUADS);
263         glVertex2f(dx, 3);
264         glVertex2f(dx, 17);
265         glVertex2f(dx+30, 17);
266         glVertex2f(dx+30, 3);
267         glEnd();
268
269         /* draw outline */
270         glColor3ub(128, 128, 128);
271         glBegin(GL_LINE_LOOP);
272         glVertex2f(dx, 3);
273         glVertex2f(dx, 17);
274         glVertex2f(dx+30, 17);
275         glVertex2f(dx+30, 3);
276         glEnd();
277
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                 BLI_snprintf(str, sizeof(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                 BLI_snprintf(str, sizeof(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                 BLI_snprintf(str, sizeof(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                 BLI_snprintf(str, sizeof(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                 BLI_snprintf(str, sizeof(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                 BLI_snprintf(str, sizeof(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 #if 0
545                 else
546                         glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_FLOAT, ibuf->rect_float);
547 #endif
548                 
549                 if (sima->flag & SI_USE_ALPHA)
550                         glDisable(GL_BLEND);
551         }
552
553         /* reset zoom */
554         glPixelZoom(1.0f, 1.0f);
555 }
556
557 static unsigned int *get_part_from_ibuf(ImBuf *ibuf, short startx, short starty, short endx, short endy)
558 {
559         unsigned int *rt, *rp, *rectmain;
560         short y, heigth, len;
561
562         /* the right offset in rectot */
563
564         rt= ibuf->rect+ (starty*ibuf->x+ startx);
565
566         len= (endx-startx);
567         heigth= (endy-starty);
568
569         rp=rectmain= MEM_mallocN(heigth*len*sizeof(int), "rect");
570         
571         for (y=0; y<heigth; y++) {
572                 memcpy(rp, rt, len*4);
573                 rt+= ibuf->x;
574                 rp+= len;
575         }
576         return rectmain;
577 }
578
579 static void draw_image_buffer_tiled(SpaceImage *sima, ARegion *ar, Scene *scene, Image *ima, ImBuf *ibuf, float fx, float fy, float zoomx, float zoomy)
580 {
581         unsigned int *rect;
582         int dx, dy, sx, sy, x, y;
583         int color_manage = scene->r.color_mgt_flag & R_COLOR_MANAGEMENT;
584
585         /* verify valid values, just leave this a while */
586         if (ima->xrep<1) return;
587         if (ima->yrep<1) return;
588         
589         glPixelZoom(zoomx, zoomy);
590
591         if (sima->curtile >= ima->xrep*ima->yrep) 
592                 sima->curtile = ima->xrep*ima->yrep - 1; 
593         
594         /* create char buffer from float if needed */
595         image_verify_buffer_float(ima, ibuf, color_manage);
596
597         /* retrieve part of image buffer */
598         dx= ibuf->x/ima->xrep;
599         dy= ibuf->y/ima->yrep;
600         sx= (sima->curtile % ima->xrep)*dx;
601         sy= (sima->curtile / ima->xrep)*dy;
602         rect= get_part_from_ibuf(ibuf, sx, sy, sx+dx, sy+dy);
603         
604         /* draw repeated */
605         for (sy=0; sy+dy<=ibuf->y; sy+= dy) {
606                 for (sx=0; sx+dx<=ibuf->x; sx+= dx) {
607                         UI_view2d_to_region_no_clip(&ar->v2d, fx + (float)sx/(float)ibuf->x, fy + (float)sy/(float)ibuf->y, &x, &y);
608
609                         glaDrawPixelsSafe(x, y, dx, dy, dx, GL_RGBA, GL_UNSIGNED_BYTE, rect);
610                 }
611         }
612
613         glPixelZoom(1.0f, 1.0f);
614
615         MEM_freeN(rect);
616 }
617
618 static void draw_image_buffer_repeated(SpaceImage *sima, ARegion *ar, Scene *scene, Image *ima, ImBuf *ibuf, float zoomx, float zoomy)
619 {
620         const double time_current= PIL_check_seconds_timer();
621
622         const int xmax= ceil(ar->v2d.cur.xmax);
623         const int ymax= ceil(ar->v2d.cur.ymax);
624         const int xmin= floor(ar->v2d.cur.xmin);
625         const int ymin= floor(ar->v2d.cur.ymin);
626
627         int x;
628
629         for (x=xmin; x<xmax; x++) {
630                 int y;
631                 for (y=ymin; y<ymax; y++) { 
632                         if (ima && (ima->tpageflag & IMA_TILES))
633                                 draw_image_buffer_tiled(sima, ar, scene, ima, ibuf, x, y, zoomx, zoomy);
634                         else
635                                 draw_image_buffer(sima, ar, scene, ima, ibuf, x, y, zoomx, zoomy);
636
637                         /* only draw until running out of time */
638                         if ((PIL_check_seconds_timer() - time_current) > 0.25)
639                                 return;
640                 }
641         }
642 }
643
644 /* draw uv edit */
645
646 /* draw grease pencil */
647 void draw_image_grease_pencil(bContext *C, short onlyv2d)
648 {
649         /* draw in View2D space? */
650         if (onlyv2d) {
651                 /* assume that UI_view2d_ortho(C) has been called... */
652                 SpaceImage *sima= (SpaceImage *)CTX_wm_space_data(C);
653                 void *lock;
654                 ImBuf *ibuf= ED_space_image_acquire_buffer(sima, &lock);
655                 
656                 /* draw grease-pencil ('image' strokes) */
657                 //if (sima->flag & SI_DISPGP)
658                         draw_gpencil_2dimage(C, ibuf);
659
660                 ED_space_image_release_buffer(sima, lock);
661         }
662         else {
663                 /* assume that UI_view2d_restore(C) has been called... */
664                 //SpaceImage *sima= (SpaceImage *)CTX_wm_space_data(C);
665                 
666                 /* draw grease-pencil ('screen' strokes) */
667                 //if (sima->flag & SI_DISPGP)
668                         draw_gpencil_view2d(C, 0);
669         }
670 }
671
672 /* XXX becomes WM paint cursor */
673 #if 0
674 static void draw_image_view_tool(Scene *scene)
675 {
676         ToolSettings *settings= scene->toolsettings;
677         Brush *brush= settings->imapaint.brush;
678         int mval[2];
679         float radius;
680         int draw= 0;
681
682         if (brush) {
683                 if (settings->imapaint.flag & IMAGEPAINT_DRAWING) {
684                         if (settings->imapaint.flag & IMAGEPAINT_DRAW_TOOL_DRAWING)
685                                 draw= 1;
686                 }
687                 else if (settings->imapaint.flag & IMAGEPAINT_DRAW_TOOL)
688                         draw= 1;
689                 
690                 if (draw) {
691                         getmouseco_areawin(mval);
692
693                         radius= brush_size(brush)*G.sima->zoom;
694                         fdrawXORcirc(mval[0], mval[1], radius);
695
696                         if (brush->innerradius != 1.0) {
697                                 radius *= brush->innerradius;
698                                 fdrawXORcirc(mval[0], mval[1], radius);
699                         }
700                 }
701         }
702 }
703 #endif
704
705 static unsigned char *get_alpha_clone_image(Scene *scene, int *width, int *height)
706 {
707         Brush *brush = paint_brush(&scene->toolsettings->imapaint.paint);
708         ImBuf *ibuf;
709         unsigned int size, alpha;
710         unsigned char *rect, *cp;
711
712         if (!brush || !brush->clone.image)
713                 return NULL;
714         
715         ibuf= BKE_image_get_ibuf(brush->clone.image, NULL);
716
717         if (!ibuf || !ibuf->rect)
718                 return NULL;
719
720         rect= MEM_dupallocN(ibuf->rect);
721         if (!rect)
722                 return NULL;
723
724         *width= ibuf->x;
725         *height= ibuf->y;
726
727         size= (*width)*(*height);
728         alpha= (unsigned char)255*brush->clone.alpha;
729         cp= rect;
730
731         while (size-- > 0) {
732                 cp[3]= alpha;
733                 cp += 4;
734         }
735
736         return rect;
737 }
738
739 static void draw_image_paint_helpers(ARegion *ar, Scene *scene, float zoomx, float zoomy)
740 {
741         Brush *brush;
742         int x, y, w, h;
743         unsigned char *clonerect;
744
745         brush= paint_brush(&scene->toolsettings->imapaint.paint);
746
747         if (brush && (brush->imagepaint_tool == PAINT_TOOL_CLONE)) {
748                 /* this is not very efficient, but glDrawPixels doesn't allow
749                  * drawing with alpha */
750                 clonerect= get_alpha_clone_image(scene, &w, &h);
751
752                 if (clonerect) {
753                         UI_view2d_to_region_no_clip(&ar->v2d, brush->clone.offset[0], brush->clone.offset[1], &x, &y);
754
755                         glPixelZoom(zoomx, zoomy);
756
757                         glEnable(GL_BLEND);
758                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
759                         glaDrawPixelsSafe(x, y, w, h, w, GL_RGBA, GL_UNSIGNED_BYTE, clonerect);
760                         glDisable(GL_BLEND);
761
762                         glPixelZoom(1.0, 1.0);
763
764                         MEM_freeN(clonerect);
765                 }
766         }
767 }
768
769 /* draw main image area */
770
771 void draw_image_main(SpaceImage *sima, ARegion *ar, Scene *scene)
772 {
773         Image *ima;
774         ImBuf *ibuf;
775         float zoomx, zoomy;
776         int show_viewer, show_render;
777         void *lock;
778
779         /* XXX can we do this in refresh? */
780 #if 0
781         what_image(sima);
782         
783         if (sima->image) {
784                 ED_image_aspect(sima->image, &xuser_asp, &yuser_asp);
785                 
786                 /* UGLY hack? until now iusers worked fine... but for flipbook viewer we need this */
787                 if (sima->image->type==IMA_TYPE_COMPOSITE) {
788                         ImageUser *iuser= ntree_get_active_iuser(scene->nodetree);
789                         if (iuser) {
790                                 BKE_image_user_calc_imanr(iuser, scene->r.cfra, 0);
791                                 sima->iuser= *iuser;
792                         }
793                 }
794                 /* and we check for spare */
795                 ibuf= ED_space_image_buffer(sima);
796         }
797 #endif
798
799         /* retrieve the image and information about it */
800         ima= ED_space_image(sima);
801         ED_space_image_zoom(sima, ar, &zoomx, &zoomy);
802         ibuf= ED_space_image_acquire_buffer(sima, &lock);
803
804         show_viewer= (ima && ima->source == IMA_SRC_VIEWER);
805         show_render= (show_viewer && ima->type == IMA_TYPE_R_RESULT);
806
807         /* draw the image or grid */
808         if (ibuf==NULL)
809                 draw_image_grid(ar, zoomx, zoomy);
810         else if (sima->flag & SI_DRAW_TILE)
811                 draw_image_buffer_repeated(sima, ar, scene, ima, ibuf, zoomx, zoomy);
812         else if (ima && (ima->tpageflag & IMA_TILES))
813                 draw_image_buffer_tiled(sima, ar, scene, ima, ibuf, 0.0f, 0.0, zoomx, zoomy);
814         else
815                 draw_image_buffer(sima, ar, scene, ima, ibuf, 0.0f, 0.0f, zoomx, zoomy);
816
817         /* paint helpers */
818         if (sima->flag & SI_DRAWTOOL)
819                 draw_image_paint_helpers(ar, scene, zoomx, zoomy);
820
821
822         /* XXX integrate this code */
823 #if 0
824         if (ibuf) {
825                 float xoffs=0.0f, yoffs= 0.0f;
826                 
827                 if (image_preview_active(sa, &xim, &yim)) {
828                         xoffs= scene->r.disprect.xmin;
829                         yoffs= scene->r.disprect.ymin;
830                         glColor3ub(0,0,0);
831                         calc_image_view(sima, 'f');     
832                         myortho2(G.v2d->cur.xmin, G.v2d->cur.xmax, G.v2d->cur.ymin, G.v2d->cur.ymax);
833                         glRectf(0.0f, 0.0f, 1.0f, 1.0f);
834                         glLoadIdentity();
835                 }
836         }
837 #endif
838
839         ED_space_image_release_buffer(sima, lock);
840
841         /* render info */
842         if (ima && show_render)
843                 draw_render_info(scene, ima, ar);
844 }
845