00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00026 #include "FloydSteinberg.h"
00027
00028 namespace halftoner
00029 {
00030
00031
00032
00033
00034
00035 FloydSteinberg::FloydSteinberg()
00036 {
00037
00038 }
00039
00040
00041
00042
00043
00044
00045 FloydSteinberg::~FloydSteinberg()
00046 {
00047
00048 }
00049
00050
00051
00052
00053
00054
00055 int FloydSteinberg::halftoneToRight(float* source, int i, int j, int offset, int width, int height, int threshold) throw()
00056 {
00057 float grey = source[offset];
00058 int bw = (grey < threshold) ? 0 : 1;
00059 float diff = grey - 255.0f * bw;
00060
00061
00062 bool one_on_right = (i + 1) < width;
00063 if (one_on_right)
00064 {
00065 float* pixel = source + offset + 1;
00066 *pixel += diff * (7.0f / 16.0f);
00067 }
00068
00069
00070 if ((j + 1) < height)
00071 {
00072 float* pixel = source + offset + width - 1;
00073
00074 if (i > 0)
00075 {
00076 *pixel += diff * (3.0f / 16.0f);
00077 }
00078 pixel++;
00079
00080
00081 *pixel += diff * (5.0f / 16.0f);
00082 pixel++;
00083
00084
00085 if (one_on_right)
00086 {
00087 *pixel += diff * (1.0f / 16.0f);
00088 }
00089 }
00090
00091 return bw;
00092 }
00093
00094 int FloydSteinberg::halftoneToLeft(float* source, int i, int j, int offset, int width, int height, int threshold) throw()
00095 {
00096 float grey = source[offset];
00097 int bw = (grey < threshold) ? 0 : 1;
00098 float diff = grey - 255.0f * bw;
00099
00100
00101 bool one_on_left = (i - 1) >= 0;
00102 if (one_on_left)
00103 {
00104 float* pixel = source + offset - 1;
00105 *pixel += diff * (7.0f / 16.0f);
00106 }
00107
00108
00109 if ((j + 1) < height)
00110 {
00111 float* pixel = source + offset + width + 1;
00112
00113 if (i + 1 < width)
00114 {
00115 *pixel += diff * (3.0f / 16.0f);
00116 }
00117 pixel--;
00118
00119
00120 *pixel += diff * (5.0f / 16.0f);
00121 pixel--;
00122
00123
00124 if (one_on_left)
00125 {
00126 *pixel += diff * (1.0f / 16.0f);
00127 }
00128 }
00129
00130 return bw;
00131 }
00132
00133 }