* added a call to png_set_strip_alpha() which could fix the alpha channel
problem we're seeing (at least I couldn't reproduce the problem anymore). * SaveToPNG() now returns an error code. * cleanup. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@14813 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
+80
-104
@@ -1,115 +1,91 @@
|
|||||||
//------------------------------------------------------------------------------
|
/*
|
||||||
// Copyright (c) 2001-2002, Haiku, Inc.
|
* Copyright 2001-2005, Haiku.
|
||||||
//
|
* Distributed under the terms of the MIT License.
|
||||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
*
|
||||||
// copy of this software and associated documentation files (the "Software"),
|
* Authors:
|
||||||
// to deal in the Software without restriction, including without limitation
|
* DarkWyrm <[email protected]>
|
||||||
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
*/
|
||||||
// and/or sell copies of the Software, and to permit persons to whom the
|
|
||||||
// Software is furnished to do so, subject to the following conditions:
|
/** Function for saving a generic framebuffer to a PNG file */
|
||||||
//
|
|
||||||
// The above copyright notice and this permission notice shall be included in
|
|
||||||
// all copies or substantial portions of the Software.
|
|
||||||
//
|
|
||||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
||||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
||||||
// DEALINGS IN THE SOFTWARE.
|
|
||||||
//
|
|
||||||
// File Name: PNGDump.cpp
|
|
||||||
// Author: DarkWyrm <[email protected]>
|
|
||||||
// Description: Function for saving a generic framebuffer to a PNG file
|
|
||||||
//
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
#include <Rect.h>
|
|
||||||
#include <Bitmap.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <png.h>
|
|
||||||
#include <ColorUtils.h>
|
|
||||||
#include "PNGDump.h"
|
#include "PNGDump.h"
|
||||||
|
|
||||||
#define DEBUG_PNGDUMP
|
#include <Rect.h>
|
||||||
|
|
||||||
void SaveToPNG(const char *filename, const BRect &bounds, color_space space,
|
#include <png.h>
|
||||||
const void *bits, const int32 &bitslength, const int32 bytesperrow)
|
|
||||||
|
#include <errno.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
|
||||||
|
#define TRACE_PNGDUMP
|
||||||
|
#ifdef TRACE_PNGDUMP
|
||||||
|
# define TRACE(x) printf x
|
||||||
|
#else
|
||||||
|
# define TRACE(x) ;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
status_t
|
||||||
|
SaveToPNG(const char* filename, const BRect& bounds, color_space space,
|
||||||
|
const void* bits, int32 bitsLength, int32 bytesPerRow)
|
||||||
{
|
{
|
||||||
#ifdef DEBUG_PNGDUMP
|
|
||||||
printf("SaveToPNG: %s ",filename);
|
|
||||||
bounds.PrintToStream();
|
|
||||||
#endif
|
|
||||||
FILE *fp;
|
|
||||||
png_structp png_ptr;
|
|
||||||
png_infop info_ptr;
|
|
||||||
|
|
||||||
fp=fopen(filename, "wb");
|
|
||||||
if(fp==NULL)
|
|
||||||
{
|
|
||||||
#ifdef DEBUG_PNGDUMP
|
|
||||||
printf("Couldn't open file\n");
|
|
||||||
#endif
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
png_ptr=png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
|
|
||||||
|
|
||||||
if(png_ptr==NULL)
|
|
||||||
{
|
|
||||||
#ifdef DEBUG_PNGDUMP
|
|
||||||
printf("Couldn't create write struct\n");
|
|
||||||
#endif
|
|
||||||
fclose(fp);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
info_ptr=png_create_info_struct(png_ptr);
|
|
||||||
if(info_ptr==NULL)
|
|
||||||
{
|
|
||||||
#ifdef DEBUG_PNGDUMP
|
|
||||||
printf("Couldn't create info struct\n");
|
|
||||||
#endif
|
|
||||||
fclose(fp);
|
|
||||||
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(setjmp(png_ptr->jmpbuf))
|
|
||||||
{
|
|
||||||
#ifdef DEBUG_PNGDUMP
|
|
||||||
printf("Couldn't set jump\n");
|
|
||||||
#endif
|
|
||||||
fclose(fp);
|
|
||||||
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
png_init_io(png_ptr, fp);
|
|
||||||
|
|
||||||
// png_set_compression_level(png_ptr, Z_NO_COMPRESSION);
|
|
||||||
|
|
||||||
png_set_bgr(png_ptr);
|
|
||||||
|
|
||||||
int32 width = bounds.IntegerWidth() + 1;
|
int32 width = bounds.IntegerWidth() + 1;
|
||||||
int32 height = bounds.IntegerHeight() + 1;
|
int32 height = bounds.IntegerHeight() + 1;
|
||||||
|
|
||||||
png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB_ALPHA,
|
|
||||||
// PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
|
|
||||||
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
|
|
||||||
|
|
||||||
png_write_info(png_ptr, info_ptr);
|
|
||||||
|
|
||||||
png_byte* row_pointers[height];
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (setjmp(png->jmpbuf)) {
|
||||||
|
png_destroy_write_struct(&png, NULL);
|
||||||
|
fclose(file);
|
||||||
|
return B_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
png_init_io(png, file);
|
||||||
|
png_set_bgr(png);
|
||||||
|
|
||||||
|
png_set_IHDR(png, info, width, height, 8, PNG_COLOR_TYPE_RGB_ALPHA,
|
||||||
|
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
|
||||||
|
PNG_FILTER_TYPE_DEFAULT);
|
||||||
|
png_set_strip_alpha(png);
|
||||||
|
|
||||||
|
png_write_info(png, info);
|
||||||
|
|
||||||
|
png_byte* rowPointers[height];
|
||||||
png_byte* index = (png_byte*)bits;
|
png_byte* index = (png_byte*)bits;
|
||||||
for (int32 i = 0; i < height; i++) {
|
for (int32 i = 0; i < height; i++) {
|
||||||
row_pointers[i] = index;
|
rowPointers[i] = index;
|
||||||
index += bytesperrow;
|
index += bytesPerRow;
|
||||||
}
|
}
|
||||||
png_write_image(png_ptr, row_pointers);
|
|
||||||
png_write_end(png_ptr, info_ptr);
|
png_write_image(png, rowPointers);
|
||||||
|
png_write_end(png, info);
|
||||||
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
|
|
||||||
fclose(fp);
|
png_destroy_write_struct(&png, NULL);
|
||||||
|
fclose(file);
|
||||||
|
return B_OK;
|
||||||
}
|
}
|
||||||
|
|||||||
+14
-28
@@ -1,34 +1,20 @@
|
|||||||
//------------------------------------------------------------------------------
|
/*
|
||||||
// Copyright (c) 2001-2002, Haiku, Inc.
|
* Copyright 2001-2005, Haiku.
|
||||||
//
|
* Distributed under the terms of the MIT License.
|
||||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
*
|
||||||
// copy of this software and associated documentation files (the "Software"),
|
* Authors:
|
||||||
// to deal in the Software without restriction, including without limitation
|
* DarkWyrm <[email protected]>
|
||||||
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
*/
|
||||||
// and/or sell copies of the Software, and to permit persons to whom the
|
|
||||||
// Software is furnished to do so, subject to the following conditions:
|
|
||||||
//
|
|
||||||
// The above copyright notice and this permission notice shall be included in
|
|
||||||
// all copies or substantial portions of the Software.
|
|
||||||
//
|
|
||||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
||||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
||||||
// DEALINGS IN THE SOFTWARE.
|
|
||||||
//
|
|
||||||
// File Name: PNGDump.H
|
|
||||||
// Author: DarkWyrm <[email protected]>
|
|
||||||
// Description: Function for saving a generic framebuffer to a PNG file
|
|
||||||
//
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
#ifndef PNGDUMP_H
|
#ifndef PNGDUMP_H
|
||||||
#define PNGDUMP_H
|
#define PNGDUMP_H
|
||||||
|
|
||||||
void SaveToPNG(const char *filename, const BRect &bounds, color_space space,
|
|
||||||
const void *bits, const int32 &bitslength, const int32 bytesperrow);
|
|
||||||
|
|
||||||
|
#include <GraphicsDefs.h>
|
||||||
|
|
||||||
|
class BRect;
|
||||||
|
|
||||||
|
|
||||||
|
status_t SaveToPNG(const char* filename, const BRect& bounds, color_space space,
|
||||||
|
const void* bits, int32 bitsLength, int32 bytesPerRow);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user