how to get it

Language used: R

Read the PNG-file

By default, the data is read column by column, so you need to reshape the data.

library(png)
my.matrix <- readPNG("int_RGB_8bit-1.png")
red <- as.vector(matrix(my.matrix[,,1], nrow = 490, byrow = T))
green <- as.vector(matrix(my.matrix[,,2], nrow = 490, byrow = T))
blue <- as.vector(matrix(my.matrix[,,3], nrow = 490, byrow = T))

Understanding the key

The last 256 pixels is the "key", meaning that they show how to transpose a color to a single byte. The first 80 pixels are a grayscale, then comes 33 pixels of a blue scale, followed by a red scale, also 33 bytes long. My first idea was that the gray part of the key indicated how the colored part of the key should be interpreted, but since the color part was longer than the gray part, that didn't really make sense. The second idea is to use the key as this: the position in the key gives the value, since the key is exactly 256 bytes, this definately makes sense.

For each color in the data part, we should record a value that corresponds to the position of that color in the key.

my.song <- matrix(0, nrow = 490, ncol = 490)
my.length <- 490 * 490
for(i in 0:255) {
## i is the position in the key and value to record
## what is the red, green and blue-values at this position in the key?
abs.pos <- my.length-255+i
## abs.pos is the absolute position in the vector of position i in the key.
my.red <- red[abs.pos]
my.green <- green[abs.pos]
my.blue <- blue[abs.pos]
## at what positions in data do we find this color?
here <- which(red == my.red & green == my.green & blue == my.blue)
## at those positions, record the i value
my.song[here] <- i
}
## Remove the last 256 bytes from the sound-data, since these are the key
my.song <- my.song[-c((my.length-255):my.length)]

Write to a binary file

my.con <- file("test.raw", "wb")
writeBin(as.integer(my.song), con = my.con, size = 1)
close(my.con)

Playing

I use aplay from ALSA.

aplay -r 8000 -c 1 -f U8 test.raw

comments powered by Disqus


Back to the index

Blog roll

R-bloggers, Debian Weekly
Valid XHTML 1.0 Strict [Valid RSS] Valid CSS! Emacs Muse Last modified: oktober 17, 2019