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