00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00026 #include "Stucki.h"
00027
00028 namespace halftoner
00029 {
00030
00031
00032
00033
00034
00035 Stucki::Stucki()
00036 {
00037
00038 }
00039
00040
00041
00042
00043
00044
00045 Stucki::~Stucki()
00046 {
00047
00048 }
00049
00050
00051
00052
00053
00054
00055 int Stucki::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 * (8.0f / 42.0f);
00067 }
00068
00069 bool two_on_right = (i + 2) < width;
00070 if (two_on_right)
00071 {
00072 float* pixel = source + offset + 2;
00073 *pixel += diff * (4.0f / 42.0f);
00074 }
00075 if (j + 1 < height)
00076 {
00077 float* pixel = source + offset + width - 2;
00078
00079 if ((i - 2) >= 0)
00080 {
00081 *pixel += diff * (2.0f / 42.0f);
00082 }
00083 pixel++;
00084
00085
00086 if (i - 1 >= 0)
00087 {
00088 *pixel += diff * (4.0f / 42.0f);
00089 }
00090 pixel++;
00091
00092
00093 *pixel += diff * (8.0f / 42.0f);
00094
00095
00096 if (one_on_right)
00097 {
00098 pixel++;
00099 *pixel += diff * (4.0f / 42.0f);
00100 }
00101
00102
00103 if (two_on_right)
00104 {
00105 pixel++;
00106 *pixel += diff * (2.0f / 42.0f);
00107 }
00108 }
00109 if (j + 2 < height)
00110 {
00111 float* pixel = source + offset + width + width - 2;
00112
00113 if (i - 2 >= 0)
00114 {
00115 *pixel += diff * (1.0f / 42.0f);
00116 }
00117 pixel++;
00118
00119
00120 if (i - 1 >= 0)
00121 {
00122 *pixel += diff * (2.0f / 42.0f);
00123 }
00124 pixel++;
00125
00126
00127 *pixel += diff * (4.0f / 42.0f);
00128
00129
00130 if (one_on_right)
00131 {
00132 pixel++;
00133 *pixel += diff * (2.0f / 42.0f);
00134 }
00135
00136 if (two_on_right)
00137 {
00138 pixel++;
00139 *pixel += diff * (1.0f / 42.0f);
00140 }
00141 }
00142
00143 return bw;
00144 }
00145
00146 int Stucki::halftoneToLeft(float* source, int i, int j, int offset, int width, int height, int threshold) throw()
00147 {
00148 float grey = source[offset];
00149 int bw = (grey < threshold) ? 0 : 1;
00150 float diff = grey - 255.0f * bw;
00151
00152
00153 bool one_on_left = (i - 1) >= 0;
00154 if (one_on_left)
00155 {
00156 float* pixel = source + offset - 1;
00157 *pixel += diff * (8.0f / 42.0f);
00158 }
00159
00160 bool two_on_left = (i - 2) >= 0;
00161 if (two_on_left)
00162 {
00163 float* pixel = source + offset - 2;
00164 *pixel += diff * (4.0f / 42.0f);
00165 }
00166 if (j + 1 < height)
00167 {
00168 float* pixel = source + offset + width + 2;
00169
00170 if ((i + 2) < width)
00171 {
00172 *pixel += diff * (2.0f / 42.0f);
00173 }
00174 pixel--;
00175
00176
00177 if ((i + 1) < width)
00178 {
00179 *pixel += diff * (4.0f / 42.0f);
00180 }
00181 pixel--;
00182
00183
00184 *pixel += diff * (8.0f / 42.0f);
00185
00186
00187 if (one_on_left)
00188 {
00189 pixel--;
00190 *pixel += diff * (4.0f / 42.0f);
00191 }
00192
00193
00194 if (two_on_left)
00195 {
00196 pixel--;
00197 *pixel += diff * (2.0f / 42.0f);
00198 }
00199 }
00200 if (j + 2 < height)
00201 {
00202 float* pixel = source + offset + width + width + 2;
00203
00204 if ((i + 2) < width)
00205 {
00206 *pixel += diff * (1.0f / 42.0f);
00207 }
00208 pixel--;
00209
00210
00211 if ((i + 1) < width)
00212 {
00213 *pixel += diff * (2.0f / 42.0f);
00214 }
00215 pixel--;
00216
00217
00218 *pixel += diff * (4.0f / 42.0f);
00219
00220
00221 if (one_on_left)
00222 {
00223 pixel--;
00224 *pixel += diff * (2.0f / 42.0f);
00225 }
00226
00227 if (two_on_left)
00228 {
00229 pixel--;
00230 *pixel += diff * (1.0f / 42.0f);
00231 }
00232 }
00233
00234 return bw;
00235 }
00236
00237 }