PNG Crop

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

Post Reply
User avatar
coyoteelabs
Engineer
Engineer
Posts: 27
Joined: 07 Aug 2006 09:52
Location: Romania

PNG Crop

Post 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 6203 times
License: GPL v2 or later

Special thanks to Zephyris for the idea and initial testing.

Version 0.3.1 released
* fixed offset detection
Attachments
pngcrop_v0.3.1.zip
PNG Crop 0.3.1 binaries
(261.98 KiB) Downloaded 286 times
pngcrop_source_v0.3.1.zip
Delphi 2010 GPL'd source code
(326.83 KiB) Downloaded 262 times
Last edited by coyoteelabs on 02 Mar 2010 07:25, edited 1 time in total.

Visual PNG codec - Win32 GUI based alternative for PNG codec
PNG crop - Win32 Console based tool for PNG cropping
PNG Resize - Win32 Console based tool for PNG Resizing (z0 -> z1 / z2)
User avatar
Zephyris
Tycoon
Tycoon
Posts: 2890
Joined: 16 May 2007 16:59

Re: PNG Crop

Post 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.
Attachments
pngcropbug.png
pngcropbug.png (40.78 KiB) Viewed 6192 times
User avatar
coyoteelabs
Engineer
Engineer
Posts: 27
Joined: 07 Aug 2006 09:52
Location: Romania

Re: PNG Crop

Post 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.

Visual PNG codec - Win32 GUI based alternative for PNG codec
PNG crop - Win32 Console based tool for PNG cropping
PNG Resize - Win32 Console based tool for PNG Resizing (z0 -> z1 / z2)
User avatar
Zephyris
Tycoon
Tycoon
Posts: 2890
Joined: 16 May 2007 16:59

Re: PNG Crop

Post by Zephyris »

Checked it on the GRVTS sprites and all seems good, it shaves a good bit of the file size!
maquinista
Tycoon
Tycoon
Posts: 1828
Joined: 10 Jul 2006 00:43
Location: Spain

Re: PNG Crop

Post by maquinista »

It could help to code de bridges easier and faster. Thanks.
Sorry if my english is too poor, I want learn it, but it isn't too easy.[/list][/size]
maquinista
Tycoon
Tycoon
Posts: 1828
Joined: 10 Jul 2006 00:43
Location: Spain

Re: PNG Crop

Post 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.
Sorry if my english is too poor, I want learn it, but it isn't too easy.[/list][/size]
Hirundo
Transport Coordinator
Transport Coordinator
Posts: 298
Joined: 27 Jan 2008 13:02

Re: PNG Crop

Post 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
Create your own NewGRF? Check out this tutorial!
User avatar
AndersI
Tycoon
Tycoon
Posts: 1732
Joined: 19 Apr 2004 20:09
Location: Sweden
Contact:

Re: PNG Crop

Post 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.
User avatar
Zephyris
Tycoon
Tycoon
Posts: 2890
Joined: 16 May 2007 16:59

Re: PNG Crop

Post by Zephyris »

Built-in support for automatic resizing be excellent...
maquinista
Tycoon
Tycoon
Posts: 1828
Joined: 10 Jul 2006 00:43
Location: Spain

Re: PNG Crop

Post 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.
Sorry if my english is too poor, I want learn it, but it isn't too easy.[/list][/size]
User avatar
Zephyris
Tycoon
Tycoon
Posts: 2890
Joined: 16 May 2007 16:59

Re: PNG Crop

Post by Zephyris »

I meant for png crop... lol
maquinista
Tycoon
Tycoon
Posts: 1828
Joined: 10 Jul 2006 00:43
Location: Spain

Re: PNG Crop

Post by maquinista »

Zephyris wrote:I meant for png crop... lol
I'm understand.

Yes, It is a great idea.
Sorry if my english is too poor, I want learn it, but it isn't too easy.[/list][/size]
User avatar
coyoteelabs
Engineer
Engineer
Posts: 27
Joined: 07 Aug 2006 09:52
Location: Romania

Re: PNG Crop

Post 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.

Visual PNG codec - Win32 GUI based alternative for PNG codec
PNG crop - Win32 Console based tool for PNG cropping
PNG Resize - Win32 Console based tool for PNG Resizing (z0 -> z1 / z2)
User avatar
coyoteelabs
Engineer
Engineer
Posts: 27
Joined: 07 Aug 2006 09:52
Location: Romania

Re: PNG Crop

Post by coyoteelabs »

As requested, here is PNG Resize

Visual PNG codec - Win32 GUI based alternative for PNG codec
PNG crop - Win32 Console based tool for PNG cropping
PNG Resize - Win32 Console based tool for PNG Resizing (z0 -> z1 / z2)
Post Reply

Return to “Graphics Development”

Who is online

Users browsing this forum: Google Adsense [Bot] and 82 guests