1/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2/* If you are missing that file, acquire a complete release at teeworlds.com. */
3#include <base/logger.h>
4#include <base/system.h>
5#include <engine/gfx/image_loader.h>
6#include <engine/gfx/image_manipulation.h>
7#include <engine/graphics.h>
8
9int DilateFile(const char *pFilename)
10{
11 IOHANDLE File = io_open(filename: pFilename, flags: IOFLAG_READ);
12 if(File)
13 {
14 io_seek(io: File, offset: 0, origin: IOSEEK_END);
15 long int FileSize = io_tell(io: File);
16 if(FileSize <= 0)
17 {
18 io_close(io: File);
19 dbg_msg(sys: "dilate", fmt: "failed to get file size (%ld). filename='%s'", FileSize, pFilename);
20 return false;
21 }
22 io_seek(io: File, offset: 0, origin: IOSEEK_START);
23 TImageByteBuffer ByteBuffer;
24 SImageByteBuffer ImageByteBuffer(&ByteBuffer);
25
26 ByteBuffer.resize(new_size: FileSize);
27 io_read(io: File, buffer: &ByteBuffer.front(), size: FileSize);
28
29 io_close(io: File);
30
31 CImageInfo Img;
32 EImageFormat ImageFormat;
33 int PngliteIncompatible;
34 if(LoadPng(ByteLoader&: ImageByteBuffer, pFileName: pFilename, PngliteIncompatible, Width&: Img.m_Width, Height&: Img.m_Height, pImageBuff&: Img.m_pData, ImageFormat))
35 {
36 if(ImageFormat != IMAGE_FORMAT_RGBA)
37 {
38 free(ptr: Img.m_pData);
39 dbg_msg(sys: "dilate", fmt: "%s: not an RGBA image", pFilename);
40 return -1;
41 }
42
43 DilateImage(pImageBuff: Img.m_pData, w: Img.m_Width, h: Img.m_Height);
44
45 // save here
46 IOHANDLE SaveFile = io_open(filename: pFilename, flags: IOFLAG_WRITE);
47 if(SaveFile)
48 {
49 TImageByteBuffer ByteBuffer2;
50 SImageByteBuffer ImageByteBuffer2(&ByteBuffer2);
51
52 if(SavePng(ImageFormat: IMAGE_FORMAT_RGBA, pRawBuffer: Img.m_pData, WrittenBytes&: ImageByteBuffer2, Width: Img.m_Width, Height: Img.m_Height))
53 io_write(io: SaveFile, buffer: &ByteBuffer2.front(), size: ByteBuffer2.size());
54 io_close(io: SaveFile);
55
56 free(ptr: Img.m_pData);
57 }
58 }
59 else
60 {
61 dbg_msg(sys: "dilate", fmt: "failed unknown image format: %s", pFilename);
62 return -1;
63 }
64 }
65 else
66 {
67 dbg_msg(sys: "dilate", fmt: "failed to open image file. filename='%s'", pFilename);
68 return -1;
69 }
70
71 return 0;
72}
73
74int main(int argc, const char **argv)
75{
76 CCmdlineFix CmdlineFix(&argc, &argv);
77 log_set_global_logger_default();
78 if(argc == 1)
79 {
80 dbg_msg(sys: "usage", fmt: "%s FILE1 [ FILE2... ]", argv[0]);
81 return -1;
82 }
83
84 for(int i = 1; i < argc; i++)
85 DilateFile(pFilename: argv[i]);
86
87 return 0;
88}
89