File: gap_gve_png.c

package info (click to toggle)
gimp-gap 2.6.0+dfsg-5
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 20,720 kB
  • ctags: 7,997
  • sloc: ansic: 119,817; sh: 3,890; makefile: 932; lisp: 97; pascal: 55
file content (203 lines) | stat: -rw-r--r-- 6,607 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/* gap_gve_png.c by Wolfgang Hofer
 *
 * (GAP ... GIMP Animation Plugins, now also known as GIMP Video)
 *
 *
 * In short, this module contains
 * .) software PNG encoder or
 *
 */
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */


/* revision history (see svn)
 * 2008.06.21   hof: created
 */


/* SYSTEM (UNIX) includes */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <unistd.h>

/* GIMP includes */
#include "glib/gstdio.h"
#include "gtk/gtk.h"
#include "libgimp/gimp.h"

/* GAP includes */
#include "gap_libgapbase.h"

#include "gtk/gtk.h"

extern int gap_debug;

/*
  p_status_to_string() is defined in gap/gap_pdb_calls.c
 */
const char * p_status_to_string(int status);


/* --------------------------------
 * p_save_as_tmp_png_file
 * --------------------------------
 * call the GIMP PNG file save plug-in as "PNG" encoding engine.
 */
gboolean
p_save_as_tmp_png_file(const char *filename, gint32 image_id, gint32 drawable_id
   , gint32 png_interlaced, gint32 png_compression)
{
   static char     *l_called_proc = "file-png-save2";
   GimpParam          *return_vals;
   int              nreturn_vals;

   if(gap_debug)
   {
     printf("GAP: PNG encode via call of %s on filename: %s, image_id:%d, drawable_id:%d %s\n"
            , l_called_proc
            , filename
            , image_id
            , drawable_id
            );
   }

   return_vals = gimp_run_procedure (l_called_proc,
                                 &nreturn_vals,
                                 GIMP_PDB_INT32,     GIMP_RUN_NONINTERACTIVE /* runmode */,
                                 GIMP_PDB_IMAGE,     image_id,
                                 GIMP_PDB_DRAWABLE,  drawable_id,
                                 GIMP_PDB_STRING,    filename,
                                 GIMP_PDB_STRING,    filename,
                                 GIMP_PDB_INT32,     png_interlaced,
                                 GIMP_PDB_INT32,     png_compression,
                                 GIMP_PDB_INT32,     0,      /* Write bKGD chunk?  */
                                 GIMP_PDB_INT32,     0,      /* Write gAMA chunk?  */
                                 GIMP_PDB_INT32,     0,      /* Write oFFs chunk?  */
                                 GIMP_PDB_INT32,     0,      /* Write pHYs chunk?  */
                                 GIMP_PDB_INT32,     0,      /* Write tIME chunk?  */
                                 GIMP_PDB_INT32,     0,      /* Write comment?  */
                                 GIMP_PDB_INT32,     0,      /* Preserve color of transparent pixels?  */
                                 GIMP_PDB_END);

   if (return_vals[0].data.d_status == GIMP_PDB_SUCCESS)
   {
      gimp_destroy_params(return_vals, nreturn_vals);
      return (TRUE);
   }

   gimp_destroy_params(return_vals, nreturn_vals);
   printf("GAP: Error: PDB call of %s failed on filename: %s, image_id:%d, drawable_id:%d, d_status:%d %s\n"
          , l_called_proc
          , filename
          , image_id
          , drawable_id
          , (int)return_vals[0].data.d_status
          , p_status_to_string(return_vals[0].data.d_status)
          );
   return(FALSE);

}  /* end p_save_as_tmp_png_file */



/* --------------------------------
 * gap_gve_png_drawable_encode_png
 * --------------------------------
 * gap_gve_png_drawable_encode_png
 *  in: drawable: Describes the picture to be compressed in GIMP terms.
 *      png_interlaced: TRUE: Generate interlaced png.
 *      png_compression: The compression of the generated PNG (0-9, where 9 is best, 0 fastest).
 *      app0_buffer: if != NULL, the content of the APP0-marker to write.
 *      app0_length: the length of the APP0-marker.
 *  out:PNG_size: The size of the buffer that is returned.
 *  returns: guchar *: A buffer, allocated by this routines, which contains
 *                     the compressed PNG, NULL on error.
 */
guchar *
gap_gve_png_drawable_encode_png(GimpDrawable *drawable, gint32 png_interlaced, gint32 *PNG_size,
                               gint32 png_compression,
                               void *app0_buffer, gint32 app0_length)
{
  guchar *buffer;
  guchar *PNG_data;
  size_t totalsize = 0;
  gint32 image_id;
  gboolean l_pngSaveOk;
  char *l_tmpname;
  
  l_tmpname = gimp_temp_name("tmp.png");
  image_id = gimp_drawable_get_image(drawable->drawable_id);
  
  l_pngSaveOk = p_save_as_tmp_png_file(l_tmpname
                       , image_id
                       , drawable->drawable_id
                       , png_interlaced
                       , png_compression
                       );
  if (l_pngSaveOk)
  {
    gint32 fileSize;
    gint32 bytesRead;
    
    fileSize = gap_file_get_filesize(l_tmpname);
    
    totalsize = app0_length + fileSize;
    buffer = g_malloc(totalsize);
    PNG_data = buffer;
    if(app0_length > 0)
    {
      memcpy(buffer, app0_buffer, app0_length);
      PNG_data = buffer + app0_length;
  
      bytesRead = gap_file_load_file_segment(l_tmpname
                      ,PNG_data
                      ,0            /* seek_index, start byte of datasegment in file */
                      ,fileSize     /* segment size in byets */
                      );
      if (bytesRead != fileSize)
      {
        g_free(buffer);
        buffer = NULL;
        totalsize = 0;
        printf("gap_gve_png_drawable_encode_png: read error: bytesRead:%d (expected: %d) file:%s\n"
               ,(int)bytesRead
               ,(int)fileSize
               ,l_tmpname
               );
      }
    }
    
  }

  if(g_file_test(l_tmpname, G_FILE_TEST_EXISTS))
  {
    g_remove(l_tmpname);
  }

  /* free the temporary buffer */
  g_free (l_tmpname);

  *PNG_size = totalsize;
  return buffer;
  
}  /* end gap_gve_png_drawable_encode_png */