Convert writer_png to C++; add Writer class; add PNGWriter class

This commit is contained in:
Eryn Wells 2013-09-10 21:01:29 -07:00
parent 568e3f4c55
commit 5d8a940f30
4 changed files with 58 additions and 26 deletions

View file

@ -9,6 +9,7 @@ files = Split("""
object.cc object.cc
object_sphere.cc object_sphere.cc
scene.cc scene.cc
writer_png.cc
""") """)
lib = env.Library('charles', files) lib = env.Library('charles', files)

21
src/writer.h Normal file
View file

@ -0,0 +1,21 @@
/* writer.h
*
* Writers handle the interface between (mostly) C libraries to write various image formats and the rest of Charles.
*
* Eryn Wells <eryn@erynwells.me>
*/
#ifndef __WRITER_H__
#define __WRITER_H__
#include <string>
#include "scene.h"
class Writer
{
public:
virtual int write_scene(const Scene &scene, const std::string &filename) = 0;
};
#endif

View file

@ -5,33 +5,37 @@
* Eryn Wells <eryn@erynwells.me> * Eryn Wells <eryn@erynwells.me>
*/ */
#include <csetjmp>
#include <cstdlib>
#include <string>
#include <setjmp.h> #include "scene.h"
#include <stdlib.h>
#include <png.h>
#include "writer_png.h" #include "writer_png.h"
extern "C" {
#include <png.h>
}
static void png_user_error(png_structp png, png_const_charp msg); static void png_user_error(png_structp png, png_const_charp msg);
static void png_user_warning(png_structp png, png_const_charp msg); static void png_user_warning(png_structp png, png_const_charp msg);
/* /*
* write_scene_png -- * PNGWriter::write_scene --
* *
* Write the given scene to file in PNG format. * Write the given scene to a file in PNG format.
*/ */
int int
write_scene_png(Scene *scene, FILE *file) PNGWriter::write_scene(const Scene &scene, const std::string &filename)
{ {
if (!scene->is_rendered) { if (!scene.is_rendered()) {
return -1; return -1;
} }
FILE *file = fopen(filename.c_str(), "wb");
if (!file) { if (!file) {
return -2; return -1;
} }
// Set up the PNG data structures. libpng requires two: a PNG object and an info object. // Set up the PNG data structures. libpng requires two: a PNG object and an info object.
@ -39,7 +43,6 @@ write_scene_png(Scene *scene, FILE *file)
if (!png) { if (!png) {
return -3; return -3;
} }
png_infop png_info = png_create_info_struct(png); png_infop png_info = png_create_info_struct(png);
if (!png_info) { if (!png_info) {
return -4; return -4;
@ -59,7 +62,7 @@ write_scene_png(Scene *scene, FILE *file)
* - No compression * - No compression
*/ */
png_set_IHDR(png, png_info, png_set_IHDR(png, png_info,
scene->width, scene->height, scene.get_width(), scene.get_height(),
8, PNG_COLOR_TYPE_RGB, 8, PNG_COLOR_TYPE_RGB,
PNG_INTERLACE_NONE, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT, PNG_COMPRESSION_TYPE_DEFAULT,
@ -72,21 +75,24 @@ write_scene_png(Scene *scene, FILE *file)
png_write_info(png, png_info); png_write_info(png, png_info);
// Write it! // Write it!
const Color *pixels = scene.get_pixels();
png_byte *row = NULL;
int nbytes = 0; int nbytes = 0;
for (int y = 0; y < scene->height; y++) { for (int y = 0; y < scene.get_height(); y++) {
png_byte *row = malloc(png_sizeof(png_byte) * scene->width * 3); row = new png_byte[scene.get_width() * 3];
if (row == NULL) { if (row == NULL) {
// TODO: DANGER! WILL ROBINSON! // TODO: DANGER! WILL ROBINSON!
} }
for (int x = 0; x < scene->width; x++) { for (int x = 0; x < scene.get_width(); x++) {
row[x*3+0] = scene->pixels[y * scene->width + x].red; Color c = pixels[y * scene.get_width() + x];
row[x*3+1] = scene->pixels[y * scene->width + x].green; row[x*3+0] = 0xff * c.red;
row[x*3+2] = scene->pixels[y * scene->width + x].blue; row[x*3+1] = 0xff * c.green;
row[x*3+2] = 0xff * c.blue;
nbytes += 3; nbytes += 3;
} }
png_write_row(png, row); png_write_row(png, row);
free(row); delete[] row;
} }
// Clean up! // Clean up!
@ -103,6 +109,7 @@ write_scene_png(Scene *scene, FILE *file)
* *
* Called by libpng when it encounters an error. * Called by libpng when it encounters an error.
*/ */
/* static */ void /* static */ void
png_user_error(png_structp png, png_user_error(png_structp png,
png_const_charp msg) png_const_charp msg)
@ -116,6 +123,7 @@ png_user_error(png_structp png,
* *
* Called by libpng when it encounters an warning. * Called by libpng when it encounters an warning.
*/ */
/* static */ void /* static */ void
png_user_warning(png_structp png, png_user_warning(png_structp png,
png_const_charp msg) png_const_charp msg)

View file

@ -5,15 +5,17 @@
* Eryn Wells <eryn@erynwells.me> * Eryn Wells <eryn@erynwells.me>
*/ */
#ifndef __WRITER_PNG_H__
#define __WRITER_PNG_H__
#ifndef __WRITER_PNG_H #include "writer.h"
#define __WRITER_PNG_H
#include <stdio.h>
#include "scene.h"
int write_scene_png(Scene *scene, FILE *file); class PNGWriter
: public Writer
{
public:
int write_scene(const Scene &scene, const std::string &filename);
};
#endif #endif