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