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