| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 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> |
| 20 |
#endif |
| 21 |
|
| 22 |
#ifdef HAVE_WINCODEC_H |
| 23 |
#ifdef __MINGW32__ |
| 24 |
#define INITGUID |
| 25 |
#endif |
| 26 |
#define CINTERFACE |
| 27 |
#define COBJMACROS |
| 28 |
#define _WIN32_IE 0x500 // Workaround bug in shlwapi.h when compiling C++ |
| 29 |
|
| 30 |
#include <ole2.h> |
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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) |
| 158 |
static void PNGAPI PNGErrorFunction(png_structp png, png_const_charp unused) { |
| 159 |
(void)unused; |
| 160 |
longjmp(png_jmpbuf(png), 1); |
| 161 |
} |
| 162 |
|
| 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 |
| 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 |
|
| 219 |
|
| 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 |
|
| 250 |
int WebPWritePPM(FILE* fout, const WebPDecBuffer* const buffer) { |
| 251 |
return WritePPMPAM(fout, buffer, 0); |
| 252 |
} |
| 253 |
|
| 254 |
int WebPWritePAM(FILE* fout, const WebPDecBuffer* const buffer) { |
| 255 |
return WritePPMPAM(fout, buffer, 1); |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
int WebPWrite16bAsPGM(FILE* fout, const WebPDecBuffer* const buffer) { |
| 263 |
uint32_t width, height; |
| 264 |
uint8_t* rgba; |
| 265 |
int stride; |
| 266 |
const uint32_t bytes_per_px = 2; |
| 267 |
uint32_t y; |
| 268 |
|
| 269 |
if (fout == NULL || buffer == NULL) return 0; |
| 270 |
|
| 271 |
width = buffer->width; |
| 272 |
height = buffer->height; |
| 273 |
rgba = buffer->u.RGBA.rgba; |
| 274 |
stride = buffer->u.RGBA.stride; |
| 275 |
|
| 276 |
if (rgba == NULL) return 0; |
| 277 |
|
| 278 |
fprintf(fout, "P5\n%u %u\n255\n", width * bytes_per_px, height); |
| 279 |
for (y = 0; y < height; ++y) { |
| 280 |
if (fwrite(rgba, width, bytes_per_px, fout) != bytes_per_px) { |
| 281 |
return 0; |
| 282 |
} |
| 283 |
rgba += stride; |
| 284 |
} |
| 285 |
return 1; |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
static void PutLE16(uint8_t* const dst, uint32_t value) { |
| 292 |
dst[0] = (value >> 0) & 0xff; |
| 293 |
dst[1] = (value >> 8) & 0xff; |
| 294 |
} |
| 295 |
|
| 296 |
static void PutLE32(uint8_t* const dst, uint32_t value) { |
| 297 |
PutLE16(dst + 0, (value >> 0) & 0xffff); |
| 298 |
PutLE16(dst + 2, (value >> 16) & 0xffff); |
| 299 |
} |
| 300 |
|
| 301 |
#define BMP_HEADER_SIZE 54 |
| 302 |
#define BMP_HEADER_ALPHA_EXTRA_SIZE 16 // for alpha info |
| 303 |
int WebPWriteBMP(FILE* fout, const WebPDecBuffer* const buffer) { |
| 304 |
int has_alpha, header_size; |
| 305 |
uint32_t width, height; |
| 306 |
uint8_t* rgba; |
| 307 |
int stride; |
| 308 |
uint32_t y; |
| 309 |
uint32_t bytes_per_px, line_size, image_size, bmp_stride, total_size; |
| 310 |
uint8_t bmp_header[BMP_HEADER_SIZE + BMP_HEADER_ALPHA_EXTRA_SIZE] = { 0 }; |
| 311 |
|
| 312 |
if (fout == NULL || buffer == NULL) return 0; |
| 313 |
|
| 314 |
has_alpha = WebPIsAlphaMode(buffer->colorspace); |
| 315 |
header_size = BMP_HEADER_SIZE + (has_alpha ? BMP_HEADER_ALPHA_EXTRA_SIZE : 0); |
| 316 |
width = buffer->width; |
| 317 |
height = buffer->height; |
| 318 |
rgba = buffer->u.RGBA.rgba; |
| 319 |
stride = buffer->u.RGBA.stride; |
| 320 |
bytes_per_px = has_alpha ? 4 : 3; |
| 321 |
line_size = bytes_per_px * width; |
| 322 |
bmp_stride = (line_size + 3) & ~3; |
| 323 |
image_size = bmp_stride * height; |
| 324 |
total_size = image_size + header_size; |
| 325 |
|
| 326 |
if (rgba == NULL) return 0; |
| 327 |
|
| 328 |
|
| 329 |
PutLE16(bmp_header + 0, 0x4d42); |
| 330 |
PutLE32(bmp_header + 2, total_size); |
| 331 |
PutLE32(bmp_header + 6, 0); |
| 332 |
PutLE32(bmp_header + 10, header_size); |
| 333 |
|
| 334 |
PutLE32(bmp_header + 14, header_size - 14); |
| 335 |
PutLE32(bmp_header + 18, width); |
| 336 |
PutLE32(bmp_header + 22, height); |
| 337 |
PutLE16(bmp_header + 26, 1); |
| 338 |
PutLE16(bmp_header + 28, bytes_per_px * 8); |
| 339 |
PutLE32(bmp_header + 30, has_alpha ? 3 : 0); |
| 340 |
PutLE32(bmp_header + 34, image_size); |
| 341 |
PutLE32(bmp_header + 38, 2400); |
| 342 |
PutLE32(bmp_header + 42, 2400); |
| 343 |
PutLE32(bmp_header + 46, 0); |
| 344 |
PutLE32(bmp_header + 50, 0); |
| 345 |
if (has_alpha) { |
| 346 |
PutLE32(bmp_header + 54, 0x00ff0000); |
| 347 |
PutLE32(bmp_header + 58, 0x0000ff00); |
| 348 |
PutLE32(bmp_header + 62, 0x000000ff); |
| 349 |
PutLE32(bmp_header + 66, 0xff000000); |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
if (fwrite(bmp_header, header_size, 1, fout) != 1) { |
| 356 |
return 0; |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
for (y = 0; y < height; ++y) { |
| 361 |
const uint8_t* const src = &rgba[(uint64_t)(height - 1 - y) * stride]; |
| 362 |
if (fwrite(src, line_size, 1, fout) != 1) { |
| 363 |
return 0; |
| 364 |
} |
| 365 |
|
| 366 |
if (bmp_stride != line_size) { |
| 367 |
const uint8_t zeroes[3] = { 0 }; |
| 368 |
if (fwrite(zeroes, bmp_stride - line_size, 1, fout) != 1) { |
| 369 |
return 0; |
| 370 |
} |
| 371 |
} |
| 372 |
} |
| 373 |
return 1; |
| 374 |
} |
| 375 |
#undef BMP_HEADER_SIZE |
| 376 |
#undef BMP_HEADER_ALPHA_EXTRA_SIZE |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
#define NUM_IFD_ENTRIES 15 |
| 382 |
#define EXTRA_DATA_SIZE 16 |
| 383 |
|
| 384 |
#define EXTRA_DATA_OFFSET (10 + 12 * NUM_IFD_ENTRIES + 4) |
| 385 |
#define TIFF_HEADER_SIZE (EXTRA_DATA_OFFSET + EXTRA_DATA_SIZE) |
| 386 |
|
| 387 |
int WebPWriteTIFF(FILE* fout, const WebPDecBuffer* const buffer) { |
| 388 |
int has_alpha; |
| 389 |
uint32_t width, height; |
| 390 |
uint8_t* rgba; |
| 391 |
int stride; |
| 392 |
uint8_t bytes_per_px = 0; |
| 393 |
const uint8_t assoc_alpha = 0; |
| 394 |
|
| 395 |
const uint8_t num_ifd_entries = 0; |
| 396 |
uint8_t tiff_header[TIFF_HEADER_SIZE] = { |
| 397 |
0x49, 0x49, 0x2a, 0x00, |
| 398 |
8, 0, 0, 0, |
| 399 |
|
| 400 |
num_ifd_entries, 0, |
| 401 |
0x00, 0x01, 3, 0, 1, 0, 0, 0, 0, 0, 0, 0, |
| 402 |
0x01, 0x01, 3, 0, 1, 0, 0, 0, 0, 0, 0, 0, |
| 403 |
0x02, 0x01, 3, 0, bytes_per_px, 0, 0, 0, |
| 404 |
EXTRA_DATA_OFFSET + 0, 0, 0, 0, |
| 405 |
0x03, 0x01, 3, 0, 1, 0, 0, 0, 1, 0, 0, 0, |
| 406 |
0x06, 0x01, 3, 0, 1, 0, 0, 0, 2, 0, 0, 0, |
| 407 |
0x11, 0x01, 4, 0, 1, 0, 0, 0, |
| 408 |
TIFF_HEADER_SIZE, 0, 0, 0, |
| 409 |
0x12, 0x01, 3, 0, 1, 0, 0, 0, 1, 0, 0, 0, |
| 410 |
0x15, 0x01, 3, 0, 1, 0, 0, 0, |
| 411 |
bytes_per_px, 0, 0, 0, |
| 412 |
0x16, 0x01, 3, 0, 1, 0, 0, 0, 0, 0, 0, 0, |
| 413 |
0x17, 0x01, 4, 0, 1, 0, 0, 0, 0, 0, 0, 0, |
| 414 |
0x1a, 0x01, 5, 0, 1, 0, 0, 0, |
| 415 |
EXTRA_DATA_OFFSET + 8, 0, 0, 0, |
| 416 |
0x1b, 0x01, 5, 0, 1, 0, 0, 0, |
| 417 |
EXTRA_DATA_OFFSET + 8, 0, 0, 0, |
| 418 |
0x1c, 0x01, 3, 0, 1, 0, 0, 0, 1, 0, 0, 0, |
| 419 |
0x28, 0x01, 3, 0, 1, 0, 0, 0, 2, 0, 0, 0, |
| 420 |
0x52, 0x01, 3, 0, 1, 0, 0, 0, |
| 421 |
assoc_alpha, 0, 0, 0, |
| 422 |
0, 0, 0, 0, |
| 423 |
|
| 424 |
8, 0, 8, 0, 8, 0, 8, 0, |
| 425 |
72, 0, 0, 0, 1, 0, 0, 0 |
| 426 |
}; |
| 427 |
uint32_t y; |
| 428 |
|
| 429 |
if (fout == NULL || buffer == NULL) return 0; |
| 430 |
|
| 431 |
has_alpha = WebPIsAlphaMode(buffer->colorspace); |
| 432 |
width = buffer->width; |
| 433 |
height = buffer->height; |
| 434 |
rgba = buffer->u.RGBA.rgba; |
| 435 |
stride = buffer->u.RGBA.stride; |
| 436 |
|
| 437 |
if (rgba == NULL) return 0; |
| 438 |
|
| 439 |
|
| 440 |
tiff_header[38] = tiff_header[102] = bytes_per_px = has_alpha ? 4 : 3; |
| 441 |
tiff_header[8] = has_alpha ? NUM_IFD_ENTRIES : NUM_IFD_ENTRIES - 1; |
| 442 |
tiff_header[186] = WebPIsPremultipliedMode(buffer->colorspace) ? 1 : 2; |
| 443 |
|
| 444 |
|
| 445 |
PutLE32(tiff_header + 10 + 8, width); |
| 446 |
PutLE32(tiff_header + 22 + 8, height); |
| 447 |
PutLE32(tiff_header + 106 + 8, height); |
| 448 |
PutLE32(tiff_header + 118 + 8, width * bytes_per_px * height); |
| 449 |
if (!has_alpha) PutLE32(tiff_header + 178, 0); |
| 450 |
|
| 451 |
|
| 452 |
if (fwrite(tiff_header, sizeof(tiff_header), 1, fout) != 1) { |
| 453 |
return 0; |
| 454 |
} |
| 455 |
|
| 456 |
for (y = 0; y < height; ++y) { |
| 457 |
if (fwrite(rgba, bytes_per_px, width, fout) != width) { |
| 458 |
return 0; |
| 459 |
} |
| 460 |
rgba += stride; |
| 461 |
} |
| 462 |
|
| 463 |
return 1; |
| 464 |
} |
| 465 |
|
| 466 |
#undef TIFF_HEADER_SIZE |
| 467 |
#undef EXTRA_DATA_OFFSET |
| 468 |
#undef EXTRA_DATA_SIZE |
| 469 |
#undef NUM_IFD_ENTRIES |
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
int WebPWriteAlphaPlane(FILE* fout, const WebPDecBuffer* const buffer) { |
| 475 |
if (fout == NULL || buffer == NULL) { |
| 476 |
return 0; |
| 477 |
} else { |
| 478 |
const uint32_t width = buffer->width; |
| 479 |
const uint32_t height = buffer->height; |
| 480 |
const uint8_t* a = buffer->u.YUVA.a; |
| 481 |
const int a_stride = buffer->u.YUVA.a_stride; |
| 482 |
uint32_t y; |
| 483 |
|
| 484 |
if (a == NULL) return 0; |
| 485 |
|
| 486 |
fprintf(fout, "P5\n%u %u\n255\n", width, height); |
| 487 |
for (y = 0; y < height; ++y) { |
| 488 |
if (fwrite(a, width, 1, fout) != 1) return 0; |
| 489 |
a += a_stride; |
| 490 |
} |
| 491 |
return 1; |
| 492 |
} |
| 493 |
} |
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
int WebPWritePGM(FILE* fout, const WebPDecBuffer* const buffer) { |
| 499 |
if (fout == NULL || buffer == NULL) { |
| 500 |
return 0; |
| 501 |
} else { |
| 502 |
const int width = buffer->width; |
| 503 |
const int height = buffer->height; |
| 504 |
const WebPYUVABuffer* const yuv = &buffer->u.YUVA; |
| 505 |
const uint8_t* src_y = yuv->y; |
| 506 |
const uint8_t* src_u = yuv->u; |
| 507 |
const uint8_t* src_v = yuv->v; |
| 508 |
const uint8_t* src_a = yuv->a; |
| 509 |
const int uv_width = (width + 1) / 2; |
| 510 |
const int uv_height = (height + 1) / 2; |
| 511 |
const int a_height = (src_a != NULL) ? height : 0; |
| 512 |
int ok = 1; |
| 513 |
int y; |
| 514 |
|
| 515 |
if (src_y == NULL || src_u == NULL || src_v == NULL) return 0; |
| 516 |
|
| 517 |
fprintf(fout, "P5\n%d %d\n255\n", |
| 518 |
(width + 1) & ~1, height + uv_height + a_height); |
| 519 |
for (y = 0; ok && y < height; ++y) { |
| 520 |
ok &= (fwrite(src_y, width, 1, fout) == 1); |
| 521 |
if (width & 1) fputc(0, fout); |
| 522 |
src_y += yuv->y_stride; |
| 523 |
} |
| 524 |
for (y = 0; ok && y < uv_height; ++y) { |
| 525 |
ok &= (fwrite(src_u, uv_width, 1, fout) == 1); |
| 526 |
ok &= (fwrite(src_v, uv_width, 1, fout) == 1); |
| 527 |
src_u += yuv->u_stride; |
| 528 |
src_v += yuv->v_stride; |
| 529 |
} |
| 530 |
for (y = 0; ok && y < a_height; ++y) { |
| 531 |
ok &= (fwrite(src_a, width, 1, fout) == 1); |
| 532 |
if (width & 1) fputc(0, fout); |
| 533 |
src_a += yuv->a_stride; |
| 534 |
} |
| 535 |
return ok; |
| 536 |
} |
| 537 |
} |
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
int WebPWriteYUV(FILE* fout, const WebPDecBuffer* const buffer) { |
| 543 |
if (fout == NULL || buffer == NULL) { |
| 544 |
return 0; |
| 545 |
} else { |
| 546 |
const int width = buffer->width; |
| 547 |
const int height = buffer->height; |
| 548 |
const WebPYUVABuffer* const yuv = &buffer->u.YUVA; |
| 549 |
const uint8_t* src_y = yuv->y; |
| 550 |
const uint8_t* src_u = yuv->u; |
| 551 |
const uint8_t* src_v = yuv->v; |
| 552 |
const uint8_t* src_a = yuv->a; |
| 553 |
const int uv_width = (width + 1) / 2; |
| 554 |
const int uv_height = (height + 1) / 2; |
| 555 |
const int a_height = (src_a != NULL) ? height : 0; |
| 556 |
int ok = 1; |
| 557 |
int y; |
| 558 |
|
| 559 |
if (src_y == NULL || src_u == NULL || src_v == NULL) return 0; |
| 560 |
|
| 561 |
for (y = 0; ok && y < height; ++y) { |
| 562 |
ok &= (fwrite(src_y, width, 1, fout) == 1); |
| 563 |
src_y += yuv->y_stride; |
| 564 |
} |
| 565 |
for (y = 0; ok && y < uv_height; ++y) { |
| 566 |
ok &= (fwrite(src_u, uv_width, 1, fout) == 1); |
| 567 |
src_u += yuv->u_stride; |
| 568 |
} |
| 569 |
for (y = 0; ok && y < uv_height; ++y) { |
| 570 |
ok &= (fwrite(src_v, uv_width, 1, fout) == 1); |
| 571 |
src_v += yuv->v_stride; |
| 572 |
} |
| 573 |
for (y = 0; ok && y < a_height; ++y) { |
| 574 |
ok &= (fwrite(src_a, width, 1, fout) == 1); |
| 575 |
src_a += yuv->a_stride; |
| 576 |
} |
| 577 |
return ok; |
| 578 |
} |
| 579 |
} |
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
int WebPSaveImage(const WebPDecBuffer* const buffer, |
| 585 |
WebPOutputFileFormat format, |
| 586 |
const char* const out_file_name) { |
| 587 |
FILE* fout = NULL; |
| 588 |
int needs_open_file = 1; |
| 589 |
const int use_stdout = |
| 590 |
(out_file_name != NULL) && !WSTRCMP(out_file_name, "-"); |
| 591 |
int ok = 1; |
| 592 |
|
| 593 |
if (buffer == NULL || out_file_name == NULL) return 0; |
| 594 |
|
| 595 |
#ifdef HAVE_WINCODEC_H |
| 596 |
needs_open_file = (format != PNG); |
| 597 |
#endif |
| 598 |
|
| 599 |
if (needs_open_file) { |
| 600 |
fout = use_stdout ? ImgIoUtilSetBinaryMode(stdout) |
| 601 |
: WFOPEN(out_file_name, "wb"); |
| 602 |
if (fout == NULL) { |
| 603 |
WFPRINTF(stderr, "Error opening output file %s\n", |
| 604 |
(const W_CHAR*)out_file_name); |
| 605 |
return 0; |
| 606 |
} |
| 607 |
} |
| 608 |
|
| 609 |
if (format == PNG || |
| 610 |
format == RGBA || format == BGRA || format == ARGB || |
| 611 |
format == rgbA || format == bgrA || format == Argb) { |
| 612 |
#ifdef HAVE_WINCODEC_H |
| 613 |
ok &= WebPWritePNG(out_file_name, use_stdout, buffer); |
| 614 |
#else |
| 615 |
ok &= WebPWritePNG(fout, buffer); |
| 616 |
#endif |
| 617 |
} else if (format == PAM) { |
| 618 |
ok &= WebPWritePAM(fout, buffer); |
| 619 |
} else if (format == PPM || format == RGB || format == BGR) { |
| 620 |
ok &= WebPWritePPM(fout, buffer); |
| 621 |
} else if (format == RGBA_4444 || format == RGB_565 || format == rgbA_4444) { |
| 622 |
ok &= WebPWrite16bAsPGM(fout, buffer); |
| 623 |
} else if (format == BMP) { |
| 624 |
ok &= WebPWriteBMP(fout, buffer); |
| 625 |
} else if (format == TIFF) { |
| 626 |
ok &= WebPWriteTIFF(fout, buffer); |
| 627 |
} else if (format == RAW_YUV) { |
| 628 |
ok &= WebPWriteYUV(fout, buffer); |
| 629 |
} else if (format == PGM || format == YUV || format == YUVA) { |
| 630 |
ok &= WebPWritePGM(fout, buffer); |
| 631 |
} else if (format == ALPHA_PLANE_ONLY) { |
| 632 |
ok &= WebPWriteAlphaPlane(fout, buffer); |
| 633 |
} |
| 634 |
if (fout != NULL && fout != stdout) { |
| 635 |
fclose(fout); |
| 636 |
} |
| 637 |
return ok; |
| 638 |
} |