mirror of
https://git.planet-casio.com/Lephenixnoir/fxsdk.git
synced 2024-12-29 13:03:37 +01:00
fxconv: expose Area and Grid in the API
This commit is contained in:
parent
f11f6929b4
commit
822b5107e5
1 changed files with 22 additions and 6 deletions
|
@ -13,6 +13,8 @@ __all__ = [
|
||||||
"FX_BLACK", "FX_DARK", "FX_LIGHT", "FX_WHITE", "FX_ALPHA",
|
"FX_BLACK", "FX_DARK", "FX_LIGHT", "FX_WHITE", "FX_ALPHA",
|
||||||
# Functions
|
# Functions
|
||||||
"quantize", "convert", "elf",
|
"quantize", "convert", "elf",
|
||||||
|
# Reusable classes
|
||||||
|
"Area", "Grid",
|
||||||
]
|
]
|
||||||
|
|
||||||
#
|
#
|
||||||
|
@ -150,16 +152,21 @@ FX_CHARSETS = [
|
||||||
#
|
#
|
||||||
|
|
||||||
class Area:
|
class Area:
|
||||||
|
"""
|
||||||
|
A subrectangle of an image, typicall used for pre-conversion cropping.
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, area, img):
|
def __init__(self, area, img):
|
||||||
"""
|
"""
|
||||||
Construct an Area object from a dict specification. The following keys
|
Construct an Area object from a dict specification. The following keys
|
||||||
may be used:
|
may be used to specific the position and size of the rectangle:
|
||||||
|
|
||||||
* "x", "y" (int strings, default to 0)
|
* "x", "y" (int strings, default to 0)
|
||||||
* "width", "height" (int strings, default to image dimensions)
|
* "width", "height" (int strings, default to image dimensions)
|
||||||
* "size" ("WxH" where W and H are the width and height)
|
* "size" ("WxH" where W and H are the width and height)
|
||||||
|
|
||||||
The Area objects has attributes "x", "y", "w" and "h".
|
The Area objects has attributes "x", "y", "w" and "h". Both positions
|
||||||
|
default to 0 and both sizes to the corresponding image dimensions.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
self.x = int(area.get("x", 0))
|
self.x = int(area.get("x", 0))
|
||||||
|
@ -171,7 +178,7 @@ class Area:
|
||||||
self.w, self.h = map(int, area["size"].split("x"))
|
self.w, self.h = map(int, area["size"].split("x"))
|
||||||
|
|
||||||
def tuple(self):
|
def tuple(self):
|
||||||
"""Return the tuple representation (x,y,w,h)."""
|
"""Return the tuple representation (x,y,w,h), suitable for .crop(). """
|
||||||
return (self.x, self.y, self.w, self.h)
|
return (self.x, self.y, self.w, self.h)
|
||||||
|
|
||||||
#
|
#
|
||||||
|
@ -179,17 +186,26 @@ class Area:
|
||||||
#
|
#
|
||||||
|
|
||||||
class Grid:
|
class Grid:
|
||||||
|
"""
|
||||||
|
A grid over an image, used to isolate glyphs in fonts and tiles in maps.
|
||||||
|
Supports several types of spacing. To apply an outer border, use crop
|
||||||
|
through an Area before using the Grid.
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, grid):
|
def __init__(self, grid):
|
||||||
"""
|
"""
|
||||||
Construct a Grid object from a dict specification. The following keys
|
Construct a Grid object from a dict specification. The following keys
|
||||||
may be used:
|
may be used to specify the dimension and spacing of the cells:
|
||||||
|
|
||||||
* "border" (int string, defaults to 0)
|
* "border" (int string, defaults to 0)
|
||||||
* "padding" (int string, defaults to 0)
|
* "padding" (int string, defaults to 0)
|
||||||
* "width", "height" (int strings, mandatory if "size" not set)
|
* "width", "height" (int strings, mandatory if "size" not set)
|
||||||
* "size" ("WxH" where W and H are the cell width/height)
|
* "size" ("WxH" where W and H are the cell width/height)
|
||||||
|
|
||||||
The Grid object has attributes "border", "padding", "w" and "h".
|
The Grid object has attributes "border", "padding", "w" and "h". Each
|
||||||
|
cell is of size "(w,h)" and has "padding" pixels of proper padding
|
||||||
|
around it. Additionally, cells are separated by a border of size
|
||||||
|
"border"; this includes an outer border.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
self.border = int(grid.get("border", 0))
|
self.border = int(grid.get("border", 0))
|
||||||
|
@ -218,7 +234,7 @@ class Grid:
|
||||||
|
|
||||||
|
|
||||||
def iter(self, img):
|
def iter(self, img):
|
||||||
"""Build an iterator on all subrectangles of the grid."""
|
"""Yields subrectangles of the grid as tuples (x,y,w,h)."""
|
||||||
b, p, w, h = self.border, self.padding, self.w, self.h
|
b, p, w, h = self.border, self.padding, self.w, self.h
|
||||||
|
|
||||||
# Padding-extended parameters
|
# Padding-extended parameters
|
||||||
|
|
Loading…
Reference in a new issue