Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (803)
Games in Android Showcase (237)
games submitted by our members
Games in WIP (867)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  J4K Tools  (Read 6386 times)
0 Members and 1 Guest are viewing this topic.
Offline davedes
« Posted 2013-01-02 21:06:16 »

While prototyping different ideas for J4K, I've written a couple of code generators that some might find useful.

Image Encoding Tool
This tool generates Java code that contains a String-encoded sprite sheet. It generates simple GIF/PNG output for ImageIO, but also includes a slightly more optimal means of encoding strings, especially if you are handling images yourself without BufferedImages.

Below is an input, where our "sprites" folder (in our class-path) contains a series of equal size images. It will compile a color table of all the images, but for the sake of optimization you should try to use the same colors among many sprites.

Keep your total number of colors less than 95 for indexed colors to work.


You can see the output below, which gives us a couple different ways of encoding our data (indexed color, or simple ImageIO string):

You simply copy + paste the generated code (printed to System.out) into your game, and then you can render images like so:

//Filenames are transformed into constants
//e.g. "res/sprites/pirate2_attack.png" --> SPRITE_PIRATE2_ATTACK
               50, 50,

Testing a 128x128 opaque sprite sheet, then JShrink -> pack200:
GIF Encoding: 2,875 bytes
Indexed Color Tables: 2,630 bytes

Testing six 11x9 transparent sprites, then JShrink -> pack200:
PNG Encoding: 1,931 bytes
Indexed Color Tables: 1,816 bytes
Without BufferedImages: 1,689 bytes (i.e. for tiled map data)

The files don't have any dependencies, and each should be self-contained. (excuse the messy code) (test case, shows proper usage)

Here is an example 4K applet template using the resulting output:

I have another little tool I'll post about later. If you've got any 4K tools of your own, post 'em here. Smiley

Offline ReBirth
« Reply #1 - Posted 2013-01-03 11:26:46 »

I don't quite understand. So basically it turns your image file (gif/png) into String?

Offline teletubo
Global Moderator

JGO Wizard

Medals: 76
Projects: 4
Exp: 8 years

« Reply #2 - Posted 2013-01-03 12:28:01 »

Nice little tool!
I tested with my images from my game Plants 4k Zombies (26 16x16 images) it did compact well, altough it didn't beat my method by just a little:

1.846 bytes

1.873 bytes

but your method definately is faster than what I do.

congrats and thanks for the tool!

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jimmt
« League of Dukes »

JGO Kernel

Medals: 167
Projects: 5
Exp: 6 years

« Reply #3 - Posted 2013-01-03 17:14:42 »

I don't quite understand. So basically it turns your image file (gif/png) into String?
With j4k, images are represented as Strings to save space, but it would still take up too much space if we stored the String with all colors as possibilities. So thus we index the colors and make a color table so that in the string, a = blue, b = red, etc. so each pixel only takes one character.
Offline davedes
« Reply #4 - Posted 2013-01-03 17:37:25 »

teletubo - what is your technique if you don't mind sharing?

Offline teletubo
Global Moderator

JGO Wizard

Medals: 76
Projects: 4
Exp: 8 years

« Reply #5 - Posted 2013-01-03 17:57:18 »

I use 16 bits to encode one color, one operation, and a counter
9 bits = R,G,B
1 bit = operation (point or rectangle)
6 bits = number N of pionts/rectangles that will use this color

the following N*16 bits store the points/rectangles:

if point, it will store 2 pixels per 16 bits:
4 bits for the x1 coordinate
4 bits for the y1 coordinate
4 bits for the x2 coordinate
4 bits for the y2 coordinate

if a rectangle, it will store one rectangle per 16 bits:
4 bits for the x1 coordinate
4 bits for the y1 coordinate
4 bits for the width
4 bits for the height

the image must be exactly 16x16, and the color 255,0,255 means transparency.

I made my converter to output one framefor each step it took to build a particular image. Each frame means 2 bytes, and a P means the next operation will be points (of that color), and an R rectangles:

I hoped it would be a great compression method compared to a index color palette and never really had a chance to benchmark it until now, and I must confess I'm a little disappointed to see that the gain is so small Smiley  
It was fun to develop it anyway Smiley


Offline Danny02
« Reply #6 - Posted 2013-01-03 18:41:20 »

@teletubo did you benchmark how big your decompression code is?
Offline davedes
« Reply #7 - Posted 2013-01-03 18:43:24 »

Thanks. Sounds neat. Smiley

My indexed colour technique isn't that fancy, but it compresses well. I've tried some other things, like packing images into a 32-bit int, or encoding only non-transparent pixels, or grouping repeating pixels. Although they all result in smaller strings, they don't compress as well, and require more lines to decode.

Offline teletubo
Global Moderator

JGO Wizard

Medals: 76
Projects: 4
Exp: 8 years

« Reply #8 - Posted 2013-01-03 20:00:43 »

@teletubo did you benchmark how big your decompression code is?

I didn't, but by looking it seems slightly larger.
Anyway for my next 4k games I'll try both methods to see which one yields better results for the given spritesheet.

Offline moogie

JGO Ninja

Medals: 16
Projects: 6
Exp: 10 years

Java games rock!

« Reply #9 - Posted 2013-01-04 23:24:09 »

I have taken another approach for minimising the byte count for storing and reading sprites... I have converted my PNG Sprite sheet into an "Uncompressed" PNG using deflate mode 0, adding it as a resource in the JAR , then using ImageIO to read it in.

The tool can be found here:

I converted your example 4K applet to use this uncompressed PNG: and after jshrink and pack200 it saved a further 8 bytes over your method (but that is noise level so I am not sure that it will win in most/ all circumstances)

I used PNGGauntlet to optimise the PNG before using my tool.

Unmodified example source jarred->jshrink->pack200 :
UncompressedPN example jarred->jshrink->pack200 :

Java4k RIP 2014
Pages: [1]
  ignore  |  Print  

Riven (388 views)
2019-09-04 15:33:17

hadezbladez (5280 views)
2018-11-16 13:46:03

hadezbladez (2204 views)
2018-11-16 13:41:33

hadezbladez (5544 views)
2018-11-16 13:35:35

hadezbladez (1149 views)
2018-11-16 13:32:03

EgonOlsen (4583 views)
2018-06-10 19:43:48

EgonOlsen (5461 views)
2018-06-10 19:43:44

EgonOlsen (3118 views)
2018-06-10 19:43:20

DesertCoockie (4014 views)
2018-05-13 18:23:11

nelsongames (4707 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!