diff --git a/src/fr/devinsy/catgenerator/cli/CatGeneratorCLI.java b/src/fr/devinsy/catgenerator/cli/CatGeneratorCLI.java index 41939ae..826acc7 100644 --- a/src/fr/devinsy/catgenerator/cli/CatGeneratorCLI.java +++ b/src/fr/devinsy/catgenerator/cli/CatGeneratorCLI.java @@ -21,12 +21,16 @@ package fr.devinsy.catgenerator.cli; import java.io.File; import java.io.IOException; import java.time.LocalDateTime; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.math.NumberUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import fr.devinsy.catgenerator.core.BirdGenerator; import fr.devinsy.catgenerator.core.CatGenerator; import fr.devinsy.catgenerator.util.BuildInformation; import fr.devinsy.strings.StringList; @@ -54,23 +58,17 @@ public final class CatGeneratorCLI message.append("CatGenerator CLI ").appendln(BuildInformation.instance().version()); message.appendln("Usage:"); - message.appendln(" catgenerator [OPTIONS] [] Build cat avatar to file."); - message.appendln(""); - message.appendln(" catgenerator [ -h | -help | --help ] Displays this help."); - message.appendln(" catgenerator [ -v | -version | --version ] Displays version information."); - message.appendln(" catgenerator Build random cat avatar to file."); - message.appendln(" catgenerator Build cat avator from keyword to file."); - message.appendln(" catgenerator -size Build resized random cat avatar to file."); - message.appendln(" catgenerator -size Build resized random cat avatar from keyword to file."); - message.appendln(" catgenerator -size Build resized random cat avatar from keyword to file."); + message.appendln(" catgenerator [OPTIONS] Build cat avatar to file."); message.appendln(""); message.appendln("Options:"); message.appendln(" -h, -help, --help Displays this help."); message.appendln(" -v, -version, --version Displays version information."); - message.appendln(" -size Resizes the avatar image. Default value is 256."); + message.appendln(" -size Resizes the avatar image. Default is 256."); + message.appendln(" -k Determinizes the build from a keyword. Default is random."); + message.appendln(" -cat To generate a cat avatar. Default."); + message.appendln(" -bird To generate a bird avatar."); message.appendln(""); message.appendln("Arguments:"); - message.appendln(" keyword Determinizes the build. If not set then build a random avatar."); message.appendln(" filename The target file name. Default extension is .png."); message.appendln(""); message.appendln("Originally inspired of:"); @@ -231,25 +229,33 @@ public final class CatGeneratorCLI logger.info("Matching 4."); CatGenerator.buildAvatarTo(normalizeFile(args[0])); } - else if (isMatching(args, ".+", ".+")) - { - logger.info("Matching 3."); - CatGenerator.buildAvatarTo(args[0], normalizeFile(args[1])); - } - else if (isMatching(args, "-size", "\\d+", ".+")) - { - logger.info("Matching 2."); - CatGenerator.buildAvatarTo(Integer.valueOf(args[1]), normalizeFile(args[2])); - } - else if (isMatching(args, "-size", "\\d+", ".+", ".+")) - { - logger.info("Matching 1."); - CatGenerator.buildAvatarTo(args[2], Integer.valueOf(args[1]), normalizeFile(args[3])); - } else { - logger.info("Bad usage."); - displayHelp(); + String regex = "(((?-bird)?|(-cat)?|(-size (?\\d+))?|(-k (?\\S+))?) )+(?.+)"; + Pattern pattern = Pattern.compile(regex); + + Matcher matcher = pattern.matcher(toCommandLine(args)); + if (matcher.matches()) + { + String fileName = matcher.group("filename"); + Integer size = NumberUtils.toInt(matcher.group("size"), -1); + String bird = matcher.group("bird"); + String keyword = matcher.group("keyword"); + + if (bird == null) + { + CatGenerator.buildAvatarTo(keyword, size, normalizeFile(fileName)); + } + else + { + BirdGenerator.buildAvatarTo(keyword, size, normalizeFile(fileName)); + } + } + else + { + logger.info("Bad usage."); + displayHelp(); + } } } catch (IOException exception) @@ -261,4 +267,34 @@ public final class CatGeneratorCLI // logger.info("Done."); } + + /** + * To command line. + * + * @param args + * the args + * @return the string + */ + public static String toCommandLine(final String[] args) + { + String result; + + StringList buffer = new StringList(); + for (String string : args) + { + if (string.contains(" ")) + { + buffer.append("\"" + string + "\""); + } + else + { + buffer.append(string); + } + } + + result = buffer.toStringSeparatedBy(' '); + + // + return result; + } } diff --git a/src/fr/devinsy/catgenerator/core/BirdGenerator.java b/src/fr/devinsy/catgenerator/core/BirdGenerator.java index dab7eea..0c25c22 100644 --- a/src/fr/devinsy/catgenerator/core/BirdGenerator.java +++ b/src/fr/devinsy/catgenerator/core/BirdGenerator.java @@ -37,12 +37,14 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * The Class BirdGenerator. + * The Class CatGenerator. */ public class BirdGenerator { private static Logger logger = LoggerFactory.getLogger(BirdGenerator.class); + public static final int DEFAULT_SIZE = 70; + /** * Builds the avatar. * @@ -54,8 +56,7 @@ public class BirdGenerator { BufferedImage result; - String seed = RandomStringUtils.randomAlphanumeric(10); - result = buildAvatar(seed); + result = buildAvatar(null, null); // return result; @@ -74,54 +75,29 @@ public class BirdGenerator { BufferedImage result; - result = buildAvatar(); - result = CatGeneratorUtils.resize(result, size, size); + result = buildAvatar(null, size); // return result; } /** - * Builds the cat. + * Builds the avatar. * * @param seed * the seed - * @param size - * the size + * @return the buffered image * @throws IOException + * Signals that an I/O exception has occurred. */ public static BufferedImage buildAvatar(final String seed) throws IOException { BufferedImage result; - if (StringUtils.isBlank(seed)) - { - result = buildAvatar(); - } - else - { - // Create an empty image. - result = new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB); - Graphics2D grapher = result.createGraphics(); - grapher.setBackground(Color.WHITE); - grapher.clearRect(0, 0, 256, 256); + result = buildAvatar(seed, null); - // Add stuff. - // Initialize the random generator. - Random generator = new Random(Integer.parseInt(DigestUtils.md5Hex(seed).substring(0, 6), 16)); - drawImage(grapher, "tail", generator.nextInt(9) + 1); - drawImage(grapher, "hoop", generator.nextInt(10) + 1); - drawImage(grapher, "body", generator.nextInt(9) + 1); - drawImage(grapher, "wing", generator.nextInt(9) + 1); - drawImage(grapher, "eyes", generator.nextInt(9) + 1); - drawImage(grapher, "bec", generator.nextInt(9) + 1); - drawImage(grapher, "accessorie", generator.nextInt(20) + 1); - - grapher.dispose(); - } // return result; - } /** @@ -134,12 +110,56 @@ public class BirdGenerator * @return the buffered image * @throws IOException */ - public static BufferedImage buildAvatar(final String seed, final int size) throws IOException + public static BufferedImage buildAvatar(final String seed, final Integer size) throws IOException { BufferedImage result; - BufferedImage avatar = buildAvatar(seed); - result = CatGeneratorUtils.resize(avatar, size, size); + // + String targetSeed; + if (StringUtils.isBlank(seed)) + { + targetSeed = RandomStringUtils.randomAlphanumeric(10); + } + else + { + targetSeed = seed; + } + + // + int targetSize; + if ((size == null) || (size < 1)) + { + targetSize = DEFAULT_SIZE; + } + else + { + targetSize = size; + } + + // Create an empty image. + result = new BufferedImage(DEFAULT_SIZE, DEFAULT_SIZE, BufferedImage.TYPE_INT_ARGB); + Graphics2D grapher = result.createGraphics(); + grapher.setBackground(Color.WHITE); + grapher.clearRect(0, 0, DEFAULT_SIZE, DEFAULT_SIZE); + + // Add stuff. + // Initialize the random generator. + Random generator = new Random(Integer.parseInt(DigestUtils.md5Hex(targetSeed).substring(0, 6), 16)); + drawImage(grapher, "tail", generator.nextInt(9) + 1); + drawImage(grapher, "hoop", generator.nextInt(10) + 1); + drawImage(grapher, "body", generator.nextInt(9) + 1); + drawImage(grapher, "wing", generator.nextInt(9) + 1); + drawImage(grapher, "eyes", generator.nextInt(9) + 1); + drawImage(grapher, "bec", generator.nextInt(9) + 1); + drawImage(grapher, "accessorie", generator.nextInt(20) + 1); + + grapher.dispose(); + + // + if (targetSize != DEFAULT_SIZE) + { + result = CatGeneratorUtils.resize(result, targetSize, targetSize); + } // return result; @@ -157,7 +177,17 @@ public class BirdGenerator */ public static void buildAvatarFromPeeperCarrotWebsite(final String seed, final File target) throws IOException { - URL source = new URL("https://www.peppercarrot.com/extras/html/2016_cat-generator/avatar.php?seed=" + seed); + String targetSeed; + if (StringUtils.isBlank(seed)) + { + targetSeed = ""; + } + else + { + targetSeed = seed; + } + + URL source = new URL("https://www.peppercarrot.com/extras/html/2016_cat-generator/avatar.php?seed=" + targetSeed); FileUtils.copyURLToFile(source, target, 5000, 5000); } @@ -171,9 +201,7 @@ public class BirdGenerator */ public static void buildAvatarTo(final File target) throws IOException { - BufferedImage result = buildAvatar(); - - ImageIO.write(result, FilenameUtils.getExtension(target.getName()), target); + buildAvatarTo(null, null, target); } /** @@ -188,8 +216,7 @@ public class BirdGenerator */ public static void buildAvatarTo(final int size, final File target) throws IOException { - BufferedImage result = buildAvatar(size); - ImageIO.write(result, FilenameUtils.getExtension(target.getName()), target); + buildAvatarTo(null, size, target); } /** @@ -204,8 +231,7 @@ public class BirdGenerator */ public static void buildAvatarTo(final String seed, final File target) throws IOException { - BufferedImage result = buildAvatar(seed); - ImageIO.write(result, FilenameUtils.getExtension(target.getName()), target); + buildAvatarTo(seed, null, target); } /** @@ -220,7 +246,7 @@ public class BirdGenerator * @throws IOException * Signals that an I/O exception has occurred. */ - public static void buildAvatarTo(final String seed, final int size, final File target) throws IOException + public static void buildAvatarTo(final String seed, final Integer size, final File target) throws IOException { BufferedImage result = buildAvatar(seed, size); ImageIO.write(result, FilenameUtils.getExtension(target.getName()), target); @@ -240,7 +266,7 @@ public class BirdGenerator */ private static void drawImage(final Graphics2D grapher, final String name, final int index) throws IOException { - URL url = BirdGenerator.class.getResource("/fr/devinsy/catgenerator/core/images/cat" + name + "_" + index + ".png"); + URL url = BirdGenerator.class.getResource("/fr/devinsy/catgenerator/core/images/bird/" + name + "_" + index + ".png"); BufferedImage part = ImageIO.read(url); grapher.drawImage(part, null, 0, 0); } diff --git a/src/fr/devinsy/catgenerator/core/CatGenerator.java b/src/fr/devinsy/catgenerator/core/CatGenerator.java index 02cc465..1ebf594 100644 --- a/src/fr/devinsy/catgenerator/core/CatGenerator.java +++ b/src/fr/devinsy/catgenerator/core/CatGenerator.java @@ -43,6 +43,8 @@ public class CatGenerator { private static Logger logger = LoggerFactory.getLogger(CatGenerator.class); + public static final int DEFAULT_SIZE = 256; + /** * Builds the avatar. * @@ -54,8 +56,7 @@ public class CatGenerator { BufferedImage result; - String seed = RandomStringUtils.randomAlphanumeric(10); - result = buildAvatar(seed); + result = buildAvatar(null, null); // return result; @@ -74,52 +75,29 @@ public class CatGenerator { BufferedImage result; - result = buildAvatar(); - result = CatGeneratorUtils.resize(result, size, size); + result = buildAvatar(null, size); // return result; } /** - * Builds the cat. + * Builds the avatar. * * @param seed * the seed - * @param size - * the size + * @return the buffered image * @throws IOException + * Signals that an I/O exception has occurred. */ public static BufferedImage buildAvatar(final String seed) throws IOException { BufferedImage result; - if (StringUtils.isBlank(seed)) - { - result = buildAvatar(); - } - else - { - // Create an empty image. - result = new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB); - Graphics2D grapher = result.createGraphics(); - grapher.setBackground(Color.WHITE); - grapher.clearRect(0, 0, 256, 256); + result = buildAvatar(seed, null); - // Add stuff. - // Initialize the random generator. - Random generator = new Random(Integer.parseInt(DigestUtils.md5Hex(seed).substring(0, 6), 16)); - drawImage(grapher, "body", generator.nextInt(15) + 1); - drawImage(grapher, "fur", generator.nextInt(10) + 1); - drawImage(grapher, "eyes", generator.nextInt(15) + 1); - drawImage(grapher, "mouth", generator.nextInt(10) + 1); - drawImage(grapher, "accessorie", generator.nextInt(20) + 1); - - grapher.dispose(); - } // return result; - } /** @@ -132,12 +110,54 @@ public class CatGenerator * @return the buffered image * @throws IOException */ - public static BufferedImage buildAvatar(final String seed, final int size) throws IOException + public static BufferedImage buildAvatar(final String seed, final Integer size) throws IOException { BufferedImage result; - BufferedImage avatar = buildAvatar(seed); - result = CatGeneratorUtils.resize(avatar, size, size); + // + String targetSeed; + if (StringUtils.isBlank(seed)) + { + targetSeed = RandomStringUtils.randomAlphanumeric(10); + } + else + { + targetSeed = seed; + } + + // + int targetSize; + if ((size == null) || (size < 1)) + { + targetSize = DEFAULT_SIZE; + } + else + { + targetSize = size; + } + + // Create an empty image. + result = new BufferedImage(DEFAULT_SIZE, DEFAULT_SIZE, BufferedImage.TYPE_INT_ARGB); + Graphics2D grapher = result.createGraphics(); + grapher.setBackground(Color.WHITE); + grapher.clearRect(0, 0, DEFAULT_SIZE, DEFAULT_SIZE); + + // Add stuff. + // Initialize the random generator. + Random generator = new Random(Integer.parseInt(DigestUtils.md5Hex(targetSeed).substring(0, 6), 16)); + drawImage(grapher, "body", generator.nextInt(15) + 1); + drawImage(grapher, "fur", generator.nextInt(10) + 1); + drawImage(grapher, "eyes", generator.nextInt(15) + 1); + drawImage(grapher, "mouth", generator.nextInt(10) + 1); + drawImage(grapher, "accessorie", generator.nextInt(20) + 1); + + grapher.dispose(); + + // + if (targetSize != DEFAULT_SIZE) + { + result = CatGeneratorUtils.resize(result, targetSize, targetSize); + } // return result; @@ -155,7 +175,17 @@ public class CatGenerator */ public static void buildAvatarFromPeeperCarrotWebsite(final String seed, final File target) throws IOException { - URL source = new URL("https://www.peppercarrot.com/extras/html/2016_cat-generator/avatar.php?seed=" + seed); + String targetSeed; + if (StringUtils.isBlank(seed)) + { + targetSeed = ""; + } + else + { + targetSeed = seed; + } + + URL source = new URL("https://www.peppercarrot.com/extras/html/2016_cat-generator/avatar.php?seed=" + targetSeed); FileUtils.copyURLToFile(source, target, 5000, 5000); } @@ -169,9 +199,7 @@ public class CatGenerator */ public static void buildAvatarTo(final File target) throws IOException { - BufferedImage result = buildAvatar(); - - ImageIO.write(result, FilenameUtils.getExtension(target.getName()), target); + buildAvatarTo(null, null, target); } /** @@ -186,8 +214,7 @@ public class CatGenerator */ public static void buildAvatarTo(final int size, final File target) throws IOException { - BufferedImage result = buildAvatar(size); - ImageIO.write(result, FilenameUtils.getExtension(target.getName()), target); + buildAvatarTo(null, size, target); } /** @@ -202,8 +229,7 @@ public class CatGenerator */ public static void buildAvatarTo(final String seed, final File target) throws IOException { - BufferedImage result = buildAvatar(seed); - ImageIO.write(result, FilenameUtils.getExtension(target.getName()), target); + buildAvatarTo(seed, null, target); } /** @@ -218,7 +244,7 @@ public class CatGenerator * @throws IOException * Signals that an I/O exception has occurred. */ - public static void buildAvatarTo(final String seed, final int size, final File target) throws IOException + public static void buildAvatarTo(final String seed, final Integer size, final File target) throws IOException { BufferedImage result = buildAvatar(seed, size); ImageIO.write(result, FilenameUtils.getExtension(target.getName()), target); @@ -238,7 +264,7 @@ public class CatGenerator */ private static void drawImage(final Graphics2D grapher, final String name, final int index) throws IOException { - URL url = CatGenerator.class.getResource("/fr/devinsy/catgenerator/core/images/cat" + name + "_" + index + ".png"); + URL url = CatGenerator.class.getResource("/fr/devinsy/catgenerator/core/images/cat/" + name + "_" + index + ".png"); BufferedImage part = ImageIO.read(url); grapher.drawImage(part, null, 0, 0); }