Giter VIP home page Giter VIP logo

texpack's Introduction

texpack

Simple cross-platform command line texture packer based on the MaxRects packing algorithm by Jukka Jylänki (https://github.com/juj/RectangleBinPack).

There's a Photoshop script port of this tool written in JavaScript. Credits go to Rhody Lugo for that. See here: https://github.com/rraallvv/Layer2SpriteSheet

Usage:

Usage: texpack -o <output-files-prefix> [options...] [<input-file>]

The <input-file> should contain a list of image files (png) separated by new
lines. If no <input-file> is given, it will read from stdin.

Options:

-h, --help            Show this help.
-o, --output          Prefix for the generated files (atlas and json).
-p, --padding         Padding between sprites.
-s, --size            Fixed size for the atlas image (i.e. 512x512).
-S, --max-size        Treat size parameter as maximum size.
-P, --POT             Keep atlas size a power of two (ignored if size is used).
-r, --allow-rotate    Allows sprites to be rotated for better packing.
-m, --metadata        Input metadata file in json format. (*)
-e, --pretty          Generated json file will be human readable.
-t, --trim            Trim input images.
-i, --indentation     Number of spaces for indentation, 0 to use tabs (default).
-u, --premultiplied   Atlas images will have premultiplied alpha.
-b, --alpha-bleeding  Post-process atlas image with an alpha bleeding algorithm.
-M, --mode            Specifies the packing heuristic. Allowed values are:
                        * auto (default; tries all modes and selects one)
                        * bottom-left
                        * short-side
                        * long-side
                        * best-area
                        * contact-point
-f, --format          Specifies the output format of the JSON file. Values are:
                        * legacy (default; uses the original JSON format created by urraka)
                        * jsonhash (Texture Atlas JSON Hash format)
                        * jsonarray (Texture Atlas JSON Array format)
                        * xml (Texture Atlas XML)

(*) The format of the metadata file should be as follows:

    {
      ".global": {"param": "value", ...},
      "someimage.png":    {"param1": "some-value", "param2": 0, ...},
      "anotherimage.png": "not necessarily an object",
      ...
    }

    Each file name should match one in the <input-file> list. The ".global" file
    name is used for global metadata.

Input:

The input file should be a plain text file with each image on a new line. For example, if your file tree looked like this:

assets
│   input.txt
│   ship.png
│
└───ambient
│   │   stars.png
│   │   supernova.png
│   │
│   └───light
│       │   lightspeed.png
│       │   glare.png
│       │   ...
│
└───planets
    │   jupiter.png

Then in your input.txt file you would put this:

ship.png
ambient/stars.png
ambient/supernova.png
ambient/light/lightspeed.png
ambient/light/glare.png
planets/jupiter.png

For ease of use, you can include several numbered files in a single line within the input file. For example, if you had a 12 frame fire animation with the files fire1.png, fire2.png ... fire12.png, instead of writing each file on their own individual line in the input file, you can write the range of numbers in brackets where the numbers would usually go. For our fire animation example, the line to import them in the input file would look like this:

fire[1-12].png

Some animation creation programs output the files with numbers like fire0001.png, fire0002.png etc. If there are leading zeros to the file numbers, you must specify them on the input file as such:

fire[0001-0012].png

Output:

At least one image (the texture atlas) and its corresponding json file will be generated. If the sprites don't fit in the atlas (when using --size or --max-size), a set of images with its json file will be generated.

The generated json file will have one of the following formats:

Legacy

{
    "width": 512,                // texture atlas width
    "height": 512,               // texture atlas height

    "sprites": {
        "image1.png": {
            "x": 0,              // coords of sprite rect in atlas
            "y": 0,

            "width": 60,         // size of sprite rect in atlas
            "height": 100,

            "rotated": true,     // whether sprite is rotated or not (clockwise)
                                 // available when using --allow-rotate

            "real_width": 100,   // original dimensions of the image (when using --trim)
            "real_height": 60,

            "xoffset": 0,        // top-left offset from which the image was trimmed
            "yoffset": 0,

            "meta": /*...*/      // sprite metadata; available if --metadata
                                 // is given and it has data for the sprite
        },
        //...
    }
}

JSON Hash

{"frames": {

"image1":
{
    "frame": {"x":249,"y":205,"w":213,"h":159},
    "rotated": false,
    "trimmed": true,
    "spriteSourceSize": {"x":0,"y":0,"w":213,"h":159},
    "sourceSize": {"w":231,"h":175}
},
"image2":
{
    "frame": {"x":20,"y":472,"w":22,"h":21},
    "rotated": false,
    "trimmed": false,
    "spriteSourceSize": {"x":0,"y":0,"w":22,"h":21},
    "sourceSize": {"w":22,"h":21}
}},
"meta": {
    "app": "https://github.com/urraka/texpack",
    "image": "atlas.png",
    "size": {"w":650,"h":497}
    }
}

JSON Array

{"frames": [

{
    "filename": "image1",
    "frame": {"x":249,"y":205,"w":213,"h":159},
    "rotated": false,
    "trimmed": true,
    "spriteSourceSize": {"x":0,"y":0,"w":213,"h":159},
    "sourceSize": {"w":231,"h":175}
},
{
    "filename": "image2",
    "frame": {"x":29,"y":472,"w":22,"h":21},
    "rotated": false,
    "trimmed": false,
    "spriteSourceSize": {"x":0,"y":0,"w":22,"h":21},
    "sourceSize": {"w":22,"h":21}
}],
"meta": {
    "app": "https://github.com/urraka/texpack",
    "image": "atlas.png",
    "size": {"w":650,"h":497}
    }
}

Example:

This will take all PNG's in the current directory and generate the texture atlas in the out/ directory.

find . -name "*.png" | texpack -o out/atlas

Building:

Building has been tested on Linux, OSX and Windows (with MSYS/mingw-w64). Visual Studio is not supported.

In order to build, you will need to make sure that libpng and zlib are installed on your system. On Linux, you can use your system package manager to install these libraries. For example:

# Arch Linux
sudo pacman -S libpng

# Ubuntu
sudo apt-get install libpng12-dev

zlib is a libpng dependency so it should be installed along with it.

Once you have that just run make on the texpack project directory. If the libraries aren't installed on the default compiler paths, you can use CFLAGS and LDFLAGS to give it the required paths. For example, if the libraries were located inside ~/libs:

export CFLAGS=-I~/libs/include
export LDFLAGS=-L~/libs/lib
make

The resulting binary file will be inside the bin/ directory.

If you don't have a package manager you may have to download and compile both zlib and libpng yourself. Here's an example of how you could do this (do not just copy paste this):

# go to project directory
cd texpack

# create a directory for the libraries and move in there
mkdir ext
cd ext

# download required files
curl -L -O http://prdownloads.sourceforge.net/libpng/libpng-1.6.14.tar.gz?download
curl -L -O http://zlib.net/zlib-1.2.8.tar.gz

# extract them
tar xf libpng*
tar xf zlib*

# compile and install zlib
cd zlib*
./configure --prefix=`pwd`/..
make
make install
cd ..

# note: that won't work on MSYS (windows). you can do this instead:
cd zlib*
export BINARY_PATH="`pwd`/../bin"
export INCLUDE_PATH="`pwd`/../include"
export LIBRARY_PATH="`pwd`/../lib"
make -f win32/Makefile.gcc
make -f win32/Makefile.gcc install
cd ..

# compile and install libpng
cd libpng*
./configure --prefix=`pwd`/.. --with-zlib-prefix=`pwd`/..
make
make install
cd ..

# get back to the texpack directory and compile it
cd ..
export CFLAGS=-I`pwd`/ext/include
export LDFLAGS=-L`pwd`/ext/lib
make

Building on Windows via CMake and vcpkg:

# install vcpkg from an administrator console
git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
bootstrap-vcpkg.bat
vcpkg integrate install 

# install dependencies
vcpkg install libpng --triplet x64-windows
vcpkg install zlib --triplet x64-windows
# or for static linking
vcpkg install libpng:x64-windows-static
vcpkg install zlib:x64-windows-static

# generate project and compile
git clone https://github.com/kerskuchen/texpack.git
cd texpack
mkdir solution
cd solution
cmake .. -G "Visual Studio 16 2019" -DCMAKE_TOOLCHAIN_FILE="D:/UserLib/vcpkg/scripts/buildsystems/vcpkg.cmake"
# or for static linking
cmake .. -G "Visual Studio 16 2019" -DCMAKE_TOOLCHAIN_FILE="D:/UserLib/vcpkg/scripts/buildsystems/vcpkg.cmake" -DVCPKG_TARGET_TRIPLET=x64-windows-static

# Then we can open the solution and build it or run the following in a developer console:
msbuild texpack.vcxproj /nologo /p:configuration=Release /p:platform=x64 /p:OutDir=..\bin\

texpack's People

Contributors

defaultsamson avatar dmi7ry avatar josefnpat avatar kerskuchen avatar urraka avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

texpack's Issues

Global metadata

There is per-object metadata, but I was wondering if it was possible to add lines to the global metadata, the "meta" hash at the bottom of the file?

Using texturepacker I was able to specify a value "scale": "4" that my library was able to understand and scale accordingly. I didn't find anything specific in the docs.

Issues compiling on linux

Hey, I finally got around to trying to make this work, and could use some help.

I had to install libzip as opposed to the mentioned libz

Now here is the error I'm getting. (also please note that the binary output is textpack.exe, which doesn't make sense on linux!)

g++ src/main.cpp src/packer.cpp src/bleeding.cpp src/png.cpp src/rbp/Rect.cpp src/rbp/MaxRectsBinPack.cpp src/json/json.cpp -lpng -lz -O3 -Wall -fno-exceptions -fno-rtti -static-libgcc -static-libstdc++ -o bin/texpack.exe
src/png.cpp: In function ‘uint8_t* png::load(const char*, int*, int*, int*)’:
src/png.cpp:80:77: error: ‘memcpy’ was not declared in this scope
   memcpy(image + y * (*width) * (*channels), rows[y], (*width) * (*channels));
                                                                             ^
src/json/json.cpp:76:23: fatal error: json/json.h: No such file or directory
 #include <json/json.h>
                       ^
compilation terminated.
makefile:23: recipe for target 'bin/texpack.exe' failed
make: *** [bin/texpack.exe] Error 1

Update Example

I would consider the example line the easiest way to "get started" with the application.

Your example line;

find *.png | texpack -o out/atlas -p 16 -m metadata.json -b

I would suggest doing the following:

  • Have texpack determine if the out director exists, and if not, make it.
  • use find . -name "*.png" for the find example, so it recursively looks through directories.
  • Use the correct binary name (currently texpack.exe 😞 )
  • Don't use flags that are not required or not defaulted.

Perhaps something like this?

mkdir -p out
find . -name "*.png" | texpack.exe -o out/atlas

Add Po2 as a flag

Currently, as mentioned in the readme: "The generated image will have a power of 2 size."

While some frameworks (such as older versions of LÖVE) suffer from PO2 Syndrome, with OpenGL's NPOT textures this is no longer the case.

May I suggest we have a --po2 flag of some sort, and default it to false? I would very much like to save precious space in my game!

make failure on Ubuntu

Howdy! I usually build and use this on arch, but I noticed that on ubuntu, it doesn't compile 😭

as a sidenote, the sudo line for the ubuntu dependencies in the docs probably doesn't need to be commented out

~/repos/texpack♠ cat /etc/issue
Ubuntu 14.04.1 LTS \n \l

~/repos/texpack♠ uname -a
Linux seppi-Aspire-E3-111 3.13.0-32-generic #57-Ubuntu SMP Tue Jul 15 03:51:08 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
~/repos/texpack♠ sudo apt-get install libpng12-dev
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following extra packages will be installed:
  zlib1g-dev
The following NEW packages will be installed:
  libpng12-dev zlib1g-dev
0 upgraded, 2 newly installed, 0 to remove and 245 not upgraded.
Need to get 389 kB of archives.
After this operation, 1,124 kB of additional disk space will be used.
Do you want to continue? [Y/n] 
Get:1 http://us.archive.ubuntu.com/ubuntu/ trusty/main zlib1g-dev amd64 1:1.2.8.dfsg-1ubuntu1 [183 kB]
Get:2 http://us.archive.ubuntu.com/ubuntu/ trusty/main libpng12-dev amd64 1.2.50-1ubuntu2 [206 kB]
Fetched 389 kB in 0s (411 kB/s)   
Selecting previously unselected package zlib1g-dev:amd64.
(Reading database ... 199480 files and directories currently installed.)
Preparing to unpack .../zlib1g-dev_1%3a1.2.8.dfsg-1ubuntu1_amd64.deb ...
Unpacking zlib1g-dev:amd64 (1:1.2.8.dfsg-1ubuntu1) ...
Selecting previously unselected package libpng12-dev.
Preparing to unpack .../libpng12-dev_1.2.50-1ubuntu2_amd64.deb ...
Unpacking libpng12-dev (1.2.50-1ubuntu2) ...
Processing triggers for man-db (2.6.7.1-1ubuntu1) ...
Setting up zlib1g-dev:amd64 (1:1.2.8.dfsg-1ubuntu1) ...
Setting up libpng12-dev (1.2.50-1ubuntu2) ...
~/repos/texpack♠ make
g++ src/main.cpp src/packer.cpp src/bleeding.cpp src/png/png.cpp src/rbp/MaxRects.cpp  -g -O2 -Wall -std=c++11   -lpng -lz -o bin/texpack
In file included from /usr/include/png.h:540:0,
         from src/png/png.cpp:5:
/usr/include/pngconf.h:371:12: error: ‘__pngconf’ does not name a type
        __pngconf.h__ in libpng already includes setjmp.h;
        ^
/usr/include/pngconf.h:372:12: error: ‘__dont__’ does not name a type
        __dont__ include it again.;
        ^
make: *** [bin/texpack] Error 1

Issues compiling on linux

$ make
src/getopt.h:64:10: fatal error: crtdefs.h: No such file or directory
   64 | #include <crtdefs.h>
      |          ^~~~~~~~~~~

My environment:

$ uname --all
Linux abe 5.7.5-arch1-1 #1 SMP PREEMPT Mon, 22 Jun 2020 08:10:02 +0000 x86_64 GNU/Linux

For a workarrond see my fork

Thank you very much

Add linux dependency instructions

What would be really great is if we could take a few common OS's and add a quick package manager line to install the dependencies.

e.g.; Arch Linux would be

pacman -S libpng zlib

If you would like, I can create a PR with a few common OS's!

Trim feature

So I'm using this library, and it's working great!

I noticed there wasn't a trim feature, so I've started hacking a workaround:

#!/bin/sh

TEXPACK="/home/seppi/repos/texpack/bin/texpack"
TEMP=`mktemp -d`
NAME="ships"
CWD=`pwd`

rm $NAME*.png
rm $NAME*.json

for file in *.png
do
  convert $file -trim +repage $TEMP/$file
done

cd $TEMP
ls $TEMP/*.png > image_list.txt
$TEXPACK --padding=2 -e image_list.txt --output=$NAME --max-size=512x512
mv $NAME* $CWD
cd $CWD

rm $TEMP -rf

While this works well enough for my purposes, the ships.json output doesn't have the images original size (due to convert trimming it).

What would be amazing is if there was a --trim flag of some sort that I could use, that would mention the images original size, and it's current dimensions in the atlas.

Otherwise I can probably hack something together, but it wouldn't be pretty 😢

[suggestion] max-size parameter as a boolean

I'm porting your texture packer to use as a script in a Photoshop tool and found out that the code could be re-factured to accept max-size as a boolean parameter and then take the values in the parameter size as maximum values accordingly, in order to keep things a bit simpler in the UI that allows the user to enter those values.

Keep up the good work.

Merge identical sprites.

An option to merge identical sprites and not have multiple versions of the same image in atlas would be nice. Identical as in same pixels after trim.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.