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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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_brush_types.h"
35 #include "DNA_camera_types.h"
36 #include "DNA_image_types.h"
37 #include "DNA_object_types.h"
38 #include "DNA_space_types.h"
39 #include "DNA_scene_types.h"
40 #include "DNA_screen_types.h"
44 #include "IMB_imbuf.h"
45 #include "IMB_imbuf_types.h"
47 #include "BKE_colortools.h"
48 #include "BKE_global.h"
49 #include "BKE_image.h"
50 #include "BKE_utildefines.h"
53 #include "BIF_glutil.h"
56 #include "ED_screen.h"
58 #include "UI_interface.h"
59 #include "UI_resources.h"
60 #include "UI_view2d.h"
64 #include "image_intern.h"
66 #define HEADER_HEIGHT 18
69 static int image_preview_active(SpaceImage *sima, Scene *scene, float *xim, float *yim)
71 /* only when compositor shows, and image handler set */
72 if(sima->image && sima->image->type==IMA_TYPE_COMPOSITE) {
77 for(a=0; a<SPACE_MAXHANDLER; a+=2) {
78 if(sima->blockhandler[a] == IMAGE_HANDLER_PREVIEW) {
79 if(xim) *xim= (scene->r.size*scene->r.xsch)/100;
80 if(yim) *yim= (scene->r.size*scene->r.ysch)/100;
90 /* are there curves? curves visible? and curves do something? */
91 static int image_curves_active(SpaceImage *sima)
94 if(curvemapping_RGBA_does_something(sima->cumap)) {
98 for(a=0; a<SPACE_MAXHANDLER; a+=2) {
99 if(sima->blockhandler[a] == IMAGE_HANDLER_CURVES)
109 static void image_verify_buffer_float(SpaceImage *sima, ImBuf *ibuf)
111 /* detect if we need to redo the curve map.
112 ibuf->rect is zero for compositor and render results after change
113 convert to 32 bits always... drawing float rects isnt supported well (atis)
115 NOTE: if float buffer changes, we have to manually remove the rect
118 if(ibuf->rect_float) {
119 if(ibuf->rect==NULL) {
120 if(image_curves_active(sima)) {
121 curvemapping_do_ibuf(sima->cumap, ibuf);
124 IMB_rect_from_float(ibuf);
130 static void draw_render_info(SpaceImage *sima, ARegion *ar)
134 int showspare= 0; // XXX BIF_show_render_spare();
135 char *str= "render text"; // XXX BIF_render_text();
142 rect.ymin= ar->winrct.ymax - ar->winrct.ymin - HEADER_HEIGHT;
143 rect.xmax= ar->winrct.xmax - ar->winrct.xmin;
144 rect.ymax= ar->winrct.ymax - ar->winrct.ymin;
146 /* clear header rect */
147 UI_GetThemeColor3fv(TH_BACK, colf);
148 glColor3f(colf[0]+0.1f, colf[1]+0.1f, colf[2]+0.1f);
149 glRecti(rect.xmin, rect.ymin, rect.xmax, rect.ymax+1);
151 UI_ThemeColor(TH_TEXT_HI);
154 UI_DrawString(12, rect.ymin + 5, "(Previous)");
155 UI_DrawString(72, rect.ymin + 5, str);
158 UI_DrawString(12, rect.ymin + 5, str);
161 void draw_image_info(ARegion *ar, int channels, int x, int y, char *cp, float *fp, int *zp, float *zpf)
166 ED_region_pixelspace(ar);
168 ofs= sprintf(str, "X: %d Y: %d ", x, y);
170 ofs+= sprintf(str+ofs, "| R: %d G: %d B: %d A: %d ", cp[0], cp[1], cp[2], cp[3]);
174 ofs+= sprintf(str+ofs, "| R: %.3f G: %.3f B: %.3f A: %.3f ", fp[0], fp[1], fp[2], fp[3]);
176 ofs+= sprintf(str+ofs, "| Val: %.3f ", fp[0]);
178 ofs+= sprintf(str+ofs, "| R: %.3f G: %.3f B: %.3f ", fp[0], fp[1], fp[2]);
182 ofs+= sprintf(str+ofs, "| Z: %.4f ", 0.5+0.5*(((float)*zp)/(float)0x7fffffff));
184 ofs+= sprintf(str+ofs, "| Z: %.3f ", *zpf);
186 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
189 glColor4f(.0,.0,.0,.25);
190 glRectf(0.0, 0.0, ar->winrct.xmax - ar->winrct.xmin + 1, 30.0);
193 glColor3ub(255, 255, 255);
195 UI_DrawString(10, 10, str);
200 static void draw_image_grid(ARegion *ar, float zoomx, float zoomy)
202 float gridsize, gridstep= 1.0f/32.0f;
206 /* the image is located inside (0,0),(1, 1) as set by view2d */
207 UI_ThemeColorShade(TH_BACK, 20);
209 UI_view2d_to_region_no_clip(&ar->v2d, 0.0f, 0.0f, &x1, &y1);
210 UI_view2d_to_region_no_clip(&ar->v2d, 1.0f, 1.0f, &x2, &y2);
211 glRectf(x1, y1, x2, y2);
213 /* gridsize adapted to zoom level */
214 gridsize= 0.5f*(zoomx+zoomy);
215 if(gridsize<=0.0f) return;
218 while(gridsize<1.0f) {
224 while(gridsize>=4.0f) {
230 /* the fine resolution level */
231 blendfac= 0.25*gridsize - floor(0.25*gridsize);
232 CLAMP(blendfac, 0.0, 1.0);
233 UI_ThemeColorShade(TH_BACK, (int)(20.0*(1.0-blendfac)));
238 glVertex2f(x1, y1*(1.0f-fac) + y2*fac);
239 glVertex2f(x2, y1*(1.0f-fac) + y2*fac);
240 glVertex2f(x1*(1.0f-fac) + x2*fac, y1);
241 glVertex2f(x1*(1.0f-fac) + x2*fac, y2);
245 /* the large resolution level */
246 UI_ThemeColor(TH_BACK);
250 glVertex2f(x1, y1*(1.0f-fac) + y2*fac);
251 glVertex2f(x2, y1*(1.0f-fac) + y2*fac);
252 glVertex2f(x1*(1.0f-fac) + x2*fac, y1);
253 glVertex2f(x1*(1.0f-fac) + x2*fac, y2);
259 static void sima_draw_alpha_backdrop(float x1, float y1, float xsize, float ysize, float zoomx, float zoomy)
261 GLubyte checker_stipple[32*32/8] =
263 255,255,0,0,255,255,0,0,255,255,0,0,255,255,0,0, \
264 255,255,0,0,255,255,0,0,255,255,0,0,255,255,0,0, \
265 255,255,0,0,255,255,0,0,255,255,0,0,255,255,0,0, \
266 255,255,0,0,255,255,0,0,255,255,0,0,255,255,0,0, \
267 0,0,255,255,0,0,255,255,0,0,255,255,0,0,255,255, \
268 0,0,255,255,0,0,255,255,0,0,255,255,0,0,255,255, \
269 0,0,255,255,0,0,255,255,0,0,255,255,0,0,255,255, \
270 0,0,255,255,0,0,255,255,0,0,255,255,0,0,255,255, \
273 glColor3ub(100, 100, 100);
274 glRectf(x1, y1, x1 + zoomx*xsize, y1 + zoomy*ysize);
275 glColor3ub(160, 160, 160);
277 glEnable(GL_POLYGON_STIPPLE);
278 glPolygonStipple(checker_stipple);
279 glRectf(x1, y1, x1 + zoomx*xsize, y1 + zoomy*ysize);
280 glDisable(GL_POLYGON_STIPPLE);
283 static void sima_draw_alpha_pixels(float x1, float y1, int rectx, int recty, unsigned int *recti)
286 /* swap bytes, so alpha is most significant one, then just draw it as luminance int */
287 if(ENDIAN_ORDER == B_ENDIAN)
288 glPixelStorei(GL_UNPACK_SWAP_BYTES, 1);
290 glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_LUMINANCE, GL_UNSIGNED_INT, recti);
291 glPixelStorei(GL_UNPACK_SWAP_BYTES, 0);
294 static void sima_draw_alpha_pixelsf(float x1, float y1, int rectx, int recty, float *rectf)
296 float *trectf= MEM_mallocN(rectx*recty*4, "temp");
299 for(a= rectx*recty -1, b= 4*a+3; a>=0; a--, b-=4)
302 glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_LUMINANCE, GL_FLOAT, trectf);
304 /* ogl trick below is slower... (on ATI 9600) */
305 // glColorMask(1, 0, 0, 0);
306 // glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_RGBA, GL_FLOAT, rectf+3);
307 // glColorMask(0, 1, 0, 0);
308 // glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_RGBA, GL_FLOAT, rectf+2);
309 // glColorMask(0, 0, 1, 0);
310 // glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_RGBA, GL_FLOAT, rectf+1);
311 // glColorMask(1, 1, 1, 1);
315 static void sima_draw_colorcorrected_pixels(float x1, float y1, ImBuf *ibuf)
317 colorcorrection_do_ibuf(ibuf, "MONOSCNR.ICM"); /* path is hardcoded here, find some place better */
319 glaDrawPixelsSafe(x1, y1, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->crect);
323 static void sima_draw_zbuf_pixels(float x1, float y1, int rectx, int recty, int *recti)
325 /* zbuffer values are signed, so we need to shift color range */
326 glPixelTransferf(GL_RED_SCALE, 0.5f);
327 glPixelTransferf(GL_GREEN_SCALE, 0.5f);
328 glPixelTransferf(GL_BLUE_SCALE, 0.5f);
329 glPixelTransferf(GL_RED_BIAS, 0.5f);
330 glPixelTransferf(GL_GREEN_BIAS, 0.5f);
331 glPixelTransferf(GL_BLUE_BIAS, 0.5f);
333 glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_LUMINANCE, GL_INT, recti);
335 glPixelTransferf(GL_RED_SCALE, 1.0f);
336 glPixelTransferf(GL_GREEN_SCALE, 1.0f);
337 glPixelTransferf(GL_BLUE_SCALE, 1.0f);
338 glPixelTransferf(GL_RED_BIAS, 0.0f);
339 glPixelTransferf(GL_GREEN_BIAS, 0.0f);
340 glPixelTransferf(GL_BLUE_BIAS, 0.0f);
343 static void sima_draw_zbuffloat_pixels(Scene *scene, float x1, float y1, int rectx, int recty, float *rect_float)
345 float bias, scale, *rectf, clipend;
348 if(scene->camera && scene->camera->type==OB_CAMERA) {
349 bias= ((Camera *)scene->camera->data)->clipsta;
350 clipend= ((Camera *)scene->camera->data)->clipend;
351 scale= 1.0f/(clipend-bias);
359 rectf= MEM_mallocN(rectx*recty*4, "temp");
360 for(a= rectx*recty -1; a>=0; a--) {
361 if(rect_float[a]>clipend)
363 else if(rect_float[a]<bias)
366 rectf[a]= 1.0f - (rect_float[a]-bias)*scale;
370 glaDrawPixelsSafe(x1, y1, rectx, recty, rectx, GL_LUMINANCE, GL_FLOAT, rectf);
375 static void draw_image_buffer(SpaceImage *sima, ARegion *ar, Scene *scene, ImBuf *ibuf, float fx, float fy, float zoomx, float zoomy)
380 glPixelZoom(zoomx, zoomy);
382 /* find window pixel coordinates of origin */
383 UI_view2d_to_region_no_clip(&ar->v2d, fx, fy, &x, &y);
385 /* this part is generic image display */
386 if(sima->flag & SI_SHOW_ALPHA) {
388 sima_draw_alpha_pixels(x, y, ibuf->x, ibuf->y, ibuf->rect);
389 else if(ibuf->rect_float && ibuf->channels==4)
390 sima_draw_alpha_pixelsf(x, y, ibuf->x, ibuf->y, ibuf->rect_float);
392 else if(sima->flag & SI_SHOW_ZBUF && (ibuf->zbuf || ibuf->zbuf_float || (ibuf->channels==1))) {
394 sima_draw_zbuf_pixels(x, y, ibuf->x, ibuf->y, ibuf->zbuf);
395 else if(ibuf->zbuf_float)
396 sima_draw_zbuffloat_pixels(scene, x, y, ibuf->x, ibuf->y, ibuf->zbuf_float);
397 else if(ibuf->channels==1)
398 sima_draw_zbuffloat_pixels(scene, x, y, ibuf->x, ibuf->y, ibuf->rect_float);
401 else if(sima->flag & SI_COLOR_CORRECTION) {
402 image_verify_buffer_float(sima, ibuf);
404 sima_draw_colorcorrected_pixels(x, y, ibuf);
409 if(sima->flag & SI_USE_ALPHA) {
410 sima_draw_alpha_backdrop(x, y, ibuf->x, ibuf->y, zoomx, zoomy);
413 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
416 /* we don't draw floats buffers directly but
417 * convert them, and optionally apply curves */
418 image_verify_buffer_float(sima, ibuf);
421 glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
423 glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_FLOAT, ibuf->rect_float);*/
425 if(sima->flag & SI_USE_ALPHA)
430 glPixelZoom(1.0f, 1.0f);
433 static unsigned int *get_part_from_ibuf(ImBuf *ibuf, short startx, short starty, short endx, short endy)
435 unsigned int *rt, *rp, *rectmain;
436 short y, heigth, len;
438 /* the right offset in rectot */
440 rt= ibuf->rect+ (starty*ibuf->x+ startx);
443 heigth= (endy-starty);
445 rp=rectmain= MEM_mallocN(heigth*len*sizeof(int), "rect");
447 for(y=0; y<heigth; y++) {
448 memcpy(rp, rt, len*4);
455 static void draw_image_buffer_tiled(SpaceImage *sima, ARegion *ar, Image *ima, ImBuf *ibuf, float fx, float fy, float zoomx, float zoomy)
458 int dx, dy, sx, sy, x, y;
460 /* verify valid values, just leave this a while */
461 if(ima->xrep<1) return;
462 if(ima->yrep<1) return;
464 glPixelZoom(zoomx, zoomy);
466 if(sima->curtile >= ima->xrep*ima->yrep)
467 sima->curtile = ima->xrep*ima->yrep - 1;
469 /* create char buffer from float if needed */
470 image_verify_buffer_float(sima, ibuf);
472 /* retrieve part of image buffer */
473 dx= ibuf->x/ima->xrep;
474 dy= ibuf->y/ima->yrep;
475 sx= (sima->curtile % ima->xrep)*dx;
476 sy= (sima->curtile / ima->xrep)*dy;
477 rect= get_part_from_ibuf(ibuf, sx, sy, sx+dx, sy+dy);
480 for(sy=0; sy+dy<=ibuf->y; sy+= dy) {
481 for(sx=0; sx+dx<=ibuf->x; sx+= dx) {
482 UI_view2d_to_region_no_clip(&ar->v2d, fx + (float)sx/(float)ibuf->x, fy + (float)sy/(float)ibuf->y, &x, &y);
484 glaDrawPixelsSafe(x, y, dx, dy, dx, GL_RGBA, GL_UNSIGNED_BYTE, rect);
488 glPixelZoom(1.0f, 1.0f);
493 static void draw_image_buffer_repeated(SpaceImage *sima, ARegion *ar, Scene *scene, Image *ima, ImBuf *ibuf, float zoomx, float zoomy)
498 time_current = PIL_check_seconds_timer();
500 for(x=floor(ar->v2d.cur.xmin); x<ar->v2d.cur.xmax; x += 1.0f) {
501 for(y=floor(ar->v2d.cur.ymin); y<ar->v2d.cur.ymax; y += 1.0f) {
502 if(ima && (ima->tpageflag & IMA_TILES))
503 draw_image_buffer_tiled(sima, ar, ima, ibuf, x, y, zoomx, zoomy);
505 draw_image_buffer(sima, ar, scene, ibuf, x, y, zoomx, zoomy);
507 /* only draw until running out of time */
508 if((PIL_check_seconds_timer() - time_current) > 0.25)
516 /* draw grease pencil */
518 static void draw_image_grease_pencil(SpaceImage *sima, ImBuf *ibuf)
521 /* draw grease-pencil ('image' strokes) */
522 if (sima->flag & SI_DISPGP)
523 ; // XXX draw_gpencil_2dimage(sa, ibuf);
526 mywinset(sa->win); /* restore scissor after gla call... */
527 wmOrtho2(-0.375, sa->winx-0.375, -0.375, sa->winy-0.375);
530 /* draw grease-pencil (screen strokes) */
531 if (sima->flag & SI_DISPGP)
532 ; // XXX draw_gpencil_2dview(sa, NULL);
535 /* XXX becomes WM paint cursor */
537 static void draw_image_view_tool(Scene *scene)
539 ToolSettings *settings= scene->toolsettings;
540 Brush *brush= settings->imapaint.brush;
546 if(settings->imapaint.flag & IMAGEPAINT_DRAWING) {
547 if(settings->imapaint.flag & IMAGEPAINT_DRAW_TOOL_DRAWING)
550 else if(settings->imapaint.flag & IMAGEPAINT_DRAW_TOOL)
554 getmouseco_areawin(mval);
556 radius= brush->size*G.sima->zoom/2;
557 fdrawXORcirc(mval[0], mval[1], radius);
559 if (brush->innerradius != 1.0) {
560 radius *= brush->innerradius;
561 fdrawXORcirc(mval[0], mval[1], radius);
568 static unsigned char *get_alpha_clone_image(Scene *scene, int *width, int *height)
570 Brush *brush = scene->toolsettings->imapaint.brush;
572 unsigned int size, alpha;
573 unsigned char *rect, *cp;
575 if(!brush || !brush->clone.image)
578 ibuf= BKE_image_get_ibuf(brush->clone.image, NULL);
580 if(!ibuf || !ibuf->rect)
583 rect= MEM_dupallocN(ibuf->rect);
590 size= (*width)*(*height);
591 alpha= (unsigned char)255*brush->clone.alpha;
602 static void draw_image_paint_helpers(SpaceImage *sima, ARegion *ar, Scene *scene, float zoomx, float zoomy)
606 unsigned char *clonerect;
608 brush= scene->toolsettings->imapaint.brush;
610 if(brush && (scene->toolsettings->imapaint.tool == PAINT_TOOL_CLONE)) {
611 /* this is not very efficient, but glDrawPixels doesn't allow
612 drawing with alpha */
613 clonerect= get_alpha_clone_image(scene, &w, &h);
616 UI_view2d_to_region_no_clip(&ar->v2d, brush->clone.offset[0], brush->clone.offset[1], &x, &y);
618 glPixelZoom(zoomx, zoomy);
621 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
622 glaDrawPixelsSafe(x, y, w, h, w, GL_RGBA, GL_UNSIGNED_BYTE, clonerect);
625 glPixelZoom(1.0, 1.0);
627 MEM_freeN(clonerect);
632 /* draw main image area */
634 void draw_image_main(SpaceImage *sima, ARegion *ar, Scene *scene)
639 int show_viewer, show_render;
641 /* XXX can we do this in refresh? */
646 ED_image_aspect(sima->image, &xuser_asp, &yuser_asp);
648 /* UGLY hack? until now iusers worked fine... but for flipbook viewer we need this */
649 if(sima->image->type==IMA_TYPE_COMPOSITE) {
650 ImageUser *iuser= ntree_get_active_iuser(scene->nodetree);
652 BKE_image_user_calc_imanr(iuser, scene->r.cfra, 0);
656 /* and we check for spare */
657 ibuf= ED_space_image_buffer(sima);
661 /* put scene context variable in iuser */
662 sima->iuser.scene= scene;
663 /* retrieve the image and information about it */
664 ima= ED_space_image(sima);
665 ibuf= ED_space_image_buffer(sima);
666 ED_space_image_zoom(sima, ar, &zoomx, &zoomy);
668 show_viewer= (ima && ima->source == IMA_SRC_VIEWER);
669 show_render= (show_viewer && ima->type == IMA_TYPE_R_RESULT);
671 /* draw the image or grid */
673 draw_image_grid(ar, zoomx, zoomy);
674 else if(sima->flag & SI_DRAW_TILE)
675 draw_image_buffer_repeated(sima, ar, scene, ima, ibuf, zoomx, zoomy);
676 else if(ima && (ima->tpageflag & IMA_TILES))
677 draw_image_buffer_tiled(sima, ar, ima, ibuf, 0.0f, 0.0, zoomx, zoomy);
679 draw_image_buffer(sima, ar, scene, ibuf, 0.0f, 0.0f, zoomx, zoomy);
682 draw_image_grease_pencil(sima, ibuf);
685 draw_image_paint_helpers(sima, ar, scene, zoomx, zoomy);
688 if(ibuf && show_render)
689 draw_render_info(sima, ar);
691 /* XXX integrate this code */
694 float xoffs=0.0f, yoffs= 0.0f;
696 if(image_preview_active(sa, &xim, &yim)) {
697 xoffs= scene->r.disprect.xmin;
698 yoffs= scene->r.disprect.ymin;
700 calc_image_view(sima, 'f');
701 myortho2(G.v2d->cur.xmin, G.v2d->cur.xmax, G.v2d->cur.ymin, G.v2d->cur.ymax);
702 glRectf(0.0f, 0.0f, 1.0f, 1.0f);