Color quantization: Difference between revisions

Content added Content deleted
(Added Sidef)
(Added Kotlin)
Line 1,237: Line 1,237:
[path, outpath] -> quantizeIO path outpath 16
[path, outpath] -> quantizeIO path outpath 16
_ -> putStrLn $ "Usage: " ++ prog ++ " <image-file> <out-file.png>"</lang>
_ -> putStrLn $ "Usage: " ++ prog ++ " <image-file> <out-file.png>"</lang>

=={{header|Kotlin}}==
{{works with|Ubuntu 16.04}}
Rather than coding this from scratch, we invoke programatically ImageMagick's 'convert' tool which has all this stuff built in.
<lang scala>// Version 1.2.41

import java.io.BufferedReader
import java.io.InputStreamReader

fun main(args: Array<String>) {
// convert 'frog' to an image which uses only 16 colors, no dithering
val pb = ProcessBuilder(
"convert",
"Quantum_frog.png",
"-dither",
"None",
"-colors",
"16",
"Quantum_frog_16.png"
)
pb.directory(null)
val proc = pb.start()
proc.waitFor()

// now show the colors used
val pb2 = ProcessBuilder(
"convert",
"Quantum_frog_16.png",
"-format",
"%c",
"-depth",
"8",
"histogram:info:-"
)
pb2.directory(null)
pb.redirectOutput(ProcessBuilder.Redirect.PIPE)
val proc2 = pb2.start()
val br = BufferedReader(InputStreamReader(proc2.inputStream))
var clrNum = 0
while (true) {
val line = br.readLine() ?: break
System.out.printf("%2d->%s\n", clrNum++, line)
}
br.close()
}</lang>

{{output}}
The resulting image is as expected and details of the 16 colors used are as follows:
<pre>
0-> 37572: ( 9, 53, 0) #093500 srgb(9,53,0)
1-> 7068: ( 13, 26, 0) #0D1A00 srgb(13,26,0)
2-> 31: ( 15,165, 21) #0FA515 srgb(15,165,21)
3-> 19609: ( 42, 96, 1) #2A6001 srgb(42,96,1)
4-> 21753: ( 57,136, 4) #398804 srgb(57,136,4)
5-> 66865: ( 77,147, 10) #4D930A srgb(77,147,10)
6-> 12275: ( 79,111, 9) #4F6F09 srgb(79,111,9)
7-> 836: ( 94,111, 74) #5E6F4A srgb(94,111,74)
8-> 25689: (105,158, 28) #699E1C srgb(105,158,28)
9-> 5095: (113,163, 85) #71A355 srgb(113,163,85)
10-> 1788: (125,129,151) #7D8197 srgb(125,129,151)
11-> 12929: (145,172, 31) #91AC1F srgb(145,172,31)
12-> 13245: (158,200, 51) #9EC833 srgb(158,200,51)
13-> 17024: (175,210, 86) #AFD256 srgb(175,210,86)
14-> 7913: (177,192, 99) #B1C063 srgb(177,192,99)
15-> 12452: (202,217,188) #CAD9BC srgb(202,217,188)
</pre>


=={{header|Mathematica}} / {{header|Wolfram Language}}==
=={{header|Mathematica}} / {{header|Wolfram Language}}==