I'm based on this code (https://github.com/freebasic/fbc/blob/m ... ng/png.bas) for have an PNG loader example, and i've strange problem when I execute this.
************************************************************
Please ignore this big DEBUG lines :
Code: Select all
IF CPCDOS_INSTANCE.SYSTEME_INSTANCE.get_DBG_DEBUG() > 0 Then
IF CPCDOS_INSTANCE.Utilisateur_Langage = 0 Then
DEBUG("[SYSTEME] Chargement d'une image PNG '" & Fichier & "' ...", CPCDOS_INSTANCE.DEBUG_INSTANCE.Ecran, CPCDOS_INSTANCE.DEBUG_INSTANCE.NonLog, CPCDOS_INSTANCE.DEBUG_INSTANCE.Couleur_ACTION, 0, CPCDOS_INSTANCE.DEBUG_INSTANCE.CRLF, CPCDOS_INSTANCE.DEBUG_INSTANCE.AvecDate, CPCDOS_INSTANCE.DEBUG_INSTANCE.SIGN_CPCDOS, RetourVAR_PNG)
Else
DEBUG("[SYSTEME] Loading PNG image '" & Fichier & "' ...", CPCDOS_INSTANCE.DEBUG_INSTANCE.Ecran, CPCDOS_INSTANCE.DEBUG_INSTANCE.NonLog, CPCDOS_INSTANCE.DEBUG_INSTANCE.Couleur_ACTION, 0, CPCDOS_INSTANCE.DEBUG_INSTANCE.CRLF, CPCDOS_INSTANCE.DEBUG_INSTANCE.AvecDate, CPCDOS_INSTANCE.DEBUG_INSTANCE.SIGN_CPCDOS, RetourVAR_PNG)
End If
End if
Code: Select all
This is my modified code :
function load_PNG(byref Fichier as String, Bits as integer) as any ptr
dim RetourVAR_PNG as String = ""
Dim PNG_Largeur as integer
Dim PNG_Hauteur as integer
Dim CanalAlphaPresent as integer = 0
Dim TestR as integer
Dim TestV as integer
Dim TestB as integer
Dim TestA as integer
Dim pitch as integer
dim as integer largeur, hauteur, profondeurbits, canaux, profondeur, typecouleur, rowbytes, y, i
dim as ubyte header(0 to 7)
dim imgPNG as byte ptr
Dim dst as ubyte ptr
dim as ubyte ptr src
dim as FILE ptr fp = fopen(Fichier, "rb" )
Dim Message_erreur as string
Dim RetourVAR as string
IF CPCDOS_INSTANCE.SYSTEME_INSTANCE.get_DBG_DEBUG() > 0 Then
IF CPCDOS_INSTANCE.Utilisateur_Langage = 0 Then
DEBUG("[SYSTEME] Chargement d'une image PNG '" & Fichier & "' ...", CPCDOS_INSTANCE.DEBUG_INSTANCE.Ecran, CPCDOS_INSTANCE.DEBUG_INSTANCE.NonLog, CPCDOS_INSTANCE.DEBUG_INSTANCE.Couleur_ACTION, 0, CPCDOS_INSTANCE.DEBUG_INSTANCE.CRLF, CPCDOS_INSTANCE.DEBUG_INSTANCE.AvecDate, CPCDOS_INSTANCE.DEBUG_INSTANCE.SIGN_CPCDOS, RetourVAR_PNG)
Else
DEBUG("[SYSTEME] Loading PNG image '" & Fichier & "' ...", CPCDOS_INSTANCE.DEBUG_INSTANCE.Ecran, CPCDOS_INSTANCE.DEBUG_INSTANCE.NonLog, CPCDOS_INSTANCE.DEBUG_INSTANCE.Couleur_ACTION, 0, CPCDOS_INSTANCE.DEBUG_INSTANCE.CRLF, CPCDOS_INSTANCE.DEBUG_INSTANCE.AvecDate, CPCDOS_INSTANCE.DEBUG_INSTANCE.SIGN_CPCDOS, RetourVAR_PNG)
End If
End if
if( fp = NULL ) then
' Fichier introuvable !
Message_erreur = ERRAVT("ERR_015", 0)
DEBUG("[SYSTEME] " & Message_erreur & " " & chr$(34) & Fichier & chr$(34) & ".", CPCDOS_INSTANCE.DEBUG_INSTANCE.Ecran, CPCDOS_INSTANCE.DEBUG_INSTANCE.NonLog, CPCDOS_INSTANCE.DEBUG_INSTANCE.Couleur_AVERTISSEMENT, 0, CPCDOS_INSTANCE.DEBUG_INSTANCE.CRLF, CPCDOS_INSTANCE.DEBUG_INSTANCE.AvecDate, CPCDOS_INSTANCE.DEBUG_INSTANCE.SIGN_AFF, RetourVAR_PNG)
Exit function
end if
if( fread( @header(0), 1, 8, fp ) <> 8 ) then ' 17-02-2017 : A revoir sur la fonction fread() pour utiliser celui de CPinti Core
Message_erreur = ERRAVT("AVT_043", 0)
DEBUG("[SYSTEME] " & Message_erreur & " " & chr$(34) & Fichier & chr$(34) & ".", CPCDOS_INSTANCE.DEBUG_INSTANCE.Ecran, CPCDOS_INSTANCE.DEBUG_INSTANCE.NonLog, CPCDOS_INSTANCE.DEBUG_INSTANCE.Couleur_AVERTISSEMENT, 0, CPCDOS_INSTANCE.DEBUG_INSTANCE.CRLF, CPCDOS_INSTANCE.DEBUG_INSTANCE.AvecDate, CPCDOS_INSTANCE.DEBUG_INSTANCE.SIGN_AFF, RetourVAR_PNG)
fclose(fp)
Exit function
end if
if(png_sig_cmp( @header(0), 0, 8 )) then
Message_erreur = ERRAVT("AVT_044", 0)
DEBUG("[SYSTEME] " & Message_erreur & " " & chr$(34) & Fichier & chr$(34) & ".", CPCDOS_INSTANCE.DEBUG_INSTANCE.Ecran, CPCDOS_INSTANCE.DEBUG_INSTANCE.NonLog, CPCDOS_INSTANCE.DEBUG_INSTANCE.Couleur_AVERTISSEMENT, 0, CPCDOS_INSTANCE.DEBUG_INSTANCE.CRLF, CPCDOS_INSTANCE.DEBUG_INSTANCE.AvecDate, CPCDOS_INSTANCE.DEBUG_INSTANCE.SIGN_AFF, RetourVAR_PNG)
fclose(fp)
Exit function
end if
dim as png_structp png = png_create_read_struct( PNG_LIBPNG_VER_STRING, NULL, @libpng_error_callback, NULL )
if(png = NULL) then
Message_erreur = ERRAVT("AVT_045", 0)
DEBUG("[SYSTEME] " & Message_erreur & " " & chr$(34) & Fichier & chr$(34) & ".", CPCDOS_INSTANCE.DEBUG_INSTANCE.Ecran, CPCDOS_INSTANCE.DEBUG_INSTANCE.NonLog, CPCDOS_INSTANCE.DEBUG_INSTANCE.Couleur_AVERTISSEMENT, 0, CPCDOS_INSTANCE.DEBUG_INSTANCE.CRLF, CPCDOS_INSTANCE.DEBUG_INSTANCE.AvecDate, CPCDOS_INSTANCE.DEBUG_INSTANCE.SIGN_AFF, RetourVAR_PNG)
fclose(fp)
Exit function
end if
dim info as png_infop = png_create_info_struct(png)
if(info = NULL) then
Message_erreur = ERRAVT("AVT_046", 0)
DEBUG("[SYSTEME] " & Message_erreur & " " & chr$(34) & Fichier & chr$(34) & ".", CPCDOS_INSTANCE.DEBUG_INSTANCE.Ecran, CPCDOS_INSTANCE.DEBUG_INSTANCE.NonLog, CPCDOS_INSTANCE.DEBUG_INSTANCE.Couleur_AVERTISSEMENT, 0, CPCDOS_INSTANCE.DEBUG_INSTANCE.CRLF, CPCDOS_INSTANCE.DEBUG_INSTANCE.AvecDate, CPCDOS_INSTANCE.DEBUG_INSTANCE.SIGN_AFF, RetourVAR_PNG)
fclose(fp)
Exit function
end if
png_init_io( png, fp )
png_set_sig_bytes( png, 8 )
png_read_info( png, info )
largeur = png_get_image_width( png, info )
hauteur = png_get_image_height( png, info )
profondeurbits = png_get_bit_depth( png, info )
canaux = png_get_channels( png, info )
profondeur = profondeurbits * canaux
typecouleur = png_get_color_type( png, info )
select case( typecouleur )
case PNG_COLOR_TYPE_RGB
IF CPCDOS_INSTANCE.SYSTEME_INSTANCE.get_DBG_DEBUG() > 0 Then
IF CPCDOS_INSTANCE.Utilisateur_Langage = 0 Then
DEBUG("[SYSTEME] Information du PNG charg‚ RVB r‚solution:" & largeur & "x" & hauteur & "x" & profondeur& " profondeur:" & profondeurbits & " canal:" & canaux, CPCDOS_INSTANCE.DEBUG_INSTANCE.Ecran, CPCDOS_INSTANCE.DEBUG_INSTANCE.NonLog, CPCDOS_INSTANCE.DEBUG_INSTANCE.Couleur_VALIDATION, 0, CPCDOS_INSTANCE.DEBUG_INSTANCE.CRLF, CPCDOS_INSTANCE.DEBUG_INSTANCE.AvecDate, CPCDOS_INSTANCE.DEBUG_INSTANCE.SIGN_CPCDOS, RetourVAR_PNG)
Else
DEBUG("[SYSTEM] PNG info. RGB Size" & largeur & "x" & hauteur & "x" & profondeur& " depth:" & profondeurbits & " canal:" & canaux, CPCDOS_INSTANCE.DEBUG_INSTANCE.Ecran, CPCDOS_INSTANCE.DEBUG_INSTANCE.NonLog, CPCDOS_INSTANCE.DEBUG_INSTANCE.Couleur_VALIDATION, 0, CPCDOS_INSTANCE.DEBUG_INSTANCE.CRLF, CPCDOS_INSTANCE.DEBUG_INSTANCE.AvecDate, CPCDOS_INSTANCE.DEBUG_INSTANCE.SIGN_CPCDOS, RetourVAR_PNG)
End If
End if
case PNG_COLOR_TYPE_RGB_ALPHA
IF CPCDOS_INSTANCE.SYSTEME_INSTANCE.get_DBG_DEBUG() > 0 Then
IF CPCDOS_INSTANCE.Utilisateur_Langage = 0 Then
DEBUG("[SYSTEME] Information du PNG charg‚ RVBA r‚solution:" & largeur & "x" & hauteur & "x" & profondeur& " profondeur:" & profondeurbits & " canal:" & canaux, CPCDOS_INSTANCE.DEBUG_INSTANCE.Ecran, CPCDOS_INSTANCE.DEBUG_INSTANCE.NonLog, CPCDOS_INSTANCE.DEBUG_INSTANCE.Couleur_VALIDATION, 0, CPCDOS_INSTANCE.DEBUG_INSTANCE.CRLF, CPCDOS_INSTANCE.DEBUG_INSTANCE.AvecDate, CPCDOS_INSTANCE.DEBUG_INSTANCE.SIGN_CPCDOS, RetourVAR_PNG)
Else
DEBUG("[SYSTEM] PNG info. RGBA Size" & largeur & "x" & hauteur & "x" & profondeur& " depth:" & profondeurbits & " canal:" & canaux, CPCDOS_INSTANCE.DEBUG_INSTANCE.Ecran, CPCDOS_INSTANCE.DEBUG_INSTANCE.NonLog, CPCDOS_INSTANCE.DEBUG_INSTANCE.Couleur_VALIDATION, 0, CPCDOS_INSTANCE.DEBUG_INSTANCE.CRLF, CPCDOS_INSTANCE.DEBUG_INSTANCE.AvecDate, CPCDOS_INSTANCE.DEBUG_INSTANCE.SIGN_CPCDOS, RetourVAR_PNG)
End If
End if
case PNG_COLOR_TYPE_GRAY
IF CPCDOS_INSTANCE.SYSTEME_INSTANCE.get_DBG_DEBUG() > 0 Then
IF CPCDOS_INSTANCE.Utilisateur_Langage = 0 Then
DEBUG("[SYSTEME] Information du PNG charg‚ Nuances de gris r‚solution:" & largeur & "x" & hauteur & "x" & profondeur& " profondeur:" & profondeurbits & " canal:" & canaux, CPCDOS_INSTANCE.DEBUG_INSTANCE.Ecran, CPCDOS_INSTANCE.DEBUG_INSTANCE.NonLog, CPCDOS_INSTANCE.DEBUG_INSTANCE.Couleur_VALIDATION, 0, CPCDOS_INSTANCE.DEBUG_INSTANCE.CRLF, CPCDOS_INSTANCE.DEBUG_INSTANCE.AvecDate, CPCDOS_INSTANCE.DEBUG_INSTANCE.SIGN_CPCDOS, RetourVAR_PNG)
Else
DEBUG("[SYSTEM] PNG info. Grade of grey Size" & largeur & "x" & hauteur & "x" & profondeur& " depth:" & profondeurbits & " canal:" & canaux, CPCDOS_INSTANCE.DEBUG_INSTANCE.Ecran, CPCDOS_INSTANCE.DEBUG_INSTANCE.NonLog, CPCDOS_INSTANCE.DEBUG_INSTANCE.Couleur_VALIDATION, 0, CPCDOS_INSTANCE.DEBUG_INSTANCE.CRLF, CPCDOS_INSTANCE.DEBUG_INSTANCE.AvecDate, CPCDOS_INSTANCE.DEBUG_INSTANCE.SIGN_CPCDOS, RetourVAR_PNG)
End If
End if
case else
IF CPCDOS_INSTANCE.SYSTEME_INSTANCE.get_DBG_DEBUG() > 0 Then
IF CPCDOS_INSTANCE.Utilisateur_Langage = 0 Then
DEBUG("[SYSTEME] Format de couleurs non support‚s", CPCDOS_INSTANCE.DEBUG_INSTANCE.Ecran, CPCDOS_INSTANCE.DEBUG_INSTANCE.NonLog, CPCDOS_INSTANCE.DEBUG_INSTANCE.Couleur_AVERTISSEMENT, 0, CPCDOS_INSTANCE.DEBUG_INSTANCE.CRLF, CPCDOS_INSTANCE.DEBUG_INSTANCE.AvecDate, CPCDOS_INSTANCE.DEBUG_INSTANCE.SIGN_AFF, RetourVAR_PNG)
Else
DEBUG("[SYSTEM] Color format not supported", CPCDOS_INSTANCE.DEBUG_INSTANCE.Ecran, CPCDOS_INSTANCE.DEBUG_INSTANCE.NonLog, CPCDOS_INSTANCE.DEBUG_INSTANCE.Couleur_AVERTISSEMENT, 0, CPCDOS_INSTANCE.DEBUG_INSTANCE.CRLF, CPCDOS_INSTANCE.DEBUG_INSTANCE.AvecDate, CPCDOS_INSTANCE.DEBUG_INSTANCE.SIGN_AFF, RetourVAR_PNG)
End If
End if
exit function
end select
PNG_Largeur = largeur
PNG_Hauteur = hauteur
imgPNG = imagecreate( largeur, hauteur )
imageinfo imgPNG, , , , pitch, dst
' dim as ubyte ptr dst = cptr( ubyte ptr, img + 1 )
png_set_interlace_handling( png )
png_read_update_info( png, info )
rowbytes = png_get_rowbytes( png, info )
src = callocate( rowbytes )
IF CPCDOS_INSTANCE.Utilisateur_Langage = 0 Then
DEBUG(" PTR source 0x" & hex(src, 8) & " " & rowbytes & " octets" , CPCDOS_INSTANCE.DEBUG_INSTANCE.Ecran, CPCDOS_INSTANCE.DEBUG_INSTANCE.NonLog, CPCDOS_INSTANCE.DEBUG_INSTANCE.Couleur_NORMAL, 0, CPCDOS_INSTANCE.DEBUG_INSTANCE.CRLF, CPCDOS_INSTANCE.DEBUG_INSTANCE.AvecDate, CPCDOS_INSTANCE.DEBUG_INSTANCE.SIGN_CPCDOS, RetourVAR_PNG)
Else
DEBUG(" Source PTR 0x" & hex(src, 8) & " " & rowbytes & " bytes" , CPCDOS_INSTANCE.DEBUG_INSTANCE.Ecran, CPCDOS_INSTANCE.DEBUG_INSTANCE.NonLog, CPCDOS_INSTANCE.DEBUG_INSTANCE.Couleur_NORMAL, 0, CPCDOS_INSTANCE.DEBUG_INSTANCE.CRLF, CPCDOS_INSTANCE.DEBUG_INSTANCE.AvecDate, CPCDOS_INSTANCE.DEBUG_INSTANCE.SIGN_CPCDOS, RetourVAR_PNG)
End if
print "**** sleep"
sleep 1000
print "**** Continue with --> PNG:" & hex(png, 8) & " SRC:" & hex(src, 8) & "."
for y = 0 to hauteur-1
' PassageDoEvents = PassageDoEvents + 1
' IF PassageDoEvents > prio then
' CPCdoevents(1) ' On libere un coup le CPU
' PassageDoEvents = 0
' End if
print "**** P1"
png_read_row( png, src, NULL )
print "**** P2"
select case( typecouleur )
case PNG_COLOR_TYPE_RGB
print "**** P3"
imageconvertrow( src, 24, dst, Bits, largeur )
print "**** P4"
dst += pitch
case PNG_COLOR_TYPE_RGB_ALPHA
select case( Bits )
case 24, 32
print "P5"
for i = 0 to rowbytes-1 step 4
dst[0] = src[i+2]
dst[1] = src[i+1]
dst[2] = src[i+0]
dst[3] = src[i+3]
If dst[0] = 255 THEN
If dst[1] = 255 THEN
If dst[2] = 255 THEN
If dst[3] = 0 THEN
dst[0] = 255 ' Rouge
dst[1] = 0 ' Vert
dst[2] = 255 ' Bleu
dst[3] = 0 ' Alpha
CanalAlphaPresent = 1
END IF
END IF
END IF
END IF
dst += 4
next
case 15, 16
print "P6"
For i = 0 to rowbytes-1 step 4
TestR = src[i+2]
TestV = src[i+1]
TestB = src[i+0]
TestA = src[i+3]
If TestR = 255 THEN
If TestV = 255 THEN
If TestB = 255 THEN
If TestA = 0 THEN
src[i+2] = 255 ' Rouge
src[i+1] = 0 ' Vert
src[i+0] = 255 ' Bleu
src[i+3] = 255 ' Alpha
CanalAlphaPresent = 1
END IF
END IF
END IF
END IF
next
'' Alpha non supporte
print "P4"
imageconvertrow( src, 32, dst, Bits, largeur )
dst += pitch
end select
case PNG_COLOR_TYPE_GRAY
select case( Bits )
case 24, 32
for i = 0 to rowbytes-1
*cptr( ulong ptr, dst ) = rgb( src[i], src[i], src[i] )
dst += 4
next
case 15, 16
For i = 0 to rowbytes-1 step 4
TestR = src[i+2]
TestV = src[i+1]
TestB = src[i+0]
TestA = src[i+3]
If TestR = 255 THEN
If TestV = 255 THEN
If TestB = 255 THEN
If TestA = 0 THEN
src[i+2] = 255 ' Rouge
src[i+1] = 0 ' Vert
src[i+0] = 255 ' Bleu
src[i+3] = 255 ' Alpha
END IF
END IF
END IF
END IF
dst += 4
next
for i = 0 to rowbytes-1
pset imgPNG, (i, y), rgb( src[i], src[i], src[i] )
next
case else
memcpy( dst, src, rowbytes )
dst += pitch
end select
end select
next
toutfermerPNG:
deallocate( src )
png_read_end( png, info )
png_destroy_read_struct( @png, @info, 0 )
fclose( fp )
print "AFFIIICHHER !!!!" : sleep
' put (0, 0), imgPNG, Alpha
function = 0
end function
This is a screenshot
If i use a little PNG RGB (16x16) image, he works ! But when i load a 1024x768 RGB PNG.. he crash here
Someone can help me ? :(
Regards