Page 1 of 1

PNG Crop

Posted: 01 Mar 2010 23:37
by coyoteelabs
PNG Crop is a small program designed for the purpose of cropping PNG sprites used in OpenTTD 32bpp.

It offers 2 methods for cropping:
* automatic mode - automatically crops as much as possible from the sprite reducing it to its smallest size possible
(like Visual PNG codec crop tool). Alpha tolerance option is included.
* manual mode - manually specify cropping coordinates (top left corner and width/height)

PNG Crop can also crop the sprite masks using the same values used for the actual sprite.

If the sprites contain x_offs and y_offs, PNG Crop will adjust these values accordingly.
PNG Crop Usage screen (no parameters passed)
PNG Crop Usage screen (no parameters passed)
pngcrop.png (35.14 KiB) Viewed 6976 times
License: GPL v2 or later

Special thanks to Zephyris for the idea and initial testing.

Version 0.3.1 released
* fixed offset detection

Re: PNG Crop

Posted: 02 Mar 2010 00:40
by Zephyris
One minor problem, it seems v0.3 doesn't write the new offset data...

*edit* This also happens when autocrop also makes a change to the size of the sprite.

Re: PNG Crop

Posted: 02 Mar 2010 07:26
by coyoteelabs
Uh, looks like the changes I made to the detection code were causing the program to think that there weren't any offsets. That's why it didn't write them.

Fixed it in 0.3.1. Redownload from 1st post.

Re: PNG Crop

Posted: 02 Mar 2010 11:49
by Zephyris
Checked it on the GRVTS sprites and all seems good, it shaves a good bit of the file size!

Re: PNG Crop

Posted: 05 Mar 2010 12:52
by maquinista
It could help to code de bridges easier and faster. Thanks.

Re: PNG Crop

Posted: 03 May 2010 18:37
by maquinista
PNG crop was very useful to code all latest sprites (papermill, oil wells, coal mines, latest Spain set sprites......) I will use it with airport sprites. But there is a problem: we need to make z1 and z2 sprites, not only z0 sprites. All the l

I suggest a program like PNG crop to make the zoom levels z1 and z2 from a sprite with only z0. My idea is that we need a program to make the z1 and z2 levels from all existing sprites. It would make easier this.

This is the correct way to scale the sprites without problems of subpixel offsets in end images:
http://www.tt-forums.net/viewtopic.php?p=872954#p872954

Maybe, this sourcecode (writen by me) could be useful. You can use it freely

Code: Select all


This function mixes the values of two pixels. Maybe It could be optimized with operations like ">>1" and ">>9" instead of /2 and /512.
void mix_pixels(int redA, int greenA, int blueA, int alphaA, int redB, int greenB, int blueB, int alphaB, int *redF, int *greenF, int *blueF, int *alphaF){
if (alphaA == 0 & alphaB == 0){
 alphaF =0;
}else{
 if (alphaA == 1 & alphaB == 1){
  alphaF =1;
  redF = (redA + redB) /2;
  greenF = (greenA + greenB) /2;
  blueF = (blueA + blueB) /2;
 }else{
  alphaF = (alphaA + alphaB) / 512;
  redF = (redA*alphaA + redB*alphaB) / 512;
  greenF = (greenA*alphaA + greenB*alphaB) / 512;
  blueF = (blueA*alphaA + blueB*alphaB) / 512;
 }
}
I think that the image doesn't need to be scaled in horizontal and vertical directions at the same time. This would make less complex to add the additional lines needed if the offsets are not 0.

It would be nice if the program adds lines to a file called "Output.txt", adding the offsets used in each sprite, and allowing to convert it into a batch file.

Re: PNG Crop

Posted: 05 May 2010 08:40
by Hirundo
When scaling images, this may be an interesting article to read. It turns out that combining pixels may not be as trivial as one would think.
http://www.4p8.com/eric.brasseur/gamma.html

Re: PNG Crop

Posted: 05 May 2010 19:49
by AndersI
Interesting article! Did some tests in PaintShop Pro 7.0, and it indeed does scaling wrong (as most other software).

But, using the simple algorithm:

- apply gamma 0.454545
- resize
- apply gamma 2.2

makes everything look good enough.

Re: PNG Crop

Posted: 07 May 2010 21:33
by Zephyris
Built-in support for automatic resizing be excellent...

Re: PNG Crop

Posted: 07 May 2010 22:58
by maquinista
Zephyris wrote:Built-in support for automatic resizing be excellent...
Now OpenTTD has automatic resizing, but the problem is that It's slow and It doesn't give a great quality, because It only resizes the sprites.

Re: PNG Crop

Posted: 08 May 2010 08:40
by Zephyris
I meant for png crop... lol

Re: PNG Crop

Posted: 08 May 2010 12:34
by maquinista
Zephyris wrote:I meant for png crop... lol
I'm understand.

Yes, It is a great idea.

Re: PNG Crop

Posted: 08 May 2010 21:54
by coyoteelabs
maquinista wrote:PNG crop was very useful to code all latest sprites (papermill, oil wells, coal mines, latest Spain set sprites......) I will use it with airport sprites.
Glad you found it useful.
maquinista wrote:But there is a problem: we need to make z1 and z2 sprites, not only z0 sprites. All the l

I suggest a program like PNG crop to make the zoom levels z1 and z2 from a sprite with only z0. My idea is that we need a program to make the z1 and z2 levels from all existing sprites. It would make easier this.
Interesting idea, I'll see if I can make one (unfortunately i am extremely busy these days so my programming time is limited :| )
Most likely, I'll make it as a separate app as that would require less programming time
maquinista wrote: It would be nice if the program adds lines to a file called "Output.txt", adding the offsets used in each sprite, and allowing to convert it into a batch file.
I'll try to add the logging function this monday if time permits it.

Re: PNG Crop

Posted: 19 May 2010 11:56
by coyoteelabs
As requested, here is PNG Resize