1 /*
2 * GStreamer
3 * Copyright (C) 2008 Filippo Argiolas <filippo.argiolas@gmail.com>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21 /**
22 * SECTION:element-gldifferencematte.
23 * @title: gldifferencematte.
24 *
25 * Saves a background frame and replace it with a pixbuf.
26 *
27 * ## Examples
28 * |[
29 * gst-launch-1.0 videotestsrc ! glupload ! gldifferencemate location=backgroundimagefile ! glimagesink
30 * ]|
31 * FBO (Frame Buffer Object) and GLSL (OpenGL Shading Language) are required.
32 *
33 */
34
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38
39 #include <stdlib.h>
40 #include <png.h>
41
42 #include <gst/gl/gstglfuncs.h>
43
44 #include "gstgldifferencematte.h"
45 #include "effects/gstgleffectssources.h"
46
47 #if PNG_LIBPNG_VER >= 10400
48 #define int_p_NULL NULL
49 #define png_infopp_NULL NULL
50 #endif
51
52 #define GST_CAT_DEFAULT gst_gl_differencematte_debug
53 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
54
55 #define DEBUG_INIT \
56 GST_DEBUG_CATEGORY_INIT (gst_gl_differencematte_debug, "gldifferencematte", 0, "gldifferencematte element");
57
58 #define gst_gl_differencematte_parent_class parent_class
59 G_DEFINE_TYPE_WITH_CODE (GstGLDifferenceMatte, gst_gl_differencematte,
60 GST_TYPE_GL_FILTER, DEBUG_INIT);
61
62 static void gst_gl_differencematte_set_property (GObject * object,
63 guint prop_id, const GValue * value, GParamSpec * pspec);
64 static void gst_gl_differencematte_get_property (GObject * object,
65 guint prop_id, GValue * value, GParamSpec * pspec);
66
67 static gboolean gst_gl_differencematte_filter_texture (GstGLFilter * filter,
68 GstGLMemory * in_tex, GstGLMemory * out_tex);
69
70 static gboolean gst_gl_differencematte_loader (GstGLFilter * filter);
71
72 enum
73 {
74 PROP_0,
75 PROP_LOCATION,
76 };
77
78
79 /* init resources that need a gl context */
80 static gboolean
gst_gl_differencematte_gl_start(GstGLBaseFilter * base_filter)81 gst_gl_differencematte_gl_start (GstGLBaseFilter * base_filter)
82 {
83 GstGLDifferenceMatte *differencematte = GST_GL_DIFFERENCEMATTE (base_filter);
84 GstGLFilter *filter = GST_GL_FILTER (base_filter);
85 GstGLContext *context = base_filter->context;
86 GstGLBaseMemoryAllocator *tex_alloc;
87 GstGLAllocationParams *params;
88 GError *error = NULL;
89 const gchar *frags[2];
90 gint i;
91
92 if (!GST_GL_BASE_FILTER_CLASS (parent_class)->gl_start (base_filter))
93 return FALSE;
94
95 tex_alloc = (GstGLBaseMemoryAllocator *)
96 gst_gl_memory_allocator_get_default (context);
97 params =
98 (GstGLAllocationParams *) gst_gl_video_allocation_params_new (context,
99 NULL, &filter->out_info, 0, NULL, GST_GL_TEXTURE_TARGET_2D, GST_GL_RGBA);
100
101 for (i = 0; i < 4; i++)
102 differencematte->midtexture[i] =
103 (GstGLMemory *) gst_gl_base_memory_alloc (tex_alloc, params);
104 gst_gl_allocation_params_free (params);
105 gst_object_unref (tex_alloc);
106
107 if (!(differencematte->identity_shader =
108 gst_gl_shader_new_default (context, &error))) {
109 GST_ELEMENT_ERROR (differencematte, RESOURCE, NOT_FOUND, ("%s",
110 "Failed to compile identity shader"), ("%s", error->message));
111 return FALSE;
112 }
113
114 frags[0] =
115 gst_gl_shader_string_get_highest_precision (context,
116 GST_GLSL_VERSION_NONE,
117 GST_GLSL_PROFILE_ES | GST_GLSL_PROFILE_COMPATIBILITY);
118
119 frags[1] = difference_fragment_source;
120 if (!(differencematte->shader[0] =
121 gst_gl_shader_new_link_with_stages (context, &error,
122 gst_glsl_stage_new_default_vertex (context),
123 gst_glsl_stage_new_with_strings (context, GL_FRAGMENT_SHADER,
124 GST_GLSL_VERSION_NONE,
125 GST_GLSL_PROFILE_ES | GST_GLSL_PROFILE_COMPATIBILITY, 2,
126 frags), NULL))) {
127 GST_ELEMENT_ERROR (differencematte, RESOURCE, NOT_FOUND, ("%s",
128 "Failed to compile difference shader"), ("%s", error->message));
129 return FALSE;
130 }
131
132 frags[1] = hconv7_fragment_source_gles2;
133 if (!(differencematte->shader[1] =
134 gst_gl_shader_new_link_with_stages (context, &error,
135 gst_glsl_stage_new_default_vertex (context),
136 gst_glsl_stage_new_with_strings (context, GL_FRAGMENT_SHADER,
137 GST_GLSL_VERSION_NONE,
138 GST_GLSL_PROFILE_ES | GST_GLSL_PROFILE_COMPATIBILITY, 2,
139 frags), NULL))) {
140 GST_ELEMENT_ERROR (differencematte, RESOURCE, NOT_FOUND, ("%s",
141 "Failed to compile convolution shader"), ("%s", error->message));
142 return FALSE;
143 }
144
145 frags[1] = vconv7_fragment_source_gles2;
146 if (!(differencematte->shader[2] =
147 gst_gl_shader_new_link_with_stages (context, &error,
148 gst_glsl_stage_new_default_vertex (context),
149 gst_glsl_stage_new_with_strings (context, GL_FRAGMENT_SHADER,
150 GST_GLSL_VERSION_NONE,
151 GST_GLSL_PROFILE_ES | GST_GLSL_PROFILE_COMPATIBILITY, 2,
152 frags), NULL))) {
153 GST_ELEMENT_ERROR (differencematte, RESOURCE, NOT_FOUND, ("%s",
154 "Failed to compile convolution shader"), ("%s", error->message));
155 return FALSE;
156 }
157
158 frags[1] = texture_interp_fragment_source;
159 if (!(differencematte->shader[3] =
160 gst_gl_shader_new_link_with_stages (context, &error,
161 gst_glsl_stage_new_default_vertex (context),
162 gst_glsl_stage_new_with_strings (context, GL_FRAGMENT_SHADER,
163 GST_GLSL_VERSION_NONE,
164 GST_GLSL_PROFILE_ES | GST_GLSL_PROFILE_COMPATIBILITY, 2,
165 frags), NULL))) {
166 GST_ELEMENT_ERROR (differencematte, RESOURCE, NOT_FOUND, ("%s",
167 "Failed to compile interpolation shader"), ("%s", error->message));
168 return FALSE;
169 }
170
171 /* FIXME: this should really be per shader */
172 filter->draw_attr_position_loc =
173 gst_gl_shader_get_attribute_location (differencematte->shader[2],
174 "a_position");
175 filter->draw_attr_texture_loc =
176 gst_gl_shader_get_attribute_location (differencematte->shader[2],
177 "a_texcoord");
178
179 return TRUE;
180 }
181
182 /* free resources that need a gl context */
183 static void
gst_gl_differencematte_gl_stop(GstGLBaseFilter * base_filter)184 gst_gl_differencematte_gl_stop (GstGLBaseFilter * base_filter)
185 {
186 GstGLDifferenceMatte *differencematte = GST_GL_DIFFERENCEMATTE (base_filter);
187 gint i;
188
189 if (differencematte->savedbgtexture) {
190 gst_memory_unref (GST_MEMORY_CAST (differencematte->savedbgtexture));
191 differencematte->savedbgtexture = NULL;
192 }
193
194 if (differencematte->newbgtexture) {
195 gst_memory_unref (GST_MEMORY_CAST (differencematte->newbgtexture));
196 differencematte->newbgtexture = NULL;
197 }
198
199 for (i = 0; i < 4; i++) {
200 if (differencematte->identity_shader) {
201 gst_object_unref (differencematte->identity_shader);
202 differencematte->identity_shader = NULL;
203 }
204
205 if (differencematte->shader[i]) {
206 gst_object_unref (differencematte->shader[i]);
207 differencematte->shader[i] = NULL;
208 }
209
210 if (differencematte->midtexture[i]) {
211 gst_memory_unref (GST_MEMORY_CAST (differencematte->midtexture[i]));
212 differencematte->midtexture[i] = NULL;
213 }
214 }
215 differencematte->location = NULL;
216 differencematte->pixbuf = NULL;
217 differencematte->bg_has_changed = FALSE;
218
219 GST_GL_BASE_FILTER_CLASS (parent_class)->gl_stop (base_filter);
220 }
221
222 static void
gst_gl_differencematte_class_init(GstGLDifferenceMatteClass * klass)223 gst_gl_differencematte_class_init (GstGLDifferenceMatteClass * klass)
224 {
225 GObjectClass *gobject_class;
226 GstElementClass *element_class;
227
228 gobject_class = (GObjectClass *) klass;
229 element_class = GST_ELEMENT_CLASS (klass);
230
231 gst_gl_filter_add_rgba_pad_templates (GST_GL_FILTER_CLASS (klass));
232
233 gobject_class->set_property = gst_gl_differencematte_set_property;
234 gobject_class->get_property = gst_gl_differencematte_get_property;
235
236 GST_GL_BASE_FILTER_CLASS (klass)->gl_start = gst_gl_differencematte_gl_start;
237 GST_GL_BASE_FILTER_CLASS (klass)->gl_stop = gst_gl_differencematte_gl_stop;
238
239 GST_GL_FILTER_CLASS (klass)->filter_texture =
240 gst_gl_differencematte_filter_texture;
241
242 g_object_class_install_property (gobject_class,
243 PROP_LOCATION,
244 g_param_spec_string ("location",
245 "Background image location",
246 "Background image location", NULL,
247 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
248
249 gst_element_class_set_metadata (element_class,
250 "Gstreamer OpenGL DifferenceMatte", "Filter/Effect/Video",
251 "Saves a background frame and replace it with a pixbuf",
252 "Filippo Argiolas <filippo.argiolas@gmail.com>");
253
254 GST_GL_BASE_FILTER_CLASS (klass)->supported_gl_api =
255 GST_GL_API_OPENGL | GST_GL_API_OPENGL3 | GST_GL_API_GLES2;
256 }
257
258 static void
gst_gl_differencematte_init(GstGLDifferenceMatte * differencematte)259 gst_gl_differencematte_init (GstGLDifferenceMatte * differencematte)
260 {
261 differencematte->shader[0] = NULL;
262 differencematte->shader[1] = NULL;
263 differencematte->shader[2] = NULL;
264 differencematte->shader[3] = NULL;
265 differencematte->location = NULL;
266 differencematte->pixbuf = NULL;
267 differencematte->savedbgtexture = 0;
268 differencematte->newbgtexture = 0;
269 differencematte->bg_has_changed = FALSE;
270
271 fill_gaussian_kernel (differencematte->kernel, 7, 30.0);
272 }
273
274 static void
gst_gl_differencematte_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)275 gst_gl_differencematte_set_property (GObject * object, guint prop_id,
276 const GValue * value, GParamSpec * pspec)
277 {
278 GstGLDifferenceMatte *differencematte = GST_GL_DIFFERENCEMATTE (object);
279
280 switch (prop_id) {
281 case PROP_LOCATION:
282 g_free (differencematte->location);
283 differencematte->bg_has_changed = TRUE;
284 differencematte->location = g_value_dup_string (value);
285 break;
286 default:
287 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
288 break;
289 }
290 }
291
292 static void
gst_gl_differencematte_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)293 gst_gl_differencematte_get_property (GObject * object, guint prop_id,
294 GValue * value, GParamSpec * pspec)
295 {
296 GstGLDifferenceMatte *differencematte = GST_GL_DIFFERENCEMATTE (object);
297
298 switch (prop_id) {
299 case PROP_LOCATION:
300 g_value_set_string (value, differencematte->location);
301 break;
302 default:
303 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
304 break;
305 }
306 }
307
308 static void
init_pixbuf_texture(GstGLDifferenceMatte * differencematte)309 init_pixbuf_texture (GstGLDifferenceMatte * differencematte)
310 {
311 GstGLContext *context = GST_GL_BASE_FILTER (differencematte)->context;
312 GstGLFilter *filter = GST_GL_FILTER (differencematte);
313 GstGLBaseMemoryAllocator *tex_alloc;
314 GstGLAllocationParams *params;
315 GstVideoInfo v_info;
316
317 tex_alloc = (GstGLBaseMemoryAllocator *)
318 gst_gl_memory_allocator_get_default (context);
319 gst_video_info_set_format (&v_info, GST_VIDEO_FORMAT_RGBA,
320 differencematte->pbuf_width, differencematte->pbuf_height);
321 params =
322 (GstGLAllocationParams *) gst_gl_video_allocation_params_new (context,
323 NULL, &v_info, 0, NULL, GST_GL_TEXTURE_TARGET_2D, GST_GL_RGBA);
324
325 differencematte->newbgtexture =
326 (GstGLMemory *) gst_gl_base_memory_alloc (tex_alloc, params);
327 gst_gl_allocation_params_free (params);
328
329 if (differencematte->savedbgtexture == NULL) {
330 params =
331 (GstGLAllocationParams *) gst_gl_video_allocation_params_new (context,
332 NULL, &filter->out_info, 0, NULL, GST_GL_TEXTURE_TARGET_2D,
333 GST_GL_RGBA);
334
335 differencematte->savedbgtexture =
336 (GstGLMemory *) gst_gl_base_memory_alloc (tex_alloc, params);
337 gst_gl_allocation_params_free (params);
338 }
339
340 gst_object_unref (tex_alloc);
341 }
342
343 static gboolean
gst_gl_differencematte_diff(GstGLFilter * filter,GstGLMemory * in_tex,gpointer stuff)344 gst_gl_differencematte_diff (GstGLFilter * filter, GstGLMemory * in_tex,
345 gpointer stuff)
346 {
347 GstGLDifferenceMatte *differencematte = GST_GL_DIFFERENCEMATTE (filter);
348 const GstGLFuncs *gl = GST_GL_BASE_FILTER (filter)->context->gl_vtable;
349
350 gst_gl_shader_use (differencematte->shader[0]);
351
352 gl->ActiveTexture (GL_TEXTURE0);
353 gl->BindTexture (GL_TEXTURE_2D, gst_gl_memory_get_texture_id (in_tex));
354
355 gst_gl_shader_set_uniform_1i (differencematte->shader[0], "current", 0);
356
357 gl->ActiveTexture (GL_TEXTURE1);
358 gl->BindTexture (GL_TEXTURE_2D,
359 gst_gl_memory_get_texture_id (differencematte->savedbgtexture));
360
361 gst_gl_shader_set_uniform_1i (differencematte->shader[0], "saved", 1);
362
363 gst_gl_filter_draw_fullscreen_quad (filter);
364
365 return TRUE;
366 }
367
368 static gboolean
gst_gl_differencematte_hblur(GstGLFilter * filter,GstGLMemory * in_tex,gpointer stuff)369 gst_gl_differencematte_hblur (GstGLFilter * filter, GstGLMemory * in_tex,
370 gpointer stuff)
371 {
372 GstGLDifferenceMatte *differencematte = GST_GL_DIFFERENCEMATTE (filter);
373 const GstGLFuncs *gl = GST_GL_BASE_FILTER (filter)->context->gl_vtable;
374
375 gst_gl_shader_use (differencematte->shader[1]);
376
377 gl->ActiveTexture (GL_TEXTURE0);
378 gl->BindTexture (GL_TEXTURE_2D, gst_gl_memory_get_texture_id (in_tex));
379
380 gst_gl_shader_set_uniform_1i (differencematte->shader[1], "tex", 0);
381
382 gst_gl_shader_set_uniform_1fv (differencematte->shader[1], "kernel", 7,
383 differencematte->kernel);
384 gst_gl_shader_set_uniform_1f (differencematte->shader[1], "gauss_width",
385 GST_VIDEO_INFO_WIDTH (&filter->out_info));
386
387 gst_gl_filter_draw_fullscreen_quad (filter);
388
389 return TRUE;
390 }
391
392 static gboolean
gst_gl_differencematte_vblur(GstGLFilter * filter,GstGLMemory * in_tex,gpointer stuff)393 gst_gl_differencematte_vblur (GstGLFilter * filter, GstGLMemory * in_tex,
394 gpointer stuff)
395 {
396 GstGLDifferenceMatte *differencematte = GST_GL_DIFFERENCEMATTE (filter);
397 const GstGLFuncs *gl = GST_GL_BASE_FILTER (filter)->context->gl_vtable;
398
399 gst_gl_shader_use (differencematte->shader[2]);
400
401 gl->ActiveTexture (GL_TEXTURE0);
402 gl->BindTexture (GL_TEXTURE_2D, gst_gl_memory_get_texture_id (in_tex));
403
404 gst_gl_shader_set_uniform_1i (differencematte->shader[2], "tex", 0);
405
406 gst_gl_shader_set_uniform_1fv (differencematte->shader[2], "kernel", 7,
407 differencematte->kernel);
408 gst_gl_shader_set_uniform_1f (differencematte->shader[2], "gauss_height",
409 GST_VIDEO_INFO_HEIGHT (&filter->out_info));
410
411 gst_gl_filter_draw_fullscreen_quad (filter);
412
413 return TRUE;
414 }
415
416 static gboolean
gst_gl_differencematte_interp(GstGLFilter * filter,GstGLMemory * in_tex,gpointer stuff)417 gst_gl_differencematte_interp (GstGLFilter * filter, GstGLMemory * in_tex,
418 gpointer stuff)
419 {
420 GstGLDifferenceMatte *differencematte = GST_GL_DIFFERENCEMATTE (filter);
421 const GstGLFuncs *gl = GST_GL_BASE_FILTER (filter)->context->gl_vtable;
422
423 gst_gl_shader_use (differencematte->shader[3]);
424
425 gl->ActiveTexture (GL_TEXTURE0);
426 gl->BindTexture (GL_TEXTURE_2D, gst_gl_memory_get_texture_id (in_tex));
427
428 gst_gl_shader_set_uniform_1i (differencematte->shader[3], "blend", 0);
429
430 gl->ActiveTexture (GL_TEXTURE1);
431 gl->BindTexture (GL_TEXTURE_2D, differencematte->newbgtexture->tex_id);
432
433 gst_gl_shader_set_uniform_1i (differencematte->shader[3], "base", 1);
434
435 gl->ActiveTexture (GL_TEXTURE2);
436 gl->BindTexture (GL_TEXTURE_2D, differencematte->midtexture[2]->tex_id);
437
438 gst_gl_shader_set_uniform_1i (differencematte->shader[3], "alpha", 2);
439
440 gst_gl_filter_draw_fullscreen_quad (filter);
441
442 return TRUE;
443 }
444
445 static gboolean
gst_gl_differencematte_filter_texture(GstGLFilter * filter,GstGLMemory * in_tex,GstGLMemory * out_tex)446 gst_gl_differencematte_filter_texture (GstGLFilter * filter,
447 GstGLMemory * in_tex, GstGLMemory * out_tex)
448 {
449 GstGLDifferenceMatte *differencematte = GST_GL_DIFFERENCEMATTE (filter);
450
451 differencematte->intexture = in_tex;
452
453 if (differencematte->bg_has_changed && (differencematte->location != NULL)) {
454
455 if (!gst_gl_differencematte_loader (filter))
456 differencematte->pixbuf = NULL;
457
458 init_pixbuf_texture (differencematte);
459
460 /* save current frame, needed to calculate difference between
461 * this frame and next ones */
462 gst_gl_filter_render_to_target_with_shader (filter, in_tex,
463 differencematte->savedbgtexture, differencematte->identity_shader);
464
465 if (differencematte->pixbuf) {
466 free (differencematte->pixbuf);
467 differencematte->pixbuf = NULL;
468 }
469
470 differencematte->bg_has_changed = FALSE;
471 }
472
473 if (differencematte->savedbgtexture != NULL) {
474 gst_gl_filter_render_to_target (filter, in_tex,
475 differencematte->midtexture[0], gst_gl_differencematte_diff, NULL);
476 gst_gl_filter_render_to_target (filter, differencematte->midtexture[0],
477 differencematte->midtexture[1], gst_gl_differencematte_hblur, NULL);
478 gst_gl_filter_render_to_target (filter, differencematte->midtexture[1],
479 differencematte->midtexture[2], gst_gl_differencematte_vblur, NULL);
480 gst_gl_filter_render_to_target (filter, in_tex, out_tex,
481 gst_gl_differencematte_interp, NULL);
482 } else {
483 gst_gl_filter_render_to_target_with_shader (filter, in_tex, out_tex,
484 differencematte->identity_shader);
485 }
486
487 return TRUE;
488 }
489
490 static void
user_warning_fn(png_structp png_ptr,png_const_charp warning_msg)491 user_warning_fn (png_structp png_ptr, png_const_charp warning_msg)
492 {
493 g_warning ("%s\n", warning_msg);
494 }
495
496 #define LOAD_ERROR(msg) { GST_WARNING ("unable to load %s: %s", differencematte->location, msg); return FALSE; }
497
498 static gboolean
gst_gl_differencematte_loader(GstGLFilter * filter)499 gst_gl_differencematte_loader (GstGLFilter * filter)
500 {
501 GstGLDifferenceMatte *differencematte = GST_GL_DIFFERENCEMATTE (filter);
502
503 png_structp png_ptr;
504 png_infop info_ptr;
505 guint sig_read = 0;
506 png_uint_32 width = 0;
507 png_uint_32 height = 0;
508 gint bit_depth = 0;
509 gint color_type = 0;
510 gint interlace_type = 0;
511 png_FILE_p fp = NULL;
512 guint y = 0;
513 guchar **rows = NULL;
514 gint filler;
515
516 if (!GST_GL_BASE_FILTER (filter)->context)
517 return TRUE;
518
519 if ((fp = fopen (differencematte->location, "rb")) == NULL)
520 LOAD_ERROR ("file not found");
521
522 png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
523
524 if (png_ptr == NULL) {
525 fclose (fp);
526 LOAD_ERROR ("failed to initialize the png_struct");
527 }
528
529 png_set_error_fn (png_ptr, NULL, NULL, user_warning_fn);
530
531 info_ptr = png_create_info_struct (png_ptr);
532 if (info_ptr == NULL) {
533 fclose (fp);
534 png_destroy_read_struct (&png_ptr, png_infopp_NULL, png_infopp_NULL);
535 LOAD_ERROR ("failed to initialize the memory for image information");
536 }
537
538 png_init_io (png_ptr, fp);
539
540 png_set_sig_bytes (png_ptr, sig_read);
541
542 png_read_info (png_ptr, info_ptr);
543
544 png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
545 &interlace_type, int_p_NULL, int_p_NULL);
546
547 if (color_type == PNG_COLOR_TYPE_RGB) {
548 filler = 0xff;
549 png_set_filler (png_ptr, filler, PNG_FILLER_AFTER);
550 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
551 }
552
553 if (color_type != PNG_COLOR_TYPE_RGB_ALPHA) {
554 fclose (fp);
555 png_destroy_read_struct (&png_ptr, png_infopp_NULL, png_infopp_NULL);
556 LOAD_ERROR ("color type is not rgb");
557 }
558
559 differencematte->pbuf_width = width;
560 differencematte->pbuf_height = height;
561
562 differencematte->pixbuf =
563 (guchar *) malloc (sizeof (guchar) * width * height * 4);
564
565 rows = (guchar **) malloc (sizeof (guchar *) * height);
566
567 for (y = 0; y < height; ++y)
568 rows[y] = (guchar *) (differencematte->pixbuf + y * width * 4);
569
570 png_read_image (png_ptr, rows);
571
572 free (rows);
573
574 png_read_end (png_ptr, info_ptr);
575 png_destroy_read_struct (&png_ptr, &info_ptr, png_infopp_NULL);
576 fclose (fp);
577
578 return TRUE;
579 }
580