C, Windows 32 bits, 987 octets
#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);}
- Le fichier est enregistré avec LF comme fin de ligne, pas avec CR + LF pour enregistrer certains octets.
- Le programme est écrit de manière à ce que les avertissements du compilateur soient générés pour économiser encore plus d'octets.
- Le fichier ne sera probablement pas compilé en tant que programme 64 bits car le tableau "
s[]
" est utilisé pour effectuer des transtypages implicites ...
- Le programme prend deux images (les noms de fichiers sont donnés via la ligne de commande):
- La première image (premier argument de ligne de commande) est l'image R / G / B au format Windows .BMP
- La deuxième image est le canal Alpha (noir signifie: 0%, toute autre couleur signifie: pas 0%); le fichier est également au format .BMP et doit avoir la même taille ou être plus grand que la première image
- L'image de sortie s'affiche dans le coin supérieur gauche de l'écran
- Je n'ai pas pu reproduire le garçon aux cheveux jaunes. Le jaune semble trop éloigné (> 50) du blanc!
Version non golfée:
#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);
}