Skip to content

Instantly share code, notes, and snippets.

@esnya
Created June 14, 2015 08:03
Show Gist options
  • Save esnya/6ca47675f0c75864b316 to your computer and use it in GitHub Desktop.
Save esnya/6ca47675f0c75864b316 to your computer and use it in GitHub Desktop.
画像中の色数を数える何か
// source/app.d
import derelict.freeimage.freeimage;
import std.algorithm;
import std.string;
import std.range;
import std.utf;
import std.stdio;
void main(string[] args)
{
DerelictFI.load();
auto fileu = args[1].toUTF16z();
auto fif = FreeImage_GetFIFFromFilenameU(fileu);
auto img = FreeImage_LoadU(fif, fileu);
auto w = FreeImage_GetWidth(img);
auto h = FreeImage_GetWidth(img);
writeln(w, h);
enum mx = 0;
enum my = 0;
bool[ubyte[3]] colors;
foreach (y; my .. (h - my)) {
foreach (x; mx .. (w - mx)) {
RGBQUAD color;
FreeImage_GetPixelColor(img, x, y, &color);
colors[[color.rgbRed, color.rgbGreen, color.rgbBlue]] = true;
}
}
auto list = colors.keys.filter!(a => colors[a])().map!(a => (format("#%02x%02x%02x", a[0], a[1], a[2])))().array;
list.sort();
foreach (color; list) {
writeln(color);
}
writeln(list.length);
}
{
"name": "colors",
"description": "A minimal D application.",
"copyright": "Copyright © 2015, ukatama",
"authors": ["ukatama"],
"dependencies": {
"derelict-fi": "~>2.0.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment