PNGWriter.cc 4.74 KB
Newer Older
1 2 3 4 5 6 7 8
//========================================================================
//
// PNGWriter.cc
//
// This file is licensed under the GPLv2 or later
//
// Copyright (C) 2009 Warren Toomey <wkt@tuhs.org>
// Copyright (C) 2009 Shen Liang <shenzhuxi@gmail.com>
9
// Copyright (C) 2009, 2011 Albert Astals Cid <aacid@kde.org>
Stefan Thomas's avatar
Stefan Thomas committed
10
// Copyright (C) 2009 Stefan Thomas <thomas@eload24.com>
Albert Astals Cid's avatar
Albert Astals Cid committed
11
// Copyright (C) 2010, 2011, 2013 Adrian Johnson <ajohnson@redneon.com>
12
// Copyright (C) 2011 Thomas Klausner <wiz@danbala.tuwien.ac.at>
Albert Astals Cid's avatar
Albert Astals Cid committed
13
// Copyright (C) 2012 Pino Toscano <pino@kde.org>
14 15 16 17 18 19 20
//
//========================================================================

#include "PNGWriter.h"

#ifdef ENABLE_LIBPNG

21
#include <zlib.h>
22
#include <stdlib.h>
23
#include <string.h>
24

25
#include "poppler/Error.h"
26
#include "goo/gmem.h"
27

28 29 30 31 32 33 34 35 36 37 38 39 40
#include <png.h>

struct PNGWriterPrivate {
  PNGWriter::Format format;
  png_structp png_ptr;
  png_infop info_ptr;
  unsigned char *icc_data;
  int icc_data_size;
  char *icc_name;
  bool sRGB_profile;
};

PNGWriter::PNGWriter(Format formatA)
41
{
42 43 44 45 46 47
  priv = new PNGWriterPrivate;
  priv->format = formatA;
  priv->icc_data = NULL;
  priv->icc_data_size = 0;
  priv->icc_name = NULL;
  priv->sRGB_profile = false;
48 49 50 51
}

PNGWriter::~PNGWriter()
{
52
  /* cleanup heap allocation */
53 54 55 56
  png_destroy_write_struct(&priv->png_ptr, &priv->info_ptr);
  if (priv->icc_data) {
    gfree(priv->icc_data);
    free(priv->icc_name);
57
  }
58 59

  delete priv;
60 61 62 63
}

void PNGWriter::setICCProfile(const char *name, unsigned char *data, int size)
{
64 65 66 67
  priv->icc_data = (unsigned char *)gmalloc(size);
  memcpy(priv->icc_data, data, size);
  priv->icc_data_size = size;
  priv->icc_name = strdup(name);
68 69 70 71
}

void PNGWriter::setSRGBProfile()
{
72
  priv->sRGB_profile = true;
73 74
}

75
bool PNGWriter::init(FILE *f, int width, int height, int hDPI, int vDPI)
76
{
77 78
  /* libpng changed the png_set_iCCP() prototype in 1.5.0 */
#if PNG_LIBPNG_VER < 10500
79
  png_charp icc_data_ptr = (png_charp)priv->icc_data;
80
#else
81
  png_const_bytep icc_data_ptr = (png_const_bytep)priv->icc_data;
82 83
#endif

84
  /* initialize stuff */
85 86
  priv->png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  if (!priv->png_ptr) {
87 88 89 90
    error(errInternal, -1, "png_create_write_struct failed");
    return false;
  }

91 92
  priv->info_ptr = png_create_info_struct(priv->png_ptr);
  if (!priv->info_ptr) {
93 94 95 96
    error(errInternal, -1, "png_create_info_struct failed");
    return false;
  }

97
  if (setjmp(png_jmpbuf(priv->png_ptr))) {
98 99 100 101 102
    error(errInternal, -1, "png_jmpbuf failed");
    return false;
  }

  /* write header */
103 104
  png_init_io(priv->png_ptr, f);
  if (setjmp(png_jmpbuf(priv->png_ptr))) {
105 106 107 108 109
    error(errInternal, -1, "Error during writing header");
    return false;
  }

  // Set up the type of PNG image and the compression level
110
  png_set_compression_level(priv->png_ptr, Z_BEST_COMPRESSION);
111 112 113 114

  // Silence silly gcc
  png_byte bit_depth = -1;
  png_byte color_type = -1;
115
  switch (priv->format) {
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    case RGB:
      bit_depth = 8;
      color_type = PNG_COLOR_TYPE_RGB;
      break;
    case RGBA:
      bit_depth = 8;
      color_type = PNG_COLOR_TYPE_RGB_ALPHA;
      break;
    case GRAY:
      bit_depth = 8;
      color_type = PNG_COLOR_TYPE_GRAY;
      break;
    case MONOCHROME:
      bit_depth = 1;
      color_type = PNG_COLOR_TYPE_GRAY;
      break;
  }
  png_byte interlace_type = PNG_INTERLACE_NONE;

135
  png_set_IHDR(priv->png_ptr, priv->info_ptr, width, height, bit_depth, color_type, interlace_type, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
136

137
  png_set_pHYs(priv->png_ptr, priv->info_ptr, hDPI/0.0254, vDPI/0.0254, PNG_RESOLUTION_METER);
138

139 140 141 142
  if (priv->icc_data)
    png_set_iCCP(priv->png_ptr, priv->info_ptr, priv->icc_name, PNG_COMPRESSION_TYPE_BASE, icc_data_ptr, priv->icc_data_size);
  else if (priv->sRGB_profile)
    png_set_sRGB(priv->png_ptr, priv->info_ptr, PNG_sRGB_INTENT_RELATIVE);
143

144 145
  png_write_info(priv->png_ptr, priv->info_ptr);
  if (setjmp(png_jmpbuf(priv->png_ptr))) {
146 147 148 149 150
    error(errInternal, -1, "error during writing png info bytes");
    return false;
  }

  return true;
151 152
}

Stefan Thomas's avatar
Stefan Thomas committed
153
bool PNGWriter::writePointers(unsigned char **rowPointers, int rowCount)
154
{
155
  png_write_image(priv->png_ptr, rowPointers);
156
  /* write bytes */
157
  if (setjmp(png_jmpbuf(priv->png_ptr))) {
158 159 160 161 162
    error(errInternal, -1, "Error during writing bytes");
    return false;
  }

  return true;
163 164
}

Stefan Thomas's avatar
Stefan Thomas committed
165
bool PNGWriter::writeRow(unsigned char **row)
166
{
167
  // Write the row to the file
168 169
  png_write_rows(priv->png_ptr, row, 1);
  if (setjmp(png_jmpbuf(priv->png_ptr))) {
170 171 172 173 174
    error(errInternal, -1, "error during png row write");
    return false;
  }

  return true;
175 176 177 178
}

bool PNGWriter::close()
{
179
  /* end write */
180 181
  png_write_end(priv->png_ptr, priv->info_ptr);
  if (setjmp(png_jmpbuf(priv->png_ptr))) {
182 183 184 185 186
    error(errInternal, -1, "Error during end of write");
    return false;
  }

  return true;
187 188 189
}

#endif