Ottenere alcune informazioni su un file immagine PNG con le funzioni del API di libpng

Da Gambas-it.org - Wikipedia.

La libreria Libpng consente anche di ottenere alcune informazioni relative a un'immagine di formato PNG.

E' necessario avere installata nel sistema e richiamare nel programma Gambas la libreria condivisa: "libpng.so "

Mostriamo un semplice esempio pratico:

Library "libpng"

Private tipo_colore As String[] = ["PNG_COLOR_TYPE_GRAY", "PNG_COLOR_MASK_PALETTE", "PNG_COLOR_TYPE_RGB",
                                   "PNG_COLOR_TYPE_PALETTE", "PNG_COLOR_TYPE_GRAY_ALPHA", Null, "PNG_COLOR_TYPE_RGBA"]

' png_structp png_create_read_struct (png_const_charp user_png_ver, png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr warn_fn)
' Allocate and initialize png_ptr struct for reading, and any other memory.
Private Extern png_create_read_struct(user_png_ver As String, error_ptr As Pointer, error_fn As Pointer, warn_fn As Pointer) As Pointer

' png_infop png_create_info_struct (png_const_structrp png_ptr)
' Allocate and initialize the info structure.
Private Extern png_create_info_struct(png_ptr As Pointer) As Pointer

' void png_init_io (png_structrp png_ptr, png_FILE_p fp)
' Initialize the input/output for the PNG file to the default functions.
Private Extern png_init_io(png_ptr As Pointer, fp As Pointer)

' void png_read_info (png_structrp png_ptr, png_inforp info_ptr)
' Read the information before the actual image data.
Private Extern png_read_info(png_ptr As Pointer, info_ptr As Pointer)

' png_uint_32 png_get_image_width (png_const_structrp png_ptr, png_const_inforp info_ptr)
' Returns image width in pixels.
Private Extern png_get_image_width(png_ptr As Pointer, info_ptr As Pointer) As Integer

' png_uint_32 png_get_image_height (png_const_structrp png_ptr, png_const_inforp info_ptr)
' Returns image height in pixels.
Private Extern png_get_image_height(png_ptr As Pointer, info_ptr As Pointer) As Integer

' png_byte png_get_color_type (png_const_structrp png_ptr, png_const_inforp info_ptr)
' Returns image color_type.
Private Extern png_get_color_type(png_ptr As Pointer, info_ptr As Pointer) As Byte

' png_byte png_get_bit_depth (png_const_structrp png_ptr, png_const_inforp info_ptr)
' Returns image bit_depth.
Private Extern png_get_bit_depth(png_ptr As Pointer, info_ptr As Pointer) As Byte

' png_uint_32 png_get_x_pixels_per_inch (png_const_structrp png_ptr, png_const_inforp info_ptr)
Private Extern png_get_x_pixels_per_inch(png_ptr As Pointer, info_ptr As Pointer) As Integer

' png_uint_32 png_get_y_pixels_per_inch (png_const_structrp png_ptr, png_const_inforp info_ptr)
Private Extern png_get_y_pixels_per_inch(png_ptr As Pointer, info_ptr As Pointer) As Integer

' void png_destroy_read_struct (png_structpp png_ptr_ptr, png_infopp info_ptr_ptr))
' Free any memory associated with the png_struct and the png_info_structs.
Private Extern png_destroy_read_struct(png_ptr As Pointer, info_ptr As Pointer)


Library "libc:6"

' FILE *fopen (const char *__restrict __filename, const char *__restrict __modes)
' Open a file and create a new stream for it.
Private Extern fopen(__filename As String, __modes As String) As Pointer

' int fclose (FILE *__stream)
' Close STREAM.
Private Extern fclose(__stream As Pointer) As Integer


Public Sub Main()
 
 Dim versione, filepng As String
 Dim png, info, fp As Pointer
 Dim w, h, xp, yp As Integer
 Dim col, bit As Byte
 
 filepng = "/percorso/del/file.png"
 Print "File immagine PNG: "; filepng
 
' Individua la versione corrente della libreria "libpng":
 versione = Scan(File.Load("/usr/include/libpng16/png.h"), "*PNG_LIBPNG_VER_STRING [\"]*[\"]*")[1]
 
 png = png_create_read_struct(versione, 0, 0, 0)
 If png == 0 Then Error.Raise("Errore !")
 
 info = png_create_info_struct(png)
 If info =0 0 Then Error.Raise("Errore !")
 
 fp = fopen(filepng, "rb")
 If fp == 0 Then Error.Raise("Errore !")
 
 png_init_io(png, fp)
 png_read_info(png, info)
 
' Ottiene la larghezza dell'immgine png caricata:
 w = png_get_image_width(png, info)
' Ottiene l'altezza dell'immgine png caricata:
 h = png_get_image_height(png, info)
 Print "Dimensioni:        "; w; "x"; h; " pixel"
' Ottiene il tipo di colore:
 col = png_get_color_type(png, info)
 Print "Tipo colore:       "; tipo_colore[col]
' Ottiene la profondità di risoluzione del colore:
 bit = png_get_bit_depth(png, info)
 Print "Profondità:        "; bit; " bit"
' Ottiene il numero di pixel per pollice:
 xp = png_get_x_pixels_per_inch(png, info)
 yp = png_get_y_pixels_per_inch(png, info)
 Print "Pixel per pollice: "; xp; "x"; yp; " pixel"
 
' Al termine libera la memoria precedentemente allocata:
 fclose(fp)
 png_destroy_read_struct(png, info)
  
End


Riferimenti