1 // Copyright 2016 Google Inc. All Rights Reserved.
2 //
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the COPYING file in the root of the source
5 // tree. An additional intellectual property rights grant can be found
6 // in the file PATENTS. All contributing project authors may
7 // be found in the AUTHORS file in the root of the source tree.
8 // -----------------------------------------------------------------------------
9 //
10 // Save image
11
12 #include "./image_enc.h"
13
14 #include <assert.h>
15 #include <string.h>
16
17 #ifdef WEBP_HAVE_PNG
18 #include <png.h>
19 #include <setjmp.h> // note: this must be included *after* png.h
20 #endif
21
22 #ifdef HAVE_WINCODEC_H
23 #ifdef __MINGW32__
24 #define INITGUID // Without this GUIDs are declared extern and fail to link
25 #endif
26 #define CINTERFACE
27 #define COBJMACROS
28 #define _WIN32_IE 0x500 // Workaround bug in shlwapi.h when compiling C++
29 // code with COBJMACROS.
30 #include <ole2.h> // CreateStreamOnHGlobal()
31 #include <shlwapi.h>
32 #include <tchar.h>
33 #include <windows.h>
34 #include <wincodec.h>
35 #endif
36
37 #include "./imageio_util.h"
38 #include "../examples/unicode.h"
39
40 //------------------------------------------------------------------------------
41 // PNG
42
43 #ifdef HAVE_WINCODEC_H
44
45 #define IFS(fn) \
46 do { \
47 if (SUCCEEDED(hr)) { \
48 hr = (fn); \
49 if (FAILED(hr)) fprintf(stderr, #fn " failed %08lx\n", hr); \
50 } \
51 } while (0)
52
53 #ifdef __cplusplus
54 #define MAKE_REFGUID(x) (x)
55 #else
56 #define MAKE_REFGUID(x) &(x)
57 #endif
58
CreateOutputStream(const char * out_file_name,int write_to_mem,IStream ** stream)59 static HRESULT CreateOutputStream(const char* out_file_name,
60 int write_to_mem, IStream** stream) {
61 HRESULT hr = S_OK;
62 if (write_to_mem) {
63 // Output to a memory buffer. This is freed when 'stream' is released.
64 IFS(CreateStreamOnHGlobal(NULL, TRUE, stream));
65 } else {
66 IFS(SHCreateStreamOnFile((const LPTSTR)out_file_name,
67 STGM_WRITE | STGM_CREATE, stream));
68 }
69 if (FAILED(hr)) {
70 _ftprintf(stderr, _T("Error opening output file %s (%08lx)\n"),
71 (const LPTSTR)out_file_name, hr);
72 }
73 return hr;
74 }
75
WriteUsingWIC(const char * out_file_name,int use_stdout,REFGUID container_guid,uint8_t * rgb,int stride,uint32_t width,uint32_t height,int has_alpha)76 static HRESULT WriteUsingWIC(const char* out_file_name, int use_stdout,
77 REFGUID container_guid,
78 uint8_t* rgb, int stride,
79 uint32_t width, uint32_t height, int has_alpha) {
80 HRESULT hr = S_OK;
81 IWICImagingFactory* factory = NULL;
82 IWICBitmapFrameEncode* frame = NULL;
83 IWICBitmapEncoder* encoder = NULL;
84 IStream* stream = NULL;
85 WICPixelFormatGUID pixel_format = has_alpha ? GUID_WICPixelFormat32bppBGRA
86 : GUID_WICPixelFormat24bppBGR;
87
88 if (out_file_name == NULL || rgb == NULL) return E_INVALIDARG;
89
90 IFS(CoInitialize(NULL));
91 IFS(CoCreateInstance(MAKE_REFGUID(CLSID_WICImagingFactory), NULL,
92 CLSCTX_INPROC_SERVER,
93 MAKE_REFGUID(IID_IWICImagingFactory),
94 (LPVOID*)&factory));
95 if (hr == REGDB_E_CLASSNOTREG) {
96 fprintf(stderr,
97 "Couldn't access Windows Imaging Component (are you running "
98 "Windows XP SP3 or newer?). PNG support not available. "
99 "Use -ppm or -pgm for available PPM and PGM formats.\n");
100 }
101 IFS(CreateOutputStream(out_file_name, use_stdout, &stream));
102 IFS(IWICImagingFactory_CreateEncoder(factory, container_guid, NULL,
103 &encoder));
104 IFS(IWICBitmapEncoder_Initialize(encoder, stream,
105 WICBitmapEncoderNoCache));
106 IFS(IWICBitmapEncoder_CreateNewFrame(encoder, &frame, NULL));
107 IFS(IWICBitmapFrameEncode_Initialize(frame, NULL));
108 IFS(IWICBitmapFrameEncode_SetSize(frame, width, height));
109 IFS(IWICBitmapFrameEncode_SetPixelFormat(frame, &pixel_format));
110 IFS(IWICBitmapFrameEncode_WritePixels(frame, height, stride,
111 height * stride, rgb));
112 IFS(IWICBitmapFrameEncode_Commit(frame));
113 IFS(IWICBitmapEncoder_Commit(encoder));
114
115 if (SUCCEEDED(hr) && use_stdout) {
116 HGLOBAL image;
117 IFS(GetHGlobalFromStream(stream, &image));
118 if (SUCCEEDED(hr)) {
119 HANDLE std_output = GetStdHandle(STD_OUTPUT_HANDLE);
120 DWORD mode;
121 const BOOL update_mode = GetConsoleMode(std_output, &mode);
122 const void* const image_mem = GlobalLock(image);
123 DWORD bytes_written = 0;
124
125 // Clear output processing if necessary, then output the image.
126 if (update_mode) SetConsoleMode(std_output, 0);
127 if (!WriteFile(std_output, image_mem, (DWORD)GlobalSize(image),
128 &bytes_written, NULL) ||
129 bytes_written != GlobalSize(image)) {
130 hr = E_FAIL;
131 }
132 if (update_mode) SetConsoleMode(std_output, mode);
133 GlobalUnlock(image);
134 }
135 }
136
137 if (frame != NULL) IUnknown_Release(frame);
138 if (encoder != NULL) IUnknown_Release(encoder);
139 if (factory != NULL) IUnknown_Release(factory);
140 if (stream != NULL) IUnknown_Release(stream);
141 return hr;
142 }
143
WebPWritePNG(const char * out_file_name,int use_stdout,const WebPDecBuffer * const buffer)144 int WebPWritePNG(const char* out_file_name, int use_stdout,
145 const WebPDecBuffer* const buffer) {
146 const uint32_t width = buffer->width;
147 const uint32_t height = buffer->height;
148 uint8_t* const rgb = buffer->u.RGBA.rgba;
149 const int stride = buffer->u.RGBA.stride;
150 const int has_alpha = WebPIsAlphaMode(buffer->colorspace);
151
152 return SUCCEEDED(WriteUsingWIC(out_file_name, use_stdout,
153 MAKE_REFGUID(GUID_ContainerFormatPng),
154 rgb, stride, width, height, has_alpha));
155 }
156
157 #elif defined(WEBP_HAVE_PNG) // !HAVE_WINCODEC_H
PNGErrorFunction(png_structp png,png_const_charp unused)158 static void PNGAPI PNGErrorFunction(png_structp png, png_const_charp unused) {
159 (void)unused; // remove variable-unused warning
160 longjmp(png_jmpbuf(png), 1);
161 }
162
WebPWritePNG(FILE * out_file,const WebPDecBuffer * const buffer)163 int WebPWritePNG(FILE* out_file, const WebPDecBuffer* const buffer) {
164 volatile png_structp png;
165 volatile png_infop info;
166
167 if (out_file == NULL || buffer == NULL) return 0;
168
169 png = png_create_write_struct(PNG_LIBPNG_VER_STRING,
170 NULL, PNGErrorFunction, NULL);
171 if (png == NULL) {
172 return 0;
173 }
174 info = png_create_info_struct(png);
175 if (info == NULL) {
176 png_destroy_write_struct((png_structpp)&png, NULL);
177 return 0;
178 }
179 if (setjmp(png_jmpbuf(png))) {
180 png_destroy_write_struct((png_structpp)&png, (png_infopp)&info);
181 return 0;
182 }
183 png_init_io(png, out_file);
184 {
185 const uint32_t width = buffer->width;
186 const uint32_t height = buffer->height;
187 png_bytep row = buffer->u.RGBA.rgba;
188 const int stride = buffer->u.RGBA.stride;
189 const int has_alpha = WebPIsAlphaMode(buffer->colorspace);
190 uint32_t y;
191
192 png_set_IHDR(png, info, width, height, 8,
193 has_alpha ? PNG_COLOR_TYPE_RGBA : PNG_COLOR_TYPE_RGB,
194 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
195 PNG_FILTER_TYPE_DEFAULT);
196 png_write_info(png, info);
197 for (y = 0; y < height; ++y) {
198 png_write_rows(png, &row, 1);
199 row += stride;
200 }
201 }
202 png_write_end(png, info);
203 png_destroy_write_struct((png_structpp)&png, (png_infopp)&info);
204 return 1;
205 }
206 #else // !HAVE_WINCODEC_H && !WEBP_HAVE_PNG
WebPWritePNG(FILE * fout,const WebPDecBuffer * const buffer)207 int WebPWritePNG(FILE* fout, const WebPDecBuffer* const buffer) {
208 if (fout == NULL || buffer == NULL) return 0;
209
210 fprintf(stderr, "PNG support not compiled. Please install the libpng "
211 "development package before building.\n");
212 fprintf(stderr, "You can run with -ppm flag to decode in PPM format.\n");
213 return 0;
214 }
215 #endif
216
217 //------------------------------------------------------------------------------
218 // PPM / PAM
219
WritePPMPAM(FILE * fout,const WebPDecBuffer * const buffer,int alpha)220 static int WritePPMPAM(FILE* fout, const WebPDecBuffer* const buffer,
221 int alpha) {
222 if (fout == NULL || buffer == NULL) {
223 return 0;
224 } else {
225 const uint32_t width = buffer->width;
226 const uint32_t height = buffer->height;
227 const uint8_t* row = buffer->u.RGBA.rgba;
228 const int stride = buffer->u.RGBA.stride;
229 const size_t bytes_per_px = alpha ? 4 : 3;
230 uint32_t y;
231
232 if (row == NULL) return 0;
233
234 if (alpha) {
235 fprintf(fout, "P7\nWIDTH %u\nHEIGHT %u\nDEPTH 4\nMAXVAL 255\n"
236 "TUPLTYPE RGB_ALPHA\nENDHDR\n", width, height);
237 } else {
238 fprintf(fout, "P6\n%u %u\n255\n", width, height);
239 }
240 for (y = 0; y < height; ++y) {
241 if (fwrite(row, width, bytes_per_px, fout) != bytes_per_px) {
242 return 0;
243 }
244 row += stride;
245 }
246 }
247 return 1;
248 }
249
WebPWritePPM(FILE * fout,const WebPDecBuffer * const buffer)250 int WebPWritePPM(FILE* fout, const WebPDecBuffer* const buffer) {
251 return WritePPMPAM(fout, buffer, 0);
252 }
253
WebPWritePAM(FILE * fout,const WebPDecBuffer * const buffer)254 int WebPWritePAM(FILE* fout, const WebPDecBuffer* const buffer) {
255 return WritePPMPAM(fout, buffer, 1);
256 }
257
258 //------------------------------------------------------------------------------
259 // Raw PGM
260
261 // Save 16b mode (RGBA4444, RGB565, ...) for debugging purpose.
WebPWrite16bAsPGM(FILE * fout,const WebPDecBuffer * const buffer)262 int WebPWrite16bAsPGM(FILE* fout, const WebPDecBuffer* const buffer) {
263 const uint32_t width = buffer->width;
264 const uint32_t height = buffer->height;
265 const uint8_t* rgba = buffer->u.RGBA.rgba;
266 const int stride = buffer->u.RGBA.stride;
267 const uint32_t bytes_per_px = 2;
268 uint32_t y;
269
270 if (fout == NULL || buffer == NULL || rgba == NULL) return 0;
271
272 fprintf(fout, "P5\n%u %u\n255\n", width * bytes_per_px, height);
273 for (y = 0; y < height; ++y) {
274 if (fwrite(rgba, width, bytes_per_px, fout) != bytes_per_px) {
275 return 0;
276 }
277 rgba += stride;
278 }
279 return 1;
280 }
281
282 //------------------------------------------------------------------------------
283 // BMP
284
PutLE16(uint8_t * const dst,uint32_t value)285 static void PutLE16(uint8_t* const dst, uint32_t value) {
286 dst[0] = (value >> 0) & 0xff;
287 dst[1] = (value >> 8) & 0xff;
288 }
289
PutLE32(uint8_t * const dst,uint32_t value)290 static void PutLE32(uint8_t* const dst, uint32_t value) {
291 PutLE16(dst + 0, (value >> 0) & 0xffff);
292 PutLE16(dst + 2, (value >> 16) & 0xffff);
293 }
294
295 #define BMP_HEADER_SIZE 54
WebPWriteBMP(FILE * fout,const WebPDecBuffer * const buffer)296 int WebPWriteBMP(FILE* fout, const WebPDecBuffer* const buffer) {
297 const int has_alpha = WebPIsAlphaMode(buffer->colorspace);
298 const uint32_t width = buffer->width;
299 const uint32_t height = buffer->height;
300 const uint8_t* rgba = buffer->u.RGBA.rgba;
301 const int stride = buffer->u.RGBA.stride;
302 const uint32_t bytes_per_px = has_alpha ? 4 : 3;
303 uint32_t y;
304 const uint32_t line_size = bytes_per_px * width;
305 const uint32_t bmp_stride = (line_size + 3) & ~3; // pad to 4
306 const uint32_t total_size = bmp_stride * height + BMP_HEADER_SIZE;
307 uint8_t bmp_header[BMP_HEADER_SIZE] = { 0 };
308
309 if (fout == NULL || buffer == NULL || rgba == NULL) return 0;
310
311 // bitmap file header
312 PutLE16(bmp_header + 0, 0x4d42); // signature 'BM'
313 PutLE32(bmp_header + 2, total_size); // size including header
314 PutLE32(bmp_header + 6, 0); // reserved
315 PutLE32(bmp_header + 10, BMP_HEADER_SIZE); // offset to pixel array
316 // bitmap info header
317 PutLE32(bmp_header + 14, 40); // DIB header size
318 PutLE32(bmp_header + 18, width); // dimensions
319 PutLE32(bmp_header + 22, -(int)height); // vertical flip!
320 PutLE16(bmp_header + 26, 1); // number of planes
321 PutLE16(bmp_header + 28, bytes_per_px * 8); // bits per pixel
322 PutLE32(bmp_header + 30, 0); // no compression (BI_RGB)
323 PutLE32(bmp_header + 34, 0); // image size (placeholder)
324 PutLE32(bmp_header + 38, 2400); // x pixels/meter
325 PutLE32(bmp_header + 42, 2400); // y pixels/meter
326 PutLE32(bmp_header + 46, 0); // number of palette colors
327 PutLE32(bmp_header + 50, 0); // important color count
328
329 // TODO(skal): color profile
330
331 // write header
332 if (fwrite(bmp_header, sizeof(bmp_header), 1, fout) != 1) {
333 return 0;
334 }
335
336 // write pixel array
337 for (y = 0; y < height; ++y) {
338 if (fwrite(rgba, line_size, 1, fout) != 1) {
339 return 0;
340 }
341 // write padding zeroes
342 if (bmp_stride != line_size) {
343 const uint8_t zeroes[3] = { 0 };
344 if (fwrite(zeroes, bmp_stride - line_size, 1, fout) != 1) {
345 return 0;
346 }
347 }
348 rgba += stride;
349 }
350 return 1;
351 }
352 #undef BMP_HEADER_SIZE
353
354 //------------------------------------------------------------------------------
355 // TIFF
356
357 #define NUM_IFD_ENTRIES 15
358 #define EXTRA_DATA_SIZE 16
359 // 10b for signature/header + n * 12b entries + 4b for IFD terminator:
360 #define EXTRA_DATA_OFFSET (10 + 12 * NUM_IFD_ENTRIES + 4)
361 #define TIFF_HEADER_SIZE (EXTRA_DATA_OFFSET + EXTRA_DATA_SIZE)
362
WebPWriteTIFF(FILE * fout,const WebPDecBuffer * const buffer)363 int WebPWriteTIFF(FILE* fout, const WebPDecBuffer* const buffer) {
364 const int has_alpha = WebPIsAlphaMode(buffer->colorspace);
365 const uint32_t width = buffer->width;
366 const uint32_t height = buffer->height;
367 const uint8_t* rgba = buffer->u.RGBA.rgba;
368 const int stride = buffer->u.RGBA.stride;
369 const uint8_t bytes_per_px = has_alpha ? 4 : 3;
370 const uint8_t assoc_alpha =
371 WebPIsPremultipliedMode(buffer->colorspace) ? 1 : 2;
372 // For non-alpha case, we omit tag 0x152 (ExtraSamples).
373 const uint8_t num_ifd_entries = has_alpha ? NUM_IFD_ENTRIES
374 : NUM_IFD_ENTRIES - 1;
375 uint8_t tiff_header[TIFF_HEADER_SIZE] = {
376 0x49, 0x49, 0x2a, 0x00, // little endian signature
377 8, 0, 0, 0, // offset to the unique IFD that follows
378 // IFD (offset = 8). Entries must be written in increasing tag order.
379 num_ifd_entries, 0, // Number of entries in the IFD (12 bytes each).
380 0x00, 0x01, 3, 0, 1, 0, 0, 0, 0, 0, 0, 0, // 10: Width (TBD)
381 0x01, 0x01, 3, 0, 1, 0, 0, 0, 0, 0, 0, 0, // 22: Height (TBD)
382 0x02, 0x01, 3, 0, bytes_per_px, 0, 0, 0, // 34: BitsPerSample: 8888
383 EXTRA_DATA_OFFSET + 0, 0, 0, 0,
384 0x03, 0x01, 3, 0, 1, 0, 0, 0, 1, 0, 0, 0, // 46: Compression: none
385 0x06, 0x01, 3, 0, 1, 0, 0, 0, 2, 0, 0, 0, // 58: Photometric: RGB
386 0x11, 0x01, 4, 0, 1, 0, 0, 0, // 70: Strips offset:
387 TIFF_HEADER_SIZE, 0, 0, 0, // data follows header
388 0x12, 0x01, 3, 0, 1, 0, 0, 0, 1, 0, 0, 0, // 82: Orientation: topleft
389 0x15, 0x01, 3, 0, 1, 0, 0, 0, // 94: SamplesPerPixels
390 bytes_per_px, 0, 0, 0,
391 0x16, 0x01, 3, 0, 1, 0, 0, 0, 0, 0, 0, 0, // 106: Rows per strip (TBD)
392 0x17, 0x01, 4, 0, 1, 0, 0, 0, 0, 0, 0, 0, // 118: StripByteCount (TBD)
393 0x1a, 0x01, 5, 0, 1, 0, 0, 0, // 130: X-resolution
394 EXTRA_DATA_OFFSET + 8, 0, 0, 0,
395 0x1b, 0x01, 5, 0, 1, 0, 0, 0, // 142: Y-resolution
396 EXTRA_DATA_OFFSET + 8, 0, 0, 0,
397 0x1c, 0x01, 3, 0, 1, 0, 0, 0, 1, 0, 0, 0, // 154: PlanarConfiguration
398 0x28, 0x01, 3, 0, 1, 0, 0, 0, 2, 0, 0, 0, // 166: ResolutionUnit (inch)
399 0x52, 0x01, 3, 0, 1, 0, 0, 0,
400 assoc_alpha, 0, 0, 0, // 178: ExtraSamples: rgbA/RGBA
401 0, 0, 0, 0, // 190: IFD terminator
402 // EXTRA_DATA_OFFSET:
403 8, 0, 8, 0, 8, 0, 8, 0, // BitsPerSample
404 72, 0, 0, 0, 1, 0, 0, 0 // 72 pixels/inch, for X/Y-resolution
405 };
406 uint32_t y;
407
408 if (fout == NULL || buffer == NULL || rgba == NULL) return 0;
409
410 // Fill placeholders in IFD:
411 PutLE32(tiff_header + 10 + 8, width);
412 PutLE32(tiff_header + 22 + 8, height);
413 PutLE32(tiff_header + 106 + 8, height);
414 PutLE32(tiff_header + 118 + 8, width * bytes_per_px * height);
415 if (!has_alpha) PutLE32(tiff_header + 178, 0); // IFD terminator
416
417 // write header
418 if (fwrite(tiff_header, sizeof(tiff_header), 1, fout) != 1) {
419 return 0;
420 }
421 // write pixel values
422 for (y = 0; y < height; ++y) {
423 if (fwrite(rgba, bytes_per_px, width, fout) != width) {
424 return 0;
425 }
426 rgba += stride;
427 }
428
429 return 1;
430 }
431
432 #undef TIFF_HEADER_SIZE
433 #undef EXTRA_DATA_OFFSET
434 #undef EXTRA_DATA_SIZE
435 #undef NUM_IFD_ENTRIES
436
437 //------------------------------------------------------------------------------
438 // Raw Alpha
439
WebPWriteAlphaPlane(FILE * fout,const WebPDecBuffer * const buffer)440 int WebPWriteAlphaPlane(FILE* fout, const WebPDecBuffer* const buffer) {
441 if (fout == NULL || buffer == NULL) {
442 return 0;
443 } else {
444 const uint32_t width = buffer->width;
445 const uint32_t height = buffer->height;
446 const uint8_t* a = buffer->u.YUVA.a;
447 const int a_stride = buffer->u.YUVA.a_stride;
448 uint32_t y;
449
450 if (a == NULL) return 0;
451
452 fprintf(fout, "P5\n%u %u\n255\n", width, height);
453 for (y = 0; y < height; ++y) {
454 if (fwrite(a, width, 1, fout) != 1) return 0;
455 a += a_stride;
456 }
457 return 1;
458 }
459 }
460
461 //------------------------------------------------------------------------------
462 // PGM with IMC4 layout
463
WebPWritePGM(FILE * fout,const WebPDecBuffer * const buffer)464 int WebPWritePGM(FILE* fout, const WebPDecBuffer* const buffer) {
465 if (fout == NULL || buffer == NULL) {
466 return 0;
467 } else {
468 const int width = buffer->width;
469 const int height = buffer->height;
470 const WebPYUVABuffer* const yuv = &buffer->u.YUVA;
471 const uint8_t* src_y = yuv->y;
472 const uint8_t* src_u = yuv->u;
473 const uint8_t* src_v = yuv->v;
474 const uint8_t* src_a = yuv->a;
475 const int uv_width = (width + 1) / 2;
476 const int uv_height = (height + 1) / 2;
477 const int a_height = (src_a != NULL) ? height : 0;
478 int ok = 1;
479 int y;
480
481 if (src_y == NULL || src_u == NULL || src_v == NULL) return 0;
482
483 fprintf(fout, "P5\n%d %d\n255\n",
484 (width + 1) & ~1, height + uv_height + a_height);
485 for (y = 0; ok && y < height; ++y) {
486 ok &= (fwrite(src_y, width, 1, fout) == 1);
487 if (width & 1) fputc(0, fout); // padding byte
488 src_y += yuv->y_stride;
489 }
490 for (y = 0; ok && y < uv_height; ++y) {
491 ok &= (fwrite(src_u, uv_width, 1, fout) == 1);
492 ok &= (fwrite(src_v, uv_width, 1, fout) == 1);
493 src_u += yuv->u_stride;
494 src_v += yuv->v_stride;
495 }
496 for (y = 0; ok && y < a_height; ++y) {
497 ok &= (fwrite(src_a, width, 1, fout) == 1);
498 if (width & 1) fputc(0, fout); // padding byte
499 src_a += yuv->a_stride;
500 }
501 return ok;
502 }
503 }
504
505 //------------------------------------------------------------------------------
506 // Raw YUV(A) planes
507
WebPWriteYUV(FILE * fout,const WebPDecBuffer * const buffer)508 int WebPWriteYUV(FILE* fout, const WebPDecBuffer* const buffer) {
509 if (fout == NULL || buffer == NULL) {
510 return 0;
511 } else {
512 const int width = buffer->width;
513 const int height = buffer->height;
514 const WebPYUVABuffer* const yuv = &buffer->u.YUVA;
515 const uint8_t* src_y = yuv->y;
516 const uint8_t* src_u = yuv->u;
517 const uint8_t* src_v = yuv->v;
518 const uint8_t* src_a = yuv->a;
519 const int uv_width = (width + 1) / 2;
520 const int uv_height = (height + 1) / 2;
521 const int a_height = (src_a != NULL) ? height : 0;
522 int ok = 1;
523 int y;
524
525 if (src_y == NULL || src_u == NULL || src_v == NULL) return 0;
526
527 for (y = 0; ok && y < height; ++y) {
528 ok &= (fwrite(src_y, width, 1, fout) == 1);
529 src_y += yuv->y_stride;
530 }
531 for (y = 0; ok && y < uv_height; ++y) {
532 ok &= (fwrite(src_u, uv_width, 1, fout) == 1);
533 src_u += yuv->u_stride;
534 }
535 for (y = 0; ok && y < uv_height; ++y) {
536 ok &= (fwrite(src_v, uv_width, 1, fout) == 1);
537 src_v += yuv->v_stride;
538 }
539 for (y = 0; ok && y < a_height; ++y) {
540 ok &= (fwrite(src_a, width, 1, fout) == 1);
541 src_a += yuv->a_stride;
542 }
543 return ok;
544 }
545 }
546
547 //------------------------------------------------------------------------------
548 // Generic top-level call
549
WebPSaveImage(const WebPDecBuffer * const buffer,WebPOutputFileFormat format,const char * const out_file_name)550 int WebPSaveImage(const WebPDecBuffer* const buffer,
551 WebPOutputFileFormat format,
552 const char* const out_file_name) {
553 FILE* fout = NULL;
554 int needs_open_file = 1;
555 const int use_stdout =
556 (out_file_name != NULL) && !WSTRCMP(out_file_name, "-");
557 int ok = 1;
558
559 if (buffer == NULL || out_file_name == NULL) return 0;
560
561 #ifdef HAVE_WINCODEC_H
562 needs_open_file = (format != PNG);
563 #endif
564
565 if (needs_open_file) {
566 fout = use_stdout ? ImgIoUtilSetBinaryMode(stdout)
567 : WFOPEN(out_file_name, "wb");
568 if (fout == NULL) {
569 WFPRINTF(stderr, "Error opening output file %s\n",
570 (const W_CHAR*)out_file_name);
571 return 0;
572 }
573 }
574
575 if (format == PNG ||
576 format == RGBA || format == BGRA || format == ARGB ||
577 format == rgbA || format == bgrA || format == Argb) {
578 #ifdef HAVE_WINCODEC_H
579 ok &= WebPWritePNG(out_file_name, use_stdout, buffer);
580 #else
581 ok &= WebPWritePNG(fout, buffer);
582 #endif
583 } else if (format == PAM) {
584 ok &= WebPWritePAM(fout, buffer);
585 } else if (format == PPM || format == RGB || format == BGR) {
586 ok &= WebPWritePPM(fout, buffer);
587 } else if (format == RGBA_4444 || format == RGB_565 || format == rgbA_4444) {
588 ok &= WebPWrite16bAsPGM(fout, buffer);
589 } else if (format == BMP) {
590 ok &= WebPWriteBMP(fout, buffer);
591 } else if (format == TIFF) {
592 ok &= WebPWriteTIFF(fout, buffer);
593 } else if (format == RAW_YUV) {
594 ok &= WebPWriteYUV(fout, buffer);
595 } else if (format == PGM || format == YUV || format == YUVA) {
596 ok &= WebPWritePGM(fout, buffer);
597 } else if (format == ALPHA_PLANE_ONLY) {
598 ok &= WebPWriteAlphaPlane(fout, buffer);
599 }
600 if (fout != NULL && fout != stdout) {
601 fclose(fout);
602 }
603 return ok;
604 }
605