Strange crash: WHAT IS "inflate_fast()" ???

DOS specific questions.
Post Reply
Cpcdos
Posts: 207
Joined: Mar 06, 2013 13:52
Location: France - LYON 69003
Contact:

Strange crash: WHAT IS "inflate_fast()" ???

Post by Cpcdos »

Hi everybody!

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
He crash after my print " ***** P1 " , during the png_read_row() execution .. euh... WHY ??
This is a screenshot
Image

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
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: Strange crash: WHAT IS "inflate_fast()" ???

Post by counting_pine »

Sorry, I think I find code written in another tongue difficult to debug..

I think if it fails on larger images, the first thing to do would be to check memory allocations and make sure they succeed.

My guess is that inflate_fast() is an internal zlib function that is decompressing the PNG image data.
Cpcdos
Posts: 207
Joined: Mar 06, 2013 13:52
Location: France - LYON 69003
Contact:

Re: Strange crash: WHAT IS "inflate_fast()" ???

Post by Cpcdos »

Hi, thank you for your reply.

That's what it seemed to me after some brief research.
I also think that there are previous memory allocation problems because, the original PNG loader code crash on the same function .. :(

I'll investigate that!
Thank you :)
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: Strange crash: WHAT IS "inflate_fast()" ???

Post by counting_pine »

If it crashes on the example code, a backtrace might be helpful. It might also be helpful to have an example image that causes the crash.
(If you can get it to crash on a simple image, such as a black rectangle of a given size and colour type, then post the details so we can recreate it.)
Cpcdos
Posts: 207
Joined: Mar 06, 2013 13:52
Location: France - LYON 69003
Contact:

Re: Strange crash: WHAT IS "inflate_fast()" ???

Post by Cpcdos »

Hi ! I've resolved my problem !

I've download the LIBZ library source, after more hours i've conclued that it use the "inflat_fast()" inclued in GCC.
So, i've modified declarations and functions names for use "_inflat_fast()" in the LIBZ ASM code, instead of "inflat_fast()" recompile this, and this works!!

Image

And my PNGs files works! :D
Image

Thank you
Best regards
Post Reply