2005-11-10 02:52:27 +03:00
|
|
|
/*
|
2006-04-01 19:51:48 +04:00
|
|
|
* Copyright 2001-2006, Haiku.
|
2005-11-10 02:52:27 +03:00
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* DarkWyrm <bpmagic@columbus.rr.com>
|
2006-01-02 03:58:37 +03:00
|
|
|
* Stephan Aßmus <superstippi@gmx.de>
|
2005-11-10 02:52:27 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
/** Function for saving a generic framebuffer to a PNG file */
|
|
|
|
|
|
|
|
#include "PNGDump.h"
|
|
|
|
|
2006-04-01 19:51:48 +04:00
|
|
|
#include <NodeInfo.h>
|
|
|
|
#include <Rect.h>
|
|
|
|
|
|
|
|
#include <png.h>
|
|
|
|
|
2005-11-10 02:52:27 +03:00
|
|
|
#include <errno.h>
|
2003-03-30 04:17:50 +04:00
|
|
|
#include <stdio.h>
|
2005-11-10 02:52:27 +03:00
|
|
|
#include <stdlib.h>
|
2003-03-30 04:17:50 +04:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
2005-11-10 02:52:27 +03:00
|
|
|
#define TRACE_PNGDUMP
|
|
|
|
#ifdef TRACE_PNGDUMP
|
|
|
|
# define TRACE(x) printf x
|
|
|
|
#else
|
|
|
|
# define TRACE(x) ;
|
2003-03-30 04:17:50 +04:00
|
|
|
#endif
|
2005-11-10 02:52:27 +03:00
|
|
|
|
2006-04-01 19:51:48 +04:00
|
|
|
|
2005-11-10 02:52:27 +03:00
|
|
|
status_t
|
|
|
|
SaveToPNG(const char* filename, const BRect& bounds, color_space space,
|
|
|
|
const void* bits, int32 bitsLength, int32 bytesPerRow)
|
|
|
|
{
|
|
|
|
int32 width = bounds.IntegerWidth() + 1;
|
|
|
|
int32 height = bounds.IntegerHeight() + 1;
|
|
|
|
|
|
|
|
TRACE(("SaveToPNG: %s (%ldx%ld)\n", filename, width, height));
|
|
|
|
|
|
|
|
FILE *file = fopen(filename, "wb");
|
|
|
|
if (file == NULL) {
|
|
|
|
TRACE(("Couldn't open file: %s\n", strerror(errno)));
|
|
|
|
return errno;
|
2003-03-30 04:17:50 +04:00
|
|
|
}
|
2005-11-10 02:52:27 +03:00
|
|
|
|
|
|
|
png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING,
|
|
|
|
NULL, NULL, NULL);
|
|
|
|
if (png == NULL) {
|
|
|
|
TRACE(("Couldn't create write struct\n"));
|
|
|
|
fclose(file);
|
|
|
|
return B_NO_MEMORY;
|
2003-03-30 04:17:50 +04:00
|
|
|
}
|
2005-11-10 02:52:27 +03:00
|
|
|
|
|
|
|
png_infop info = png_create_info_struct(png);
|
|
|
|
if (info == NULL) {
|
|
|
|
TRACE(("Couldn't create info struct\n"));
|
|
|
|
png_destroy_write_struct(&png, NULL);
|
|
|
|
fclose(file);
|
|
|
|
return B_NO_MEMORY;
|
2003-03-30 04:17:50 +04:00
|
|
|
}
|
2005-11-10 02:52:27 +03:00
|
|
|
|
|
|
|
if (setjmp(png->jmpbuf)) {
|
|
|
|
png_destroy_write_struct(&png, NULL);
|
|
|
|
fclose(file);
|
|
|
|
return B_ERROR;
|
2003-03-30 04:17:50 +04:00
|
|
|
}
|
2005-05-15 04:22:55 +04:00
|
|
|
|
2005-11-10 02:52:27 +03:00
|
|
|
png_init_io(png, file);
|
|
|
|
png_set_bgr(png);
|
|
|
|
|
2006-03-13 21:43:13 +03:00
|
|
|
// TODO: support other color spaces if needed
|
|
|
|
|
|
|
|
switch (space) {
|
|
|
|
case B_RGB32:
|
|
|
|
case B_RGBA32:
|
|
|
|
{
|
|
|
|
// create file without alpha channel
|
|
|
|
png_set_IHDR(png, info, width, height, 8, PNG_COLOR_TYPE_RGB,
|
|
|
|
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
|
|
|
|
PNG_FILTER_TYPE_DEFAULT);
|
|
|
|
png_write_info(png, info);
|
|
|
|
|
|
|
|
// convert from 32 bit RGB to 24 bit RGB while saving
|
|
|
|
png_byte* src = (png_byte*)bits;
|
|
|
|
int srcRowBytes = width * 4;
|
|
|
|
int dstRowBytes = width * 3;
|
|
|
|
int srcRowOffset = bytesPerRow - srcRowBytes;
|
|
|
|
png_byte tempRow[dstRowBytes];
|
|
|
|
for (int row = 0; row < height; row++) {
|
|
|
|
for (int i = 0; i < dstRowBytes; i += 3, src += 4) {
|
|
|
|
tempRow[i] = src[0];
|
|
|
|
tempRow[i + 1] = src[1];
|
|
|
|
tempRow[i + 2] = src[2];
|
|
|
|
}
|
|
|
|
src += srcRowOffset;
|
|
|
|
png_write_row(png, tempRow);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
TRACE(("Unsupported color space\n"));
|
|
|
|
png_destroy_write_struct(&png, NULL);
|
|
|
|
fclose(file);
|
|
|
|
return B_ERROR;
|
|
|
|
}
|
2003-03-30 04:17:50 +04:00
|
|
|
}
|
2005-11-10 02:52:27 +03:00
|
|
|
|
2006-01-02 03:58:37 +03:00
|
|
|
png_write_end(png, info);
|
2005-11-10 02:52:27 +03:00
|
|
|
png_destroy_write_struct(&png, NULL);
|
2006-01-02 03:58:37 +03:00
|
|
|
|
2005-11-10 02:52:27 +03:00
|
|
|
fclose(file);
|
2006-04-01 19:51:48 +04:00
|
|
|
|
|
|
|
// Set the file type manually, so that it doesn't have to be
|
|
|
|
// picked up by the registrar or Tracker, first
|
|
|
|
BNode node(filename);
|
|
|
|
BNodeInfo nodeInfo(&node);
|
|
|
|
if (nodeInfo.InitCheck() == B_OK)
|
|
|
|
nodeInfo.SetType("image/png");
|
|
|
|
|
2005-11-10 02:52:27 +03:00
|
|
|
return B_OK;
|
2003-03-30 04:17:50 +04:00
|
|
|
}
|