Mercurial > vba-clojure
comparison src/common/Util.cpp @ 41:f94fa48624d0
upgraded Util.cpp to work with libpng 1.5
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Tue, 06 Mar 2012 12:38:47 -0600 |
parents | f9f4f1b99eed |
children | a6d060a64246 |
comparison
equal
deleted
inserted
replaced
40:dba83a5d0835 | 41:f94fa48624d0 |
---|---|
282 return true; | 282 return true; |
283 } | 283 } |
284 | 284 |
285 bool utilWritePNGFile(const char *fileName, int w, int h, u8 *pix) | 285 bool utilWritePNGFile(const char *fileName, int w, int h, u8 *pix) |
286 { | 286 { |
287 u8 writeBuffer[256 * 3]; | 287 u8 writeBuffer[256 * 3]; |
288 | 288 |
289 FILE *fp = fopen(fileName, "wb"); | 289 FILE *fp = fopen(fileName, "wb"); |
290 | 290 |
291 if (!fp) | 291 if (!fp) |
292 { | 292 { |
293 systemMessage(MSG_ERROR_CREATING_FILE, N_("Error creating file %s"), fileName); | 293 systemMessage(MSG_ERROR_CREATING_FILE, N_("Error creating file %s"), fileName); |
294 return false; | 294 return false; |
295 } | 295 } |
296 | 296 |
297 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, | 297 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, |
298 NULL, | 298 NULL, |
299 NULL, | 299 NULL, |
300 NULL); | 300 NULL); |
301 if (!png_ptr) | 301 if (!png_ptr) |
302 { | 302 { |
303 fclose(fp); | 303 fclose(fp); |
304 return false; | 304 return false; |
305 } | 305 } |
306 | 306 |
307 png_infop info_ptr = png_create_info_struct(png_ptr); | 307 png_infop info_ptr = png_create_info_struct(png_ptr); |
308 | 308 |
309 if (!info_ptr) | 309 if (!info_ptr) |
310 { | 310 { |
311 png_destroy_write_struct(&png_ptr, NULL); | 311 png_destroy_write_struct(&png_ptr, NULL); |
312 fclose(fp); | 312 fclose(fp); |
313 return false; | 313 return false; |
314 } | 314 } |
315 | 315 |
316 if (setjmp(png_ptr->jmpbuf)) | 316 if (setjmp(png_jmpbuf(png_ptr))) |
317 { | 317 { |
318 png_destroy_write_struct(&png_ptr, NULL); | 318 png_destroy_write_struct(&png_ptr, NULL); |
319 fclose(fp); | 319 fclose(fp); |
320 return false; | 320 return false; |
321 } | 321 } |
322 | 322 |
323 png_init_io(png_ptr, fp); | 323 png_init_io(png_ptr, fp); |
324 | 324 |
325 png_set_IHDR(png_ptr, | 325 png_set_IHDR(png_ptr, |
326 info_ptr, | 326 info_ptr, |
327 w, | 327 w, |
328 h, | 328 h, |
329 8, | 329 8, |
330 PNG_COLOR_TYPE_RGB, | 330 PNG_COLOR_TYPE_RGB, |
331 PNG_INTERLACE_NONE, | 331 PNG_INTERLACE_NONE, |
332 PNG_COMPRESSION_TYPE_DEFAULT, | 332 PNG_COMPRESSION_TYPE_DEFAULT, |
333 PNG_FILTER_TYPE_DEFAULT); | 333 PNG_FILTER_TYPE_DEFAULT); |
334 | 334 |
335 png_write_info(png_ptr, info_ptr); | 335 png_write_info(png_ptr, info_ptr); |
336 | 336 |
337 u8 *b = writeBuffer; | 337 u8 *b = writeBuffer; |
338 | 338 |
339 int sizeX = w; | 339 int sizeX = w; |
340 int sizeY = h; | 340 int sizeY = h; |
341 | 341 |
342 switch (systemColorDepth) | 342 switch (systemColorDepth) |
343 { | 343 { |
344 case 16: | 344 case 16: |
345 { | 345 { |
346 u16 *p = (u16 *)(pix + (w + 2) * 2); // skip first black line | 346 u16 *p = (u16 *)(pix + (w + 2) * 2); // skip first black line |
347 for (int y = 0; y < sizeY; y++) | 347 for (int y = 0; y < sizeY; y++) |
348 { | 348 { |
349 for (int x = 0; x < sizeX; x++) | 349 for (int x = 0; x < sizeX; x++) |
350 { | 350 { |
351 u16 v = *p++; | 351 u16 v = *p++; |
352 | 352 |
353 *b++ = ((v >> systemRedShift) & 0x001f) << 3; // R | 353 *b++ = ((v >> systemRedShift) & 0x001f) << 3; // R |
354 *b++ = ((v >> systemGreenShift) & 0x001f) << 3; // G | 354 *b++ = ((v >> systemGreenShift) & 0x001f) << 3; // G |
355 *b++ = ((v >> systemBlueShift) & 0x01f) << 3; // B | 355 *b++ = ((v >> systemBlueShift) & 0x01f) << 3; // B |
356 } | 356 } |
357 p++; // skip black pixel for filters | 357 p++; // skip black pixel for filters |
358 p++; // skip black pixel for filters | 358 p++; // skip black pixel for filters |
359 png_write_row(png_ptr, writeBuffer); | 359 png_write_row(png_ptr, writeBuffer); |
360 | 360 |
361 b = writeBuffer; | 361 b = writeBuffer; |
362 } | 362 } |
363 break; | 363 break; |
364 } | 364 } |
365 case 24: | 365 case 24: |
366 { | 366 { |
367 u8 *pixU8 = (u8 *)pix; | 367 u8 *pixU8 = (u8 *)pix; |
368 for (int y = 0; y < sizeY; y++) | 368 for (int y = 0; y < sizeY; y++) |
369 { | 369 { |
370 for (int x = 0; x < sizeX; x++) | 370 for (int x = 0; x < sizeX; x++) |
371 { | 371 { |
372 if (systemRedShift < systemBlueShift) | 372 if (systemRedShift < systemBlueShift) |
373 { | 373 { |
374 *b++ = *pixU8++; // R | 374 *b++ = *pixU8++; // R |
375 *b++ = *pixU8++; // G | 375 *b++ = *pixU8++; // G |
376 *b++ = *pixU8++; // B | 376 *b++ = *pixU8++; // B |
377 } | 377 } |
378 else | 378 else |
379 { | 379 { |
380 int blue = *pixU8++; | 380 int blue = *pixU8++; |
381 int green = *pixU8++; | 381 int green = *pixU8++; |
382 int red = *pixU8++; | 382 int red = *pixU8++; |
383 | 383 |
384 *b++ = red; | 384 *b++ = red; |
385 *b++ = green; | 385 *b++ = green; |
386 *b++ = blue; | 386 *b++ = blue; |
387 } | 387 } |
388 } | 388 } |
389 png_write_row(png_ptr, writeBuffer); | 389 png_write_row(png_ptr, writeBuffer); |
390 | 390 |
391 b = writeBuffer; | 391 b = writeBuffer; |
392 } | 392 } |
393 break; | 393 break; |
394 } | 394 } |
395 case 32: | 395 case 32: |
396 { | 396 { |
397 u32 *pixU32 = (u32 *)(pix + 4 * (w + 1)); | 397 u32 *pixU32 = (u32 *)(pix + 4 * (w + 1)); |
398 for (int y = 0; y < sizeY; y++) | 398 for (int y = 0; y < sizeY; y++) |
399 { | 399 { |
400 for (int x = 0; x < sizeX; x++) | 400 for (int x = 0; x < sizeX; x++) |
401 { | 401 { |
402 u32 v = *pixU32++; | 402 u32 v = *pixU32++; |
403 | 403 |
404 *b++ = ((v >> systemRedShift) & 0x001f) << 3; // R | 404 *b++ = ((v >> systemRedShift) & 0x001f) << 3; // R |
405 *b++ = ((v >> systemGreenShift) & 0x001f) << 3; // G | 405 *b++ = ((v >> systemGreenShift) & 0x001f) << 3; // G |
406 *b++ = ((v >> systemBlueShift) & 0x001f) << 3; // B | 406 *b++ = ((v >> systemBlueShift) & 0x001f) << 3; // B |
407 } | 407 } |
408 pixU32++; | 408 pixU32++; |
409 | 409 |
410 png_write_row(png_ptr, writeBuffer); | 410 png_write_row(png_ptr, writeBuffer); |
411 | 411 |
412 b = writeBuffer; | 412 b = writeBuffer; |
413 } | 413 } |
414 break; | 414 break; |
415 } | 415 } |
416 } | 416 } |
417 | 417 |
418 png_write_end(png_ptr, info_ptr); | 418 png_write_end(png_ptr, info_ptr); |
419 | 419 |
420 png_destroy_write_struct(&png_ptr, &info_ptr); | 420 png_destroy_write_struct(&png_ptr, &info_ptr); |
421 | 421 |
422 fclose(fp); | 422 fclose(fp); |
423 | 423 |
424 return true; | 424 return true; |
425 } | 425 } |
426 | 426 |
427 static int utilReadInt2(FILE *f) | 427 static int utilReadInt2(FILE *f) |
428 { | 428 { |
429 int res = 0; | 429 int res = 0; |