4 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * Contributor(s): Blender Foundation, 2002-2009
25 * ***** END GPL LICENSE BLOCK *****
32 #include "MEM_guardedalloc.h"
34 #include "DNA_camera_types.h"
35 #include "DNA_object_types.h"
36 #include "DNA_space_types.h"
37 #include "DNA_scene_types.h"
38 #include "DNA_screen_types.h"
39 #include "DNA_brush_types.h"
43 #include "BLI_threads.h"
44 #include "BLI_utildefines.h"
46 #include "IMB_imbuf.h"
47 #include "IMB_imbuf_types.h"
49 #include "BKE_context.h"
50 #include "BKE_global.h"
51 #include "BKE_image.h"
52 #include "BKE_paint.h"
55 #include "BKE_colortools.h"
59 #include "BIF_glutil.h"
63 #include "ED_gpencil.h"
66 #include "UI_interface.h"
67 #include "UI_resources.h"
68 #include "UI_view2d.h"
71 #include "RE_pipeline.h"
73 #include "image_intern.h"
75 #define HEADER_HEIGHT 18
77 static void image_verify_buffer_float(Image *ima, ImBuf *ibuf, int color_manage)
79 /* detect if we need to redo the curve map.
80 ibuf->rect is zero for compositor and render results after change
81 convert to 32 bits always... drawing float rects isnt supported well (atis)
83 NOTE: if float buffer changes, we have to manually remove the rect
86 if(ibuf->rect_float && ibuf->rect==NULL) {
88 if(ima && ima->source == IMA_SRC_VIEWER)
89 ibuf->profile = IB_PROFILE_LINEAR_RGB;
92 ibuf->profile = IB_PROFILE_NONE;
94 IMB_rect_from_float(ibuf);
98 static void draw_render_info(Scene *scene, Image *ima, ARegion *ar)
104 rr= BKE_image_acquire_renderresult(scene, ima);
109 rect.ymin= ar->winrct.ymax - ar->winrct.ymin - HEADER_HEIGHT;
110 rect.xmax= ar->winrct.xmax - ar->winrct.xmin;
111 rect.ymax= ar->winrct.ymax - ar->winrct.ymin;
113 /* clear header rect */
114 UI_GetThemeColor3fv(TH_BACK, colf);
116 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
117 glColor4f(colf[0]+0.1f, colf[1]+0.1f, colf[2]+0.1f, 0.5f);
118 glRecti(rect.xmin, rect.ymin, rect.xmax, rect.ymax+1);
121 UI_ThemeColor(TH_TEXT_HI);
123 UI_DrawString(12, rect.ymin + 5, rr->text);
126 BKE_image_release_renderresult(scene, ima);
129 void draw_image_info(ARegion *ar, int channels, int x, int y, char *cp, float *fp, int *zp, float *zpf)
134 ofs= sprintf(str, "X: %4d Y: %4d ", x, y);
136 ofs+= sprintf(str+ofs, "| R: %3d G: %3d B: %3d A: %3d ", cp[0], cp[1], cp[2], cp[3]);
140 ofs+= sprintf(str+ofs, "| R: %.3f G: %.3f B: %.3f A: %.3f ", fp[0], fp[1], fp[2], fp[3]);
142 ofs+= sprintf(str+ofs, "| Val: %.3f ", fp[0]);
144 ofs+= sprintf(str+ofs, "| R: %.3f G: %.3f B: %.3f ", fp[0], fp[1], fp[2]);
148 ofs+= sprintf(str+ofs, "| Z: %.4f ", 0.5+0.5*(((float)*zp)/(float)0x7fffffff));
150 ofs+= sprintf(str+ofs, "| Z: %.3f ", *zpf);
152 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
155 glColor4f(.0,.0,.0,.25);
156 glRecti(0.0, 0.0, ar->winrct.xmax - ar->winrct.xmin + 1, 20);
159 glColor3ub(255, 255, 255);
161 // UI_DrawString(6, 6, str); // works ok but fixed width is nicer.
162 BLF_size(blf_mono_font, 11, 72);
163 BLF_position(blf_mono_font, 6, 6, 0);
164 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
170 static void draw_image_grid(ARegion *ar, float zoomx, float zoomy)
172 float gridsize, gridstep= 1.0f/32.0f;
176 /* the image is located inside (0,0),(1, 1) as set by view2d */
177 UI_ThemeColorShade(TH_BACK, 20);
179 UI_view2d_to_region_no_clip(&ar->v2d, 0.0f, 0.0f, &x1, &y1);
180 UI_view2d_to_region_no_clip(&ar->v2d, 1.0f, 1.0f, &x2, &y2);
181 glRectf(x1, y1, x2, y2);
183 /* gridsize adapted to zoom level */
184 gridsize= 0.5f*(zoomx+zoomy);
185 if(gridsize<=0.0f) return;
188 while(gridsize<1.0f) {
194 while(gridsize>=4.0f) {
200 /* the fine resolution level */
201 blendfac= 0.25*gridsize - floor(0.25*gridsize);
202 CLAMP(blendfac, 0.0, 1.0);
203 UI_ThemeColorShade(TH_BACK, (int)(20.0*(1.0-blendfac)));
208 glVertex2f(x1, y1*(1.0f-fac) + y2*fac);
209 glVertex2f(x2, y1*(1.0f-fac) + y2*fac);
210 glVertex2f(x1*(1.0f-fac) + x2*fac, y1);
211 glVertex2f(x1*(1.0f-fac) + x2*fac, y2);
215 /* the large resolution level */
216 UI_ThemeColor(TH_BACK);
220 glVertex2f(x1, y1*(1.0f-fac) + y2*fac);
221 glVertex2f(x2, y1*(1.0f-fac) + y2*fac);
222 glVertex2f(x1*(1.0f-fac) + x2*fac, y1);
223 glVertex2f(x1*(1.0f-fac) + x2*fac, y2);
229 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])
231 GLubyte checker_stipple[32*32/8] =
233 255,255,0,0,255,255,0,0,255,255,0,0,255,255,0,0, \
234 255,255,0,0,255,255,0,0,255,255,0,0,255,255,0,0, \
235 255,255,0,0,255,255,0,0,255,255,0,0,255,255,0,0, \
236 255,255,0,0,255,255,0,0,255,255,0,0,255,255,0,0, \
237 0,0,255,255,0,0,255,255,0,0,255,255,0,0,255,255, \
238 0,0,255,255,0,0,255,255,0,0,255,255,0,0,255,255, \
239 0,0,255,255,0,0,255,255,0,0,255,255,0,0,255,255, \
240 0,0,255,255,0,0,255,255,0,0,255,255,0,0,255,255, \
244 glRectf(x1, y1, x1 + zoomx*xsize, y1 + zoomy*ysize);
247 glEnable(GL_POLYGON_STIPPLE);
248 glPolygonStipple(checker_stipple);
249 glRectf(x1, y1, x1 + zoomx*xsize, y1 + zoomy*ysize);
250 glDisable(GL_POLYGON_STIPPLE);
253 static void sima_draw_alpha_pixels(float x1, float y1, int rectx, int recty, unsigned int *recti)
256 /* swap bytes, so alpha is most significant one, then just draw it as luminance int */
257 if(ENDIAN_ORDER == B_ENDIAN)
258 glPixelStorei(GL_UNPACK_SWAP_BYTES, 1);
260 glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_LUMINANCE, GL_UNSIGNED_INT, recti);
261 glPixelStorei(GL_UNPACK_SWAP_BYTES, 0);
264 static void sima_draw_alpha_pixelsf(float x1, float y1, int rectx, int recty, float *rectf)
266 float *trectf= MEM_mallocN(rectx*recty*4, "temp");
269 for(a= rectx*recty -1, b= 4*a+3; a>=0; a--, b-=4)
272 glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_LUMINANCE, GL_FLOAT, trectf);
274 /* ogl trick below is slower... (on ATI 9600) */
275 // glColorMask(1, 0, 0, 0);
276 // glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_RGBA, GL_FLOAT, rectf+3);
277 // glColorMask(0, 1, 0, 0);
278 // glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_RGBA, GL_FLOAT, rectf+2);
279 // glColorMask(0, 0, 1, 0);
280 // glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_RGBA, GL_FLOAT, rectf+1);
281 // glColorMask(1, 1, 1, 1);
285 static int sima_draw_colorcorrected_pixels(float x1, float y1, ImBuf *ibuf)
287 colorcorrection_do_ibuf(ibuf, "MONOSCNR.ICM"); /* path is hardcoded here, find some place better */
290 glaDrawPixelsSafe(x1, y1, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->crect);
298 static void sima_draw_zbuf_pixels(float x1, float y1, int rectx, int recty, int *recti)
300 /* zbuffer values are signed, so we need to shift color range */
301 glPixelTransferf(GL_RED_SCALE, 0.5f);
302 glPixelTransferf(GL_GREEN_SCALE, 0.5f);
303 glPixelTransferf(GL_BLUE_SCALE, 0.5f);
304 glPixelTransferf(GL_RED_BIAS, 0.5f);
305 glPixelTransferf(GL_GREEN_BIAS, 0.5f);
306 glPixelTransferf(GL_BLUE_BIAS, 0.5f);
308 glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_LUMINANCE, GL_INT, recti);
310 glPixelTransferf(GL_RED_SCALE, 1.0f);
311 glPixelTransferf(GL_GREEN_SCALE, 1.0f);
312 glPixelTransferf(GL_BLUE_SCALE, 1.0f);
313 glPixelTransferf(GL_RED_BIAS, 0.0f);
314 glPixelTransferf(GL_GREEN_BIAS, 0.0f);
315 glPixelTransferf(GL_BLUE_BIAS, 0.0f);
318 static void sima_draw_zbuffloat_pixels(Scene *scene, float x1, float y1, int rectx, int recty, float *rect_float)
320 float bias, scale, *rectf, clipend;
323 if(scene->camera && scene->camera->type==OB_CAMERA) {
324 bias= ((Camera *)scene->camera->data)->clipsta;
325 clipend= ((Camera *)scene->camera->data)->clipend;
326 scale= 1.0f/(clipend-bias);
334 rectf= MEM_mallocN(rectx*recty*4, "temp");
335 for(a= rectx*recty -1; a>=0; a--) {
336 if(rect_float[a]>clipend)
338 else if(rect_float[a]<bias)
341 rectf[a]= 1.0f - (rect_float[a]-bias)*scale;
345 glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_LUMINANCE, GL_FLOAT, rectf);
350 static void draw_image_buffer(SpaceImage *sima, ARegion *ar, Scene *scene, Image *ima, ImBuf *ibuf, float fx, float fy, float zoomx, float zoomy)
353 int color_manage = scene->r.color_mgt_flag & R_COLOR_MANAGEMENT;
356 glPixelZoom(zoomx, zoomy);
358 /* find window pixel coordinates of origin */
359 UI_view2d_to_region_no_clip(&ar->v2d, fx, fy, &x, &y);
361 /* this part is generic image display */
362 if(sima->flag & SI_SHOW_ALPHA) {
364 sima_draw_alpha_pixels(x, y, ibuf->x, ibuf->y, ibuf->rect);
365 else if(ibuf->rect_float && ibuf->channels==4)
366 sima_draw_alpha_pixelsf(x, y, ibuf->x, ibuf->y, ibuf->rect_float);
368 else if(sima->flag & SI_SHOW_ZBUF && (ibuf->zbuf || ibuf->zbuf_float || (ibuf->channels==1))) {
370 sima_draw_zbuf_pixels(x, y, ibuf->x, ibuf->y, ibuf->zbuf);
371 else if(ibuf->zbuf_float)
372 sima_draw_zbuffloat_pixels(scene, x, y, ibuf->x, ibuf->y, ibuf->zbuf_float);
373 else if(ibuf->channels==1)
374 sima_draw_zbuffloat_pixels(scene, x, y, ibuf->x, ibuf->y, ibuf->rect_float);
377 else if(sima->flag & SI_COLOR_CORRECTION) {
378 image_verify_buffer_float(ima, ibuf, color_manage);
380 if(sima_draw_colorcorrected_pixels(x, y, ibuf)==0) {
381 unsigned char col1[3]= {100, 0, 100}, col2[3]= {160, 0, 160}; /* pink says 'warning' in blender land */
382 sima_draw_alpha_backdrop(x, y, ibuf->x, ibuf->y, zoomx, zoomy, col1, col2);
388 if(sima->flag & SI_USE_ALPHA) {
389 unsigned char col1[3]= {100, 100, 100}, col2[3]= {160, 160, 160};
390 sima_draw_alpha_backdrop(x, y, ibuf->x, ibuf->y, zoomx, zoomy, col1, col2);
393 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
396 /* we don't draw floats buffers directly but
397 * convert them, and optionally apply curves */
398 image_verify_buffer_float(ima, ibuf, color_manage);
401 glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
403 glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_FLOAT, ibuf->rect_float);*/
405 if(sima->flag & SI_USE_ALPHA)
410 glPixelZoom(1.0f, 1.0f);
413 static unsigned int *get_part_from_ibuf(ImBuf *ibuf, short startx, short starty, short endx, short endy)
415 unsigned int *rt, *rp, *rectmain;
416 short y, heigth, len;
418 /* the right offset in rectot */
420 rt= ibuf->rect+ (starty*ibuf->x+ startx);
423 heigth= (endy-starty);
425 rp=rectmain= MEM_mallocN(heigth*len*sizeof(int), "rect");
427 for(y=0; y<heigth; y++) {
428 memcpy(rp, rt, len*4);
435 static void draw_image_buffer_tiled(SpaceImage *sima, ARegion *ar, Scene *scene, Image *ima, ImBuf *ibuf, float fx, float fy, float zoomx, float zoomy)
438 int dx, dy, sx, sy, x, y;
439 int color_manage = scene->r.color_mgt_flag & R_COLOR_MANAGEMENT;
441 /* verify valid values, just leave this a while */
442 if(ima->xrep<1) return;
443 if(ima->yrep<1) return;
445 glPixelZoom(zoomx, zoomy);
447 if(sima->curtile >= ima->xrep*ima->yrep)
448 sima->curtile = ima->xrep*ima->yrep - 1;
450 /* create char buffer from float if needed */
451 image_verify_buffer_float(ima, ibuf, color_manage);
453 /* retrieve part of image buffer */
454 dx= ibuf->x/ima->xrep;
455 dy= ibuf->y/ima->yrep;
456 sx= (sima->curtile % ima->xrep)*dx;
457 sy= (sima->curtile / ima->xrep)*dy;
458 rect= get_part_from_ibuf(ibuf, sx, sy, sx+dx, sy+dy);
461 for(sy=0; sy+dy<=ibuf->y; sy+= dy) {
462 for(sx=0; sx+dx<=ibuf->x; sx+= dx) {
463 UI_view2d_to_region_no_clip(&ar->v2d, fx + (float)sx/(float)ibuf->x, fy + (float)sy/(float)ibuf->y, &x, &y);
465 glaDrawPixelsSafe(x, y, dx, dy, dx, GL_RGBA, GL_UNSIGNED_BYTE, rect);
469 glPixelZoom(1.0f, 1.0f);
474 static void draw_image_buffer_repeated(SpaceImage *sima, ARegion *ar, Scene *scene, Image *ima, ImBuf *ibuf, float zoomx, float zoomy)
476 const double time_current= PIL_check_seconds_timer();
478 const int xmax= ceil(ar->v2d.cur.xmax);
479 const int ymax= ceil(ar->v2d.cur.ymax);
480 const int xmin= floor(ar->v2d.cur.xmin);
481 const int ymin= floor(ar->v2d.cur.ymin);
485 for(x=xmin; x<xmax; x++) {
487 for(y=ymin; y<ymax; y++) {
488 if(ima && (ima->tpageflag & IMA_TILES))
489 draw_image_buffer_tiled(sima, ar, scene, ima, ibuf, x, y, zoomx, zoomy);
491 draw_image_buffer(sima, ar, scene, ima, ibuf, x, y, zoomx, zoomy);
493 /* only draw until running out of time */
494 if((PIL_check_seconds_timer() - time_current) > 0.25)
502 /* draw grease pencil */
503 void draw_image_grease_pencil(bContext *C, short onlyv2d)
505 /* draw in View2D space? */
507 /* assume that UI_view2d_ortho(C) has been called... */
508 SpaceImage *sima= (SpaceImage *)CTX_wm_space_data(C);
510 ImBuf *ibuf= ED_space_image_acquire_buffer(sima, &lock);
512 /* draw grease-pencil ('image' strokes) */
513 //if (sima->flag & SI_DISPGP)
514 draw_gpencil_2dimage(C, ibuf);
516 ED_space_image_release_buffer(sima, lock);
519 /* assume that UI_view2d_restore(C) has been called... */
520 //SpaceImage *sima= (SpaceImage *)CTX_wm_space_data(C);
522 /* draw grease-pencil ('screen' strokes) */
523 //if (sima->flag & SI_DISPGP)
524 draw_gpencil_view2d(C, 0);
528 /* XXX becomes WM paint cursor */
530 static void draw_image_view_tool(Scene *scene)
532 ToolSettings *settings= scene->toolsettings;
533 Brush *brush= settings->imapaint.brush;
539 if(settings->imapaint.flag & IMAGEPAINT_DRAWING) {
540 if(settings->imapaint.flag & IMAGEPAINT_DRAW_TOOL_DRAWING)
543 else if(settings->imapaint.flag & IMAGEPAINT_DRAW_TOOL)
547 getmouseco_areawin(mval);
549 radius= brush_size(brush)*G.sima->zoom;
550 fdrawXORcirc(mval[0], mval[1], radius);
552 if (brush->innerradius != 1.0) {
553 radius *= brush->innerradius;
554 fdrawXORcirc(mval[0], mval[1], radius);
561 static unsigned char *get_alpha_clone_image(Scene *scene, int *width, int *height)
563 Brush *brush = paint_brush(&scene->toolsettings->imapaint.paint);
565 unsigned int size, alpha;
566 unsigned char *rect, *cp;
568 if(!brush || !brush->clone.image)
571 ibuf= BKE_image_get_ibuf(brush->clone.image, NULL);
573 if(!ibuf || !ibuf->rect)
576 rect= MEM_dupallocN(ibuf->rect);
583 size= (*width)*(*height);
584 alpha= (unsigned char)255*brush->clone.alpha;
595 static void draw_image_paint_helpers(ARegion *ar, Scene *scene, float zoomx, float zoomy)
599 unsigned char *clonerect;
601 brush= paint_brush(&scene->toolsettings->imapaint.paint);
603 if(brush && (brush->imagepaint_tool == PAINT_TOOL_CLONE)) {
604 /* this is not very efficient, but glDrawPixels doesn't allow
605 drawing with alpha */
606 clonerect= get_alpha_clone_image(scene, &w, &h);
609 UI_view2d_to_region_no_clip(&ar->v2d, brush->clone.offset[0], brush->clone.offset[1], &x, &y);
611 glPixelZoom(zoomx, zoomy);
614 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
615 glaDrawPixelsSafe(x, y, w, h, w, GL_RGBA, GL_UNSIGNED_BYTE, clonerect);
618 glPixelZoom(1.0, 1.0);
620 MEM_freeN(clonerect);
625 /* draw main image area */
627 void draw_image_main(SpaceImage *sima, ARegion *ar, Scene *scene)
632 int show_viewer, show_render;
635 /* XXX can we do this in refresh? */
640 ED_image_aspect(sima->image, &xuser_asp, &yuser_asp);
642 /* UGLY hack? until now iusers worked fine... but for flipbook viewer we need this */
643 if(sima->image->type==IMA_TYPE_COMPOSITE) {
644 ImageUser *iuser= ntree_get_active_iuser(scene->nodetree);
646 BKE_image_user_calc_imanr(iuser, scene->r.cfra, 0);
650 /* and we check for spare */
651 ibuf= ED_space_image_buffer(sima);
655 /* retrieve the image and information about it */
656 ima= ED_space_image(sima);
657 ED_space_image_zoom(sima, ar, &zoomx, &zoomy);
658 ibuf= ED_space_image_acquire_buffer(sima, &lock);
660 show_viewer= (ima && ima->source == IMA_SRC_VIEWER);
661 show_render= (show_viewer && ima->type == IMA_TYPE_R_RESULT);
663 /* draw the image or grid */
665 draw_image_grid(ar, zoomx, zoomy);
666 else if(sima->flag & SI_DRAW_TILE)
667 draw_image_buffer_repeated(sima, ar, scene, ima, ibuf, zoomx, zoomy);
668 else if(ima && (ima->tpageflag & IMA_TILES))
669 draw_image_buffer_tiled(sima, ar, scene, ima, ibuf, 0.0f, 0.0, zoomx, zoomy);
671 draw_image_buffer(sima, ar, scene, ima, ibuf, 0.0f, 0.0f, zoomx, zoomy);
674 if(sima->flag & SI_DRAWTOOL)
675 draw_image_paint_helpers(ar, scene, zoomx, zoomy);
678 /* XXX integrate this code */
681 float xoffs=0.0f, yoffs= 0.0f;
683 if(image_preview_active(sa, &xim, &yim)) {
684 xoffs= scene->r.disprect.xmin;
685 yoffs= scene->r.disprect.ymin;
687 calc_image_view(sima, 'f');
688 myortho2(G.v2d->cur.xmin, G.v2d->cur.xmax, G.v2d->cur.ymin, G.v2d->cur.ymax);
689 glRectf(0.0f, 0.0f, 1.0f, 1.0f);
695 ED_space_image_release_buffer(sima, lock);
698 if(ima && show_render)
699 draw_render_info(scene, ima, ar);