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