<! -- @ Page {margin: 2 cm} P {margin-bottom: 0.21} -->
Next we will analyze the 565 algorithm with jitter conversion:
Void to_565_raw_dither (int width)
{
Unsigned char in [3];
Unsigned short out;
Int I = 0;
Int e;
Create an array for saving the error of two points.
Int * error = malloc (width + 2) * 3 * sizeof (int ));
Int * next_error = malloc (width + 2) * 3 * sizeof (int ));
Clear the error array.
Memset (error, 0, (width + 2) * 3 * sizeof (int ));
Memset (next_error, 0, (width + 2) * 3 * sizeof (int ));
The calculated array is calculated using three point errors, so it starts from-3.
Error + = 3; // array goes from [-3 .. (width + 1) * 3 + 2)]
Next_error + = 3;
Read data from the original file.
While (read (0, in, 3) = 3 ){
Calculate the value of the current vertex. Of course, the previous vertex value has been used for diffusion.
Int r = in [0] + error [I * 3 + 0];
Int rb = (r <0 )? 0: (r> 255 )? 255: r );
Int g = in [1] + error [I * 3 + 1];
Int gb = (g <0 )? 0: (g> 255 )? 255: g );
Int B = in [2] + error [I * 3 + 2];
Int bb = (B <0 )? 0: (B> 255 )? 255: B );
Out = to565 (rb, gb, bb );
Write (1, & out, 2 );
Calculate the error value to adjust the color of the next vertex.
# Define apply_error (ch ){
Next_error [(I-1) * 3 + ch] + = e * 3/16;
Next_error [(I) * 3 + ch] + = e * 5/16;
Next_error [(I + 1) * 3 + ch] + = e * 1/16;
Error [(I + 1) * 3 + ch] + = e-(e * 1/16) + (e * 3/16) + (e * 5/16 ));
}
The calculated color is equal to the error value recovered from 565.
E = r-from565_r (out );
Apply_error (0 );
E = g-from565_g (out );
Apply_error (1 );
E = B-from565_ B (out );
Apply_error (2 );
# Undef apply_error
++ I;
Finally, clear the error value.
If (I = width ){
// Error <-next_error; next_error <-0
Int * temp = error; error = next_error; next_error = temp;
Memset (next_error, 0, (width + 1) * 3 * sizeof (int ));
I = 0;
}
}
Delete the allocated space.
Free (error-3 );
Free (next_error-3 );
Return;
}
This brief code analysis has been completed here. Although such a program does not exist, it still involves many technologies, for example, shell understanding, 565 color encoding, travel encoding, and chromatic aberration jitter algorithms. Therefore, to write the above program, we need to understand a wide range of knowledge. Who said the knowledge is useless?