PNGCodec

Discuss, get help with, or post new graphics for TTDPatch and OpenTTD, using the NewGRF system, here. Graphics for plain TTD also acceptable here.

Moderator: Graphics Moderators

TrueBrain
OpenTTD Developer
OpenTTD Developer
Posts: 1370
Joined: 31 May 2004 09:21

PNGCodec

Post by TrueBrain »

What is PNGCodec?

PNGCodec is a small application to add meta-data to PNG files. This is needed for 32bpp, as each image needs a x_offs and y_offs. With this program, you can add that very simple.

WARNING: editing your png in any editing program mostly results in the loss of this meta-data. This is not something we can help, those editing programs are just a bit stupid.

Where can I get it?

http://www.openttd.org/download-pngcodec

Usage?

Run 'pngcodec' and you get a help list. For correct workings of 32bpp in OpenTTD you need to set x_offs and y_offs. All other tags are ignored.

What is this x_offs and y_offs?

When OpenTTD draws a sprite, it does this at the top-center of the tile. Most images do not want to have their top-left corner (0,0) there. So you need to indicate the offset from the top-center of the tile to your top-left corner. For instance, a normal flat landscape tile has a x_offs of -31, indicating that the tile should be drawn 31 pixels more to the left then the top-center of the tile.
Last edited by Jupix on 24 Dec 2011 20:56, edited 4 times in total.
Reason: Deglued
The only thing necessary for the triumph of evil is for good men to do nothing.
Jupix
Chief Executive
Chief Executive
Posts: 683
Joined: 19 Feb 2005 09:08
Location: Finland
Contact:

Post by Jupix »

I took the liberty of copying this post over to the wiki.
#################
User avatar
GeekToo
Tycoon
Tycoon
Posts: 961
Joined: 03 Jun 2007 22:22

Post by GeekToo »

An x-offset of -31 does work correctly, but I was just wondering, why shouldn't it be -32, as would make sense for the center of a 64 px tile?
In other words, is it (width x)/2 - 1 or floor((width x -1)/2)
Rubidium
OpenTTD Developer
OpenTTD Developer
Posts: 3815
Joined: 09 Feb 2006 19:15

Post by Rubidium »

Tiles are 63x31
User avatar
GeekToo
Tycoon
Tycoon
Posts: 961
Joined: 03 Jun 2007 22:22

Post by GeekToo »

Hmm, got it from:
http://blog.openttd.org/?p=15
The 32bpp graphics have to be in the same dimension as the grf graphics. The blitter does no scaling, and it doesn’t know how to handle zoom-in 2x rescaling or anything. So the dimensions for a plain tile has to be 64×31.
Rubidium
OpenTTD Developer
OpenTTD Developer
Posts: 3815
Joined: 09 Feb 2006 19:15

Post by Rubidium »

At least it has to do with the fact that the sprites aren't 64x32; don't know exactly and I'm not in the mood to go funting for it. However, I guess the dark edges have to do something with it.
User avatar
XeryusTC
Tycoon
Tycoon
Posts: 15415
Joined: 02 May 2005 11:05
Skype: XeryusTC
Location: localhost

Post by XeryusTC »

My best guess would be that it is for the same reason as why 2 octaves have 15 notes and not 16, you already counted 1 note (or one pixel row in this case). If you take the height of a tile you need 16 rows of pixels for the upper half but the bottom half only needs 15 rows because you already have the "middle" and the widest part of the tile (sprite wise).
Don't panic - My YouTube channel - Follow me on twitter (@XeryusTC) - Play Tribes: Ascend - Tired of Dropbox? Try SpiderOak (use this link and we both get 1GB extra space)
Image
OpenTTD: manual #openttdcoop: blog | wiki | public server | NewGRF pack | DevZone
Image Image Image Image Image Image Image
User avatar
GeekToo
Tycoon
Tycoon
Posts: 961
Joined: 03 Jun 2007 22:22

Post by GeekToo »

Rubidium wrote:At least it has to do with the fact that the sprites aren't 64x32;
I didn't say that, the 32 was referring to the x-offset
Rubidium wrote: don't know exactly and I'm not in the mood to go funting for it.
I was, and did some more searching:
from the original trg1r nfo:

3981 sprites/trg1r.pcx 162 12968 09 31 64 -31 0

So I suppose the official size is 64 x 31 with an x offset of -31.

This implies my question stays: does anybody know how to calculate the 'correct' offset?

edit: sorry XeryusTC: crossposting.
Does this mean that the tiles overlap?
zombie
Traffic Manager
Traffic Manager
Posts: 153
Joined: 19 May 2005 22:19
Location: Germany

Post by zombie »

Hi.

If you have plain tiles only (PNGs dimensions of exactly 64x31) the offset is always -31/0. If you have a tile with a building or something else (dimension of 64 by 31+x) the offset should be roughly -31/-x. In this case the tile for the sprites has to be at the bottom of the PNG without any frames or empty lines. This "formula" is quite accurate by only +/-1 pixel of deviation for the PNGs I got from Sergej's page.

I guess you always have to try and finetune the offset values.

EDIT: I guess I know why the x_offs is -31. If each sprite is 64 pixel wide the center of the image is between two pixels. Therefore the developers had to choose which pixel to use as a reference and they just chose the left one. If the sprites were 63 pixels wide it would still be -31.

Kind regards

Zombie
User avatar
GeekToo
Tycoon
Tycoon
Posts: 961
Joined: 03 Jun 2007 22:22

Post by GeekToo »

Hi Zombie,
Thanks for the input and the formulas. I'll start experimenting with it.
I guess I know why the x_offs is -31. If each sprite is 64 pixel wide the center of the image is between two pixels.
Correct, but the centre is between pixel 32 and 33. That why I found this kind of odd that the offset is -31.
Sure you can get it right with trial and error, but that takes time, so I was looking for a way to do it 'the first time right'
[/quote]
zombie
Traffic Manager
Traffic Manager
Posts: 153
Joined: 19 May 2005 22:19
Location: Germany

Post by zombie »

Hi.

You are right, the center is between pixels 32 and 33. If x_offs is set to -31 and pixel 32 is the defined center of the sprite you get pixel 1 (center pixel + x_offs). The graphic engine starts painting the sprite at pixel 1 and that's perfect.

EDIT: I just rechecked the PNGs from Sergej I included in OpenTTD and found out that the deviation of +/- 1 pixel is necessary if the sprites use anti aliasing. In this case the ground tiles are a bit higher or lower and I had to fine tune the offset values. If the ground tiles have clear borders according to the original sprites the formula should always work.

Kind regards

Zombie
Last edited by zombie on 26 Jun 2007 21:23, edited 1 time in total.
peter1138
OpenTTD Developer
OpenTTD Developer
Posts: 1659
Joined: 30 Mar 2005 09:43

Post by peter1138 »

Incorrect. The centre is between 31 and 32. That leaves 32 pixels between 0 and 31, and 32 pixels between 32 and 63 (all inclusive).
He's like, some kind of OpenTTD developer.
Rubidium
OpenTTD Developer
OpenTTD Developer
Posts: 3815
Joined: 09 Feb 2006 19:15

Post by Rubidium »

GeekToo wrote:Correct, but the centre is between pixel 32 and 33.
Pixels have 0 as base, i.e. the x pixels are numbered 0..63 when it is 64 pixels wide, so -31 puts pixel 31 in the "center", which would be the 32nd pixel.
zombie
Traffic Manager
Traffic Manager
Posts: 153
Joined: 19 May 2005 22:19
Location: Germany

Post by zombie »

Hi.

I just made a zoomed screenshot of the base landscape tile from the temperate set. The image shows the tile with the grid from MS paint and I added some numbers to make things a bit easier to explain.

When OpenTTD draws the map the engine recognizes pixel 31/0 as the origin of a tile. If you don't provide any offsets the OpenTTD engine paints the sprites starting at this coordinate. This way the sprite does not align to the grid of the map array. The map is not properly rendered.

If you provide proper offsets of -31/0 the engine starts painting the sprite at coordinate 0/0. The sprite is alligned to the grid.

Kind regards

Zombie
Attachments
tile_blowup.png
tile_blowup.png (9.4 KiB) Viewed 35234 times
zombie
Traffic Manager
Traffic Manager
Posts: 153
Joined: 19 May 2005 22:19
Location: Germany

Post by zombie »

Hi again ;)

I just verified the formula against some original sprites from trg1r.grf and the formula is correct for PNGs with a width of 64 pixels:

x_offs = -31
y_offs = png.height - 31

Kind regards

Zombie
Last edited by zombie on 27 Jun 2007 16:41, edited 1 time in total.
User avatar
GeekToo
Tycoon
Tycoon
Posts: 961
Joined: 03 Jun 2007 22:22

Post by GeekToo »

Ok zombie and rubidium. Though this makes sense for a pixel index and not for an offset, it is implemented the way it is, and the formula for calculating the x-offset is probably:

-floor((width x -1)/2)
(for non-C programmers: floor (31.5) would result in 31)
User avatar
athanasios
Tycoon
Tycoon
Posts: 3138
Joined: 23 Jun 2005 00:09
Contact:

Post by athanasios »

Why couldn't we have a nice GUI program to do these calculations by just placing the sprite on a base tile and help many artists use their time in a more productive way?
http://members.fortunecity.com/gamesart
"If no one is a fool I am also a fool." -The TTD maniac.


I prefer to be contacted through PMs. Thanks.
DaleStan
TTDPatch Developer
TTDPatch Developer
Posts: 10285
Joined: 18 Feb 2004 03:06
Contact:

Post by DaleStan »

Because you haven't written it yet, of course.
To get a good answer, ask a Smart Question. Similarly, if you want a bug fixed, write a Useful Bug Report. No TTDPatch crashlog? Then follow directions.
Projects: NFORenum (download) | PlaneSet (Website) | grfcodec (download) | grfdebug.log parser
User avatar
PouncingAnt
Transport Coordinator
Transport Coordinator
Posts: 357
Joined: 09 Nov 2004 22:33

Post by PouncingAnt »

You say png Codec is a program? I just downloaded the version for win32, and my system is pretty sure its a "file" :roll:
is this my laptop, windowsXP, or something wrong with the compile? (of course I couldn't possibly be at fault? :twisted: )
NB: the below challenges are still open for submission, so feel free to perform necromancy on them!
Try the PouncingAnt National Monopoly Challenge
Or even better, the PouncingAnt National Monopoly Challenge 2
Or better still, the PouncingAnt National Monopoly Challenge 3
Or, the PouncingAnt National Monopoly Challenge 4

Or try my scenario instead!

-(A lazy) OpenTTD Japanese Translator-
-(A lazy) PNGcodec user-
"You get what you pay for, so pay attention!"

Patches:
Company Station Stats
User avatar
athanasios
Tycoon
Tycoon
Posts: 3138
Joined: 23 Jun 2005 00:09
Contact:

Post by athanasios »

This shows how correct I was for my previous post. Finally someone got into the trouble to run that program and what a pity: it is broken. :roll:
http://members.fortunecity.com/gamesart
"If no one is a fool I am also a fool." -The TTD maniac.


I prefer to be contacted through PMs. Thanks.
Post Reply

Return to “Graphics Development”

Who is online

Users browsing this forum: Google [Bot] and 8 guests