<< Chapter < Page | Chapter >> Page > |
In Media Representation in Processing we saw how one devotes bits to each channel corresponding to a primary color. If we add to these the alpha channel, the total number of bits per pixel becomes . We do not always have the possibility to use such a big amount of memory for colors. Therefore, one has to adopt various strategies in order to reduce the number of bits per pixel.
A first solution comes from the observation that usually in an image, not all of the representable colors are present at the same time. Supposing that the number of colors necessary for an ordinary image is not greater than , one can think about memorizing the codes of the colors in a table( palette ), whose elements are accessible by means of an index of only bits. Thus, the image will require a memory space of bits per pixel plus the space necessary for the palette. For examples and further explanations see color depth in Wikipedia.
Alternatively, in order to have a low number of bits per pixel, one can apply a digital processing technique called dithering . The idea is that of obtaining a mixture of colors in a perceptual way, exploiting the proximity of small points of different color. An exhaustivepresentation of the phenomenon can be found at the voice dithering of Wikipedia.
The Floyd-Steinberg's algorithm is one of the most popular techniques for the distribution of colored pixels in order to obtain adithering effect. The idea is to minimize the visual artifacts by processing the error-diffusion. The algorithm can be resumedas follows:
By means of Processing, implement a program to process the file lena , a "dithered" black and white version of the famous Lena image. The image, treated only in the left half, should result similar to that of [link]
size(300, 420);
PImage a; // Declare variable "a" of type PImagea = loadImage("lena.jpg"); // Load the images into the program
image(a, 0, 0); // Displays the image from point (0,0)int[][] output = new int[width][height];for (int i=0; i<width; i++)
for (int j=0; j<height; j++) {
output[i][j] = (int)red(get(i, j));}
int grayVal;float errore;
float val = 1.0/16.0;float[][]kernel = { {0, 0, 0},
{0, -1, 7*val},{3*val, 5*val, val }};
for(int y=0; y<height; y++) {
for(int x=0; x<width; x++) {
grayVal = output[x][y];// (int)red(get(x, y));if (grayVal<128) errore=grayVal;
else errore=grayVal-256;for(int k=-1; k<=1; k++) {
for(int j=-1; j<=0 /*1*/; j++) {
// Reflect x-j to not exceed array boundaryint xp = x-j;
int yp = y-k;if (xp<0) {
xp = xp + width;} else if (x-j>= width) {
xp = xp - width;}
// Reflect y-k to not exceed array boundaryif (yp<0) {
yp = yp + height;} else if (yp>= height) {
yp = yp - height;}
output[xp][yp] = (int)(output[xp][yp] + errore * kernel[-j+1][-k+1]);}
}}
}for(int i=0; i<height; i++)
for(int j=0; j<width; j++)
if (output[j][i]<128) output[j][i]= 0;
else output[j][i] = 255;// Display the result of dithering on half imageloadPixels();
for(int i=0; i<height; i++) {
for(int j=0; j<width/2; j++) {
pixels[i*width + j]=
color(output[j][i], output[j][i], output[j][i]);}
}updatePixels();
Notification Switch
Would you like to follow the 'Media processing in processing' conversation and receive update notifications?