25
\$\begingroup\$

My Downgoat avatar has several distinct features

These features can however also apply to any other animal.

Specification

Given an image of an animal (technically can be anything), you must apply the Downgoat properties to it.

Border pixels are the outer most pixels of the image.

The background color is defined as the most common color in the border pixels. When that color is interpreted as an (r, g, b, a) color, with each channel from [0, 256), the background is defined as the area where the every channel of the given pixels' is within 50 of the "background color".

The process:

  • Setting the background and all transparent pixels to #232323, RGBA(35, 35, 35, 1.00)
  • Set all pixels which aren't the background to #FC0D1B, RGBA(252, 13, 27, 1.00).
  • Vertically reflect the image
  • Add the text -1 in Arial font, in the center of the image. The total height of the text should be 25% of the image's smallest dimension (i.e. min(width, height) * 0.25).

Rules

  • Feel free to assume the fonts are already installed
  • Your threshold is supposed to be 50, however you can take this number as input too. You do not get any decreased byte count anything for doing this however
  • Refer to our image I/O defaults for any questions related to I/O formats.
  • Image will always be at least 3x3 in size
  • If there is no most common "border pixel color", it doesn't matter which border pixel color you use as the "background color", this however must be consistent.

Examples

Input: Goat

Output: Downgoat


Input: Squid

Output: Downsquid

Input: Downgoat

Output: Downdowngoat


Input: Dennis

Output: Downdennis


Input: Trichoplax

Output: Downtrichoplax


More examples?

An online implementation is available here: vihan.org/p/downgoatify/#50


This is so shortest code in bytes wins. Good luck!

\$\endgroup\$
  • 6
    \$\begingroup\$ -1 First test case not an animal -_- \$\endgroup\$ – Geobits Nov 14 '16 at 1:00
  • 10
    \$\begingroup\$ -1ˢᵗ test case not an animal either. \$\endgroup\$ – Adám Nov 14 '16 at 1:16
  • 8
    \$\begingroup\$ Not a fan of demon dennis \$\endgroup\$ – downrep_nation Nov 14 '16 at 7:41
  • 1
    \$\begingroup\$ You could eliminate the worry about whether it's an animal by changing "animal" to "avatar" in the title and throughout the question. \$\endgroup\$ – Glenn Randers-Pehrson Nov 14 '16 at 12:57
  • 4
    \$\begingroup\$ What is the point of enforcing the font to use? The only thing this does is penalize languages where changing font takes more bytes \$\endgroup\$ – Fatalize Nov 14 '16 at 14:38
10
\$\begingroup\$

ImageMagick 7.0.3 + bash + sed, 379 bytes

M=magick K=\#232323 P='-chop x%[fx:u[0].h-2]-0+1'
$M $1 -depth 8 ppm:W;$M W $P T;$M W -rotate 90 -shave 1x $P L
C=`$M T L +append -statistic mode +0 txt:-|sed -e "1d;s/.*#/#/;s/ .*//;q"`
$M W -background $K -flatten -fill $K -fuzz 20% -opaque $C +fuzz -fill \#FC0D1B +opaque $K -flip -pointsize %[fx:.282*min\(u[0].w,u[0].h\)] -fill white -draw 'gravity center text 0,0 "-1"' x:

Ungolfed

# Make aliases for things we'll use repeatedly
M=magick K=\#232323 P='-chop x%[fx:u[0].h-2]-0+1'

# Copy the input file to a PPM file
$M $1 -depth 8 ppm:W

# Extract the border pixels into "T" (top and bottom), and "L" (left and right)
# PPM files, removing the ends from the "L" because they were already counted 
$M W $P T;$M W -rotate 90 -shave 1x $P L

# Put the borders into one strip and reduce to a txt image of the "mode"
# color on stdout, then convert the first pixel to hex format string "C"
C=`$M T L +append -statistic mode +0 txt:-|sed -e "1d;s/.*#/#/;s/ .*//;q"`

# Make background "#232323" and compose transparent pixels against it
# Convert any pixels with color within "fuzz" distance of background to #232323
# Convert any remaining pixels to "#FC0D1B"
# Flip the image vertically
# Determine pointsize for text, 1/5 of min(w,h). 1 pixel == 1.44 points
# Draw text "-1" centered on the image
# Display the result to an X window

$M W -background $K -flatten                     \
-fill $K -fuzz 20% -opaque $C                    \
+fuzz -fill \#FC0D1B +opaque $K                  \
-flip                                            \
-pointsize %[fx:.282*min\(u[0].w,u[0].h\)]       \
-fill white -draw 'gravity center text 0,0 "-1"' \
x:

Inputs and Outputs

I'm getting a quite different answer for the dennis image, probably because ImageMagick's "-fuzz" computes a sphere with diameter of 2N units in rgb coordinates while the rules call for computing a cube with sides of 101 units in rgb coordinates. Varying the "fuzz" helped some. Also, the JPEG artifacts in the original seem to be interfering with the conversion.

\$\endgroup\$
  • \$\begingroup\$ I'm not familliar with imageMagick, so this may be very uneducated, but can you replace the #0000 color code with simply #0? \$\endgroup\$ – tuskiomi Nov 16 '16 at 20:15
  • \$\begingroup\$ No, #0 doesn't work. Neither does #000 because that's opaque black and we need transparent black here. \$\endgroup\$ – Glenn Randers-Pehrson Nov 16 '16 at 20:22
  • \$\begingroup\$ The bug that I mentioned, # ImageMagick 7.0.1 through 7.0.3-7 fails here has been reported to the IM developers and is fixed in version 7.0.3-8. \$\endgroup\$ – Glenn Randers-Pehrson Nov 20 '16 at 16:52
10
\$\begingroup\$

C, 32-bit Windows, 987 bytes

#include <windows.h>
#define A CreateCompatibleDC(c)
#define F z=GetPixel(d,x,y);r=z;g=z>>8;b=z>>16
#define C(X,Y) (X<0||Y<0||X>=s[2]||Y>=s[3]||!GetPixel(e,X,Y))
#define D ((C(x-1,y)||C(x+1,y)||C(x,y-1)||C(x,y+1))&&!C(x,y))
#define E(X,Y) ((Z+X-Y)*(Z+X-Y)<2501)
main(int a,int*n){HDC c,d,e,f,w;int x,y,s[9],z,*o,m,t,Z;unsigned char r,g,b,R,G,B;c=GetDC(w=GetDesktopWindow());d=A;e=A;SelectObject(d,f=LoadImage(0,n[1],0,0,0,16));SelectObject(e,LoadImage(0,n[2],0,0,0,16));GetObject(f,24,s+1);o=LocalAlloc(64/*Fixed,ZeroInit*/,8*s[2]*s[3]);for(x=t=Z=s[1]=s[0]=0;x<s[2];x++)for(y=0;y<s[3];y++)if D{F;for(m=0;m<t&&o[m]!=z;m+=2);o[m]=z;o[m+1]++;t+=2*(m>=t);}for(x=y=1;x<t;x+=2)if(o[x]>o[y])y=x;z=o[y-1];R=z;G=z>>8;B=z>>16;for(x=0;x<s[2];x++)for(y=0;y<s[3];y++){F;SetPixel(c,x,s[3]-y-1,(C(x,y)||(E(r,R)&&E(g,G)&&E(b,B)))?0x232323:0x1B0DFC);}SelectObject(c,CreateFont(-(s[2]>>2),0,0,0,400,0,0,0,0,0,0,0,0,"Arial"));SetBkMode(c,1);SetTextColor(c,0xFFFFFF);DrawText(c,"-1",2,s,37);ReleaseDC(w,c);}
  • The file is saved with LF as line end, not with CR+LF to save some bytes.
  • The program is written in a way that compiler warnings are generated to save some more bytes.
  • The file will probably not compile as 64-bit program because the array "s[]" is used to do some implicit castings...
  • The program takes two images (file names are given via command line):
    • The first image (first command line argument) is the R/G/B image in Windows .BMP format
    • The second image is the Alpha channel (black means: 0%, any other colour means: not 0%); the file is also in .BMP format and must have the same size or be larger than the first image
  • The output image is displayed in the left upper corner of the screen
  • I couldn't reproduce the boy with the yellow hair. Yellow seems to be too far away (> 50) from white!

Ungolfed version:

#include <windows.h>

/*
 * Although this line costs us 32 bytes
 * Replacing "CreateCompatibleDC(c)" we'll
 * save 42 bytes in the golfed version later
 * so we save 10 bytes using this define!
 */
#define A CreateCompatibleDC(c)

/*
 * Macro: Get a pixel value at (x,y) to z
 * Also get r, g, b
 */
#define F z=GetPixel(d,x,y); \
    r=z; \
    g=z>>8; \
    b=z>>16

/*
 * Macro checking if a pixel is a
 * transparent colour or lies outside the
 * image
 */
#define C(X,Y) (X<0 || Y<0 || X>=s[2] || Y>=s[3] || !GetPixel(e,X,Y))

/*
 * Macro checking if a pixel at (x,y) is a border pixel
 */
#define D ((C(x-1,y) || C(x+1,y) || C(x,y-1) || C(x,y+1)) && !C(x,y))

/*
 * Macro checking if the difference between X and Y is less than 50
 * The variable "Z" must be type "int" and zero. It is used to
 * perform an implicit cast from "BYTE" to "int".
 */
#define E(X,Y) ((Z+X-Y)*(Z+X-Y)<2501)

/*
 * Note that the second argument is "char **",
 * not "int *".
 * We ignore resulting compiler warnings...
 */
main(int a, int * n)
{
    HDC c, d, e, f, w;
    int x, y, s[9], z, *o, m, t, Z;
    unsigned char r, g, b, R, G, B;

    /*
     * Get the HDC handle to the
     * screen (allowing us to create HDCs
     * for accessing bitmap files as well as
     * drawing directly to the screen!)
     */
    c=GetDC(w=GetDesktopWindow());
    /*
     * Create two virtual HDCs for accessing
     * the bitmap files.
     */
    d=A; /* Macro */
    e=A; /* Macro */
    /*
     * Load the two images:
     * The first argument is the image file with
     * the R/G/B channel
     * The second argument is the image file
     * containing the mask defined by the Alpha
     * channel:
     *   Black means = Alpha=0
     *   White means = Alpha>0
     *   (Any other colour means: Alpha>0)
     *
     * Note that "f" is of the type "HBITMAP",
     * not "HDC". We save 4 bytes in the golfed
     * version using HDC instead of HBITMAP and
     * compile the C file with compiler warnings
     * switched off!
     *
     * The second image should have the same size
     * as the first one. However it may be larger
     * than the first one. It must not be smaller!
     */
    SelectObject(d,f=LoadImage(0,n[1],0,0,0,16 /* 16=LR_LOADFROMFILE */));
    SelectObject(e,LoadImage(0,n[2],0,0,0,16));
    /*
     * Get the image information (size)
     */
    GetObject(f,24,s+1);
    /*
     * Search all background colours
     */
    o=LocalAlloc(64 /* Fixed, ZeroInit */,8*s[2]*s[3]);
    for(x=t=Z=s[1]=s[0]=0;x<s[2];x++)
        for(y=0;y<s[3];y++)
            if D
    {
        F; /* Macro */
        for(m=0;m<t && o[m]!=z;m+=2);
        o[m]=z;
        o[m+1]++;
        t+=2*(m>=t);
    }
    /*
     * Search the most common one
     */
    for(x=y=1;x<t;x+=2) if(o[x]>o[y]) y=x;
    z=o[y-1];
    R=z;
    G=z>>8;
    B=z>>16;
    /*
     * Draw the image directly to the screen
     */
    for(x=0;x<s[2];x++)
        for(y=0;y<s[3];y++)
    {
        F; /* Macro */
        /* C and E are macros: */
        SetPixel(c,x,s[3]-y-1,(C(x,y) ||
            (E(r,R) && E(g,G) && E(b,B)))?
            0x232323:0x1B0DFC);
    }
    /*
     * Draw the text over the image
     */
    SelectObject(c,CreateFont(-(s[2]>>2),0,0,0,400,0,0,0,0,0,0,0,0,"Arial"));
    SetBkMode(c,1 /* TRANSPARENT */);
    SetTextColor(c,0xFFFFFF);
    DrawText(c,"-1",2,s,37 /* center, vcenter, singleline */);
    /*
     * Unfortunately DrawText() won't work
     * when not doing this!
     */
    ReleaseDC(w,c);
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.