Code archives/Graphics/Bitmaps in DefData statements

This code has been declared by its author to be Public Domain code.

Download source code

Bitmaps in DefData statements by matibee2011
This contribution provides utility and code for converting bitmaps into DefData statements which you can embed into your forum posts. This makes it possible to post your code as text-only without relying on external image hosting.

** DISCLAIMER ** This is only really suitable for small graphic files such as sprites and tiles due to the nature and size of the DefData statments.

The code below is for a drag and drop utility that creates the DefData statements from the target image. Notice how the first line is a label that reflects the bitmap filename. This is used to locate the correct data when using multiple embedded images.

Here is a quick sample from which you'll need to copy the DataToImage function.

SuperStrict
Import MaxGui.Drivers

Global window:TGadget = CreateWindow:TGadget("Blitzmax image to data utility.  Drag file below...",60,60,320,320,Null,WINDOW_TITLEBAR|WINDOW_RESIZABLE|WINDOW_CLIENTCOORDS|WINDOW_ACCEPTFILES)
Global te:TGadget = CreateTextArea:TGadget( 10, 10, 300, 300, window:TGadget )
SetGadgetLayout( te, EDGE_ALIGNED, EDGE_ALIGNED, EDGE_ALIGNED, EDGE_ALIGNED )

Repeat
	WaitEvent()
	Select EventID()
		Case EVENT_WINDOWACCEPT
			Local file$ = EventExtra().tostring()
			Print file$
			Local image:TImage = LoadImage(file)
			If image <> Null
				Local txt$ = StripDir( file$ )
				txt$ = Replace( txt$, ".", "_" )
				txt$ = Replace( txt$, " ", "_" )
				txt$ = "#" + txt$ + "~n"
				txt$ :+ "DefData " + ImageWidth( image ) + ", " + ImageHeight( image ) + "~n"
				Local tp:TPixmap = LockImage( image )
				For Local y:Int = 0 To ImageHeight( image ) - 1
					Local rowstring$ = "DefData ~q"
					For Local x:Int = 0 To ImageWidth( image ) - 1
						rowstring :+ Hex$( ReadPixel( tp, x, y ) )
					Next 
					txt$ :+ rowstring + "~q~n"										
				Next 
				UnlockImage( image )
				te.SetText( txt$ )
			Else 
				Notify( "Invalid image file!" )
			End If 				
		Case EVENT_APPTERMINATE
			End
		Case EVENT_WINDOWCLOSE
			Select EventSource()
				Case window End 
			End Select
		End Select
Forever

Comments

dw8172016
I'm always interested in looking at compression algorithms. Have to ask, in the first code, are you using a single hex-character (0-F) to represent a single pixel from a 16-color image ?


AdamStrange2016
Simple answer - NO

complex answer - look at the code data format:
width, height
color data packed alpha x00 red xFF green xFF blue xFF

So there is no compression and no 16 colour image. its a 32 bit alpha

Infant. if you look at the actual code it tells you in very simple terms what it is doing. Reading a line of data , then chunking it into 8 x width bytes (2 bytes per hex number) with is 0..255. it even uses word$ to further enforce what it is doing.
:p


Code Archives Forum