Using PalEdit to edit palettes

headkaze · 13327

Offline headkaze

  • Administrator
  • Blue Gene Super Computer
  • **********
    • Posts: 7838
on: February 13, 2009, 11:46:30 pm
I spent too much time on this little util as so many issues came up with 8 bit graphics and .NET it was rediculous. First of all the Bitmap.FromImage() method would not load certain 8 bit PNG's so I moved to using the FreeImage library. But then that had a bug with transparency! Luckily I fixed the bug and reported it to the developers who will be patching it for next release. So anyway lets get straight on with PalEdit and how we can use it to effect.

The first demo is to create a very simple effect of sprites flashing white when we shoot their shields. Since were currently using 16 color sprites we have 16 x 16 color palettes at our disposal. So we simply create another palette and switch to it in code.

1. So first thing to do is load up PalEdit and select File->Open and open up Sprites.png. You will see the first 16 colors we use for the sprites.
2. Now select the first 16 colors by holding the mouse and dragging it over the color swatches and select Edit->Copy
3. Select the last 16 color swatches in the pallete and then select Edit->Paste. Now you have a copy of the first 16 colors into the last 16 swatches
4. Now with the last 16 colors still selected drag the brightness slider all the way up to the top to make them brighter
5. Select File->Save As and save it as Sprites.png

Now in code all we have to do to make a sprite flash is to switch it from using color palette 0 to 15.

Changing ATTR2_PALETTE(0) to ATTR2_PALETTE(15) will make the sprite bright giving the effect of being hit. Simple!

There is much more to PalEdit than that though as we use up more shared palettes in the game it will help us manage them. Surely Photoshop should have these features built into it? It would have been nice and saved me a number of hours!

Download PalEdit v1.2
« Last Edit: July 21, 2009, 01:36:16 am by headkaze »



Offline flash

  • Administrator
  • Blue Gene Super Computer
  • **********
    • Posts: 13180
Reply #1 on: February 14, 2009, 12:01:16 am
Sounds like a great tool HK... I will have to try and add this to the code and see how it works. Sounds simple though. This could also be used for many other color effects, so we could have a little bit of fun here.

Great bit of coding (as always) HK

Coding for the love of it!


Offline headkaze

  • Administrator
  • Blue Gene Super Computer
  • **********
    • Posts: 7838
Reply #2 on: February 16, 2009, 05:13:32 am
A new version of PalEdit has been released that adds a few new features, some of which will be demonstrated in the next demo. You can grab the latest version from the first post (PalEdit.zip). There is still a bug with saving gif's with < 256 colors that will hopefully be solved in the next release of the FreeImage library.

For this demo you need PalEdit v1.1 or higher. In this demo were going to demonstrate using PalEdit to make the StarBack.png background a different hue. This is about adding a different visual feel to the background just by simply changing the palette. The background can be different colors for different levels for example. So lets get started.

1. Run PalEdit and select File->Open and open up StarBack.png. Because we will be making visual adjustments select View->Bitmap to get a window with the image in it.
2. You can use the mouse wheel to zoom in and out of the image in the preview window. Find a part of the image that shows the blue planet and star dust
3. Now drag the mouse over the blue hues and use the Hue slider to rotate the blue to a red.
4. The first thing we notice is that a part of the star dust is blue. If we left click on the blue area directly on the bitmap we can see it shows that it's using palette entry 1. We don't want that because we need to keep that blue for other graphics that share this palette.
5. What we have to do now is start from the beginning, reload StarBack.png and select View->Bitmap. Find the same part of the star dust that uses palette entry 1 by left clicking around until it selects it from the palette.
6. Now select a similar blue further down in the palette and right click on the bitmap to change it to use that color. Now select File->Save to save StarBack.png.
7. Now were ready to select those blues again and do a hue rotate until they are red. Notice now there is no blue area in the star dust anymore.
8. Now do a File->Export->X1R5G5B5 .s and save it as StarBackPal2.s
9. Copy the newly created StarBack.png which has the palette entry 2 moved as well as StarBackPal2.s into the data and source folders respectively
10. Change the following code in initvideo.s

Code: [Select]
ldr r0, =StarBackPal2
ldr r1, =BG_PALETTE
ldr r2, =StarBackPalLen
bl dmaCopy
ldr r1, =BG_PALETTE_SUB
bl dmaCopy

You should now have a red StarBack in Warhawk!
« Last Edit: February 16, 2009, 05:50:28 am by headkaze »



Offline headkaze

  • Administrator
  • Blue Gene Super Computer
  • **********
    • Posts: 7838
Reply #3 on: February 16, 2009, 05:14:12 am
And here is a screenshot in Warhawk :)

I've also attached the new StarBack.png and StarBackPal2.s files incase you want to see the two files that were modified in the demo.
« Last Edit: February 16, 2009, 05:49:35 am by headkaze »



Offline flash

  • Administrator
  • Blue Gene Super Computer
  • **********
    • Posts: 13180
Reply #4 on: February 16, 2009, 09:25:32 am
That is a great idea, could we not combine the created palettes into a single starbackpal.s file (eventually) sequentually and use this to select the correct levels palette?

Code: [Select]
ldr r0, =StarBackPal
ldr r3,=level
ldr r3,[r3]
add r0,r3, lsl #11
ldr r1, =BG_PALETTE
ldr r2, =StarBackPalLen
bl dmaCopy
ldr r1, =BG_PALETTE_SUB
bl dmaCopy

Coding for the love of it!


Offline headkaze

  • Administrator
  • Blue Gene Super Computer
  • **********
    • Posts: 7838
Reply #5 on: February 16, 2009, 09:45:49 am
For sure mate infact there are few changes we'll need to make for the different levels as quite a bit is hard coded.

For example we wil need a place to store the current level.

Eg
Code: [Select]
CurrentLevel:
    .word 0

Then we need to change all the Level1 pointers to CurrentLevel and when there is a level change we should have something like

Code: [Select]
cmp LevelNumber, #1
moveq CurrentLevel, Level1
cmp LevelNumver, #2
moveq CurrentLevel, Level2

Or more than likely each level will need it's own initLevel routine, eg. initLevel1, initLevel2 and we can do all the dmacopy's of the new graphics and data etc. there. Perhaps we should just add in another level so we can get the gist of it? I'm happy to get something like that working, just need a new file like initlevel.s and then go from there.



Offline flash

  • Administrator
  • Blue Gene Super Computer
  • **********
    • Posts: 13180
Reply #6 on: February 16, 2009, 10:51:21 am
Yeah, should not be too hard,

I have alraady added "Level" as a variable.

I will have to work out the length of attack wave data to be allocated per level.

Coding for the love of it!


Offline headkaze

  • Administrator
  • Blue Gene Super Computer
  • **********
    • Posts: 7838
Reply #7 on: February 16, 2009, 01:41:01 pm
I found out something interesting today; DISPLAY_BG_EXT_PALETTE

It allows you to use 16 x 256 color palettes instead of 16 x 16 color palettes. So instead of only having 16 colors you have 256 for a total of 4096 colors! Not sure about how much extra VRAM we need though.

Liran's tutorial goes into it a bit more. Anyway something to consider.



Offline flash

  • Administrator
  • Blue Gene Super Computer
  • **********
    • Posts: 13180
Reply #8 on: February 16, 2009, 03:44:48 pm
The extended paletes is something perhaps worth looking into at some stage.

It could be a handy feature, though I am not sure if there is anything we could do with it currently, but I bet we could use it for some great completion effects :)

Coding for the love of it!


Offline flash

  • Administrator
  • Blue Gene Super Computer
  • **********
    • Posts: 13180
Reply #9 on: February 16, 2009, 03:50:35 pm
Sadly, the Palette editor creates an exception upon view bitmap

Code: [Select]
************** Exception Text **************
System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.
   at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
   at System.Drawing.Image.Save(String filename, ImageFormat format)
   at System.Drawing.Image.Save(String filename)
   at PalEdit.frmBitmap..ctor(PaletteControl palControl, PictureBox picMagnify, Color transparentColor)
   at PalEdit.frmMain.mnuBitmap_Click(Object sender, EventArgs e)
   at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
   at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
   at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
   at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
   at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
   at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
   at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
   at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.ToolStrip.WndProc(Message& m)
   at System.Windows.Forms.ToolStripDropDown.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.1434 (REDBITS.050727-1400)
    CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
PalEdit
    Assembly Version: 1.0.0.0
    Win32 Version: 1.0.0.0
    CodeBase: file:///C:/devkitPro/source/nds/PalEdit/PalEdit.exe
----------------------------------------
System.Windows.Forms
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.1434 (REDBITS.050727-1400)
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.1434 (REDBITS.050727-1400)
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.1434 (REDBITS.050727-1400)
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
FreeImageNET
    Assembly Version: 3.11.0.0
    Win32 Version: 3.11.0.0
    CodeBase: file:///C:/devkitPro/source/nds/PalEdit/FreeImageNET.DLL
« Last Edit: February 16, 2009, 10:17:49 pm by Flash »

Coding for the love of it!


Offline headkaze

  • Administrator
  • Blue Gene Super Computer
  • **********
    • Posts: 7838
Reply #10 on: February 17, 2009, 12:35:17 am
That really confused me for a second! I can see in the stack trace it's trying to save but then it shouldn't be doing that. So then I checked out the code and sure enough there was the line bitmap.Save("C:\\Test.gif");. Must have been some code left over when I was having all those problems. So I've removed that line and uploaded it again. Thanks for reporting the issue :)