| 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" |
| 20 |
#include "NUM2.h" |
| 21 |
#include "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 (linux) && ! defined (NO_GRAPHICS) |
| 29 |
#include <cairo/cairo.h> |
| 30 |
#endif |
| 31 |
|
| 32 |
#include "oo_DESTROY.h" |
| 33 |
#include "Photo_def.h" |
| 34 |
#include "oo_COPY.h" |
| 35 |
#include "Photo_def.h" |
| 36 |
#include "oo_EQUAL.h" |
| 37 |
#include "Photo_def.h" |
| 38 |
#include "oo_CAN_WRITE_AS_ENCODING.h" |
| 39 |
#include "Photo_def.h" |
| 40 |
#include "oo_WRITE_TEXT.h" |
| 41 |
#include "Photo_def.h" |
| 42 |
#include "oo_READ_TEXT.h" |
| 43 |
#include "Photo_def.h" |
| 44 |
#include "oo_WRITE_BINARY.h" |
| 45 |
#include "Photo_def.h" |
| 46 |
#include "oo_READ_BINARY.h" |
| 47 |
#include "Photo_def.h" |
| 48 |
#include "oo_DESCRIPTION.h" |
| 49 |
#include "Photo_def.h" |
| 50 |
|
| 51 |
Thing_implement (Photo, SampledXY, 0); |
| 52 |
|
| 53 |
void structPhoto :: v1_info () { |
| 54 |
our structDaata :: v1_info (); |
| 55 |
MelderInfo_writeLine (U"xmin: ", our xmin); |
| 56 |
MelderInfo_writeLine (U"xmax: ", our xmax); |
| 57 |
MelderInfo_writeLine (U"Number of columns: ", our nx); |
| 58 |
MelderInfo_writeLine (U"dx: ", our dx, U" (-> sampling rate ", 1.0 / our dx, U" )"); |
| 59 |
MelderInfo_writeLine (U"x1: ", our x1); |
| 60 |
MelderInfo_writeLine (U"ymin: ", our ymin); |
| 61 |
MelderInfo_writeLine (U"ymax: ", our ymax); |
| 62 |
MelderInfo_writeLine (U"Number of rows: ", our ny); |
| 63 |
MelderInfo_writeLine (U"dy: ", our dy, U" (-> sampling rate ", 1.0 / our dy, U" )"); |
| 64 |
MelderInfo_writeLine (U"y1: ", our y1); |
| 65 |
} |
| 66 |
|
| 67 |
void Photo_init (Photo me, |
| 68 |
double xmin, double xmax, integer nx, double dx, double x1, |
| 69 |
double ymin, double ymax, integer ny, double dy, double y1) |
| 70 |
{ |
| 71 |
SampledXY_init (me, xmin, xmax, nx, dx, x1, ymin, ymax, ny, dy, y1); |
| 72 |
my d_red = Matrix_create (xmin, xmax, nx, dx, x1, ymin, ymax, ny, dy, y1); |
| 73 |
my d_green = Matrix_create (xmin, xmax, nx, dx, x1, ymin, ymax, ny, dy, y1); |
| 74 |
my d_blue = Matrix_create (xmin, xmax, nx, dx, x1, ymin, ymax, ny, dy, y1); |
| 75 |
my d_transparency = Matrix_create (xmin, xmax, nx, dx, x1, ymin, ymax, ny, dy, y1); |
| 76 |
} |
| 77 |
|
| 78 |
autoPhoto Photo_create |
| 79 |
(double xmin, double xmax, integer nx, double dx, double x1, |
| 80 |
double ymin, double ymax, integer ny, double dy, double y1) |
| 81 |
{ |
| 82 |
try { |
| 83 |
autoPhoto me = Thing_new (Photo); |
| 84 |
Photo_init (me.get(), xmin, xmax, nx, dx, x1, ymin, ymax, ny, dy, y1); |
| 85 |
return me; |
| 86 |
} catch (MelderError) { |
| 87 |
Melder_throw (U"Photo object not created."); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
autoPhoto Photo_createSimple (integer numberOfRows, integer numberOfColumns) { |
| 92 |
try { |
| 93 |
autoPhoto me = Thing_new (Photo); |
| 94 |
Photo_init (me.get(), 0.5, numberOfColumns + 0.5, numberOfColumns, 1, 1, |
| 95 |
0.5, numberOfRows + 0.5, numberOfRows, 1, 1); |
| 96 |
return me; |
| 97 |
} catch (MelderError) { |
| 98 |
Melder_throw (U"Photo object not created."); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
autoPhoto Photo_readFromImageFile (MelderFile file) { |
| 103 |
try { |
| 104 |
#if defined (linux) && ! defined (NO_GRAPHICS) |
| 105 |
cairo_surface_t *surface = cairo_image_surface_create_from_png (MelderFile_peekPath8 (file)); |
| 106 |
//if (cairo_surface_status) |
| 107 |
// Melder_throw (U"Error opening PNG file."); |
| 108 |
integer width = cairo_image_surface_get_width (surface); |
| 109 |
integer height = cairo_image_surface_get_height (surface); |
| 110 |
if (width == 0 || height == 0) { |
| 111 |
cairo_surface_destroy (surface); |
| 112 |
Melder_throw (U"Error reading PNG file."); |
| 113 |
} |
| 114 |
uint8 *imageData = cairo_image_surface_get_data (surface); |
| 115 |
integer bytesPerRow = cairo_image_surface_get_stride (surface); |
| 116 |
cairo_format_t format = cairo_image_surface_get_format (surface); |
| 117 |
autoPhoto me = Photo_createSimple (height, width); |
| 118 |
if (format == CAIRO_FORMAT_ARGB32) { |
| 119 |
for (integer irow = 1; irow <= height; irow ++) { |
| 120 |
uint8 *rowAddress = imageData + bytesPerRow * (height - irow); |
| 121 |
for (integer icol = 1; icol <= width; icol ++) { |
| 122 |
my d_blue -> z [irow] [icol] = (* rowAddress ++) / 255.0; |
| 123 |
my d_green -> z [irow] [icol] = (* rowAddress ++) / 255.0; |
| 124 |
my d_red -> z [irow] [icol] = (* rowAddress ++) / 255.0; |
| 125 |
my d_transparency -> z [irow] [icol] = 1.0 - (* rowAddress ++) / 255.0; |
| 126 |
} |
| 127 |
} |
| 128 |
} else if (format == CAIRO_FORMAT_RGB24) { |
| 129 |
for (integer irow = 1; irow <= height; irow ++) { |
| 130 |
uint8 *rowAddress = imageData + bytesPerRow * (height - irow); |
| 131 |
for (integer icol = 1; icol <= width; icol ++) { |
| 132 |
my d_blue -> z [irow] [icol] = (* rowAddress ++) / 255.0; |
| 133 |
my d_green -> z [irow] [icol] = (* rowAddress ++) / 255.0; |
| 134 |
my d_red -> z [irow] [icol] = (* rowAddress ++) / 255.0; |
| 135 |
my d_transparency -> z [irow] [icol] = 0.0; rowAddress ++; |
| 136 |
} |
| 137 |
} |
| 138 |
} else { |
| 139 |
cairo_surface_destroy (surface); |
| 140 |
Melder_throw (U"Unsupported PNG format ", format, U"."); |
| 141 |
} |
| 142 |
cairo_surface_destroy (surface); |
| 143 |
return me; |
| 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 (MelderError) { |
| 223 |
Melder_throw (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 (linux) && ! defined (NO_GRAPHICS) |
| 236 |
static void _lin_saveAsImageFile (Photo me, MelderFile file, conststring32 which) { |
| 237 |
cairo_format_t format = CAIRO_FORMAT_ARGB32; |
| 238 |
integer bytesPerRow = cairo_format_stride_for_width (format, my nx); // likely to be my nx * 4 |
| 239 |
integer numberOfRows = my ny; |
| 240 |
uint8 *imageData = Melder_malloc_f (uint8, bytesPerRow * numberOfRows); |
| 241 |
for (integer irow = 1; irow <= my ny; irow ++) { |
| 242 |
uint8 *rowAddress = imageData + bytesPerRow * (my ny - irow); |
| 243 |
for (integer icol = 1; icol <= my nx; icol ++) { |
| 244 |
* rowAddress ++ = Melder_iround (my d_blue -> z [irow] [icol] * 255.0); |
| 245 |
* rowAddress ++ = Melder_iround (my d_green -> z [irow] [icol] * 255.0); |
| 246 |
* rowAddress ++ = Melder_iround (my d_red -> z [irow] [icol] * 255.0); |
| 247 |
* rowAddress ++ = 255 - Melder_iround (my d_transparency -> z [irow] [icol] * 255.0); |
| 248 |
} |
| 249 |
} |
| 250 |
cairo_surface_t *surface = cairo_image_surface_create_for_data (imageData, |
| 251 |
format, my nx, my ny, bytesPerRow); |
| 252 |
cairo_surface_write_to_png (surface, MelderFile_peekPath8 (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_saveAsPNG (Photo me, 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 (linux) && ! defined (NO_GRAPHICS) |
| 354 |
_lin_saveAsImageFile (me, file, U"image/png"); |
| 355 |
#endif |
| 356 |
} |
| 357 |
|
| 358 |
void Photo_saveAsTIFF (Photo me, 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_saveAsGIF (Photo me, 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_saveAsWindowsBitmapFile (Photo me, 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_saveAsJPEG (Photo me, 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_saveAsJPEG2000 (Photo me, 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_saveAsAppleIconFile (Photo me, 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_saveAsWindowsIconFile (Photo me, 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_replaceRed (Photo me, Matrix red) { |
| 436 |
my d_red = Data_copy (red); |
| 437 |
} |
| 438 |
|
| 439 |
void Photo_replaceGreen (Photo me, Matrix green) { |
| 440 |
my d_green = Data_copy (green); |
| 441 |
} |
| 442 |
|
| 443 |
void Photo_replaceBlue (Photo me, Matrix blue) { |
| 444 |
my d_blue = Data_copy (blue); |
| 445 |
} |
| 446 |
|
| 447 |
void Photo_replaceTransparency (Photo me, Matrix transparency) { |
| 448 |
my d_transparency = Data_copy (transparency); |
| 449 |
} |
| 450 |
|
| 451 |
static void _Photo_cellArrayOrImage (Photo me, Graphics g, double xmin, double xmax, double ymin, double ymax, bool interpolate) { |
| 452 |
Function_unidirectionalAutowindow (me, & xmin, & xmax); |
| 453 |
SampledXY_unidirectionalAutowindowY (me, & ymin, & ymax); |
| 454 |
integer ixmin, ixmax, iymin, iymax; |
| 455 |
Sampled_getWindowSamples (me, xmin - 0.49999 * my dx, xmax + 0.49999 * my dx, & ixmin, & ixmax); |
| 456 |
SampledXY_getWindowSamplesY (me, ymin - 0.49999 * my dy, ymax + 0.49999 * my dy, & iymin, & iymax); |
| 457 |
if (ixmin > ixmax || iymin > iymax) { |
| 458 |
Melder_crash (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_setWindow (g, xmin, xmax, ymin, ymax); |
| 464 |
automatrix z = newmatrixraw <MelderColour> (iymax - (iymin - 1), ixmax - (ixmin - 1)); |
| 465 |
for (integer iy = iymin; iy <= iymax; iy ++) { |
| 466 |
for (integer ix = ixmin; ix <= ixmax; ix ++) { |
| 467 |
MelderColour& cell = z [iy - (iymin - 1)] [ix - (ixmin - 1)]; |
| 468 |
cell. red = my d_red -> z [iy] [ix]; |
| 469 |
cell. green = my d_green -> z [iy] [ix]; |
| 470 |
cell. blue = my d_blue -> z [iy] [ix]; |
| 471 |
cell. transparency = my d_transparency -> z [iy] [ix]; |
| 472 |
} |
| 473 |
} |
| 474 |
if (interpolate) |
| 475 |
Graphics_image_colour (g, z.all(), |
| 476 |
Sampled_indexToX (me, ixmin - 0.5), Sampled_indexToX (me, ixmax + 0.5), |
| 477 |
SampledXY_indexToY (me, iymin - 0.5), SampledXY_indexToY (me, iymax + 0.5), 0.0, 1.0); |
| 478 |
else |
| 479 |
Graphics_cellArray_colour (g, z.all(), |
| 480 |
Sampled_indexToX (me, ixmin - 0.5), Sampled_indexToX (me, ixmax + 0.5), |
| 481 |
SampledXY_indexToY (me, iymin - 0.5), SampledXY_indexToY (me, iymax + 0.5), 0.0, 1.0); |
| 482 |
//Graphics_rectangle (g, xmin, xmax, ymin, ymax); |
| 483 |
} |
| 484 |
|
| 485 |
void Photo_paintImageInside (Photo me, Graphics g, double xmin, double xmax, double ymin, double ymax) { |
| 486 |
_Photo_cellArrayOrImage (me, g, xmin, xmax, ymin, ymax, true); |
| 487 |
} |
| 488 |
|
| 489 |
void Photo_paintImage (Photo me, Graphics g, double xmin, double xmax, double ymin, double ymax) { |
| 490 |
Graphics_setInner (g); |
| 491 |
_Photo_cellArrayOrImage (me, g, xmin, xmax, ymin, ymax, true); |
| 492 |
Graphics_unsetInner (g); |
| 493 |
} |
| 494 |
|
| 495 |
void Photo_paintCellsInside (Photo me, Graphics g, double xmin, double xmax, double ymin, double ymax) { |
| 496 |
_Photo_cellArrayOrImage (me, g, xmin, xmax, ymin, ymax, false); |
| 497 |
} |
| 498 |
|
| 499 |
void Photo_paintCells (Photo me, Graphics g, double xmin, double xmax, double ymin, double ymax) { |
| 500 |
Graphics_setInner (g); |
| 501 |
_Photo_cellArrayOrImage (me, g, xmin, xmax, ymin, ymax, false); |
| 502 |
Graphics_unsetInner (g); |
| 503 |
} |
| 504 |
|
| 505 |
/* End of file Photo.cpp */ |