static gboolean gst_gl_differencematte_loader (GstGLFilter * filter) { GstGLDifferenceMatte *differencematte = GST_GL_DIFFERENCEMATTE (filter); png_structp png_ptr; png_infop info_ptr; guint sig_read = 0; png_uint_32 width = 0; png_uint_32 height = 0; gint bit_depth = 0; gint color_type = 0; gint interlace_type = 0; png_FILE_p fp = NULL; guint y = 0; guchar **rows = NULL; if (!filter->display) return TRUE; if ((fp = fopen (differencematte->location, "rb")) == NULL) LOAD_ERROR ("file not found"); png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (png_ptr == NULL) { fclose (fp); LOAD_ERROR ("failed to initialize the png_struct"); } png_set_error_fn (png_ptr, NULL, NULL, user_warning_fn); info_ptr = png_create_info_struct (png_ptr); if (info_ptr == NULL) { fclose (fp); png_destroy_read_struct (&png_ptr, png_infopp_NULL, png_infopp_NULL); LOAD_ERROR ("failed to initialize the memory for image information"); } png_init_io (png_ptr, fp); png_set_sig_bytes (png_ptr, sig_read); png_read_info (png_ptr, info_ptr); png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, int_p_NULL, int_p_NULL); if (color_type != PNG_COLOR_TYPE_RGB_ALPHA) { fclose (fp); png_destroy_read_struct (&png_ptr, png_infopp_NULL, png_infopp_NULL); LOAD_ERROR ("color type is not rgb"); } filter->width = width; filter->height = height; differencematte->pixbuf = (guchar *) malloc (sizeof (guchar) * width * height * 4); rows = (guchar **) malloc (sizeof (guchar *) * height); for (y = 0; y < height; ++y) rows[y] = (guchar *) (differencematte->pixbuf + y * width * 4); png_read_image (png_ptr, rows); free (rows); png_read_end (png_ptr, info_ptr); png_destroy_read_struct (&png_ptr, &info_ptr, png_infopp_NULL); fclose (fp); return TRUE; }
static gint gst_gl_overlay_load_png (GstGLFilter * filter) { GstGLOverlay *overlay = GST_GL_OVERLAY (filter); png_structp png_ptr; png_infop info_ptr; png_uint_32 width = 0; png_uint_32 height = 0; gint bit_depth = 0; gint color_type = 0; gint interlace_type = 0; png_FILE_p fp = NULL; guint y = 0; guchar **rows = NULL; gint filler; png_byte magic[8]; gint n_read; if (!filter->context) return 1; if ((fp = fopen (overlay->location, "rb")) == NULL) LOAD_ERROR ("file not found"); /* Read magic number */ n_read = fread (magic, 1, sizeof (magic), fp); if (n_read != sizeof (magic)) { fclose (fp); LOAD_ERROR ("can't read PNG magic number"); } /* Check for valid magic number */ if (png_sig_cmp (magic, 0, sizeof (magic))) { fclose (fp); LOAD_ERROR ("not a valid PNG image"); } png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (png_ptr == NULL) { fclose (fp); LOAD_ERROR ("failed to initialize the png_struct"); } png_set_error_fn (png_ptr, NULL, NULL, user_warning_fn); info_ptr = png_create_info_struct (png_ptr); if (info_ptr == NULL) { fclose (fp); png_destroy_read_struct (&png_ptr, png_infopp_NULL, png_infopp_NULL); LOAD_ERROR ("failed to initialize the memory for image information"); } png_init_io (png_ptr, fp); png_set_sig_bytes (png_ptr, sizeof (magic)); png_read_info (png_ptr, info_ptr); png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, int_p_NULL, int_p_NULL); if (color_type == PNG_COLOR_TYPE_RGB) { filler = 0xff; png_set_filler (png_ptr, filler, PNG_FILLER_AFTER); color_type = PNG_COLOR_TYPE_RGB_ALPHA; } if (color_type != PNG_COLOR_TYPE_RGB_ALPHA) { fclose (fp); png_destroy_read_struct (&png_ptr, png_infopp_NULL, png_infopp_NULL); LOAD_ERROR ("color type is not rgb"); } overlay->width = width; overlay->height = height; overlay->pixbuf = (guchar *) malloc (sizeof (guchar) * width * height * 4); rows = (guchar **) malloc (sizeof (guchar *) * height); for (y = 0; y < height; ++y) rows[y] = (guchar *) (overlay->pixbuf + y * width * 4); png_read_image (png_ptr, rows); free (rows); png_read_end (png_ptr, info_ptr); png_destroy_read_struct (&png_ptr, &info_ptr, png_infopp_NULL); fclose (fp); return 1; }
//Called in the gl thread static void gst_gl_bumper_init_resources (GstGLFilter * filter) { GstGLBumper *bumper = GST_GL_BUMPER (filter); GstGLContext *context = filter->context; const GstGLFuncs *gl = context->gl_vtable; png_structp png_ptr; png_infop info_ptr; png_uint_32 width = 0; png_uint_32 height = 0; gint bit_depth = 0; gint color_type = 0; gint interlace_type = 0; png_FILE_p fp = NULL; guint y = 0; guchar *raw_data = NULL; guchar **rows = NULL; png_byte magic[8]; gint n_read; if (!bumper->location) { gst_gl_context_set_error (context, "A filename is required"); return; } /* BEGIN load png image file */ if ((fp = fopen (bumper->location, "rb")) == NULL) LOAD_ERROR (context, "file not found"); /* Read magic number */ n_read = fread (magic, 1, sizeof (magic), fp); if (n_read != sizeof (magic)) { fclose (fp); LOAD_ERROR (context, "can't read PNG magic number"); } /* Check for valid magic number */ if (png_sig_cmp (magic, 0, sizeof (magic))) { fclose (fp); LOAD_ERROR (context, "not a valid PNG image"); } png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (png_ptr == NULL) { fclose (fp); LOAD_ERROR (context, "failed to initialize the png_struct"); } png_set_error_fn (png_ptr, NULL, NULL, user_warning_fn); info_ptr = png_create_info_struct (png_ptr); if (info_ptr == NULL) { fclose (fp); png_destroy_read_struct (&png_ptr, png_infopp_NULL, png_infopp_NULL); LOAD_ERROR (context, "failed to initialize the memory for image information"); } png_init_io (png_ptr, fp); png_set_sig_bytes (png_ptr, sizeof (magic)); png_read_info (png_ptr, info_ptr); png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, int_p_NULL, int_p_NULL); if (color_type != PNG_COLOR_TYPE_RGB) { fclose (fp); png_destroy_read_struct (&png_ptr, png_infopp_NULL, png_infopp_NULL); LOAD_ERROR (context, "color type is not rgb"); } raw_data = (guchar *) malloc (sizeof (guchar) * width * height * 3); rows = (guchar **) malloc (sizeof (guchar *) * height); for (y = 0; y < height; ++y) rows[y] = (guchar *) (raw_data + y * width * 3); png_read_image (png_ptr, rows); free (rows); png_read_end (png_ptr, info_ptr); png_destroy_read_struct (&png_ptr, &info_ptr, png_infopp_NULL); fclose (fp); /* END load png image file */ bumper->bumpmap_width = width; bumper->bumpmap_height = height; gl->GenTextures (1, &bumper->bumpmap); gl->BindTexture (GL_TEXTURE_2D, bumper->bumpmap); gl->TexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, bumper->bumpmap_width, bumper->bumpmap_height, 0, GL_RGB, GL_UNSIGNED_BYTE, raw_data); gl->TexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); gl->TexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); gl->TexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); gl->TexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); free (raw_data); }