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
4#include <base/logger.h>
5#include <base/os.h>
6#include <base/system.h>
7
8#include <engine/gfx/image_loader.h>
9#include <engine/gfx/image_manipulation.h>
10
11static bool DilateFile(const char *pFilename, bool DryRun)
12{
13 CImageInfo Image;
14 int PngliteIncompatible;
15 if(!CImageLoader::LoadPng(File: io_open(filename: pFilename, flags: IOFLAG_READ), pFilename, Image, PngliteIncompatible))
16 return false;
17
18 if(Image.m_Format != CImageInfo::FORMAT_RGBA)
19 {
20 log_error("dilate", "ERROR: only RGBA PNG images are supported");
21 Image.Free();
22 return false;
23 }
24
25 if(DryRun)
26 {
27 CImageInfo OldImage = Image.DeepCopy();
28 DilateImage(Image);
29 const bool EqualImages = Image.DataEquals(Other: OldImage);
30 Image.Free();
31 OldImage.Free();
32 log_info("dilate", "'%s' is %sdilated", pFilename, EqualImages ? "" : "NOT ");
33 return EqualImages;
34 }
35 else
36 {
37 DilateImage(Image);
38 const bool SaveResult = CImageLoader::SavePng(File: io_open(filename: pFilename, flags: IOFLAG_WRITE), pFilename, Image);
39 Image.Free();
40 return SaveResult;
41 }
42}
43
44int main(int argc, const char **argv)
45{
46 CCmdlineFix CmdlineFix(&argc, &argv);
47 log_set_global_logger_default();
48
49 if(argc == 1)
50 {
51 log_error("dilate", "Usage: %s [--dry-run] <image1.png> [<image2.png> ...]", argv[0]);
52 return -1;
53 }
54
55 const bool DryRun = str_comp(a: argv[1], b: "--dry-run") == 0;
56 if(DryRun && argc < 3)
57 {
58 log_error("dilate", "Usage: %s [--dry-run] <image1.png> [<image2.png> ...]", argv[0]);
59 return -1;
60 }
61
62 bool Success = true;
63 for(int i = (DryRun ? 2 : 1); i < argc; i++)
64 {
65 Success &= DilateFile(pFilename: argv[i], DryRun);
66 }
67 return Success ? 0 : -1;
68}
69