Main Menu

News:

SyntaxBoom, now with pwetty syntax highlighted code boxes! \o/ 

https://www.syntaxboom.com/forum/index.php?topic=96

Shoutbox

Baggey

2025-09-24, 17:57:11
They'll be using Expanding foam to glue bricks together next  :-X

Dabzy

2025-09-24, 06:09:52
You can also get the expanding foam post fix, but, I wouldnt trust it really, especially where I live on the side of a valley and when the storms blow in the right direction, whistling down the valley, nowt is safe!

Baggey

2025-09-23, 08:53:01
That Postcrete stuff is amazing. I never know how much water to add. May be i should read the Instructions  ;D 

Dabzy

2025-09-22, 21:33:46
Cannot beat a breaky uppy mode, saves the hand cramps and chipped knuckles knocking ten bells out of a chod of conc with a hammer and chisel.

GfK

2025-09-22, 21:28:44
I have a massive JCB drill with a concrete breaky uppy mode which has got me out of jail free a couple of times replacing rotted fence posts that has been concreted in.

Amon

2025-09-22, 19:23:30
What about Roly?

Dabzy

2025-09-22, 19:22:35
Putting my 2 deckings in.... I've dug enough post holes to last me a life time... I feel sorry for the future poor sod who may want to shift'em... Like most things I do, I tend to go over the top, and as such, I've probably got shares in postcrete! :D

GfK

2025-09-22, 19:10:57
Round is a shape.

Baggey

2025-09-22, 19:04:49
Consult a qualified electrician for compliance with BS 7671 and local building Regs! Avoid areas where future digging is likely. ;)

Jackdaw

2025-09-22, 18:18:24
That depends on where the cable is to run. Minimum depth in a garden in 450mm. Under pavements 600mm.

Members
  • Total Members: 55
  • Latest: Amon
Stats
  • Total Posts: 1,607
  • Total Topics: 198
  • Online today: 12
  • Online ever: 54 (Sep 14, 2025, 08:48 AM)
Users Online
  • Users: 2
  • Guests: 7
  • Total: 9
  • Baggey
Welcome to SyntaxBoom. Please login or sign up.

Recent

Int To Object

Started by _PJ_, Aug 09, 2025, 08:47 PM

Previous topic - Next topic

_PJ_

(Technically this is NG issue, but I am assuming it is the same in "vanilla" BMax)

So I was wanting to automate the process of building a palette library for a bunch of sprite images (so I can later simplify the images and also do some code-based palette swapping etc)

but I ran into some issues with trying to make a list of the pixel values, as the TList commands refer to Object arguments.

Usually, Strings and whatever can simply recast as "Object" but not Integers it seems:::

Here's a simplified example:
Global Palette:TList

Local Image:TPixmap=LoadPixmap("Test.png")

For Local Y:Int= 0 Until Image.height
	For Local X:Int= 0 Until Image.width
		Local Pixel:Int=ReadPixel(Image,X,Y)
		If ( Not (ExistInPalette(Pixel))) Then AddToPalette(Pixel)
	Next
Next

DebugLog("Found "+Palette.Count()+" colours")


Function ExistInPalette:Byte(Pixel:Int)
	If (Palette=Null) Then Return False
	Return Palette.Contains(Pixel)
End Function

Function AddToPalette(Pixel:Int)
	If (Palette=Null) Then Palette=New TList
	Palette.AddLast(Pixel)
End Function

Working with an array may be easier, iterating through each array item, but I don't know how big the array may need to be in advance...

Sledge

You might be better off with an IntMap rather than a List:

https://blitzmax.org/docs/en/api/brl/brl.map/tintmap/

You could use the Int pixel values as keys (the actual value the key returns wouldn't really matter -- a string representation of the key would be my choice). You can check for a key before adding via Contains(), then at the end of the process get a collection of all the keys with Keys(), which would be your entire palette.

It's a bit of a weird case for a Map because the keys themselves are the values that you care about, but hey ho!

Midimaster

Local Image:TPixmap=LoadPixmap("Test.png")

For Local Y:Int= 0 Until Image.height
	For Local X:Int= 0 Until Image.width
		Local Pixel:Int=ReadPixel(Image,X,Y)
		TPalette.Add Pixel
	Next
Next

Type TPalette
	Global List:TList= New TList
	Field Pixel:Int

	Function Add(Pixel:Int)
		If Contains(Pixel)=True Then Return
		Item:TPalette = New TPalette
		Item.Pixel = Pixel
		List.AddLast Item
	End Function 

	Function Contains:Int(Pixel)
		For Local Item:TPalette = EachIn List
			If Item.Pixel=Pixel then Return True
		Next 
		Return False
	End Function 
End Type

_PJ_

Awesome!
Thanks so much for the qwuick replies :)

@Sledge I am not very familiar with the "Map" types and so was a little intimidated to try and risk overcomplicating and making things a lot worse - but thanks for the explanation - it would certainly be a neater approach!


@Midimaster - Thanks. I guess having the Pixel explicit cast as integer within the type scope and then using a custom iteration "contains" helps the compiler to interpret the Object?

What's funny as I originally wrote a function with similar iteration and equivalence check, but then changed it when I remembered the "contains" method :D

Midimaster

Quote...using a custom iteration "contains" helps the compiler to interpret the Object?...

No it is the Local x:Type EachIn, that picks only the searched type in a list.

If your list contains objects of different types then the code line...

QuoteFor Local Item:TPalette = EachIn List

... already casts and limits the iteration to only one type.

_PJ_

Got it!

Thanks, Midimaster