xref: /petsc/src/sys/classes/draw/impls/image/drawimage.c (revision 63a3b9bc7a1f24f247904ccba9383635fe6abade)
1 #include <../src/sys/classes/draw/impls/image/drawimage.h>   /*I  "petscdraw.h" I*/
2 #include <petsc/private/drawimpl.h>                          /*I  "petscdraw.h" I*/
3 
4 #if defined(PETSC_USE_DEBUG)
5 #define PetscDrawValidColor(color) PetscCheck((color)>=0&&(color)<256,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Color value %" PetscInt_FMT " out of range [0..255]",(PetscInt)(color))
6 #else
7 #define PetscDrawValidColor(color) do {} while (0)
8 #endif
9 
10 #define XTRANS(draw,img,x)  ((int)(((img)->w-1)*((draw)->port_xl + ((((x) - (draw)->coor_xl)*((draw)->port_xr - (draw)->port_xl))/((draw)->coor_xr - (draw)->coor_xl)))))
11 #define YTRANS(draw,img,y)  (((img)->h-1) - (int)(((img)->h-1)*((draw)->port_yl + ((((y) - (draw)->coor_yl)*((draw)->port_yr - (draw)->port_yl))/((draw)->coor_yr - (draw)->coor_yl)))))
12 
13 #define ITRANS(draw,img,i)  ((draw)->coor_xl + (((PetscReal)(i))*((draw)->coor_xr - (draw)->coor_xl)/((img)->w-1) - (draw)->port_xl)/((draw)->port_xr - (draw)->port_xl))
14 #define JTRANS(draw,img,j)  ((draw)->coor_yl + (((PetscReal)(j))/((img)->h-1) + (draw)->port_yl - 1)*((draw)->coor_yr - (draw)->coor_yl)/((draw)->port_yl - (draw)->port_yr))
15 
16 static PetscErrorCode PetscDrawSetViewport_Image(PetscDraw draw,PetscReal xl,PetscReal yl,PetscReal xr,PetscReal yr)
17 {
18   PetscImage img = (PetscImage)draw->data;
19   PetscFunctionBegin;
20   {
21     int xmax = img->w - 1,   ymax = img->h - 1;
22     int xa = (int)(xl*xmax), ya = ymax - (int)(yr*ymax);
23     int xb = (int)(xr*xmax), yb = ymax - (int)(yl*ymax);
24     PetscImageSetClip(img,xa,ya,xb+1-xa,yb+1-ya);
25   }
26   PetscFunctionReturn(0);
27 }
28 
29 /*
30 static PetscErrorCode PetscDrawSetCoordinates_Image(PetscDraw draw,PetscReal xl,PetscReal yl,PetscReal xr,PetscReal yr)
31 {
32   PetscFunctionBegin;
33   PetscFunctionReturn(0);
34 }*/
35 #define PetscDrawSetCoordinates_Image NULL
36 
37 static PetscErrorCode PetscDrawCoordinateToPixel_Image(PetscDraw draw,PetscReal x,PetscReal y,int *i,int *j)
38 {
39   PetscImage img = (PetscImage)draw->data;
40   PetscFunctionBegin;
41   if (i) *i = XTRANS(draw,img,x);
42   if (j) *j = YTRANS(draw,img,y);
43   PetscFunctionReturn(0);
44 }
45 
46 static PetscErrorCode PetscDrawPixelToCoordinate_Image(PetscDraw draw,int i,int j,PetscReal *x,PetscReal *y)
47 {
48   PetscImage img = (PetscImage)draw->data;
49   PetscFunctionBegin;
50   if (x) *x = ITRANS(draw,img,i);
51   if (y) *y = JTRANS(draw,img,j);
52   PetscFunctionReturn(0);
53 }
54 
55 /*
56 static PetscErrorCode PetscDrawPointSetSize_Image(PetscDraw draw,PetscReal width)
57 {
58   PetscFunctionBegin;
59   PetscFunctionReturn(0);
60 }*/
61 #define PetscDrawPointSetSize_Image NULL
62 
63 static PetscErrorCode PetscDrawPoint_Image(PetscDraw draw,PetscReal x,PetscReal y,int c)
64 {
65   PetscImage img = (PetscImage)draw->data;
66   PetscFunctionBegin;
67   PetscDrawValidColor(c);
68   {
69     int j, xx = XTRANS(draw,img,x);
70     int i, yy = YTRANS(draw,img,y);
71     for (i=-1; i<=1; i++)
72       for (j=-1; j<=1; j++)
73         PetscImageDrawPixel(img,xx+j,yy+i,c);
74   }
75   PetscFunctionReturn(0);
76 }
77 
78 static PetscErrorCode PetscDrawPointPixel_Image(PetscDraw draw,int x,int y,int c)
79 {
80   PetscImage img = (PetscImage)draw->data;
81   PetscFunctionBegin;
82   PetscDrawValidColor(c);
83   {
84     PetscImageDrawPixel(img,x,y,c);
85   }
86   PetscFunctionReturn(0);
87 }
88 
89 /*
90 static PetscErrorCode PetscDrawLineSetWidth_Image(PetscDraw draw,PetscReal width)
91 {
92   PetscFunctionBegin;
93   PetscFunctionReturn(0);
94 }*/
95 #define PetscDrawLineSetWidth_Image NULL
96 
97 static PetscErrorCode PetscDrawLineGetWidth_Image(PetscDraw draw,PetscReal *width)
98 {
99   PetscImage img = (PetscImage)draw->data;
100   PetscFunctionBegin;
101   {
102     int lw = 1;
103     *width = lw*(draw->coor_xr - draw->coor_xl)/(img->w*(draw->port_xr - draw->port_xl));
104   }
105   PetscFunctionReturn(0);
106 }
107 
108 static PetscErrorCode PetscDrawLine_Image(PetscDraw draw,PetscReal xl,PetscReal yl,PetscReal xr,PetscReal yr,int c)
109 {
110   PetscImage img = (PetscImage)draw->data;
111   PetscFunctionBegin;
112   {
113     int x_1 = XTRANS(draw,img,xl), x_2 = XTRANS(draw,img,xr);
114     int y_1 = YTRANS(draw,img,yl), y_2 = YTRANS(draw,img,yr);
115     PetscImageDrawLine(img,x_1,y_1,x_2,y_2,c);
116   }
117   PetscFunctionReturn(0);
118 }
119 
120 static PetscErrorCode PetscDrawArrow_Image(PetscDraw draw,PetscReal xl,PetscReal yl,PetscReal xr,PetscReal yr,int c)
121 {
122   PetscImage img = (PetscImage)draw->data;
123   PetscFunctionBegin;
124   PetscDrawValidColor(c);
125   {
126     int x_1 = XTRANS(draw,img,xl), x_2 = XTRANS(draw,img,xr);
127     int y_1 = YTRANS(draw,img,yl), y_2 = YTRANS(draw,img,yr);
128     if (x_1 == x_2 && y_1 == y_2) PetscFunctionReturn(0);
129     PetscImageDrawLine(img,x_1,y_1,x_2,y_2,c);
130     if (x_1 == x_2 && PetscAbs(y_1 - y_2) > 7) {
131       if (y_2 > y_1) {
132         PetscImageDrawLine(img,x_2,y_2,x_2-3,y_2-3,c);
133         PetscImageDrawLine(img,x_2,y_2,x_2+3,y_2-3,c);
134       } else {
135         PetscImageDrawLine(img,x_2,y_2,x_2-3,y_2+3,c);
136         PetscImageDrawLine(img,x_2,y_2,x_2+3,y_2+3,c);
137       }
138     }
139     if (y_1 == y_2 && PetscAbs(x_1 - x_2) > 7) {
140       if (x_2 > x_1) {
141         PetscImageDrawLine(img,x_2-3,y_2-3,x_2,y_2,c);
142         PetscImageDrawLine(img,x_2-3,y_2+3,x_2,y_2,c);
143       } else {
144         PetscImageDrawLine(img,x_2,y_2,x_2+3,y_2-3,c);
145         PetscImageDrawLine(img,x_2,y_2,x_2+3,y_2+3,c);
146       }
147     }
148    }
149   PetscFunctionReturn(0);
150 }
151 
152 static PetscErrorCode PetscDrawRectangle_Image(PetscDraw draw,PetscReal xl,PetscReal yl,PetscReal xr,PetscReal yr,int c1,int c2,int c3,int c4)
153 {
154   PetscImage img = (PetscImage)draw->data;
155   PetscFunctionBegin;
156   PetscDrawValidColor(c1);
157   PetscDrawValidColor(c2);
158   PetscDrawValidColor(c3);
159   PetscDrawValidColor(c4);
160   {
161     int x = XTRANS(draw,img,xl), w = XTRANS(draw,img,xr) + 1 - x;
162     int y = YTRANS(draw,img,yr), h = YTRANS(draw,img,yl) + 1 - y;
163     int c  = (c1 + c2 + c3 + c4)/4;
164     PetscImageDrawRectangle(img,x,y,w,h,c);
165   }
166   PetscFunctionReturn(0);
167 }
168 
169 static PetscErrorCode PetscDrawEllipse_Image(PetscDraw draw,PetscReal x,PetscReal y,PetscReal a,PetscReal b,int c)
170 {
171   PetscImage img = (PetscImage)draw->data;
172   PetscFunctionBegin;
173   PetscDrawValidColor(c);
174   a = PetscAbsReal(a);
175   b = PetscAbsReal(b);
176   {
177     int xc = XTRANS(draw,img,x), w = XTRANS(draw,img,x + a/2) + 0 - xc;
178     int yc = YTRANS(draw,img,y), h = YTRANS(draw,img,y - b/2) + 0 - yc;
179     if (PetscAbsReal(a-b) <= 0)  w = h = PetscMin(w,h); /* workaround truncation errors */
180     PetscImageDrawEllipse(img,xc,yc,w,h,c);
181   }
182   PetscFunctionReturn(0);
183 }
184 
185 static PetscErrorCode PetscDrawTriangle_Image(PetscDraw draw,PetscReal X_1,PetscReal Y_1,PetscReal X_2,PetscReal Y_2,PetscReal X_3,PetscReal Y_3,int c1,int c2,int c3)
186 {
187   PetscImage img = (PetscImage)draw->data;
188   PetscFunctionBegin;
189   PetscDrawValidColor(c1);
190   PetscDrawValidColor(c2);
191   PetscDrawValidColor(c3);
192   {
193     int x_1 = XTRANS(draw,img,X_1), x_2 = XTRANS(draw,img,X_2), x_3 = XTRANS(draw,img,X_3);
194     int y_1 = YTRANS(draw,img,Y_1), y_2 = YTRANS(draw,img,Y_2), y_3 = YTRANS(draw,img,Y_3);
195     PetscImageDrawTriangle(img,x_1,y_1,c1,x_2,y_2,c2,x_3,y_3,c3);
196   }
197   PetscFunctionReturn(0);
198 }
199 
200 /*
201 static PetscErrorCode PetscDrawStringSetSize_Image(PetscDraw draw,PetscReal w,PetscReal h)
202 {
203   PetscFunctionBegin;
204   PetscFunctionReturn(0);
205 }*/
206 #define PetscDrawStringSetSize_Image NULL
207 
208 static PetscErrorCode PetscDrawStringGetSize_Image(PetscDraw draw,PetscReal *w,PetscReal  *h)
209 {
210   PetscImage img = (PetscImage)draw->data;
211   PetscFunctionBegin;
212   {
213     int tw = PetscImageFontWidth;
214     int th = PetscImageFontHeight;
215     if (w) *w = tw*(draw->coor_xr - draw->coor_xl)/(img->w*(draw->port_xr - draw->port_xl));
216     if (h) *h = th*(draw->coor_yr - draw->coor_yl)/(img->h*(draw->port_yr - draw->port_yl));
217   }
218   PetscFunctionReturn(0);
219 }
220 
221 static PetscErrorCode PetscDrawString_Image(PetscDraw draw,PetscReal x,PetscReal y,int c,const char text[])
222 {
223   PetscImage     img = (PetscImage)draw->data;
224   PetscToken     token;
225   char           *subtext;
226   PetscFunctionBegin;
227   PetscDrawValidColor(c);
228   {
229     int xx = XTRANS(draw,img,x);
230     int yy = YTRANS(draw,img,y);
231     PetscCall(PetscTokenCreate(text,'\n',&token));
232     PetscCall(PetscTokenFind(token,&subtext));
233     while (subtext) {
234       PetscImageDrawText(img,xx,yy,c,subtext);
235       yy += PetscImageFontHeight;
236       PetscCall(PetscTokenFind(token,&subtext));
237     }
238     PetscCall(PetscTokenDestroy(&token));
239   }
240   PetscFunctionReturn(0);
241 }
242 
243 static PetscErrorCode PetscDrawStringVertical_Image(PetscDraw draw,PetscReal x,PetscReal y,int c,const char text[])
244 {
245   PetscImage img = (PetscImage)draw->data;
246   PetscFunctionBegin;
247   PetscDrawValidColor(c);
248   {
249     char chr[2] = {0, 0};
250     int  xx = XTRANS(draw,img,x);
251     int  yy = YTRANS(draw,img,y);
252     int  offset = PetscImageFontHeight;
253     while ((chr[0] = *text++)) {
254       PetscImageDrawText(img,xx,yy+offset,c,chr);
255       yy += PetscImageFontHeight;
256     }
257   }
258   PetscFunctionReturn(0);
259 }
260 
261 /*
262 static PetscErrorCode PetscDrawStringBoxed_Image(PetscDraw draw,PetscReal sxl,PetscReal syl,int sc,int bc,const char text[],PetscReal *w,PetscReal *h)
263 {
264   PetscFunctionBegin;
265   if (w) *w = 0;
266   if (h) *h = 0;
267   PetscFunctionReturn(0);
268 */
269 #define PetscDrawStringBoxed_Image NULL
270 
271 /*
272 static PetscErrorCode PetscDrawFlush_Image(PetscDraw draw)
273 {
274   PetscFunctionBegin;
275   PetscFunctionReturn(0);
276 }*/
277 #define PetscDrawFlush_Image NULL
278 
279 static PetscErrorCode PetscDrawClear_Image(PetscDraw draw)
280 {
281   PetscImage     img = (PetscImage)draw->data;
282   PetscFunctionBegin;
283   {
284     PetscImageClear(img);
285   }
286   PetscFunctionReturn(0);
287 }
288 
289 /*
290 static PetscErrorCode PetscDrawSetDoubleBuffer_Image(PetscDraw draw)
291 {
292   PetscFunctionBegin;
293   PetscFunctionReturn(0);
294 }*/
295 #define PetscDrawSetDoubleBuffer_Image NULL
296 
297 static PetscErrorCode PetscDrawGetPopup_Image(PetscDraw draw,PetscDraw *popup)
298 {
299   PetscBool      flg = PETSC_FALSE;
300 
301   PetscFunctionBegin;
302   PetscCall(PetscOptionsGetBool(((PetscObject)draw)->options,((PetscObject)draw)->prefix,"-draw_popup",&flg,NULL));
303   if (!flg) {*popup = NULL; PetscFunctionReturn(0);}
304   PetscCall(PetscDrawCreate(PetscObjectComm((PetscObject)draw),NULL,NULL,0,0,220,220,popup));
305   PetscCall(PetscDrawSetType(*popup,PETSC_DRAW_IMAGE));
306   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)*popup,"popup_"));
307   PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)*popup,((PetscObject)draw)->prefix));
308   draw->popup = *popup;
309   PetscFunctionReturn(0);
310 }
311 
312 /*
313 static PetscErrorCode PetscDrawSetTitle_Image(PetscDraw draw,const char title[])
314 {
315   PetscFunctionBegin;
316   PetscFunctionReturn(0);
317 }*/
318 #define PetscDrawSetTitle_Image NULL
319 
320 /*
321 static PetscErrorCode PetscDrawCheckResizedWindow_Image(PetscDraw draw)
322 {
323   PetscFunctionBegin;
324   PetscFunctionReturn(0);
325 }*/
326 #define PetscDrawCheckResizedWindow_Image NULL
327 
328 static PetscErrorCode PetscDrawResizeWindow_Image(PetscDraw draw,int w,int h)
329 {
330   PetscImage     img = (PetscImage)draw->data;
331 
332   PetscFunctionBegin;
333   if (w == img->w && h == img->h) PetscFunctionReturn(0);
334   PetscCall(PetscFree(img->buffer));
335 
336   img->w = w; img->h = h;
337   PetscCall(PetscCalloc1((size_t)(img->w*img->h),&img->buffer));
338   PetscCall(PetscDrawSetViewport_Image(draw,draw->port_xl,draw->port_yl,draw->port_xr,draw->port_yr));
339   PetscFunctionReturn(0);
340 }
341 
342 static PetscErrorCode PetscDrawDestroy_Image(PetscDraw draw)
343 {
344   PetscImage     img = (PetscImage)draw->data;
345 
346   PetscFunctionBegin;
347   PetscCall(PetscDrawDestroy(&draw->popup));
348   PetscCall(PetscFree(img->buffer));
349   PetscCall(PetscFree(draw->data));
350   PetscFunctionReturn(0);
351 }
352 
353 /*
354 static PetscErrorCode PetscDrawView_Image(PetscDraw draw,PetscViewer viewer)
355 {
356   PetscFunctionBegin;
357   PetscFunctionReturn(0);
358 }*/
359 #define PetscDrawView_Image NULL
360 
361 /*
362 static PetscErrorCode PetscDrawGetMouseButton_Image(PetscDraw draw,PetscDrawButton *button,PetscReal *x_user,PetscReal *y_user,PetscReal *x_phys,PetscReal *y_phys)
363 {
364   PetscFunctionBegin;
365   *button = PETSC_BUTTON_NONE;
366   if (x_user) *x_user = 0;
367   if (y_user) *y_user = 0;
368   if (x_phys) *x_phys = 0;
369   if (y_phys) *y_phys = 0;
370   PetscFunctionReturn(0);
371 }*/
372 #define PetscDrawGetMouseButton_Image NULL
373 
374 /*
375 static PetscErrorCode PetscDrawPause_Image(PetscDraw draw)
376 {
377   PetscFunctionBegin;
378   PetscFunctionReturn(0);
379 }*/
380 #define PetscDrawPause_Image NULL
381 
382 /*
383 static PetscErrorCode PetscDrawBeginPage_Image(PetscDraw draw)
384 {
385   PetscFunctionBegin;
386   PetscFunctionReturn(0);
387 }*/
388 #define PetscDrawBeginPage_Image NULL
389 
390 /*
391 static PetscErrorCode PetscDrawEndPage_Image(PetscDraw draw)
392 {
393   PetscFunctionBegin;
394   PetscFunctionReturn(0);
395 }*/
396 #define PetscDrawEndPage_Image NULL
397 
398 static PetscErrorCode PetscDrawGetSingleton_Image(PetscDraw draw,PetscDraw *sdraw)
399 {
400   PetscImage     pimg = (PetscImage)draw->data;
401   PetscImage     simg;
402 
403   PetscFunctionBegin;
404   PetscCall(PetscDrawCreate(PETSC_COMM_SELF,NULL,NULL,0,0,draw->w,draw->h,sdraw));
405   PetscCall(PetscDrawSetType(*sdraw,PETSC_DRAW_IMAGE));
406   (*sdraw)->ops->resizewindow = NULL;
407   simg = (PetscImage)(*sdraw)->data;
408   PetscCall(PetscArraycpy(simg->buffer,pimg->buffer,pimg->w*pimg->h));
409   PetscFunctionReturn(0);
410 }
411 
412 static PetscErrorCode PetscDrawRestoreSingleton_Image(PetscDraw draw,PetscDraw *sdraw)
413 {
414   PetscImage     pimg = (PetscImage)draw->data;
415   PetscImage     simg = (PetscImage)(*sdraw)->data;
416 
417   PetscFunctionBegin;
418   PetscCall(PetscArraycpy(pimg->buffer,simg->buffer,pimg->w*pimg->h));
419   PetscCall(PetscDrawDestroy(sdraw));
420   PetscFunctionReturn(0);
421 }
422 
423 /*
424 static PetscErrorCode PetscDrawSave_Image(PetscDraw draw)
425 {
426   PetscFunctionBegin;
427   PetscFunctionReturn(0);
428 }*/
429 #define PetscDrawSave_Image NULL
430 
431 static PetscErrorCode PetscDrawGetImage_Image(PetscDraw draw,unsigned char palette[256][3],unsigned int *w,unsigned int *h,unsigned char *pixels[])
432 {
433   PetscImage     img = (PetscImage)draw->data;
434   unsigned char  *buffer = NULL;
435   PetscMPIInt    rank,size;
436 
437   PetscFunctionBegin;
438   if (w) *w = (unsigned int)img->w;
439   if (h) *h = (unsigned int)img->h;
440   if (pixels) *pixels = NULL;
441   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)draw),&rank));
442   if (rank == 0) {
443     PetscCall(PetscMemcpy(palette,img->palette,sizeof(img->palette)));
444     PetscCall(PetscMalloc1((size_t)(img->w*img->h),&buffer));
445     if (pixels) *pixels = buffer;
446   }
447   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)draw),&size));
448   if (size == 1) {
449     PetscCall(PetscArraycpy(buffer,img->buffer,img->w*img->h));
450   } else {
451     PetscCallMPI(MPI_Reduce(img->buffer,buffer,img->w*img->h,MPI_UNSIGNED_CHAR,MPI_MAX,0,PetscObjectComm((PetscObject)draw)));
452   }
453   PetscFunctionReturn(0);
454 }
455 
456 static struct _PetscDrawOps DvOps = {
457   PetscDrawSetDoubleBuffer_Image,
458   PetscDrawFlush_Image,
459   PetscDrawLine_Image,
460   PetscDrawLineSetWidth_Image,
461   PetscDrawLineGetWidth_Image,
462   PetscDrawPoint_Image,
463   PetscDrawPointSetSize_Image,
464   PetscDrawString_Image,
465   PetscDrawStringVertical_Image,
466   PetscDrawStringSetSize_Image,
467   PetscDrawStringGetSize_Image,
468   PetscDrawSetViewport_Image,
469   PetscDrawClear_Image,
470   PetscDrawRectangle_Image,
471   PetscDrawTriangle_Image,
472   PetscDrawEllipse_Image,
473   PetscDrawGetMouseButton_Image,
474   PetscDrawPause_Image,
475   PetscDrawBeginPage_Image,
476   PetscDrawEndPage_Image,
477   PetscDrawGetPopup_Image,
478   PetscDrawSetTitle_Image,
479   PetscDrawCheckResizedWindow_Image,
480   PetscDrawResizeWindow_Image,
481   PetscDrawDestroy_Image,
482   PetscDrawView_Image,
483   PetscDrawGetSingleton_Image,
484   PetscDrawRestoreSingleton_Image,
485   PetscDrawSave_Image,
486   PetscDrawGetImage_Image,
487   PetscDrawSetCoordinates_Image,
488   PetscDrawArrow_Image,
489   PetscDrawCoordinateToPixel_Image,
490   PetscDrawPixelToCoordinate_Image,
491   PetscDrawPointPixel_Image,
492   PetscDrawStringBoxed_Image
493 };
494 
495 static const unsigned char BasicColors[PETSC_DRAW_BASIC_COLORS][3] = {
496   { 255, 255, 255 }, /* white */
497   {   0,   0,   0 }, /* black */
498   { 255,   0,   0 }, /* red */
499   {   0, 255,   0 }, /* green */
500   {   0, 255, 255 }, /* cyan */
501   {   0,   0, 255 }, /* blue */
502   { 255,   0, 255 }, /* magenta */
503   { 127, 255, 212 }, /* aquamarine */
504   {  34, 139,  34 }, /* forestgreen */
505   { 255, 165,   0 }, /* orange */
506   { 238, 130, 238 }, /* violet */
507   { 165,  42,  42 }, /* brown */
508   { 255, 192, 203 }, /* pink */
509   { 255, 127,  80 }, /* coral */
510   { 190, 190, 190 }, /* gray */
511   { 255, 255,   0 }, /* yellow */
512   { 255, 215,   0 }, /* gold */
513   { 255, 182, 193 }, /* lightpink */
514   {  72, 209, 204 }, /* mediumturquoise */
515   { 240, 230, 140 }, /* khaki */
516   { 105, 105, 105 }, /* dimgray */
517   {  54, 205,  50 }, /* yellowgreen */
518   { 135, 206, 235 }, /* skyblue */
519   {   0, 100,   0 }, /* darkgreen */
520   {   0,   0, 128 }, /* navyblue */
521   { 244, 164,  96 }, /* sandybrown */
522   {  95, 158, 160 }, /* cadetblue */
523   { 176, 224, 230 }, /* powderblue */
524   { 255,  20, 147 }, /* deeppink */
525   { 216, 191, 216 }, /* thistle */
526   {  50, 205,  50 }, /* limegreen */
527   { 255, 240, 245 }, /* lavenderblush */
528   { 221, 160, 221 }, /* plum */
529 };
530 
531 /*MC
532    PETSC_DRAW_IMAGE - PETSc graphics device that uses a raster buffer
533 
534    Options Database Keys:
535 .  -draw_size w,h - size of image in pixels
536 
537    Level: beginner
538 
539 .seealso:  PetscDrawOpenImage(), PetscDrawSetFromOptions()
540 M*/
541 PETSC_EXTERN PetscErrorCode PetscDrawCreate_Image(PetscDraw);
542 
543 PETSC_EXTERN PetscErrorCode PetscDrawCreate_Image(PetscDraw draw)
544 {
545   PetscImage     img;
546   int            w = draw->w, h = draw->h;
547   PetscInt       size[2], nsize = 2;
548   PetscBool      set;
549 
550   PetscFunctionBegin;
551   draw->pause   = 0;
552   draw->coor_xl = 0; draw->coor_xr = 1;
553   draw->coor_yl = 0; draw->coor_yr = 1;
554   draw->port_xl = 0; draw->port_xr = 1;
555   draw->port_yl = 0; draw->port_yr = 1;
556 
557   size[0] = w; if (size[0] < 1) size[0] = 300;
558   size[1] = h; if (size[1] < 1) size[1] = size[0];
559   PetscCall(PetscOptionsGetIntArray(((PetscObject)draw)->options,((PetscObject)draw)->prefix,"-draw_size",size,&nsize,&set));
560   if (set && nsize == 1) size[1] = size[0];
561   if (size[0] < 1) size[0] = 300;
562   if (size[1] < 1) size[1] = size[0];
563   draw->w = w = size[0]; draw->x = 0;
564   draw->h = h = size[1]; draw->x = 0;
565 
566   PetscCall(PetscNewLog(draw,&img));
567   PetscCall(PetscMemcpy(draw->ops,&DvOps,sizeof(DvOps)));
568   draw->data = (void*)img;
569 
570   img->w = w; img->h = h;
571   PetscCall(PetscCalloc1((size_t)(img->w*img->h),&img->buffer));
572   PetscImageSetClip(img,0,0,img->w,img->h);
573   {
574     int i,k,ncolors = 256-PETSC_DRAW_BASIC_COLORS;
575     unsigned char R[256-PETSC_DRAW_BASIC_COLORS];
576     unsigned char G[256-PETSC_DRAW_BASIC_COLORS];
577     unsigned char B[256-PETSC_DRAW_BASIC_COLORS];
578     PetscCall(PetscDrawUtilitySetCmap(NULL,ncolors,R,G,B));
579     for (k=0; k<PETSC_DRAW_BASIC_COLORS; k++) {
580       img->palette[k][0] = BasicColors[k][0];
581       img->palette[k][1] = BasicColors[k][1];
582       img->palette[k][2] = BasicColors[k][2];
583     }
584     for (i=0; i<ncolors; i++, k++) {
585       img->palette[k][0] = R[i];
586       img->palette[k][1] = G[i];
587       img->palette[k][2] = B[i];
588     }
589   }
590 
591   if (!draw->savefilename) PetscCall(PetscDrawSetSave(draw,draw->title));
592   PetscFunctionReturn(0);
593 }
594 
595 /*@C
596    PetscDrawOpenImage - Opens an image for use with the PetscDraw routines.
597 
598    Collective
599 
600    Input Parameters:
601 +  comm - the communicator that will share image
602 -  filename - optional name of the file
603 -  w, h - the image width and height in pixels
604 
605    Output Parameters:
606 .  draw - the drawing context.
607 
608    Level: beginner
609 
610 .seealso: PetscDrawSetSave(), PetscDrawSetFromOptions(), PetscDrawCreate(), PetscDrawDestroy()
611 @*/
612 PetscErrorCode PetscDrawOpenImage(MPI_Comm comm,const char filename[],int w,int h,PetscDraw *draw)
613 {
614   PetscFunctionBegin;
615   PetscCall(PetscDrawCreate(comm,NULL,NULL,0,0,w,h,draw));
616   PetscCall(PetscDrawSetType(*draw,PETSC_DRAW_IMAGE));
617   PetscCall(PetscDrawSetSave(*draw,filename));
618   PetscFunctionReturn(0);
619 }
620