png-save.c 4.25 KB
Newer Older
1 2 3 4 5
/* This file is an image processing operation for GEGL
 *
 * GEGL is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
6
 * version 3 of the License, or (at your option) any later version.
7 8 9 10 11 12 13
 *
 * GEGL is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
14
 * License along with GEGL; if not, see <http://www.gnu.org/licenses/>.
15 16
 *
 * Copyright 2006 Øyvind Kolås <pippin@gimp.org>
17
 *           2006 Dominik Ernst <dernst@gmx.de>
18
 */
19 20
#ifdef GEGL_CHANT_PROPERTIES

21 22 23 24
gegl_chant_string (path, "File", "",
                   "Target path and filename, use '-' for stdout.")
gegl_chant_int    (compression, "Compression",
                   1, 9, 1, "PNG compression level from 1 to 9")
25 26 27

#else

28 29
#define GEGL_CHANT_TYPE_SINK
#define GEGL_CHANT_C_FILE       "png-save.c"
30

31
#include "gegl-chant.h"
32
#include <png.h>
33
#include <stdio.h>
34 35 36 37

gint
gegl_buffer_export_png (GeglBuffer  *gegl_buffer,
                        const gchar *path,
38
                        gint         compression,
39 40 41
                        gint         src_x,
                        gint         src_y,
                        gint         width,
42
                        gint         height)
43 44 45 46 47 48 49 50
{
  gint           row_stride = width * 4;
  FILE          *fp;
  gint           i;
  png_struct    *png;
  png_info      *info;
  guchar        *pixels;
  png_color_16   white;
51 52
  gchar          format_string[16];
  gint           bit_depth = 8;
53 54 55 56 57 58 59 60 61 62 63 64 65

  if (!strcmp (path, "-"))
    {
      fp = stdout;
    }
  else
    {
      fp = fopen (path, "wb");
    }
  if (!fp)
    {
      return -1;
    }
66 67 68 69

  strcpy (format_string, "R'G'B'A ");

  {
70 71
    const Babl *babl; /*= gegl_buffer->format;*/
    BablType   **type;
72

73 74
    g_object_get (gegl_buffer, "format", &babl, NULL);
    type = babl->format.type;
75

76
    for (i=0; i<babl->format.components; i++)
77 78 79 80 81 82 83 84 85 86 87 88 89 90
      if ((*type)->bits > 8)
        bit_depth = 16;
  }

  if (bit_depth == 16)
    {
      strcat (format_string, "u16");
      row_stride *= 2;
    }
  else
    {
      strcat (format_string, "u8");
    }

91 92 93 94 95 96 97 98
  png = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  if (png == NULL)
    return -1;

  info = png_create_info_struct (png);

  if (setjmp (png_jmpbuf (png)))
    return -1;
99
  png_set_compression_level (png, compression);
100 101 102
  png_init_io (png, fp);

  png_set_IHDR (png, info,
103
     width, height, bit_depth, PNG_COLOR_TYPE_RGB_ALPHA,
104 105 106 107 108 109 110 111
     PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_DEFAULT);

  white.red = 0xff;
  white.blue = 0xff;
  white.green = 0xff;
  png_set_bKGD (png, info, &white);

  png_write_info (png, info);
112

113
#if BYTE_ORDER == LITTLE_ENDIAN
114 115
  if (bit_depth > 8)
    png_set_swap (png);
116
#endif
117

118 119 120 121
  pixels = g_malloc0 (row_stride);

  for (i=0; i< height; i++)
    {
122
      GeglRectangle rect = {src_x, src_y+i, width, 1};
123
      gegl_buffer_get (gegl_buffer, 1.0, &rect, babl_format (format_string), pixels, GEGL_AUTO_ROWSTRIDE);
124

125 126 127 128 129 130 131 132 133 134 135 136 137 138
      png_write_rows (png, &pixels, 1);
    }

  png_write_end (png, info);

  png_destroy_write_struct (&png, &info);
  g_free (pixels);

  if (fp!=stdout)
    fclose (fp);

  return 0;
}

139 140 141 142
static gboolean
process (GeglOperation       *operation,
         GeglBuffer          *input,
         const GeglRectangle *result)
143
{
144 145 146 147 148 149 150 151 152 153
  GeglChantO *o = GEGL_CHANT_PROPERTIES (operation);

  gegl_buffer_export_png (input, o->path, o->compression,
                          result->x, result->y,
                          result->width, result->height);
  return  TRUE;
}


static void
154
gegl_chant_class_init (GeglChantClass *klass)
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
{
  GeglOperationClass     *operation_class;
  GeglOperationSinkClass *sink_class;

  operation_class = GEGL_OPERATION_CLASS (klass);
  sink_class      = GEGL_OPERATION_SINK_CLASS (klass);

  sink_class->process = process;
  sink_class->needs_full = TRUE;

  operation_class->name        = "png-save";
  operation_class->categories  = "output";
  operation_class->description =
        "PNG image saver (passes the buffer through, saves as a side-effect.)";

170 171
}

172
#endif