PNG Image Header-Stripper

Applications, Games, Tools, User libs and useful stuff coded in PureBasic
Carm3D
User
User
Posts: 65
Joined: Mon Feb 17, 2025 10:04 am

PNG Image Header-Stripper

Post by Carm3D »

Hi. Here is a little tool to strip the PNG header data from a PNG image. It will then save it as a .bin file so you can use it in your Pure Basic applications and you won't have to tell anyone you didn't use the Packer library.

After loading the .bin in your program, use some pokes to restore the header

I haven't tested that part out yet but it should look something like this:

Code: Select all

Define PNGHeader.s = Chr(137) + "PNG" + Chr(13) + Chr(10) + Chr(26) + Chr(10) ; PNG header
Use Poke to inject that in the beginning and you should be in business. Anyway here is the stripper program. It also creates a config text file so it can remember the last location you went to. Enjoy!

Code: Select all

EnableExplicit

Global ConfigFile.s = GetPathPart(ProgramFilename()) + "PNG_Header-Stripper_Config.TXT"
Global SourcePath.s, SelectedSource.s

Define.s ImageFile, OutputFile
Define FileSize, *ImageMemory, *StrippedMemory

;******************************************************
;                      Procedures
;******************************************************

Procedure EndProgram()
	; Update Config.TXT with the latest source and destination paths
	If CreateFile(0, ConfigFile)
		; Write SelectedSource if it’s set, otherwise use SourcePath or "0"
		If SelectedSource <> ""
			WriteStringN(0, SelectedSource)
		ElseIf SourcePath <> ""
			WriteStringN(0, SourcePath)
		Else
			WriteStringN(0, "0")
		EndIf
	Else
		ConsoleColor(12, 0)
		PrintN("Warning: Could not update " + ConfigFile)
	EndIf
	ConsoleColor(8, 0)
	PrintN("")
	PrintN("Press RETURN to exit...")
	Input()
	End
EndProcedure

; Open the console for user interaction
OpenConsole()

; Check if Config.TXT exists, create if not
If FileSize(ConfigFile) = -1
	If CreateFile(0, ConfigFile)
		WriteStringN(0, "0")
		CloseFile(0)
	Else
		ConsoleColor(12, 0)
		PrintN("Error: Could not create " + ConfigFile)
		EndProgram()
	EndIf
EndIf

; Read Config.TXT
If ReadFile(0, ConfigFile)
	SourcePath = ReadString(0)
	CloseFile(0)
Else
	ConsoleColor(12, 0)
	PrintN("Error: Could not read " + ConfigFile)
	EndProgram()
EndIf

; Validate source and destination paths
If SourcePath <> "0" And FileSize(SourcePath) <> -2
	SourcePath = "0"
EndIf

Define InitialSource.s = ""
If SourcePath <> "0" ; Set initial value from Multi-Folder_Packer_Config.TXT once
	InitialSource = SourcePath
EndIf

ConsoleColor(14, 0)
PrintN("PNG Header-Stripper by Carm3D v1.1")
PrintN("")
ConsoleColor(8, 0)
PrintN("This little program will load a PNG image,")
PrintN("strip out the PNG header data, then save")
PrintN("it as a binary file for your Pure Basic")
PrintN("projects.  Enjoy!")
PrintN("")


; Open a file requester to select the image
ImageFile = OpenFileRequester("Select a PNG Image", SourcePath, "PNG Files (*.png)|*.png", 0)

If ImageFile <> ""
	OutputFile = ImageFile + ".bin"  ; Save with .bin extension

	; Load the image into memory
	If ReadFile(0, ImageFile)
		FileSize = Lof(0)
		*ImageMemory = AllocateMemory(FileSize)

		If *ImageMemory
			ReadData(0, *ImageMemory, FileSize)
			CloseFile(0)

			; Strip the first 8 bytes (PNG header)
			*StrippedMemory = *ImageMemory + 8
			FileSize - 8

			; Save the stripped binary data
			If CreateFile(1, OutputFile)
				WriteData(1, *StrippedMemory, FileSize)
				CloseFile(1)
				PrintN("Saved " + OutputFile + " without PNG header.")
			Else
				PrintN("Error: Could not create output file.")
				EndProgram()
			EndIf

			FreeMemory(*ImageMemory)
		; Extract the directory from the full path (excluding the filename)
		SelectedSource = GetPathPart(ImageFile)
		Else
			PrintN("Error: Could not allocate memory.")
			EndProgram()
		EndIf
	Else
		PrintN("Error: Could not open " + ImageFile)
		EndProgram()
	EndIf
Else
	PrintN("No file selected. Exiting...")
	EndProgram()
EndIf

EndProgram()
Carm3D
User
User
Posts: 65
Joined: Mon Feb 17, 2025 10:04 am

Re: PNG Image Header-Stripper

Post by Carm3D »

Yay it works! Here is the procedure I made in my destination program to load the stripped file and restore the PNG header. Note that I made ImageNumber a global variable.

Code: Select all

Procedure PNGHeaderRestore(BinFile.s, HeaderData.s)
	; Open the .bin file to read its contents
	If ReadFile(0, BinFile)
		; Get the size of the .bin file (the file without the header)
		Define BinFileSize = Lof(0)

		; Allocate memory for the full PNG file (with the header)
		Define *FullPNGData = AllocateMemory(BinFileSize + 8) ; 8 bytes for PNG header

		If *FullPNGData
			; Read the .bin data (image data without header) into memory
			ReadData(0, *FullPNGData + 8, BinFileSize)  ; Start after the header space

			; Write the PNG header byte-by-byte
			PokeB(*FullPNGData, 137) ; 89
			PokeB(*FullPNGData + 1, 80) ; 50
			PokeB(*FullPNGData + 2, 78) ; 4E
			PokeB(*FullPNGData + 3, 71) ; 47
			PokeB(*FullPNGData + 4, 13) ; 0D
			PokeB(*FullPNGData + 5, 10) ; 0A
			PokeB(*FullPNGData + 6, 26) ; 1A
			PokeB(*FullPNGData + 7, 10) ; 0A

			; Read the .bin data (image data without header) into memory
			ReadData(0, *FullPNGData + 8, BinFileSize)  ; Start after the header space

			; CatchImage
			UsePNGImageEncoder()
			UsePNGImageDecoder()
			ImageNumber = CatchImage(#PB_Any, *FullPNGData, BinFileSize + 8)

			If ImageNumber <> 0
				FreeMemory(*FullPNGData)
				CloseFile(0)
			Else
				MessageRequester("Error", "Fatal error! Failed to create internal image.", #PB_MessageRequester_Error)
			EndIf
		Else
			Debug "Error: Could not allocate memory for the full PNG."
		EndIf
	Else
		Debug "Error: Could not read .bin file."
	EndIf
EndProcedure
Post Reply