Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
AntynK committed Feb 10, 2023
0 parents commit 96f16a9
Show file tree
Hide file tree
Showing 57 changed files with 1,723 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
152 changes: 152 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintainted in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
Binary file added Art convertor.exe
Binary file not shown.
Binary file added Colouring art.exe
Binary file not shown.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 AntynK

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Colouring art
It is a simple time killer game, in which you have to paint an image.

## Setup
Firstly you need to download Colouring art.exe(or source code) and assets.bak file, after that run .exe file or source code(file colouring_art.pyw), the game will create some folders.

In the game by default there are 2 images, but you can add your images.
(They are in the arts folder).

## Adding custom images
You can use the Art convertor.exe(or art_converter.py) utility, it is a simple console program which by default converts images from input folder to output folder. You can change the input folder with flag -i and the output folder with flag -o(output directory will be created automatically if it does not exist).

## About .pickart file
Game uses .pickart files(pick - name of python built-in module [pickle](https://docs.python.org/3.9/library/pickle.html)), files are compressed with [gzip](https://docs.python.org/3.9/library/gzip.html).

File structure:
```
{
"info":{
"size": (1, 1),
"version": 1
},
"palette":[(int, int, int), ...],
"pixels": [
[(color_index, is_painted), ...]
]
}
```
"info" contains the size of image and version of file.

"palette" contains tuples with 3 ints (r, g, b), they must be less or equal 255 and bigger or equal 0.

"pixels" is a 2d array which contains color_index(int) and painted(bool).

## Security
[Pickle](https://docs.python.org/3.9/library/pickle.html) module has security issues, the game does not use standard pickle.load(), instead it uses [restricted loader](https://docs.python.org/3/library/pickle.html#restricting-globals) which blocks all external classes.
39 changes: 39 additions & 0 deletions art_converter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from pathlib import Path

from argparse import ArgumentParser

from converter.convert_to_pickart import convert_to_pickart
from converter.convert_to_png import convert_to_png

parser = ArgumentParser()

parser.add_argument("-i", "--input", default="input")
parser.add_argument("-o", "--output", default="output")
parser.add_argument("-m", "--mode", choices=("0", "1"), default="0")

args = parser.parse_args()


def main():
input_dir: Path = Path(args.input)
output_dir: Path = Path(args.output)
if input_dir.name == "input":
input_dir.mkdir(parents=True, exist_ok=True)

if not input_dir.is_dir():
print(f"'{input_dir}' folder does not exist.")
return
output_dir.mkdir(parents=True, exist_ok=True)

if args.mode == "0":
for filename in input_dir.glob("*.png"):
print(f"Converting '{filename}'.")
convert_to_pickart(filename, output_dir)
elif args.mode == "1":
for filename in input_dir.glob("*.pickart"):
print(f"Converting '{filename}'.")
convert_to_png(filename, output_dir)


if __name__ == "__main__":
main()
Binary file added arts/plants/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions arts/plants/style.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"bg": [
89,
186,
4
]
}
Binary file added arts/plants/tree_01.pickart
Binary file not shown.
Binary file added arts/plants/tree_02.pickart
Binary file not shown.
Binary file added arts/progress.dat
Binary file not shown.
Binary file added assets.bak
Binary file not shown.
Binary file added assets/default_image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/fonts/Silkscreen-Regular.ttf
Binary file not shown.
Binary file added assets/icons/arrow.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/icons/exit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/icons/export.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/icons/save.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/selected_color_img.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions build.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pyinstaller.exe colouring_art.pyw --onefile --icon "logo.ico" --distpath "." --name "Colouring art"
5 changes: 5 additions & 0 deletions colouring_art.pyw
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from engine.game import Game

game = Game()

game.start()
21 changes: 21 additions & 0 deletions converter/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Andrii Karandashov

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
48 changes: 48 additions & 0 deletions converter/convert_to_pickart.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import pygame
import struct
import pickle
from gzip import GzipFile
from io import BytesIO
from dataclasses import asdict
from pathlib import Path

from engine.pickart.pickart_file_data import PickartFileData


def convert_to_pickart(filename: Path, output_dir: Path):
image = pygame.image.load(filename)
file_data = BytesIO(image.get_buffer().raw) # type: ignore

byte_size = image.get_bytesize()
format_ = byte_size * "B"

width, height = image.get_size()

data = []
palette = {}

info = {
"size": (width, height),
"version": 1,
}

for _ in range(height):
row_list = []
for _ in range(width):
color = struct.unpack_from(format_, file_data.read(byte_size))
if len(color) == 4 and color[3] == 0:
row_list.append([None, False])
continue

if color not in palette:
palette[color] = len(palette)

color_index = palette[color]
row_list.append([color_index, False])

data.append(row_list)

result = PickartFileData(info, list(palette.keys()), data)

with GzipFile(f"{output_dir}/{Path(filename).stem}.pickart", "wb") as file:
file.write(pickle.dumps(asdict(result)))
30 changes: 30 additions & 0 deletions converter/convert_to_png.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import struct
from pathlib import Path


import pygame

from engine.pickart.pickart_file import PickartFile


def convert_to_png(filename: Path, output_dir: Path):
file = PickartFile(filename)

buffer = b""
palette = file.get_palette()
fmt = ">" + "B" * file.fmt.value

for col in file.get_pixels():
row_ = b""
for row in col:
if row[0] is None:
row_ += struct.pack(fmt, 0, 0, 0, 0)
continue

color = palette[row[0]]
color = color.color if row[1] else color.grayscale
row_ += struct.pack(fmt, *color)
buffer += row_
surf = pygame.image.fromstring(buffer, file.get_size(), file.fmt.name) # type: ignore

pygame.image.save(surf, f"{output_dir}\\{Path(filename).stem}.png")
Loading

0 comments on commit 96f16a9

Please sign in to comment.