Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
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 1737 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.

1  
ImageTool.create(ImageTool.dir("/res/sprites/"));


You can see the output below, which gives us a couple different ways of encoding our data (indexed color, or simple ImageIO string):
http://www.java-gaming.org/?action=pastebin&id=365

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

1  
2  
3  
4  
5  
//Filenames are transformed into constants
//e.g. "res/sprites/pirate2_attack.png" --> SPRITE_PIRATE2_ATTACK
g.drawImage(SPRITES[SPRITE_PIRATE2_ATTACK],
               50, 50,
               null);


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.
ImageTool.java (excuse the messy code)
ImageToolTest.java (test case, shows proper usage)

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

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
« League of Dukes »

JGO Ninja


Medals: 48
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:

mine:
1.846 bytes

yours:
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: 133
Projects: 4
Exp: 3 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
« League of Dukes »

JGO Ninja


Medals: 48
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
« League of Dukes »

JGO Ninja


Medals: 48
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 Knight


Medals: 12
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: http://pastebin.java-gaming.org/19df7886132

I converted your example 4K applet to use this uncompressed PNG: http://pastebin.java-gaming.org/9df78916237 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 : http://kiwi6.com/file/6relfsr330
UncompressedPN example jarred->jshrink->pack200 : http://kiwi6.com/file/kq499tmh28
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (35 views)
2014-09-24 16:13:29

Pippogeek (28 views)
2014-09-24 16:12:22

Pippogeek (17 views)
2014-09-24 16:12:06

Grunnt (40 views)
2014-09-23 14:38:19

radar3301 (23 views)
2014-09-21 23:33:17

BurntPizza (59 views)
2014-09-21 02:42:18

BurntPizza (29 views)
2014-09-21 01:30:30

moogie (34 views)
2014-09-21 00:26:15

UprightPath (47 views)
2014-09-20 20:14:06

BurntPizza (51 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org 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‑gaming.org
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!