Debian Codegraph @

fon/Photo.cpp

raw symbols SCIP
1 /* Photo.cpp
2 *
3 * Copyright (C) 2013-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 "Photo.h"cxx . . $ `<file>/fon/Photo.h`/
20 #include "NUM2.h"cxx . . $ `<file>/dwsys/NUM2.h`/
21 #include "Formula.h"cxx . . $ `<file>/sys/Formula.h`/
22 #if defined (_WIN32)
23 #include <GraphicsP.h>
24 #elif defined (macintosh)
25 #include "macport_on.h"
26 #include <Cocoa/Cocoa.h>
27 #include "macport_off.h"
28 #elif defined (linuxcxx . . $ `<built-in>:480:9`!) && ! defined (NO_GRAPHICS)
29 #include <cairo/cairo.h>
30 #endif
31
32 #include "oo_DESTROY.h"cxx . . $ `<file>/sys/oo_DESTROY.h`/
33 #include "Photo_def.h"cxx . . $ `<file>/fon/Photo_def.h`/
34 #include "oo_COPY.h"cxx . . $ `<file>/sys/oo_COPY.h`/
35 #include "Photo_def.h"cxx . . $ `<file>/fon/Photo_def.h`/
36 #include "oo_EQUAL.h"cxx . . $ `<file>/sys/oo_EQUAL.h`/
37 #include "Photo_def.h"cxx . . $ `<file>/fon/Photo_def.h`/
38 #include "oo_CAN_WRITE_AS_ENCODING.h"cxx . . $ `<file>/sys/oo_CAN_WRITE_AS_ENCODING.h`/
39 #include "Photo_def.h"cxx . . $ `<file>/fon/Photo_def.h`/
40 #include "oo_WRITE_TEXT.h"cxx . . $ `<file>/sys/oo_WRITE_TEXT.h`/
41 #include "Photo_def.h"cxx . . $ `<file>/fon/Photo_def.h`/
42 #include "oo_READ_TEXT.h"cxx . . $ `<file>/sys/oo_READ_TEXT.h`/
43 #include "Photo_def.h"cxx . . $ `<file>/fon/Photo_def.h`/
44 #include "oo_WRITE_BINARY.h"cxx . . $ `<file>/sys/oo_WRITE_BINARY.h`/
45 #include "Photo_def.h"cxx . . $ `<file>/fon/Photo_def.h`/
46 #include "oo_READ_BINARY.h"cxx . . $ `<file>/sys/oo_READ_BINARY.h`/
47 #include "Photo_def.h"cxx . . $ `<file>/fon/Photo_def.h`/
48 #include "oo_DESCRIPTION.h"cxx . . $ `<file>/sys/oo_DESCRIPTION.h`/
49 #include "Photo_def.h"cxx . . $ `<file>/fon/Photo_def.h`/
50
51 Thing_implementcxx . . $ `sys/Thing.h:65:9`! (Photo, SampledXY, 0);
52
53 void structPhoto :: v1_info () {
54 ourcxx . . $ `melder/melder.h:42:9`! structDaatacxx . . $ structDaata# :: v1_infocxx . . $ structThing#v1_info(49f6e7a06ebc5aa8). ();
55 MelderInfo_writeLinecxx . . $ MelderInfo_writeLine(3eb398314feba58b). (U"xmin: ", ourcxx . . $ `melder/melder.h:42:9`! xmincxx . . $ structFunction#xmin.);
56 MelderInfo_writeLinecxx . . $ MelderInfo_writeLine(3eb398314feba58b). (U"xmax: ", ourcxx . . $ `melder/melder.h:42:9`! xmaxcxx . . $ structFunction#xmax.);
57 MelderInfo_writeLinecxx . . $ MelderInfo_writeLine(3eb398314feba58b). (U"Number of columns: ", ourcxx . . $ `melder/melder.h:42:9`! nxcxx . . $ structSampled#nx.);
58 MelderInfo_writeLinecxx . . $ MelderInfo_writeLine(3eb398314feba58b). (U"dx: ", ourcxx . . $ `melder/melder.h:42:9`! dxcxx . . $ structSampled#dx., U" (-> sampling rate ", 1.0 / ourcxx . . $ `melder/melder.h:42:9`! dxcxx . . $ structSampled#dx., U" )");
59 MelderInfo_writeLinecxx . . $ MelderInfo_writeLine(3eb398314feba58b). (U"x1: ", ourcxx . . $ `melder/melder.h:42:9`! x1cxx . . $ structSampled#x1.);
60 MelderInfo_writeLinecxx . . $ MelderInfo_writeLine(3eb398314feba58b). (U"ymin: ", ourcxx . . $ `melder/melder.h:42:9`! ymincxx . . $ structSampledXY#ymin.);
61 MelderInfo_writeLinecxx . . $ MelderInfo_writeLine(3eb398314feba58b). (U"ymax: ", ourcxx . . $ `melder/melder.h:42:9`! ymaxcxx . . $ structSampledXY#ymax.);
62 MelderInfo_writeLinecxx . . $ MelderInfo_writeLine(3eb398314feba58b). (U"Number of rows: ", ourcxx . . $ `melder/melder.h:42:9`! nycxx . . $ structSampledXY#ny.);
63 MelderInfo_writeLinecxx . . $ MelderInfo_writeLine(3eb398314feba58b). (U"dy: ", ourcxx . . $ `melder/melder.h:42:9`! dycxx . . $ structSampledXY#dy., U" (-> sampling rate ", 1.0 / ourcxx . . $ `melder/melder.h:42:9`! dycxx . . $ structSampledXY#dy., U" )");
64 MelderInfo_writeLinecxx . . $ MelderInfo_writeLine(3eb398314feba58b). (U"y1: ", ourcxx . . $ `melder/melder.h:42:9`! y1cxx . . $ structSampledXY#y1.);
65 }
66
67 void Photo_initcxx . . $ Photo_init(ee2d916835cc0b80). (Photo me,
68 double xmin, double xmax, integercxx . . $ integer# nx, double dx, double x1,
69 double ymin, double ymax, integercxx . . $ integer# ny, double dy, double y1)
70 {
71 SampledXY_initcxx . . $ SampledXY_init(6070c73102fabd7). (me, xmin, xmax, nx, dx, x1, ymin, ymax, ny, dy, y1);
72 mycxx . . $ `melder/melder.h:44:9`! d_red =cxx . . $ autoSomeThing#`operator=`(80cc59814f7161e8). Matrix_createcxx . . $ Matrix_create(494a3ae9ff7e3b60). (xmin, xmax, nx, dx, x1, ymin, ymax, ny, dy, y1);
73 mycxx . . $ `melder/melder.h:44:9`! d_green =cxx . . $ autoSomeThing#`operator=`(80cc59814f7161e8). Matrix_createcxx . . $ Matrix_create(494a3ae9ff7e3b60). (xmin, xmax, nx, dx, x1, ymin, ymax, ny, dy, y1);
74 mycxx . . $ `melder/melder.h:44:9`! d_blue =cxx . . $ autoSomeThing#`operator=`(80cc59814f7161e8). Matrix_createcxx . . $ Matrix_create(494a3ae9ff7e3b60). (xmin, xmax, nx, dx, x1, ymin, ymax, ny, dy, y1);
75 mycxx . . $ `melder/melder.h:44:9`! d_transparency =cxx . . $ autoSomeThing#`operator=`(80cc59814f7161e8). Matrix_createcxx . . $ Matrix_create(494a3ae9ff7e3b60). (xmin, xmax, nx, dx, x1, ymin, ymax, ny, dy, y1);
76 }
77
78 autoPhoto Photo_createcxx . . $ Photo_create(9cc57878c61d2f55).
79 (double xmin, double xmax, integercxx . . $ integer# nx, double dx, double x1,
80 double ymin, double ymax, integercxx . . $ integer# ny, double dy, double y1)
81 {
82 try {
83 autoPhoto me = Thing_newcxx . . $ `sys/Thing.h:514:9`! (Photo);
84 Photo_initcxx . . $ Photo_init(ee2d916835cc0b80). (me.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), xmin, xmax, nx, dx, x1, ymin, ymax, ny, dy, y1);
85 return mecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).;
86 } catch (MelderErrorcxx . . $ MelderError#) {
87 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Photo object not created.");
88 }
89 }
90
91 autoPhoto Photo_createSimplecxx . . $ Photo_createSimple(5ba437357d2f75b3). (integercxx . . $ integer# numberOfRows, integercxx . . $ integer# numberOfColumns) {
92 try {
93 autoPhoto me = Thing_newcxx . . $ `sys/Thing.h:514:9`! (Photo);
94 Photo_initcxx . . $ Photo_init(ee2d916835cc0b80). (me.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), 0.5, numberOfColumns + 0.5, numberOfColumns, 1, 1,
95 0.5, numberOfRows + 0.5, numberOfRows, 1, 1);
96 return mecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).;
97 } catch (MelderErrorcxx . . $ MelderError#) {
98 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Photo object not created.");
99 }
100 }
101
102 autoPhoto Photo_readFromImageFilecxx . . $ Photo_readFromImageFile(4beee1b83b67140c). (MelderFilecxx . . $ MelderFile# file) {
103 try {
104 #if defined (linuxcxx . . $ `<built-in>:480:9`!) && ! defined (NO_GRAPHICS)
105 cairo_surface_t *surface = cairo_image_surface_create_from_png (MelderFile_peekPath8cxx . . $ MelderFile_peekPath8(3a3888509ce82e67). (file));
106 //if (cairo_surface_status)
107 // Melder_throw (U"Error opening PNG file.");
108 integercxx . . $ integer# width = cairo_image_surface_get_width (surface);
109 integercxx . . $ integer# height = cairo_image_surface_get_height (surface);
110 if (width == 0 || height == 0) {
111 cairo_surface_destroy (surface);
112 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Error reading PNG file.");
113 }
114 uint8cxx . . $ uint8# *imageData = cairo_image_surface_get_data (surface);
115 integercxx . . $ integer# bytesPerRow = cairo_image_surface_get_stride (surface);
116 cairo_format_t format = cairo_image_surface_get_format (surface);
117 autoPhoto me = Photo_createSimplecxx . . $ Photo_createSimple(5ba437357d2f75b3). (height, width);
118 if (format == CAIRO_FORMAT_ARGB32) {
119 for (integercxx . . $ integer# irow = 1; irow <= height; irow ++) {
120 uint8cxx . . $ uint8# *rowAddress = imageData + bytesPerRow * (height - irow);
121 for (integercxx . . $ integer# icol = 1; icol <= width; icol ++) {
122 mycxx . . $ `melder/melder.h:44:9`! d_blue ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).irow] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).icol] = (* rowAddress ++) / 255.0;
123 mycxx . . $ `melder/melder.h:44:9`! d_green ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).irow] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).icol] = (* rowAddress ++) / 255.0;
124 mycxx . . $ `melder/melder.h:44:9`! d_red ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).irow] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).icol] = (* rowAddress ++) / 255.0;
125 mycxx . . $ `melder/melder.h:44:9`! d_transparency ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).irow] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).icol] = 1.0 - (* rowAddress ++) / 255.0;
126 }
127 }
128 } else if (format == CAIRO_FORMAT_RGB24) {
129 for (integercxx . . $ integer# irow = 1; irow <= height; irow ++) {
130 uint8cxx . . $ uint8# *rowAddress = imageData + bytesPerRow * (height - irow);
131 for (integercxx . . $ integer# icol = 1; icol <= width; icol ++) {
132 mycxx . . $ `melder/melder.h:44:9`! d_blue ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).irow] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).icol] = (* rowAddress ++) / 255.0;
133 mycxx . . $ `melder/melder.h:44:9`! d_green ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).irow] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).icol] = (* rowAddress ++) / 255.0;
134 mycxx . . $ `melder/melder.h:44:9`! d_red ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).irow] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).icol] = (* rowAddress ++) / 255.0;
135 mycxx . . $ `melder/melder.h:44:9`! d_transparency ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).irow] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).icol] = 0.0; rowAddress ++;
136 }
137 }
138 } else {
139 cairo_surface_destroy (surface);
140 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Unsupported PNG format ", format, U".");
141 }
142 cairo_surface_destroy (surface);
143 return mecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).;
144 #elif defined (_WIN32)
145 Gdiplus::Bitmap gdiplusBitmap (MelderFile_peekPathW (file));
146 integer width = gdiplusBitmap. GetWidth ();
147 integer height = gdiplusBitmap. GetHeight ();
148 if (width == 0 || height == 0)
149 Melder_throw (U"Error reading PNG file.");
150 autoPhoto me = Photo_createSimple (height, width);
151 for (integer irow = 1; irow <= height; irow ++) {
152 for (integer icol = 1; icol <= width; icol ++) {
153 Gdiplus::Color gdiplusColour;
154 gdiplusBitmap. GetPixel (icol - 1, height - irow, & gdiplusColour);
155 my d_red -> z [irow] [icol] = (gdiplusColour. GetRed ()) / 255.0;
156 my d_green -> z [irow] [icol] = (gdiplusColour. GetGreen ()) / 255.0;
157 my d_blue -> z [irow] [icol] = (gdiplusColour. GetBlue ()) / 255.0;
158 my d_transparency -> z [irow] [icol] = 1.0 - (gdiplusColour. GetAlpha ()) / 255.0;
159 }
160 }
161 return me;
162 #elif defined (macintosh)
163 autoPhoto me;
164 CFURLRef url = CFURLCreateWithFileSystemPath (nullptr, (CFStringRef) MelderFile_peekPathCfstring (file), kCFURLPOSIXPathStyle, false);
165 CGImageSourceRef imageSource = CGImageSourceCreateWithURL (url, nullptr);
166 CFRelease (url);
167 if (! imageSource)
168 Melder_throw (U"Cannot open picture file ", file, U".");
169 CGImageRef image = CGImageSourceCreateImageAtIndex (imageSource, 0, nullptr);
170 CFRelease (imageSource);
171 if (image) {
172 const integer width = uinteger_to_integer_a (CGImageGetWidth (image));
173 const integer height = uinteger_to_integer_a (CGImageGetHeight (image));
174 me = Photo_createSimple (height, width);
175 const integer bitsPerPixel = uinteger_to_integer_a (CGImageGetBitsPerPixel (image));
176 const integer bitsPerComponent = uinteger_to_integer_a (CGImageGetBitsPerComponent (image));
177 const integer bytesPerRow = uinteger_to_integer_a (CGImageGetBytesPerRow (image));
178 //TRACE
179 trace (
180 bitsPerPixel, U" bits per pixel, ",
181 bitsPerComponent, U" bits per component, ",
182 bytesPerRow, U" bytes per row"
183 );
184 /*
185 Now we probably need to use:
186 CGColorSpaceRef CGImageGetColorSpace (CGImageRef image);
187 CGImageAlphaInfo CGImageGetAlphaInfo (CGImageRef image);
188 */
189 CGDataProviderRef dataProvider = CGImageGetDataProvider (image); // not retained, so don't release
190 CFDataRef data = CGDataProviderCopyData (dataProvider);
191 uint8 *pixelData = (uint8_t *) CFDataGetBytePtr (data);
192 if (bitsPerPixel == 4 * bitsPerComponent) {
193 for (integer irow = 1; irow <= height; irow ++) {
194 uint8 *rowAddress = pixelData + bytesPerRow * (height - irow);
195 for (integer icol = 1; icol <= width; icol ++) {
196 my d_red -> z [irow] [icol] = (*rowAddress ++) / 255.0;
197 my d_green -> z [irow] [icol] = (*rowAddress ++) / 255.0;
198 my d_blue -> z [irow] [icol] = (*rowAddress ++) / 255.0;
199 my d_transparency -> z [irow] [icol] = 1.0 - (*rowAddress ++) / 255.0;
200 }
201 }
202 } else if (bitsPerPixel == bitsPerComponent) {
203 for (integer irow = 1; irow <= height; irow ++) {
204 uint8 *rowAddress = pixelData + bytesPerRow * (height - irow);
205 for (integer icol = 1; icol <= width; icol ++) {
206 my d_red -> z [irow] [icol] =
207 my d_green -> z [irow] [icol] =
208 my d_blue -> z [irow] [icol] = 1.0 - (*rowAddress ++) / 255.0;
209 my d_transparency -> z [irow] [icol] = 0.0;
210 }
211 }
212 } else {
213 Melder_throw (U"Unknown colour format in picture file.");
214 }
215 CFRelease (data);
216 CGImageRelease (image);
217 }
218 return me;
219 #else
220 Melder_throw (U"Photo_readFromImageFile not implemented.");
221 #endif
222 } catch (MelderErrorcxx . . $ MelderError#) {
223 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Picture file ", file, U" not opened as Photo.");
224 }
225 }
226
227 #if defined (macintosh)
228 #include <time.h>
229 #include "macport_on.h"
230 static void _mac_releaseDataCallback (void * /* info */, const void *data, size_t /* size */) {
231 Melder_free (data);
232 }
233 #endif
234
235 #if defined (linuxcxx . . $ `<built-in>:480:9`!) && ! defined (NO_GRAPHICS)
236 static void _lin_saveAsImageFilecxx . . $ _lin_saveAsImageFile(43fdd1b4a52a86dc). (Photo me, MelderFilecxx . . $ MelderFile# file, conststring32cxx . . $ conststring32# which) {
237 cairo_format_t format = CAIRO_FORMAT_ARGB32;
238 integercxx . . $ integer# bytesPerRow = cairo_format_stride_for_width (format, mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.); // likely to be my nx * 4
239 integercxx . . $ integer# numberOfRows = mycxx . . $ `melder/melder.h:44:9`! nycxx . . $ structSampledXY#ny.;
240 uint8cxx . . $ uint8# *imageData = Melder_malloc_fcxx . . $ `melder/melder_alloc.h:30:9`! (uint8, bytesPerRow * numberOfRows);
241 for (integercxx . . $ integer# irow = 1; irow <= mycxx . . $ `melder/melder.h:44:9`! nycxx . . $ structSampledXY#ny.; irow ++) {
242 uint8cxx . . $ uint8# *rowAddress = imageData + bytesPerRow * (mycxx . . $ `melder/melder.h:44:9`! nycxx . . $ structSampledXY#ny. - irow);
243 for (integercxx . . $ integer# icol = 1; icol <= mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.; icol ++) {
244 * rowAddress ++ = Melder_iroundcxx . . $ `melder/melder_ftoi.h:78:9`! (mycxx . . $ `melder/melder.h:44:9`! d_blue ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).irow] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).icol] * 255.0);
245 * rowAddress ++ = Melder_iroundcxx . . $ `melder/melder_ftoi.h:78:9`! (mycxx . . $ `melder/melder.h:44:9`! d_green ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).irow] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).icol] * 255.0);
246 * rowAddress ++ = Melder_iroundcxx . . $ `melder/melder_ftoi.h:78:9`! (mycxx . . $ `melder/melder.h:44:9`! d_red ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).irow] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).icol] * 255.0);
247 * rowAddress ++ = 255 - Melder_iroundcxx . . $ `melder/melder_ftoi.h:78:9`! (mycxx . . $ `melder/melder.h:44:9`! d_transparency ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).irow] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).icol] * 255.0);
248 }
249 }
250 cairo_surface_t *surface = cairo_image_surface_create_for_data (imageData,
251 format, mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx., mycxx . . $ `melder/melder.h:44:9`! nycxx . . $ structSampledXY#ny., bytesPerRow);
252 cairo_surface_write_to_png (surface, MelderFile_peekPath8cxx . . $ MelderFile_peekPath8(3a3888509ce82e67). (file));
253 cairo_surface_destroy (surface);
254 }
255 #endif
256
257 #ifdef _WIN32
258 static void _win_saveAsImageFile (Photo me, MelderFile file, conststring32 mimeType) {
259 Gdiplus::Bitmap gdiplusBitmap (my nx, my ny, PixelFormat32bppARGB);
260 for (integer irow = 1; irow <= my ny; irow ++) {
261 for (integer icol = 1; icol <= my nx; icol ++) {
262 Gdiplus::Color gdiplusColour (
263 255 - Melder_iround (my d_transparency -> z [irow] [icol] * 255.0),
264 Melder_iround (my d_red -> z [irow] [icol] * 255.0),
265 Melder_iround (my d_green -> z [irow] [icol] * 255.0),
266 Melder_iround (my d_blue -> z [irow] [icol] * 255.0));
267 gdiplusBitmap. SetPixel (icol - 1, my ny - irow, gdiplusColour);
268 }
269 }
270 /*
271 * The 'mimeType' parameter specifies a "class encoder". Look it up.
272 */
273 UINT numberOfImageEncoders, sizeOfImageEncoderArray;
274 Gdiplus::GetImageEncodersSize (& numberOfImageEncoders, & sizeOfImageEncoderArray);
275 if (sizeOfImageEncoderArray == 0)
276 Melder_throw (U"Cannot find image encoders.");
277 Gdiplus::ImageCodecInfo *imageEncoderInfos = Melder_malloc (Gdiplus::ImageCodecInfo, sizeOfImageEncoderArray);
278 Gdiplus::GetImageEncoders (numberOfImageEncoders, sizeOfImageEncoderArray, imageEncoderInfos);
279 for (int iencoder = 0; iencoder < numberOfImageEncoders; iencoder ++) {
280 trace (U"Supported MIME type: ", Melder_peekWto32 (imageEncoderInfos [iencoder]. MimeType));
281 if (str32equ (Melder_peekWto32 (imageEncoderInfos [iencoder]. MimeType), mimeType)) {
282 Gdiplus::EncoderParameters *p = nullptr;
283 Gdiplus::EncoderParameters encoderParameters;
284 if (str32equ (mimeType, U"image/jpeg")) {
285 encoderParameters. Count = 1;
286 GUID guid = { 0x1D5B'E4B5, 0xFA4A, 0x452D, { 0x9C, 0xDD, 0x5D, 0xB3, 0x51, 0x05, 0xE7, 0xEB }}; // EncoderQuality
287 encoderParameters. Parameter [0]. Guid = guid;
288 encoderParameters. Parameter [0]. Type = Gdiplus::EncoderParameterValueTypeLong;
289 encoderParameters. Parameter [0]. NumberOfValues = 1;
290 ULONG quality = 100;
291 encoderParameters. Parameter [0]. Value = & quality;
292 p = & encoderParameters;
293 }
294 gdiplusBitmap. Save (MelderFile_peekPathW (file),
295 & imageEncoderInfos [iencoder]. Clsid, p);
296 Melder_free (imageEncoderInfos);
297 return;
298 }
299 }
300 Melder_throw (U"Unknown MIME type ", mimeType, U".");
301 }
302 #endif
303
304 #ifdef macintosh
305 static void _mac_saveAsImageFile (Photo me, MelderFile file, const void *which) {
306 const integer bytesPerRow = my nx * 4;
307 const integer numberOfRows = my ny;
308 unsigned char *const imageData = Melder_malloc_f (unsigned char, bytesPerRow * numberOfRows);
309 for (integer irow = 1; irow <= my ny; irow ++) {
310 uint8 *rowAddress = imageData + bytesPerRow * (my ny - irow);
311 for (integer icol = 1; icol <= my nx; icol ++) {
312 * rowAddress ++ = (uint8) Melder_iround (my d_red -> z [irow] [icol] * 255.0); // BUG: should be tested for speed
313 * rowAddress ++ = (uint8) Melder_iround (my d_green -> z [irow] [icol] * 255.0);
314 * rowAddress ++ = (uint8) Melder_iround (my d_blue -> z [irow] [icol] * 255.0);
315 * rowAddress ++ = 255 - (uint8) Melder_iround (my d_transparency -> z [irow] [icol] * 255.0);
316 }
317 }
318 static CGColorSpaceRef colourSpace = nullptr;
319 if (! colourSpace) {
320 colourSpace = CGColorSpaceCreateWithName (kCGColorSpaceGenericRGB); // used to be kCGColorSpaceUserRGB
321 Melder_assert (colourSpace);
322 }
323 CGDataProviderRef dataProvider = CGDataProviderCreateWithData (nullptr,
324 imageData,
325 integer_to_uinteger_a (bytesPerRow * numberOfRows),
326 _mac_releaseDataCallback // needed?
327 );
328 Melder_assert (dataProvider);
329 CGImageRef image = CGImageCreate (integer_to_uinteger_a (my nx), integer_to_uinteger_a (numberOfRows),
330 8, 32, integer_to_uinteger_a (bytesPerRow), colourSpace, kCGImageAlphaLast, dataProvider, nullptr, false, kCGRenderingIntentDefault);
331 CGDataProviderRelease (dataProvider);
332 Melder_assert (image);
333 NSString *path = (NSString *) MelderFile_peekPathCfstring (file);
334 CFURLRef url = (CFURLRef) [NSURL fileURLWithPath: path isDirectory: NO];
335 CGImageDestinationRef destination = CGImageDestinationCreateWithURL (url, (CFStringRef) which, 1, nullptr);
336 CGImageDestinationAddImage (destination, image, nil);
337
338 if (! CGImageDestinationFinalize (destination)) {
339 //Melder_throw;
340 }
341
342 CFRelease (destination);
343 CGColorSpaceRelease (colourSpace);
344 CGImageRelease (image);
345 }
346 #endif
347
348 void Photo_saveAsPNGcxx . . $ Photo_saveAsPNG(c5be2ee79548ebb0). (Photo me, MelderFilecxx . . $ MelderFile# file) {
349 #if defined (_WIN32)
350 _win_saveAsImageFile (me, file, U"image/png");
351 #elif defined (macintosh)
352 _mac_saveAsImageFile (me, file, kUTTypePNG);
353 #elif defined (linuxcxx . . $ `<built-in>:480:9`!) && ! defined (NO_GRAPHICS)
354 _lin_saveAsImageFilecxx . . $ _lin_saveAsImageFile(43fdd1b4a52a86dc). (me, file, U"image/png");
355 #endif
356 }
357
358 void Photo_saveAsTIFFcxx . . $ Photo_saveAsTIFF(c5be2ee79548ebb0). (Photo me, MelderFilecxx . . $ MelderFile# file) {
359 #if defined (_WIN32)
360 _win_saveAsImageFile (me, file, U"image/tiff");
361 #elif defined (macintosh)
362 _mac_saveAsImageFile (me, file, kUTTypeTIFF);
363 #else
364 (void) me;
365 (void) file;
366 #endif
367 }
368
369 void Photo_saveAsGIFcxx . . $ Photo_saveAsGIF(c5be2ee79548ebb0). (Photo me, MelderFilecxx . . $ MelderFile# file) {
370 #if defined (_WIN32)
371 _win_saveAsImageFile (me, file, U"image/gif");
372 #elif defined (macintosh)
373 _mac_saveAsImageFile (me, file, kUTTypeGIF);
374 #else
375 (void) me;
376 (void) file;
377 #endif
378 }
379
380 void Photo_saveAsWindowsBitmapFilecxx . . $ Photo_saveAsWindowsBitmapFile(c5be2ee79548ebb0). (Photo me, MelderFilecxx . . $ MelderFile# file) {
381 #if defined (_WIN32)
382 _win_saveAsImageFile (me, file, U"image/bmp");
383 #elif defined (macintosh)
384 _mac_saveAsImageFile (me, file, kUTTypeBMP);
385 #else
386 (void) me;
387 (void) file;
388 #endif
389 }
390
391 void Photo_saveAsJPEGcxx . . $ Photo_saveAsJPEG(c5be2ee79548ebb0). (Photo me, MelderFilecxx . . $ MelderFile# file) {
392 #if defined (_WIN32)
393 _win_saveAsImageFile (me, file, U"image/jpeg");
394 #elif defined (macintosh)
395 _mac_saveAsImageFile (me, file, kUTTypeJPEG);
396 #else
397 (void) me;
398 (void) file;
399 #endif
400 }
401
402 void Photo_saveAsJPEG2000cxx . . $ Photo_saveAsJPEG2000(c5be2ee79548ebb0). (Photo me, MelderFilecxx . . $ MelderFile# file) {
403 #if defined (_WIN32)
404 _win_saveAsImageFile (me, file, U"image/jpeg2000");
405 #elif defined (macintosh)
406 _mac_saveAsImageFile (me, file, kUTTypeJPEG2000);
407 #else
408 (void) me;
409 (void) file;
410 #endif
411 }
412
413 void Photo_saveAsAppleIconFilecxx . . $ Photo_saveAsAppleIconFile(c5be2ee79548ebb0). (Photo me, MelderFilecxx . . $ MelderFile# file) {
414 #if defined (_WIN32)
415 _win_saveAsImageFile (me, file, U"image/ICNS");
416 #elif defined (macintosh)
417 _mac_saveAsImageFile (me, file, kUTTypeAppleICNS);
418 #else
419 (void) me;
420 (void) file;
421 #endif
422 }
423
424 void Photo_saveAsWindowsIconFilecxx . . $ Photo_saveAsWindowsIconFile(c5be2ee79548ebb0). (Photo me, MelderFilecxx . . $ MelderFile# file) {
425 #if defined (_WIN32)
426 _win_saveAsImageFile (me, file, U"image/icon");
427 #elif defined (macintosh)
428 _mac_saveAsImageFile (me, file, kUTTypeICO);
429 #else
430 (void) me;
431 (void) file;
432 #endif
433 }
434
435 void Photo_replaceRedcxx . . $ Photo_replaceRed(4c38104ca1184f4d). (Photo me, Matrixcxx . . $ Matrix# red) {
436 mycxx . . $ `melder/melder.h:44:9`! d_red =cxx . . $ autoSomeThing#`operator=`(80cc59814f7161e8). Data_copycxx . . $ Data_copy(97a542a26794c80e). (red);
437 }
438
439 void Photo_replaceGreencxx . . $ Photo_replaceGreen(4c38104ca1184f4d). (Photo me, Matrixcxx . . $ Matrix# green) {
440 mycxx . . $ `melder/melder.h:44:9`! d_green =cxx . . $ autoSomeThing#`operator=`(80cc59814f7161e8). Data_copycxx . . $ Data_copy(97a542a26794c80e). (green);
441 }
442
443 void Photo_replaceBluecxx . . $ Photo_replaceBlue(4c38104ca1184f4d). (Photo me, Matrixcxx . . $ Matrix# blue) {
444 mycxx . . $ `melder/melder.h:44:9`! d_blue =cxx . . $ autoSomeThing#`operator=`(80cc59814f7161e8). Data_copycxx . . $ Data_copy(97a542a26794c80e). (blue);
445 }
446
447 void Photo_replaceTransparencycxx . . $ Photo_replaceTransparency(4c38104ca1184f4d). (Photo me, Matrixcxx . . $ Matrix# transparency) {
448 mycxx . . $ `melder/melder.h:44:9`! d_transparency =cxx . . $ autoSomeThing#`operator=`(80cc59814f7161e8). Data_copycxx . . $ Data_copy(97a542a26794c80e). (transparency);
449 }
450
451 static void _Photo_cellArrayOrImagecxx . . $ _Photo_cellArrayOrImage(ecff009a056dcf56). (Photo me, Graphicscxx . . $ Graphics# g, double xmin, double xmax, double ymin, double ymax, bool interpolate) {
452 Function_unidirectionalAutowindowcxx . . $ Function_unidirectionalAutowindow(88b7840c59a57eaf). (me, & xmin, & xmax);
453 SampledXY_unidirectionalAutowindowYcxx . . $ SampledXY_unidirectionalAutowindowY(b6a531eed30bf40e). (me, & ymin, & ymax);
454 integercxx . . $ integer# ixmin, ixmax, iymin, iymax;
455 Sampled_getWindowSamplescxx . . $ Sampled_getWindowSamples(fe99094ed5bb3f6a). (me, xmin - 0.49999 * mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx., xmax + 0.49999 * mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx., & ixmin, & ixmax);
456 SampledXY_getWindowSamplesYcxx . . $ SampledXY_getWindowSamplesY(4302ed892b02b0fc). (me, ymin - 0.49999 * mycxx . . $ `melder/melder.h:44:9`! dycxx . . $ structSampledXY#dy., ymax + 0.49999 * mycxx . . $ `melder/melder.h:44:9`! dycxx . . $ structSampledXY#dy., & iymin, & iymax);
457 if (ixmin > ixmax || iymin > iymax) {
458 Melder_crashcxx . . $ `melder/melder_error.h:98:9`! (U"ixmin ", ixmin, U" ixmax ", ixmax, U" iymin ", iymin, U" iymax ", iymax);
459 return;
460 }
461 if (xmin >= xmax || ymin >= ymax)
462 return;
463 Graphics_setWindowcxx . . $ Graphics_setWindow(72bdb7937dbb13da). (g, xmin, xmax, ymin, ymax);
464 automatrix z = newmatrixrawcxx . . $ newmatrixraw(902b25f6b635a55a). <MelderColourcxx . . $ MelderColour#> (iymax - (iymin - 1), ixmax - (ixmin - 1));
465 for (integercxx . . $ integer# iy = iymin; iy <= iymax; iy ++) {
466 for (integercxx . . $ integer# ix = ixmin; ix <= ixmax; ix ++) {
467 MelderColourcxx . . $ MelderColour#& cell = z [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).iy - (iymin - 1)] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).ix - (ixmin - 1)];
468 cell. redcxx . . $ MelderColour#red. = mycxx . . $ `melder/melder.h:44:9`! d_red ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).iy] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).ix];
469 cell. greencxx . . $ MelderColour#green. = mycxx . . $ `melder/melder.h:44:9`! d_green ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).iy] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).ix];
470 cell. bluecxx . . $ MelderColour#blue. = mycxx . . $ `melder/melder.h:44:9`! d_blue ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).iy] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).ix];
471 cell. transparencycxx . . $ MelderColour#transparency. = mycxx . . $ `melder/melder.h:44:9`! d_transparency ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).iy] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).ix];
472 }
473 }
474 if (interpolate)
475 Graphics_image_colourcxx . . $ Graphics_image_colour(5885e50e053285eb). (g, zcxx . . $ constmatrixview#constmatrixview(c98a2930b9469e58)..allcxx . . $ automatrix#all(2114117ba8ed4224).(),
476 Sampled_indexToXcxx . . $ Sampled_indexToX(65bcd2c9acbb3dd5). (me, ixmin - 0.5), Sampled_indexToXcxx . . $ Sampled_indexToX(65bcd2c9acbb3dd5). (me, ixmax + 0.5),
477 SampledXY_indexToYcxx . . $ SampledXY_indexToY(3ae72866b16d2115). (me, iymin - 0.5), SampledXY_indexToYcxx . . $ SampledXY_indexToY(3ae72866b16d2115). (me, iymax + 0.5), 0.0, 1.0);
478 else
479 Graphics_cellArray_colourcxx . . $ Graphics_cellArray_colour(5885e50e053285eb). (g, zcxx . . $ constmatrixview#constmatrixview(c98a2930b9469e58)..allcxx . . $ automatrix#all(2114117ba8ed4224).(),
480 Sampled_indexToXcxx . . $ Sampled_indexToX(65bcd2c9acbb3dd5). (me, ixmin - 0.5), Sampled_indexToXcxx . . $ Sampled_indexToX(65bcd2c9acbb3dd5). (me, ixmax + 0.5),
481 SampledXY_indexToYcxx . . $ SampledXY_indexToY(3ae72866b16d2115). (me, iymin - 0.5), SampledXY_indexToYcxx . . $ SampledXY_indexToY(3ae72866b16d2115). (me, iymax + 0.5), 0.0, 1.0);
482 //Graphics_rectangle (g, xmin, xmax, ymin, ymax);
483 }
484
485 void Photo_paintImageInsidecxx . . $ Photo_paintImageInside(ab4968d4013843ec). (Photo me, Graphicscxx . . $ Graphics# g, double xmin, double xmax, double ymin, double ymax) {
486 _Photo_cellArrayOrImagecxx . . $ _Photo_cellArrayOrImage(ecff009a056dcf56). (me, g, xmin, xmax, ymin, ymax, true);
487 }
488
489 void Photo_paintImagecxx . . $ Photo_paintImage(ab4968d4013843ec). (Photo me, Graphicscxx . . $ Graphics# g, double xmin, double xmax, double ymin, double ymax) {
490 Graphics_setInnercxx . . $ Graphics_setInner(2d838ed765120c5d). (g);
491 _Photo_cellArrayOrImagecxx . . $ _Photo_cellArrayOrImage(ecff009a056dcf56). (me, g, xmin, xmax, ymin, ymax, true);
492 Graphics_unsetInnercxx . . $ Graphics_unsetInner(2d838ed765120c5d). (g);
493 }
494
495 void Photo_paintCellsInsidecxx . . $ Photo_paintCellsInside(ab4968d4013843ec). (Photo me, Graphicscxx . . $ Graphics# g, double xmin, double xmax, double ymin, double ymax) {
496 _Photo_cellArrayOrImagecxx . . $ _Photo_cellArrayOrImage(ecff009a056dcf56). (me, g, xmin, xmax, ymin, ymax, false);
497 }
498
499 void Photo_paintCellscxx . . $ Photo_paintCells(ab4968d4013843ec). (Photo me, Graphicscxx . . $ Graphics# g, double xmin, double xmax, double ymin, double ymax) {
500 Graphics_setInnercxx . . $ Graphics_setInner(2d838ed765120c5d). (g);
501 _Photo_cellArrayOrImagecxx . . $ _Photo_cellArrayOrImage(ecff009a056dcf56). (me, g, xmin, xmax, ymin, ymax, false);
502 Graphics_unsetInnercxx . . $ Graphics_unsetInner(2d838ed765120c5d). (g);
503 }
504
505 /* End of file Photo.cpp */