Debian Codegraph @

sys/Graphics_image.cpp

raw
1 /* Graphics_image.cpp
2 *
3 * Copyright (C) 1992-2021,2024-2026 Paul Boersma
4 *
5 * This code is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or (at
8 * your option) any later version.
9 *
10 * This code is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this work. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "GraphicsP.h"
20
21 #include "../fon/Photo.h"
22
23 #if gdi
24 #include <gdiplus.h>
25 #elif quartz
26 #include <time.h>
27 #include "macport_on.h"
28 static void _mac_releaseDataCallback (void *info, const void *data, size_t size) {
29 (void) info;
30 (void) size;
31 Melder_free (data);
32 }
33 #endif
34
35 #define wdx(x) ((x) * my scaleX + my deltaX)
36 #define wdy(y) ((y) * my scaleY + my deltaY)
37
38 static void _GraphicsScreen_cellArrayOrImage (GraphicsScreen me,
39 constMATVU const& z_float, constmatrixview <MelderColour> const& z_rgbt, constmatrixview <unsigned char> const& z_byte,
40 integer ix1, integer ix2, integer x1DC, integer x2DC,
41 integer iy1, integer iy2, integer y1DC, integer y2DC,
42 double minimum, double maximum,
43 integer clipx1, integer clipx2, integer clipy1, integer clipy2, bool interpolate)
44 {
45 /*double t=Melder_clock();*/
46 integer nx = ix2 - ix1 + 1; /* The number of cells along the horizontal axis. */
47 integer ny = iy2 - iy1 + 1; /* The number of cells along the vertical axis. */
48 double dx = (double) (x2DC - x1DC) / (double) nx; /* Horizontal pixels per cell. Positive. */
49 double dy = (double) (y2DC - y1DC) / (double) ny; /* Vertical pixels per cell. Negative. */
50 double scale = 255.0 / (maximum - minimum), offset = 255.0 + minimum * scale;
51 if (x2DC <= x1DC || y1DC <= y2DC) return;
52 trace (U"scale ", scale);
53 /* Clip by the intersection of the world window and the outline of the cells. */
54 //Melder_casual (U"clipy1 ", clipy1, U" clipy2 ", clipy2);
55 if (clipx1 < x1DC) clipx1 = x1DC;
56 if (clipx2 > x2DC) clipx2 = x2DC;
57 if (clipy1 > y1DC) clipy1 = y1DC;
58 if (clipy2 < y2DC) clipy2 = y2DC;
59 /*
60 * The first decision is whether we are going to use the standard rectangle drawing
61 * (cellArray only), or whether we are going to write into a bitmap.
62 * The standard drawing is best for small numbers of cells,
63 * provided that some cells are larger than a pixel.
64 */
65 if (! interpolate && nx * ny < 3000 && (dx > 1.0 || dy < -1.0)) {
66 try {
67 /*unsigned int cellWidth = (unsigned int) dx + 1;*/
68 unsigned int cellHeight = (unsigned int) (- (int) dy) + 1;
69 integer ix, iy;
70 #if cairo
71 cairo_pattern_t *grey [256];
72 for (int igrey = 0; igrey < sizeof (grey) / sizeof (*grey); igrey ++) {
73 double v = igrey / ((double) (sizeof (grey) / sizeof (*grey)) - 1.0);
74 grey [igrey] = cairo_pattern_create_rgb (v, v, v);
75 }
76 #elif gdi
77 static HBRUSH greyBrush [256];
78 RECT rect;
79 if (! greyBrush [0])
80 for (int igrey = 0; igrey <= 255; igrey ++)
81 greyBrush [igrey] = CreateSolidBrush (RGB (igrey, igrey, igrey)); // once
82 #elif quartz
83 CGContextSetAlpha (my d_macGraphicsContext, 1.0);
84 CGContextSetBlendMode (my d_macGraphicsContext, kCGBlendModeNormal);
85 #endif
86 autoINTVEC leftsBuffer = zero_INTVEC (ix2 - ix1 + 2);
87 integer *lefts = & leftsBuffer [1 - ix1];
88 for (ix = ix1; ix <= ix2 + 1; ix ++)
89 lefts [ix] = x1DC + (integer) ((ix - ix1) * dx);
90 for (iy = iy1; iy <= iy2; iy ++) {
91 integer bottom = y1DC + (integer) ((iy - iy1) * dy), top = bottom - cellHeight;
92 if (top > clipy1 || bottom < clipy2)
93 continue;
94 Melder_clipLeft (clipy2, & top);
95 Melder_clipRight (& bottom, clipy1);
96 #if gdi
97 rect. bottom = bottom;
98 rect. top = top;
99 #endif
100 for (ix = ix1; ix <= ix2; ix ++) {
101 integer left = lefts [ix], right = lefts [ix + 1];
102 if (right < clipx1 || left > clipx2)
103 continue;
104 Melder_clipLeft (clipx1, & left);
105 Melder_clipRight (& right, clipx2);
106 if (! NUMisEmpty (z_rgbt)) {
107 #if cairo
108 // NYI
109 #elif gdi
110 // NYI
111 #elif quartz
112 double red = z_rgbt [iy] [ix]. red;
113 double green = z_rgbt [iy] [ix]. green;
114 double blue = z_rgbt [iy] [ix]. blue;
115 double transparency = z_rgbt [iy] [ix]. transparency;
116 Melder_clip (0.0, & red, 1.0);
117 Melder_clip (0.0, & green, 1.0);
118 Melder_clip (0.0, & blue, 1.0);
119 CGContextSetRGBFillColor (my d_macGraphicsContext, red, green, blue, 1.0 - transparency);
120 CGContextFillRect (my d_macGraphicsContext, CGRectMake (left, top, right - left, bottom - top));
121 #endif
122 } else {
123 #if cairo
124 integer value = offset - scale * ( ! NUMisEmpty (z_float) ? z_float [iy] [ix] : z_byte [iy] [ix] );
125 cairo_set_source (my d_cairoGraphicsContext, grey [value <= 0 ? 0 : value >= sizeof (grey) / sizeof (*grey) ? sizeof (grey) / sizeof (*grey) : value]);
126 cairo_rectangle (my d_cairoGraphicsContext, left, top, right - left, bottom - top);
127 cairo_fill (my d_cairoGraphicsContext);
128 #elif gdi
129 integer value = offset - scale * ( ! NUMisEmpty (z_float) ? z_float [iy] [ix] : z_byte [iy] [ix] );
130 rect. left = left; rect. right = right;
131 FillRect (my d_gdiGraphicsContext, & rect, greyBrush [value <= 0 ? 0 : value >= 255 ? 255 : value]);
132 #elif quartz
133 double value = offset - scale * ( ! NUMisEmpty (z_float) ? z_float [iy] [ix] : z_byte [iy] [ix] );
134 double igrey = ( value <= 0 ? 0 : value >= 255 ? 255 : value ) / 255.0;
135 CGContextSetRGBFillColor (my d_macGraphicsContext, igrey, igrey, igrey, 1.0);
136 CGContextFillRect (my d_macGraphicsContext, CGRectMake (left, top, right - left, bottom - top));
137 #endif
138 }
139 }
140 }
141
142 #if cairo
143 for (int igrey = 0; igrey < sizeof (grey) / sizeof (*grey); igrey ++)
144 cairo_pattern_destroy (grey [igrey]);
145 #elif quartz
146 CGContextSetRGBFillColor (my d_macGraphicsContext, 0.0, 0.0, 0.0, 1.0);
147 #endif
148 } catch (MelderError) { }
149 } else {
150 integer xDC, yDC;
151 integer undersampling = 1;
152 /*
153 * Prepare for off-screen bitmap drawing.
154 */
155 #if cairo
156 integer arrayWidth = clipx2 - clipx1;
157 integer arrayHeight = clipy1 - clipy2;
158 trace (U"arrayWidth ", arrayWidth, U", arrayHeight ", arrayHeight);
159 cairo_surface_t *sfc = cairo_image_surface_create (CAIRO_FORMAT_RGB24, arrayWidth, arrayHeight);
160 unsigned char *bits = cairo_image_surface_get_data (sfc);
161 int scanLineLength = cairo_image_surface_get_stride (sfc);
162 unsigned char grey [256];
163 trace (
164 U"image surface address ", Melder_pointer (sfc),
165 U", bits address ", Melder_pointer (bits),
166 U", scanLineLength ", scanLineLength,
167 U", numberOfGreys ", sizeof (grey) / sizeof (*grey)
168 );
169 for (int igrey = 0; igrey < sizeof (grey) / sizeof (*grey); igrey++)
170 grey [igrey] = 255 - (unsigned char) (igrey * 255.0 / (sizeof (grey) / sizeof (*grey) - 1));
171 #elif gdi
172 integer bitmapWidth = clipx2 - clipx1, bitmapHeight = clipy1 - clipy2;
173 int igrey;
174 /*
175 * Create a device-independent bitmap, 32 bits deep.
176 */
177 struct { BITMAPINFOHEADER header; } bitmapInfo;
178 integer scanLineLength = bitmapWidth * 4; // for 24 bits: (bitmapWidth * 3 + 3) & ~3L;
179 HBITMAP bitmap;
180 unsigned char *bits; // a pointer to memory allocated by VirtualAlloc or by CreateDIBSection ()
181 bitmapInfo. header.biSize = sizeof (BITMAPINFOHEADER);
182 bitmapInfo. header.biWidth = bitmapWidth; // scanLineLength;
183 bitmapInfo. header.biHeight = bitmapHeight;
184 bitmapInfo. header.biPlanes = 1;
185 bitmapInfo. header.biBitCount = 32;
186 bitmapInfo. header.biCompression = 0;
187 bitmapInfo. header.biSizeImage = 0;
188 bitmapInfo. header.biXPelsPerMeter = 0;
189 bitmapInfo. header.biYPelsPerMeter = 0;
190 bitmapInfo. header.biClrUsed = 0;
191 bitmapInfo. header.biClrImportant = 0;
192 bitmap = CreateDIBSection (my d_gdiGraphicsContext /* ignored */, (CONST BITMAPINFO *) & bitmapInfo,
193 DIB_RGB_COLORS, (VOID **) & bits, nullptr, 0);
194 #elif quartz
195 integer bytesPerRow = (clipx2 - clipx1) * 4;
196 Melder_assert (bytesPerRow > 0);
197 integer numberOfRows = clipy1 - clipy2;
198 Melder_assert (numberOfRows > 0);
199 unsigned char *imageData = Melder_malloc_f (unsigned char, bytesPerRow * numberOfRows);
200 #endif
201 /*
202 * Draw into the bitmap.
203 */
204 #if cairo
205 #define ROW_START_ADDRESS (bits + (clipy1 - 1 - yDC) * scanLineLength)
206 #define PUT_PIXEL \
207 if (1) { \
208 unsigned char kar = value <= 0 ? 0 : value >= 255 ? 255 : (int) value; \
209 *pixelAddress ++ = kar; \
210 *pixelAddress ++ = kar; \
211 *pixelAddress ++ = kar; \
212 *pixelAddress ++ = 0; \
213 }
214 #elif gdi
215 #define ROW_START_ADDRESS (bits + (clipy1 - 1 - yDC) * scanLineLength)
216 #define PUT_PIXEL \
217 if (1) { \
218 unsigned char kar = value <= 0 ? 0 : value >= 255 ? 255 : (int) value; \
219 *pixelAddress ++ = kar; \
220 *pixelAddress ++ = kar; \
221 *pixelAddress ++ = kar; \
222 *pixelAddress ++ = 0; \
223 }
224 #elif quartz
225 #define ROW_START_ADDRESS (imageData + (clipy1 - 1 - yDC) * bytesPerRow)
226 #define PUT_PIXEL \
227 if (my colourScale == kGraphics_colourScale::GREY) { \
228 unsigned char kar = value <= 0 ? 0 : value >= 255 ? 255 : (int) value; \
229 *pixelAddress ++ = kar; \
230 *pixelAddress ++ = kar; \
231 *pixelAddress ++ = kar; \
232 *pixelAddress ++ = 255; \
233 } else if (my colourScale == kGraphics_colourScale::BLUE_TO_RED) { \
234 if (value < 0.0) { \
235 *pixelAddress ++ = 0; \
236 *pixelAddress ++ = 0; \
237 *pixelAddress ++ = 63; \
238 *pixelAddress ++ = 255; \
239 } else if (value < 64.0) { \
240 *pixelAddress ++ = 0; \
241 *pixelAddress ++ = 0; \
242 *pixelAddress ++ = (int) (value * 3 + 63.999); \
243 *pixelAddress ++ = 255; \
244 } else if (value < 128.0) { \
245 *pixelAddress ++ = (int) (value * 4 - 256.0); \
246 *pixelAddress ++ = (int) (value * 4 - 256.0); \
247 *pixelAddress ++ = 255; \
248 *pixelAddress ++ = 255; \
249 } else if (value < 192.0) { \
250 *pixelAddress ++ = 255; \
251 *pixelAddress ++ = (int) ((256.0 - value) * 4 - 256.0); \
252 *pixelAddress ++ = (int) ((256.0 - value) * 4 - 256.0); \
253 *pixelAddress ++ = 255; \
254 } else if (value < 256.0) { \
255 *pixelAddress ++ = (int) ((256.0 - value) * 3 + 63.999); \
256 *pixelAddress ++ = 0; \
257 *pixelAddress ++ = 0; \
258 *pixelAddress ++ = 255; \
259 } else { \
260 *pixelAddress ++ = 63; \
261 *pixelAddress ++ = 0; \
262 *pixelAddress ++ = 0; \
263 *pixelAddress ++ = 255; \
264 } \
265 }
266 #else
267 #define ROW_START_ADDRESS nullptr
268 #define PUT_PIXEL
269 #endif
270 if (interpolate) {
271 try {
272 autoINTVEC ileftBuffer = zero_INTVEC (clipx2 - clipx1 + 1);
273 autoINTVEC irightBuffer = zero_INTVEC (clipx2 - clipx1 + 1);
274 autoVEC leftWeightBuffer = zero_VEC (clipx2 - clipx1 + 1);
275 autoVEC rightWeightBuffer = zero_VEC (clipx2 - clipx1 + 1);
276 integer *ileft = & ileftBuffer [1 - clipx1];
277 integer *iright = & irightBuffer [1 - clipx1];
278 double *leftWeight = & leftWeightBuffer [1 - clipx1];
279 double *rightWeight = & rightWeightBuffer [1 - clipx1];
280 for (xDC = clipx1; xDC < clipx2; xDC += undersampling) {
281 double ix_real = ix1 - 0.5 + ((double) nx * (xDC - x1DC)) / (x2DC - x1DC);
282 ileft [xDC] = (integer) floor (ix_real), iright [xDC] = ileft [xDC] + 1;
283 rightWeight [xDC] = ix_real - ileft [xDC];
284 leftWeight [xDC] = 1.0 - rightWeight [xDC];
285 if (ileft [xDC] < ix1)
286 ileft [xDC] = ix1;
287 if (iright [xDC] > ix2)
288 iright [xDC] = ix2;
289 }
290 for (yDC = clipy2; yDC < clipy1; yDC += undersampling) {
291 double iy_real = iy2 + 0.5 - ((double) ny * (yDC - y2DC)) / (y1DC - y2DC);
292 integer itop = Melder_iceiling (iy_real), ibottom = itop - 1;
293 double bottomWeight = itop - iy_real, topWeight = 1.0 - bottomWeight;
294 unsigned char *pixelAddress = ROW_START_ADDRESS;
295 if (itop > iy2) itop = iy2;
296 if (ibottom < iy1) ibottom = iy1;
297 if (! NUMisEmpty (z_float)) {
298 constVECVU ztop = z_float [itop], zbottom = z_float [ibottom];
299 for (xDC = clipx1; xDC < clipx2; xDC += undersampling) {
300 double interpol =
301 rightWeight [xDC] *
302 (topWeight * ztop [iright [xDC]] + bottomWeight * zbottom [iright [xDC]]) +
303 leftWeight [xDC] *
304 (topWeight * ztop [ileft [xDC]] + bottomWeight * zbottom [ileft [xDC]]);
305 double value = offset - scale * interpol;
306 PUT_PIXEL
307 }
308 } else if (! NUMisEmpty (z_rgbt)) {
309 constvectorview <MelderColour> ztop = z_rgbt [itop], zbottom = z_rgbt [ibottom];
310 for (xDC = clipx1; xDC < clipx2; xDC += undersampling) {
311 double red =
312 rightWeight [xDC] * (topWeight * ztop [iright [xDC]]. red + bottomWeight * zbottom [iright [xDC]]. red) +
313 leftWeight [xDC] * (topWeight * ztop [ileft [xDC]]. red + bottomWeight * zbottom [ileft [xDC]]. red);
314 double green =
315 rightWeight [xDC] * (topWeight * ztop [iright [xDC]]. green + bottomWeight * zbottom [iright [xDC]]. green) +
316 leftWeight [xDC] * (topWeight * ztop [ileft [xDC]]. green + bottomWeight * zbottom [ileft [xDC]]. green);
317 double blue =
318 rightWeight [xDC] * (topWeight * ztop [iright [xDC]]. blue + bottomWeight * zbottom [iright [xDC]]. blue) +
319 leftWeight [xDC] * (topWeight * ztop [ileft [xDC]]. blue + bottomWeight * zbottom [ileft [xDC]]. blue);
320 double transparency =
321 rightWeight [xDC] * (topWeight * ztop [iright [xDC]]. transparency + bottomWeight * zbottom [iright [xDC]]. transparency) +
322 leftWeight [xDC] * (topWeight * ztop [ileft [xDC]]. transparency + bottomWeight * zbottom [ileft [xDC]]. transparency);
323 Melder_clip (0.0, & red, 1.0);
324 Melder_clip (0.0, & green, 1.0);
325 Melder_clip (0.0, & blue, 1.0);
326 Melder_clip (0.0, & transparency, 1.0);
327
328 #if cairo
329 *pixelAddress ++ = blue * 255.0;
330 *pixelAddress ++ = green * 255.0;
331 *pixelAddress ++ = red * 255.0;
332 *pixelAddress ++ = transparency * 255.0;
333 #elif gdi
334 *pixelAddress ++ = blue * 255.0;
335 *pixelAddress ++ = green * 255.0;
336 *pixelAddress ++ = red * 255.0;
337 *pixelAddress ++ = 0;
338 #elif quartz
339 *pixelAddress ++ = red * 255.0;
340 *pixelAddress ++ = green * 255.0;
341 *pixelAddress ++ = blue * 255.0;
342 *pixelAddress ++ = (1.0 - transparency) * 255.0;
343 #endif
344 }
345 } else {
346 constvectorview <unsigned char> ztop = z_byte [itop], zbottom = z_byte [ibottom];
347 for (xDC = clipx1; xDC < clipx2; xDC += undersampling) {
348 double interpol =
349 rightWeight [xDC] *
350 (topWeight * ztop [iright [xDC]] + bottomWeight * zbottom [iright [xDC]]) +
351 leftWeight [xDC] *
352 (topWeight * ztop [ileft [xDC]] + bottomWeight * zbottom [ileft [xDC]]);
353 double value = offset - scale * interpol;
354 PUT_PIXEL
355 }
356 }
357 }
358 } catch (MelderError) {
359 Melder_clearError ();
360 }
361 } else {
362 try {
363 autoINTVEC ixBuffer = zero_INTVEC (clipx2 - clipx1 + 1);
364 integer *ix = & ixBuffer [1 - clipx1];
365 for (xDC = clipx1; xDC < clipx2; xDC += undersampling)
366 ix [xDC] = Melder_ifloor (ix1 + (nx * (xDC - x1DC)) / (x2DC - x1DC));
367 for (yDC = clipy2; yDC < clipy1; yDC += undersampling) {
368 integer iy = Melder_iceiling (iy2 - (ny * (yDC - y2DC)) / (y1DC - y2DC));
369 unsigned char *pixelAddress = ROW_START_ADDRESS;
370 Melder_assert (iy >= iy1 && iy <= iy2);
371 if (! NUMisEmpty (z_float)) {
372 constVECVU ziy = z_float [iy];
373 for (xDC = clipx1; xDC < clipx2; xDC += undersampling) {
374 double value = offset - scale * ziy [ix [xDC]];
375 PUT_PIXEL
376 }
377 } else {
378 constvectorview <unsigned char> ziy = z_byte [iy];
379 for (xDC = clipx1; xDC < clipx2; xDC += undersampling) {
380 double value = offset - scale * ziy [ix [xDC]];
381 PUT_PIXEL
382 }
383 }
384 }
385 } catch (MelderError) {
386 Melder_clearError ();
387 }
388 }
389 /*
390 * Copy the bitmap to the screen.
391 */
392 #if cairo
393 cairo_matrix_t clip_trans;
394 cairo_matrix_init_identity (& clip_trans);
395 cairo_matrix_scale (& clip_trans, 1, -1); // we painted in the reverse y-direction
396 cairo_matrix_translate (& clip_trans, - clipx1, - clipy1);
397 cairo_pattern_t *bitmap_pattern = cairo_pattern_create_for_surface (sfc);
398 trace (U"bitmap pattern ", Melder_pointer (bitmap_pattern));
399 if (cairo_status_t status = cairo_pattern_status (bitmap_pattern)) {
400 Melder_casual (U"bitmap pattern status: ", Melder_peek8to32_u (cairo_status_to_string (status)));
401 } else {
402 cairo_pattern_set_matrix (bitmap_pattern, & clip_trans);
403 cairo_save (my d_cairoGraphicsContext);
404 cairo_set_source (my d_cairoGraphicsContext, bitmap_pattern);
405 cairo_paint (my d_cairoGraphicsContext);
406 cairo_restore (my d_cairoGraphicsContext);
407 }
408 cairo_pattern_destroy (bitmap_pattern);
409 #elif gdi
410 SetDIBitsToDevice (my d_gdiGraphicsContext, clipx1, clipy2, bitmapWidth, bitmapHeight, 0, 0, 0, bitmapHeight,
411 bits, (CONST BITMAPINFO *) & bitmapInfo, DIB_RGB_COLORS);
412 //StretchDIBits (my d_gdiGraphicsContext, clipx1, clipy2, bitmapWidth, bitmapHeight, 0, 0, 0, bitmapHeight,
413 // bits, (CONST BITMAPINFO *) & bitmapInfo, DIB_RGB_COLORS, SRCCOPY);
414 #elif quartz
415 CGImageRef image;
416 static CGColorSpaceRef colourSpace = nullptr;
417 if (! colourSpace) {
418 colourSpace = CGColorSpaceCreateWithName (kCGColorSpaceGenericRGB); // used to be kCGColorSpaceUserRGB
419 Melder_assert (colourSpace != nullptr);
420 }
421 if (1) {
422 CGDataProviderRef dataProvider = CGDataProviderCreateWithData (nullptr,
423 imageData,
424 bytesPerRow * numberOfRows,
425 _mac_releaseDataCallback // we need this because we cannot release the image data immediately after drawing,
426 // because in PDF files the imageData has to stay available through EndPage
427 );
428 Melder_assert (dataProvider != nullptr);
429 image = CGImageCreate (clipx2 - clipx1, numberOfRows,
430 8, 32, bytesPerRow, colourSpace, kCGImageAlphaLast, dataProvider, nullptr, false, kCGRenderingIntentDefault);
431 CGDataProviderRelease (dataProvider);
432 } else if (0) {
433 Melder_assert (CGBitmapContextCreate != nullptr);
434 CGContextRef bitmaptest = CGBitmapContextCreate (imageData, 100, 100,
435 8, 800, colourSpace, 0);
436 Melder_assert (bitmaptest != nullptr);
437 CGContextRef bitmap = CGBitmapContextCreate (nullptr /* imageData */, clipx2 - clipx1, numberOfRows,
438 8, bytesPerRow, colourSpace, kCGImageAlphaLast);
439 Melder_assert (bitmap != nullptr);
440 image = CGBitmapContextCreateImage (bitmap);
441 // release bitmap?
442 }
443 Melder_assert (image != nullptr);
444 CGContextDrawImage (my d_macGraphicsContext, CGRectMake (clipx1, clipy2, clipx2 - clipx1, clipy1 - clipy2), image);
445 //CGColorSpaceRelease (colourSpace);
446 CGImageRelease (image);
447 #endif
448 /*
449 * Clean up.
450 */
451 #if cairo
452 cairo_surface_destroy (sfc);
453 #elif gdi
454 DeleteBitmap (bitmap);
455 #endif
456 }
457 #if gdi
458 end:
459 return;
460 #endif
461 }
462
463 static void _GraphicsPostscript_cellArrayOrImage (GraphicsPostscript me,
464 constMATVU const& z_float,
465 constmatrixview <MelderColour> const& z_rgbt,
466 constmatrixview <unsigned char> const& z_byte,
467 integer ix1, integer ix2, integer x1DC, integer x2DC,
468 integer iy1, integer iy2, integer y1DC, integer y2DC,
469 double minimum, double maximum,
470 integer clipx1, integer clipx2, integer clipy1, integer clipy2, bool interpolate)
471 {
472 integer interpolateX = 1, interpolateY = 1;
473 integer nx = ix2 - ix1 + 1, ny = iy2 - iy1 + 1, filling = 0;
474 double scale = ( my photocopyable ? 200.1f : 255.1f ) / (maximum - minimum);
475 double offset = 255.1f + minimum * scale;
476 int minimalGrey = my photocopyable ? 55 : 0;
477 my d_printf (my d_file, "gsave N %s %s M %s %s L %s %s L %s %s L closepath clip\n",
478 Melder8_integer (clipx1), Melder8_integer (clipy1), Melder8_integer (clipx2 - clipx1), Melder8_integer (0),
479 Melder8_integer (0), Melder8_integer (clipy2 - clipy1), Melder8_integer (clipx1 - clipx2), Melder8_integer (0));
480 my d_printf (my d_file, "%s %s translate %s %s scale\n",
481 Melder8_integer (x1DC), Melder8_integer (y1DC), Melder8_integer (x2DC - x1DC), Melder8_integer (y2DC - y1DC));
482 if (interpolate) {
483 /* The smallest image resolution is 300 dpi. If a sample takes up more than 25.4/300 mm, the 300 dpi resolution is achieved by interpolation. */
484 const double smallestImageResolution = 300.0;
485 double colSize_pixels = (double) (x2DC - x1DC) / nx;
486 double rowSize_pixels = (double) (y2DC - y1DC) / ny;
487 double colSize_inches = colSize_pixels / my resolution;
488 double rowSize_inches = rowSize_pixels / my resolution;
489 interpolateX = Melder_iceiling (colSize_inches * smallestImageResolution); // number of interpolation points per horizontal sample
490 interpolateY = Melder_iceiling (rowSize_inches * smallestImageResolution); // number of interpolation points per vertical sample
491 }
492
493 if (interpolateX <= 1 && interpolateY <= 1) {
494 /* Do not interpolate. */
495 my d_printf (my d_file, "/picstr %s string def %s %s 8 [%s 0 0 %s 0 0]\n"
496 "{ currentfile picstr readhexstring pop } image\n",
497 Melder8_integer (nx), Melder8_integer (nx), Melder8_integer (ny), Melder8_integer (nx), Melder8_integer (ny));
498 } else if (interpolateX > 1 && interpolateY > 1) {
499 /* Interpolate both horizontally and vertically. */
500 integer nx_new = nx * interpolateX;
501 integer ny_new = ny * interpolateY;
502 /* Interpolation between rows requires us to remember two original rows: */
503 my d_printf (my d_file, "/lorow %s string def /hirow %s string def\n",
504 Melder8_integer (nx), Melder8_integer (nx));
505 /* New rows (scanlines) are longer: */
506 my d_printf (my d_file, "/scanline %s string def\n",
507 Melder8_integer (nx_new));
508 /* The first four arguments to the 'image' command, */
509 /* namely the new number of columns, the new number of rows, the bit depth, and the matrix: */
510 my d_printf (my d_file, "%s %s 8 [%s 0 0 %s 0 0]\n",
511 Melder8_integer (nx_new), Melder8_integer (ny_new), Melder8_integer (nx_new), Melder8_integer (ny_new));
512 /* Since our imageproc is going to output only one scanline at a time, */
513 /* the outer loop variable (scanline number) has to be initialized outside the imageproc: */
514 my d_printf (my d_file, "/irow 0 def\n");
515 /* The imageproc starts here. First, we fill one or two original rows if necessary; */
516 /* they are read as hexadecimal strings from the current file, i.e. just after the image command. */
517 my d_printf (my d_file, "{\n"
518 /* First test: are we in the first scanline? If so, read two original rows: */
519 "irow 0 eq { currentfile lorow readhexstring pop pop lorow hirow copy pop } if\n"
520 /* Second test: did we just pass an original data row? */
521 /* If so, */
522 /* (1) move that row backwards; */
523 /* (2) read a new one unless we just passed the last original row: */
524 "irow %s mod %s eq { hirow lorow copy pop\n"
525 "irow %s ne { currentfile hirow readhexstring pop pop } if } if\n",
526 Melder8_integer (interpolateY), Melder8_integer (interpolateY / 2),
527 Melder8_integer (ny_new - interpolateY + interpolateY / 2));
528 /* Where are we between those two rows? */
529 my d_printf (my d_file, "/rowphase irow %s add %s mod %s div def\n",
530 Melder8_integer (interpolateY - interpolateY / 2), Melder8_integer (interpolateY), Melder8_integer (interpolateY));
531 /* Inner loop starts here. It cycles through all new columns: */
532 my d_printf (my d_file, "0 1 %s {\n", Melder8_integer (nx_new - 1));
533 /* Get the inner loop variable: */
534 my d_printf (my d_file, " /icol exch def\n");
535 /* Where are the two original columns? */
536 my d_printf (my d_file, " /locol icol %s sub %s idiv def\n",
537 Melder8_integer (interpolateX / 2), Melder8_integer (interpolateX));
538 my d_printf (my d_file, " /hicol icol %s ge { %s } { icol %s add %s idiv } ifelse def\n",
539 Melder8_integer (nx_new - interpolateX / 2), Melder8_integer (nx - 1),
540 Melder8_integer (interpolateX / 2), Melder8_integer (interpolateX));
541 /* Where are we between those two columns? */
542 my d_printf (my d_file, " /colphase icol %s add %s mod %s div def\n",
543 Melder8_integer (interpolateX - interpolateX / 2), Melder8_integer (interpolateX), Melder8_integer (interpolateX));
544 /* Four-point interpolation: */
545 my d_printf (my d_file,
546 " /plow lorow locol get def\n"
547 " /phigh lorow hicol get def\n"
548 " /qlow hirow locol get def\n"
549 " /qhigh hirow hicol get def\n"
550 " /value\n"
551 " plow phigh plow sub colphase mul add 1 rowphase sub mul\n"
552 " qlow qhigh qlow sub colphase mul add rowphase mul\n"
553 " add def\n"
554 " scanline icol value 0 le { 0 } { value 255 ge { 255 } { value } ifelse } ifelse cvi put\n"
555 "} for\n"
556 "/irow irow 1 add def scanline } image\n");
557 } else if (interpolateX > 1) {
558 /* Interpolate horizontally only. */
559 integer nx_new = nx * interpolateX;
560 /* Remember one original row: */
561 my d_printf (my d_file, "/row %s string def\n", Melder8_integer (nx));
562 /* New rows (scanlines) are longer: */
563 my d_printf (my d_file, "/scanline %s string def\n", Melder8_integer (nx_new));
564 /* The first four arguments to the 'image' command, */
565 /* namely the new number of columns, the number of rows, the bit depth, and the matrix: */
566 my d_printf (my d_file, "%s %s 8 [%s 0 0 %s 0 0]\n",
567 Melder8_integer (nx_new), Melder8_integer (ny), Melder8_integer (nx_new), Melder8_integer (ny));
568 /* The imageproc starts here. We fill one original row. */
569 my d_printf (my d_file, "{\n"
570 "currentfile row readhexstring pop pop\n");
571 /* Loop starts here. It cycles through all new columns: */
572 my d_printf (my d_file, "0 1 %s {\n", Melder8_integer (nx_new - 1));
573 /* Get the loop variable: */
574 my d_printf (my d_file, " /icol exch def\n");
575 /* Where are the two original columns? */
576 my d_printf (my d_file, " /locol icol %s sub %s idiv def\n",
577 Melder8_integer (interpolateX / 2), Melder8_integer (interpolateX));
578 my d_printf (my d_file, " /hicol icol %s ge { %s } { icol %s add %s idiv } ifelse def\n",
579 Melder8_integer (nx_new - interpolateX / 2), Melder8_integer (nx - 1),
580 Melder8_integer (interpolateX / 2), Melder8_integer (interpolateX));
581 /* Where are we between those two columns? */
582 my d_printf (my d_file, " /colphase icol %s add %s mod %s div def\n",
583 Melder8_integer (interpolateX - interpolateX / 2), Melder8_integer (interpolateX), Melder8_integer (interpolateX));
584 /* Two-point interpolation: */
585 my d_printf (my d_file,
586 " /plow row locol get def\n"
587 " /phigh row hicol get def\n"
588 " /value plow phigh plow sub colphase mul add def\n"
589 " scanline icol value 0 le { 0 } { value 255 ge { 255 } { value } ifelse } ifelse cvi put\n"
590 "} for\n"
591 "scanline } image\n");
592 } else {
593 /* Interpolate vertically only. */
594 integer ny_new = ny * interpolateY;
595 /* Interpolation between rows requires us to remember two original rows: */
596 my d_printf (my d_file, "/lorow %s string def /hirow %s string def\n", Melder8_integer (nx), Melder8_integer (nx));
597 /* New rows (scanlines) are equally long: */
598 my d_printf (my d_file, "/scanline %s string def\n", Melder8_integer (nx));
599 /* The first four arguments to the 'image' command, */
600 /* namely the number of columns, the new number of rows, the bit depth, and the matrix: */
601 my d_printf (my d_file, "%s %s 8 [%s 0 0 %s 0 0]\n",
602 Melder8_integer (nx), Melder8_integer (ny_new), Melder8_integer (nx), Melder8_integer (ny_new));
603 /* Since our imageproc is going to output only one scanline at a time, */
604 /* the outer loop variable (scanline number) has to be initialized outside the imageproc: */
605 my d_printf (my d_file, "/irow 0 def\n");
606 /* The imageproc starts here. First, we fill one or two original rows if necessary; */
607 /* they are read as hexadecimal strings from the current file, i.e. just after the image command. */
608 my d_printf (my d_file, "{\n"
609 /* First test: are we in the first scanline? If so, read two original rows: */
610 "irow 0 eq { currentfile lorow readhexstring pop pop lorow hirow copy pop } if\n"
611 /* Second test: did we just pass an original data row? */
612 /* If so, */
613 /* (1) move that row backwards; */
614 /* (2) read a new one unless we just passed the last original row: */
615 "irow %s mod %s eq { hirow lorow copy pop\n"
616 "irow %s ne { currentfile hirow readhexstring pop pop } if } if\n",
617 Melder8_integer (interpolateY), Melder8_integer (interpolateY / 2),
618 Melder8_integer (ny_new - interpolateY + interpolateY / 2));
619 /* Where are we between those two rows? */
620 my d_printf (my d_file, "/rowphase irow %s add %s mod %s div def\n",
621 Melder8_integer (interpolateY - interpolateY / 2),
622 Melder8_integer (interpolateY), Melder8_integer (interpolateY));
623 /* Inner loop starts here. It cycles through all columns: */
624 my d_printf (my d_file, "0 1 %s {\n", Melder8_integer (nx - 1));
625 /* Get the inner loop variable: */
626 my d_printf (my d_file, " /icol exch def\n");
627 /* Two-point interpolation: */
628 my d_printf (my d_file,
629 " /p lorow icol get def\n"
630 " /q hirow icol get def\n"
631 " /value\n"
632 " p 1 rowphase sub mul\n"
633 " q rowphase mul\n"
634 " add def\n"
635 " scanline icol value 0 le { 0 } { value 255 ge { 255 } { value } ifelse } ifelse cvi put\n"
636 "} for\n"
637 "/irow irow 1 add def scanline } image\n");
638 }
639 for (integer iy = iy1; iy <= iy2; iy ++) for (integer ix = ix1; ix <= ix2; ix ++) {
640 int value = (int) (offset - scale * ( ! NUMisEmpty (z_float) ? z_float [iy] [ix] : z_byte [iy] [ix] ));
641 my d_printf (my d_file, "%.2x", value <= minimalGrey ? minimalGrey : value >= 255 ? 255 : value);
642 if (++ filling == 39) { my d_printf (my d_file, "\n"); filling = 0; }
643 }
644 if (filling) my d_printf (my d_file, "\n");
645 my d_printf (my d_file, "grestore\n");
646 }
647
648 static void _cellArrayOrImage (Graphics me,
649 constMATVU const& z_float, constmatrixview <MelderColour> const& z_rgbt, constmatrixview <unsigned char> const& z_byte,
650 integer ix1, integer ix2, integer x1DC, integer x2DC,
651 integer iy1, integer iy2, integer y1DC, integer y2DC, double minimum, double maximum,
652 integer clipx1, integer clipx2, integer clipy1, integer clipy2, bool interpolate)
653 {
654 if (my screen) {
655 _GraphicsScreen_cellArrayOrImage (static_cast <GraphicsScreen> (me), z_float, z_rgbt, z_byte, ix1, ix2, x1DC, x2DC, iy1, iy2, y1DC, y2DC,
656 minimum, maximum, clipx1, clipx2, clipy1, clipy2, interpolate);
657 } else if (my postScript) {
658 _GraphicsPostscript_cellArrayOrImage (static_cast <GraphicsPostscript> (me), z_float, z_rgbt, z_byte, ix1, ix2, x1DC, x2DC, iy1, iy2, y1DC, y2DC,
659 minimum, maximum, clipx1, clipx2, clipy1, clipy2, interpolate);
660 }
661 _Graphics_setColour (me, my colour);
662 }
663
664 static void _autoSize (
665 const constGraphics me,
666 const double x1WC, const double x2WC, const double y1WC, const double y2WC,
667 const integer naturalWidthDC, const integer naturalHeightDC,
668 /* mutable out */ integer *const x1DC, /* mutable out */ integer *const x2DC,
669 /* mutable out */ integer *const y1DC, /* mutable out */ integer *const y2DC
670 ) {
671 *x1DC = wdx (x1WC);
672 *x2DC = wdx (x2WC);
673 *y1DC = wdy (y1WC);
674 *y2DC = wdy (y2WC);
675 if (x1WC == x2WC) {
676 if (y1WC == y2WC) {
677 /*
678 Horizontal and vertical autosizing.
679 */
680 const integer width = naturalWidthDC;
681 *x1DC -= width / 2;
682 *x2DC = *x1DC + width;
683 const integer height = naturalHeightDC;
684 if (my yIsZeroAtTheTop) {
685 *y2DC -= height / 2;
686 *y1DC = *y2DC + height;
687 } else {
688 *y1DC -= height / 2;
689 *y2DC = *y1DC + height;
690 }
691 } else {
692 /*
693 Horizontal autosizing only.
694 */
695 const integer height = ( my yIsZeroAtTheTop ? *y1DC - *y2DC : *y2DC - *y1DC );
696 const integer width = height / (double) naturalHeightDC * (double) naturalWidthDC;
697 *x1DC -= width / 2;
698 *x2DC = *x1DC + width;
699 }
700 } else if (y1WC == y2WC) {
701 /*
702 Vertical autosizing only.
703 */
704 const integer width = *x2DC - *x1DC;
705 const integer height = width / (double) naturalWidthDC * (double) naturalHeightDC;
706 if (my yIsZeroAtTheTop) {
707 *y2DC -= height / 2;
708 *y1DC = *y2DC + height;
709 } else {
710 *y1DC -= height / 2;
711 *y2DC = *y1DC + height;
712 }
713 }
714 }
715
716 void Graphics_cellArray (Graphics me, constMATVU const& z,
717 double x1WC, double x2WC, double y1WC, double y2WC, double minimum, double maximum)
718 {
719 if (z.nrow < 1 || z.ncol < 1 || minimum == maximum)
720 return;
721 if (my recording) {
722 op (CELL_ARRAY, 8 + z.nrow * z.ncol);
723 put (x1WC); put (x2WC); put (y1WC); put (y2WC);
724 put (minimum); put (maximum);
725 put (z.nrow); put (z.ncol);
726 for (integer irow = 1; irow <= z.nrow; irow ++)
727 for (integer icol = 1; icol <= z.ncol; icol ++)
728 put (z [irow] [icol]);
729 } else {
730 /* mutable delayed init */ integer x1DC, x2DC, y1DC, y2DC;
731 _autoSize (me, x1WC, x2WC, y1WC, y2WC, z.ncol, z.nrow, & x1DC, & x2DC, & y1DC, & y2DC);
732 _cellArrayOrImage (me, z, constmatrixview<MelderColour>(), constmatrixview<unsigned char>(),
733 1, z.ncol, x1DC, x2DC, 1, z.nrow, y1DC, y2DC, minimum, maximum,
734 wdx (my d_x1WC), wdx (my d_x2WC), wdy (my d_y1WC), wdy (my d_y2WC), false
735 );
736 }
737 }
738
739 void Graphics_cellArray_colour (Graphics me, constmatrixview <MelderColour> const& z,
740 double x1WC, double x2WC, double y1WC, double y2WC, double minimum, double maximum)
741 {
742 if (z.nrow < 1 || z.ncol < 1 || minimum == maximum)
743 return;
744 if (my recording) {
745 op (CELL_ARRAY_COLOUR, 8 + z.nrow * z.ncol * 4);
746 put (x1WC); put (x2WC); put (y1WC); put (y2WC);
747 put (minimum); put (maximum);
748 put (z.nrow); put (z.ncol);
749 for (integer irow = 1; irow <= z.nrow; irow ++) {
750 constvectorview <MelderColour> row = z [irow];
751 for (integer icol = 1; icol <= z.ncol; icol ++) {
752 put (row [icol]. red);
753 put (row [icol]. green);
754 put (row [icol]. blue);
755 put (row [icol]. transparency);
756 }
757 }
758 } else {
759 /* mutable delayed init */ integer x1DC, x2DC, y1DC, y2DC;
760 _autoSize (me, x1WC, x2WC, y1WC, y2WC, z.ncol, z.nrow, & x1DC, & x2DC, & y1DC, & y2DC);
761 _cellArrayOrImage (me, constMATVU(), z, constmatrixview<unsigned char>(),
762 1, z.ncol, x1DC, x2DC, 1, z.nrow, y1DC, y2DC, minimum, maximum,
763 wdx (my d_x1WC), wdx (my d_x2WC), wdy (my d_y1WC), wdy (my d_y2WC), false
764 );
765 }
766 }
767
768 void Graphics_cellArray8 (Graphics me, constmatrixview<unsigned char> const& z,
769 double x1WC, double x2WC, double y1WC, double y2WC, unsigned char minimum, unsigned char maximum)
770 {
771 if (z.nrow < 1 || z.ncol < 1 || minimum == maximum)
772 return;
773 if (my recording) {
774 op (CELL_ARRAY8, 8 + z.nrow * z.ncol);
775 put (x1WC); put (x2WC); put (y1WC); put (y2WC);
776 put (minimum); put (maximum);
777 put (z.nrow); put (z.ncol);
778 for (integer irow = 1; irow <= z.nrow; irow ++)
779 for (integer icol = 1; icol <= z.ncol; icol ++)
780 put (z [irow] [icol]);
781 } else {
782 /* mutable delayed init */ integer x1DC, x2DC, y1DC, y2DC;
783 _autoSize (me, x1WC, x2WC, y1WC, y2WC, z.ncol, z.nrow, & x1DC, & x2DC, & y1DC, & y2DC);
784 _cellArrayOrImage (me, constMATVU(), constmatrixview<MelderColour>(), z,
785 1, z.ncol, x1DC, x2DC, 1, z.nrow, y1DC, y2DC, minimum, maximum,
786 wdx (my d_x1WC), wdx (my d_x2WC), wdy (my d_y1WC), wdy (my d_y2WC), false
787 );
788 }
789 }
790
791 void Graphics_image (Graphics me, constMATVU const& z,
792 double x1WC, double x2WC, double y1WC, double y2WC, double minimum, double maximum)
793 {
794 if (z.nrow < 1 || z.ncol < 1 || minimum == maximum)
795 return;
796 if (my recording) {
797 op (IMAGE, 8 + z.nrow * z.ncol);
798 put (x1WC); put (x2WC); put (y1WC); put (y2WC);
799 put (minimum); put (maximum);
800 put (z.nrow); put (z.ncol);
801 for (integer irow = 1; irow <= z.nrow; irow ++)
802 for (integer icol = 1; icol <= z.ncol; icol ++)
803 put (z [irow] [icol]);
804 } else {
805 /* mutable delayed init */ integer x1DC, x2DC, y1DC, y2DC;
806 _autoSize (me, x1WC, x2WC, y1WC, y2WC, z.ncol, z.nrow, & x1DC, & x2DC, & y1DC, & y2DC);
807 _cellArrayOrImage (me, z, constmatrixview<MelderColour>(), constmatrixview<unsigned char>(),
808 1, z.ncol, x1DC, x2DC, 1, z.nrow, y1DC, y2DC, minimum, maximum,
809 wdx (my d_x1WC), wdx (my d_x2WC), wdy (my d_y1WC), wdy (my d_y2WC), true
810 );
811 }
812 }
813
814 void Graphics_image_colour (Graphics me, constmatrixview <MelderColour> const& z,
815 double x1WC, double x2WC, double y1WC, double y2WC, double minimum, double maximum)
816 {
817 if (z.nrow < 1 || z.ncol < 1 || minimum == maximum)
818 return;
819 if (my recording) {
820 op (IMAGE_COLOUR, 8 + z.nrow * z.ncol * 4);
821 put (x1WC); put (x2WC); put (y1WC); put (y2WC);
822 put (minimum); put (maximum);
823 put (z.nrow); put (z.ncol);
824 for (integer irow = 1; irow <= z.nrow; irow ++) {
825 constvectorview <MelderColour> row = z [irow];
826 for (integer icol = 1; icol <= z.ncol; icol ++) {
827 put (row [icol]. red);
828 put (row [icol]. green);
829 put (row [icol]. blue);
830 put (row [icol]. transparency);
831 }
832 }
833 } else {
834 /* mutable delayed init */ integer x1DC, x2DC, y1DC, y2DC;
835 _autoSize (me, x1WC, x2WC, y1WC, y2WC, z.ncol, z.nrow, & x1DC, & x2DC, & y1DC, & y2DC);
836 _cellArrayOrImage (me, constMATVU(), z, constmatrixview<unsigned char>(),
837 1, z.ncol, x1DC, x2DC, 1, z.nrow, y1DC, y2DC, minimum, maximum,
838 wdx (my d_x1WC), wdx (my d_x2WC), wdy (my d_y1WC), wdy (my d_y2WC), true
839 );
840 }
841 }
842
843 void Graphics_image8 (Graphics me, constmatrixview <unsigned char> const& z,
844 double x1WC, double x2WC, double y1WC, double y2WC, uint8 minimum, uint8 maximum)
845 {
846 if (z.nrow < 1 || z.ncol < 1 || minimum == maximum)
847 return;
848 if (my recording) {
849 op (IMAGE8, 8 + z.nrow * z.ncol);
850 put (x1WC); put (x2WC); put (y1WC); put (y2WC);
851 put (minimum); put (maximum);
852 put (z.nrow); put (z.ncol);
853 for (integer irow = 1; irow <= z.nrow; irow ++)
854 for (integer icol = 1; icol <= z.ncol; icol ++)
855 put (z [irow] [icol]);
856 } else {
857 /* mutable delayed init */ integer x1DC, x2DC, y1DC, y2DC;
858 _autoSize (me, x1WC, x2WC, y1WC, y2WC, z.ncol, z.nrow, & x1DC, & x2DC, & y1DC, & y2DC);
859 _cellArrayOrImage (me, constMATVU(), constmatrixview<MelderColour>(), z,
860 1, z.ncol, x1DC, x2DC, 1, z.nrow, y1DC, y2DC, minimum, maximum,
861 wdx (my d_x1WC), wdx (my d_x2WC), wdy (my d_y1WC), wdy (my d_y2WC), true
862 );
863 }
864 }
865
866 static void _GraphicsScreen_imageFromFile (GraphicsScreen me, conststring32 relativeFileName, double x1, double x2, double y1, double y2) {
867 integer x1DC = wdx (x1), x2DC = wdx (x2), y1DC = wdy (y1), y2DC = wdy (y2);
868 integer width = x2DC - x1DC, height = my yIsZeroAtTheTop ? y1DC - y2DC : y2DC - y1DC;
869 #if 0
870 structMelderFile file { };
871 Melder_relativePathToFile (relativeFileName, & file);
872 try {
873 autoPhoto photo = Photo_readFromImageFile (& file);
874 if (x1 == x2 && y1 == y2) {
875 width = photo -> nx, x1DC -= width / 2, x2DC = x1DC + width;
876 height = photo -> ny, y2DC -= height / 2, y1DC = y2DC + height;
877 } else if (x1 == x2) {
878 width = height * (double) photo -> nx / (double) photo -> ny;
879 x1DC -= width / 2, x2DC = x1DC + width;
880 } else if (y1 == y2) {
881 height = width * (double) photo -> ny / (double) photo -> nx;
882 y2DC -= height / 2, y1DC = y2DC + height;
883 }
884 automatrix <MelderColour> z = newmatrixraw <MelderColour> (photo -> ny, photo -> nx);
885 for (integer iy = 1; iy <= photo -> ny; iy ++) {
886 for (integer ix = 1; ix <= photo -> nx; ix ++) {
887 z [iy] [ix]. red = photo -> d_red -> z [iy] [ix];
888 z [iy] [ix]. green = photo -> d_green -> z [iy] [ix];
889 z [iy] [ix]. blue = photo -> d_blue -> z [iy] [ix];
890 z [iy] [ix]. transparency = photo -> d_transparency -> z [iy] [ix];
891 }
892 }
893 _cellArrayOrImage (me, constMATVU(), z.get(), constmatrixview<unsigned char>(),
894 1, photo -> nx, x1DC, x2DC, 1, photo -> ny, y1DC, y2DC,
895 0.0, 1.0,
896 //wdx (my d_x1WC), wdx (my d_x2WC), wdy (my d_y1WC), wdy (my d_y2WC), // in case of clipping
897 LONG_MIN, LONG_MAX, LONG_MAX, LONG_MIN, // in case of no clipping
898 true
899 );
900 } catch (MelderError) {
901 Melder_clearError ();
902 }
903 #elif gdi
904 if (my d_useGdiplus) {
905 structMelderFile file { };
906 Melder_relativePathToFile (relativeFileName, & file);
907 Gdiplus::Bitmap image (MelderFile_peekPathW (& file));
908 if (x1 == x2 && y1 == y2) {
909 width = image. GetWidth (), x1DC -= width / 2, x2DC = x1DC + width;
910 height = image. GetHeight (), y2DC -= height / 2, y1DC = y2DC + height;
911 } else if (x1 == x2) {
912 width = height * (double) image. GetWidth () / (double) image. GetHeight ();
913 x1DC -= width / 2, x2DC = x1DC + width;
914 } else if (y1 == y2) {
915 height = width * (double) image. GetHeight () / (double) image. GetWidth ();
916 y2DC -= height / 2, y1DC = y2DC + height;
917 }
918 Gdiplus::Graphics dcplus (my d_gdiGraphicsContext);
919 Gdiplus::Rect rect (x1DC, y2DC, width, height);
920 dcplus. DrawImage (& image, rect);
921 } else {
922 }
923 #elif quartz
924 structMelderFile file { };
925 Melder_relativePathToFile (relativeFileName, & file);
926 CFURLRef url = CFURLCreateWithFileSystemPath (nullptr, (CFStringRef) MelderFile_peekPathCfstring (& file), kCFURLPOSIXPathStyle, false);
927 CGImageSourceRef imageSource = CGImageSourceCreateWithURL (url, nullptr);
928 CFRelease (url);
929 if (imageSource) {
930 CGImageRef image = CGImageSourceCreateImageAtIndex (imageSource, 0, nullptr);
931 CFRelease (imageSource);
932 if (image) {
933 if (x1 == x2 && y1 == y2) {
934 width = CGImageGetWidth (image), x1DC -= width / 2, x2DC = x1DC + width;
935 height = CGImageGetHeight (image), y2DC -= height / 2, y1DC = y2DC + height;
936 } else if (x1 == x2) {
937 width = height * (double) CGImageGetWidth (image) / (double) CGImageGetHeight (image);
938 x1DC -= width / 2, x2DC = x1DC + width;
939 } else if (y1 == y2) {
940 height = width * (double) CGImageGetHeight (image) / (double) CGImageGetWidth (image);
941 y2DC -= height / 2, y1DC = y2DC + height;
942 }
943 CGContextSaveGState (my d_macGraphicsContext);
944
945 //NSCAssert(my d_macGraphicsContext, @"nil context");
946
947 CGContextTranslateCTM (my d_macGraphicsContext, 0, y1DC);
948 CGContextScaleCTM (my d_macGraphicsContext, 1.0, -1.0);
949 CGContextDrawImage (my d_macGraphicsContext, CGRectMake (x1DC, 0, width, height), image);
950 CGContextRestoreGState (my d_macGraphicsContext);
951 CGImageRelease (image);
952 }
953 }
954 #endif
955 }
956
957 void Graphics_imageFromFile (Graphics me, conststring32 relativeFileName, double x1, double x2, double y1, double y2) {
958 if (my recording) {
959 conststring8 txt_utf8 = Melder_peek32to8 (relativeFileName);
960 int length = strlen (txt_utf8) / sizeof (double) + 1; // TODO: integer overflow
961 op (IMAGE_FROM_FILE, 5 + length); put (x1); put (x2); put (y1); put (y2); sput (txt_utf8, length)
962 } else if (my screen) {
963 _GraphicsScreen_imageFromFile (static_cast <GraphicsScreen> (me), relativeFileName, x1, x2, y1, y2);
964 }
965 }
966
967 void Graphics_imageFromFile_embedded (Graphics me, conststring32 relativeFileName, double x1, double x2, double y1, double y2) {
968 if (Melder_endsWith_caseAware (relativeFileName, U".pdf"))
969 Melder_throw (U"Cannot embed PDF pictures. Use “Insert picture from file...” instead.");
970 structMelderFile file { };
971 Melder_relativePathToFile (relativeFileName, & file);
972 try {
973 autoPhoto photo = Photo_readFromImageFile (& file);
974 automatrix <MelderColour> z = newmatrixraw <MelderColour> (photo -> ny, photo -> nx);
975 for (integer iy = 1; iy <= photo -> ny; iy ++) {
976 for (integer ix = 1; ix <= photo -> nx; ix ++) {
977 z [iy] [ix]. red = photo -> d_red -> z [iy] [ix];
978 z [iy] [ix]. green = photo -> d_green -> z [iy] [ix];
979 z [iy] [ix]. blue = photo -> d_blue -> z [iy] [ix];
980 z [iy] [ix]. transparency = photo -> d_transparency -> z [iy] [ix];
981 }
982 }
983 Graphics_image_colour (me, z.get(), x1, x2, y1, y2, 0.0, 1.0); // this does clipping to the canvas
984 } catch (MelderError) {
985 Melder_clearError ();
986 }
987 }
988
989 /* End of file Graphics_image.cpp */