| 1 |
// -*- C++ -*- |
| 2 |
// $Id: NonLinearLaplacianHalftoning.cpp 4759 2008-06-19 19:02:27Z vbuzuev $ |
| 3 |
|
| 4 |
// DYMO LabelWriter Drivers |
| 5 |
// Copyright (C) 2008 Sanford L.P. |
| 6 |
|
| 7 |
// This program is free software; you can redistribute it and/or |
| 8 |
// modify it under the terms of the GNU General Public License |
| 9 |
// as published by the Free Software Foundation; either version 2 |
| 10 |
// of the License, or (at your option) any later version. |
| 11 |
|
| 12 |
// This program is distributed in the hope that it will be useful, |
| 13 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
// GNU General Public License for more details. |
| 16 |
|
| 17 |
// You should have received a copy of the GNU General Public License |
| 18 |
// along with this program; if not, write to the Free Software |
| 19 |
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 20 |
|
| 21 |
#include "NonLinearLaplacianHalftoning.h" |
| 22 |
#include <algorithm> |
| 23 |
|
| 24 |
//namespace dymo |
| 25 |
namespace DymoPrinterDriver |
| 26 |
{ |
| 27 |
|
| 28 |
// helper class defines image 'block' of 18 pixels |
| 29 |
class CNLLBlock |
| 30 |
{ |
| 31 |
public: |
| 32 |
// create a block from image with coordinates of pixel #1 at (x1, y1) |
| 33 |
CNLLBlock(CNLLHalftoning& Parent, const CHalftoneFilter::image_buffer_t& Image, int x1, int y1, CHalftoneFilter::image_buffer_t& OutputImage); |
| 34 |
|
| 35 |
// return true if at least on pixels of the block is insize the image |
| 36 |
bool IsInImage(); |
| 37 |
|
| 38 |
// fill block information |
| 39 |
void FillBlock(); |
| 40 |
void OutputBlock(); |
| 41 |
|
| 42 |
private: |
| 43 |
// return intense value of the block - original number of pixels to draw in 'black' |
| 44 |
size_t GetBlockIntenseValue(); |
| 45 |
|
| 46 |
// fill info for one of 18 pixels |
| 47 |
// PixelNo - ordinal number of the pixel in the block |
| 48 |
// (x, y) - coords of the pixel in original image |
| 49 |
void FillPixel(size_t PixelNo, int x, int y); |
| 50 |
|
| 51 |
// Split class 1 pixels to class 2 and class 5 to class 4 |
| 52 |
void ReduceClasses(); |
| 53 |
void ReduceClasses(size_t ClassFrom, size_t ClassTo); |
| 54 |
|
| 55 |
// return Laplacian value for pixel with coords (x, y) |
| 56 |
int GetNLL(int x, int y); |
| 57 |
|
| 58 |
// return grayscale value [0, 255] of pixel with coords (x, y) |
| 59 |
int GetPixelGray(int x, int y); |
| 60 |
|
| 61 |
// output Pixels of specific class |
| 62 |
// return number of pixels drawn |
| 63 |
size_t OutputClass(size_t ClassNo, size_t MaxPixelsToOutput); |
| 64 |
|
| 65 |
void OutputPixel(size_t PixelNo); |
| 66 |
void OutputPixel(int x, int y); |
| 67 |
|
| 68 |
// return true if pixel (x, y) is inside image |
| 69 |
bool IsInImage(int x, int y); |
| 70 |
|
| 71 |
CNLLHalftoning& Parent_; |
| 72 |
const CHalftoneFilter::image_buffer_t& Image_; |
| 73 |
CHalftoneFilter::image_buffer_t& OutputImage_; |
| 74 |
int x1_; |
| 75 |
int y1_; |
| 76 |
|
| 77 |
std::vector<int> Pixels_; // pixels' gray values |
| 78 |
std::vector<size_t> Classes_; // pixels' classes |
| 79 |
size_t ImageWidth_; |
| 80 |
size_t ImageHeight_; |
| 81 |
|
| 82 |
typedef struct |
| 83 |
{ |
| 84 |
int x; |
| 85 |
int y; |
| 86 |
} point_t; |
| 87 |
|
| 88 |
typedef struct |
| 89 |
{ |
| 90 |
size_t p1; |
| 91 |
size_t p2; |
| 92 |
size_t p3; |
| 93 |
size_t p4; |
| 94 |
} square_block_t; |
| 95 |
|
| 96 |
static const point_t PixelOffsets_[18]; |
| 97 |
static const square_block_t Squares_[8]; |
| 98 |
}; |
| 99 |
|
| 100 |
/* |
| 101 |
original |
| 102 |
const CNLLBlock::point_t CNLLBlock::PixelOffsets_[18] = |
| 103 |
{ |
| 104 |
{ 0, 0}, |
| 105 |
{-1, 0}, |
| 106 |
{ 0, -1}, |
| 107 |
{-1, -1}, |
| 108 |
{ 0, 1}, |
| 109 |
{-1, 1}, |
| 110 |
{ 1, 0}, |
| 111 |
{-2, 0}, |
| 112 |
{ 1, -1}, |
| 113 |
{-2, -1}, |
| 114 |
{ 1, 1}, |
| 115 |
{-2, 1}, |
| 116 |
{ 0, -2}, |
| 117 |
{-1, -2}, |
| 118 |
{ 0, 2}, |
| 119 |
{-1, 2}, |
| 120 |
{ 2, 0}, |
| 121 |
{-3, 0}, |
| 122 |
}; |
| 123 |
*/ |
| 124 |
|
| 125 |
/* |
| 126 |
const CNLLBlock::point_t CNLLBlock::PixelOffsets_[18] = |
| 127 |
{ |
| 128 |
{ 0, 0}, |
| 129 |
{-1, -2}, |
| 130 |
{ 0, 2}, |
| 131 |
{-3, 0}, |
| 132 |
{ 2, 0}, |
| 133 |
{-1, -1}, |
| 134 |
{ 1, 1}, |
| 135 |
{-1, 1}, |
| 136 |
{ 1, -1}, |
| 137 |
{-1, 2}, |
| 138 |
{ 0, -2}, |
| 139 |
{-2, -1}, |
| 140 |
{-2, 1}, |
| 141 |
{ 0, -1}, |
| 142 |
{-2, 0}, |
| 143 |
{ 1, 0}, |
| 144 |
{-1, 0}, |
| 145 |
{ 0, 1}, |
| 146 |
}; |
| 147 |
|
| 148 |
const CNLLBlock::square_block_t CNLLBlock::Squares_[8] = |
| 149 |
{ |
| 150 |
{ 2, 11, 6, 14}, |
| 151 |
{12, 6, 15, 17}, |
| 152 |
{ 6, 14, 17, 1}, |
| 153 |
{14, 9, 1, 16}, |
| 154 |
{15, 17, 13, 8}, |
| 155 |
{17, 1, 8, 18}, |
| 156 |
{ 1, 16, 18, 7}, |
| 157 |
{ 8, 18, 10, 3} |
| 158 |
}; |
| 159 |
*/ |
| 160 |
|
| 161 |
/* good |
| 162 |
const CNLLBlock::point_t CNLLBlock::PixelOffsets_[18] = |
| 163 |
{ |
| 164 |
{ 0, 0}, |
| 165 |
{-1, -1}, |
| 166 |
{ 1, 1}, |
| 167 |
{-1, 1}, |
| 168 |
{ 1, -1}, |
| 169 |
{-2, 0}, |
| 170 |
{ 2, 0}, |
| 171 |
{ 0, -2}, |
| 172 |
{ 0, 2}, |
| 173 |
{-1, 0}, |
| 174 |
{-2, -1}, |
| 175 |
{ 0, 1}, |
| 176 |
{-2, 1}, |
| 177 |
{ 0, -1}, |
| 178 |
{-3, 0}, |
| 179 |
{-1, -2}, |
| 180 |
{-1, 2}, |
| 181 |
{ 1, 0}, |
| 182 |
}; |
| 183 |
|
| 184 |
const CNLLBlock::square_block_t CNLLBlock::Squares_[8] = |
| 185 |
{ |
| 186 |
{16, 8, 2, 14}, |
| 187 |
{11, 2, 6, 10}, |
| 188 |
{ 2, 14, 10, 1}, |
| 189 |
{14, 5, 1, 18}, |
| 190 |
{ 6, 10, 13, 4}, |
| 191 |
{10, 1, 4, 12}, |
| 192 |
{ 1, 18, 12, 3}, |
| 193 |
{ 4, 12, 17, 9} |
| 194 |
}; |
| 195 |
*/ |
| 196 |
|
| 197 |
const CNLLBlock::point_t CNLLBlock::PixelOffsets_[18] = |
| 198 |
{ |
| 199 |
{ 0, 0}, |
| 200 |
{-1, 1}, |
| 201 |
{-1, -1}, |
| 202 |
{ 1, -1}, |
| 203 |
{ 1, 1}, |
| 204 |
{-2, 0}, |
| 205 |
{ 2, 0}, |
| 206 |
{ 0, -2}, |
| 207 |
{ 0, 2}, |
| 208 |
{-1, 0}, |
| 209 |
{-2, -1}, |
| 210 |
{-2, 1}, |
| 211 |
{ 0, -1}, |
| 212 |
{ 0, 1}, |
| 213 |
{-3, 0}, |
| 214 |
{-1, -2}, |
| 215 |
{-1, 2}, |
| 216 |
{ 1, 0}, |
| 217 |
}; |
| 218 |
|
| 219 |
const CNLLBlock::square_block_t CNLLBlock::Squares_[8] = |
| 220 |
{ |
| 221 |
{17, 8, 3, 13}, |
| 222 |
{11, 3, 6, 10}, |
| 223 |
{ 3, 13, 10, 1}, |
| 224 |
{13, 4, 1, 18}, |
| 225 |
{ 6, 10, 12, 2}, |
| 226 |
{10, 1, 2, 14}, |
| 227 |
{ 1, 18, 14, 5}, |
| 228 |
{ 2, 14, 16, 9} |
| 229 |
}; |
| 230 |
|
| 231 |
|
| 232 |
/* |
| 233 |
const CNLLBlock::point_t CNLLBlock::PixelOffsets_[18] = |
| 234 |
{ |
| 235 |
{ 0, 0}, |
| 236 |
{-1, -2}, |
| 237 |
{ 0, 2}, |
| 238 |
{-3, 0}, |
| 239 |
{ 2, 0}, |
| 240 |
{-2, 1}, |
| 241 |
{ 1, -1}, |
| 242 |
{-1, 0}, |
| 243 |
{ 1, 1}, |
| 244 |
{-2, -1}, |
| 245 |
{-1, 1}, |
| 246 |
{ 0, -2}, |
| 247 |
{ 1, 0}, |
| 248 |
{-1, 2}, |
| 249 |
{-1, -1}, |
| 250 |
{ 0, 1}, |
| 251 |
{-2, 0}, |
| 252 |
{ 0, -1}, |
| 253 |
}; |
| 254 |
*/ |
| 255 |
|
| 256 |
CNLLHalftoning::CNLLHalftoning(int Threshold, image_t InputImageType, image_t OutputImageType): |
| 257 |
CHalftoneFilter(InputImageType, OutputImageType), Threshold_(Threshold) |
| 258 |
{ |
| 259 |
if (GetOutputImageType() != itBW) |
| 260 |
throw EHalftoneError(EHalftoneError::heUnsupportedImageType); |
| 261 |
} |
| 262 |
|
| 263 |
CNLLHalftoning::~CNLLHalftoning() |
| 264 |
{ |
| 265 |
} |
| 266 |
|
| 267 |
bool |
| 268 |
CNLLHalftoning::IsProcessLineSupported() |
| 269 |
{ |
| 270 |
return false; |
| 271 |
} |
| 272 |
|
| 273 |
void |
| 274 |
CNLLHalftoning::ProcessLine( |
| 275 |
const buffer_t& InputLine, buffer_t& OutputLine) |
| 276 |
{ |
| 277 |
} |
| 278 |
|
| 279 |
void |
| 280 |
CNLLHalftoning::ProcessImage( |
| 281 |
const void* ImageData, size_t ImageWidth, size_t ImageHeight, size_t LineDelta, std::vector<buffer_t>& OutputImage) |
| 282 |
{ |
| 283 |
// TODO: non-implemented yet |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
void |
| 289 |
CNLLHalftoning::ProcessImage(const std::vector<buffer_t>& InputImage, std::vector<buffer_t>& OutputImage) |
| 290 |
{ |
| 291 |
OutputImage.clear(); |
| 292 |
if (InputImage.size() == 0) return; |
| 293 |
|
| 294 |
ImageWidth_ = CalcImageWidth(InputImage[0]); |
| 295 |
ImageHeight_ = InputImage.size(); |
| 296 |
|
| 297 |
// create an empty output image |
| 298 |
buffer_t EmptyLine; |
| 299 |
EmptyLine.resize(ImageWidth_ / 8 + 1, 0); |
| 300 |
OutputImage.resize(ImageHeight_, EmptyLine); |
| 301 |
|
| 302 |
// split the image to 18-pixels block |
| 303 |
size_t RowCount = (InputImage.size() + 1) / 3 + 1; |
| 304 |
for (size_t r = 0; r < RowCount; ++r) |
| 305 |
{ |
| 306 |
// get coordinates of pixel #1 |
| 307 |
size_t x1 = (r % 2) ? 3 : 0; |
| 308 |
size_t y1 = 3 * r; |
| 309 |
|
| 310 |
// for all blocks in the row |
| 311 |
// both leftest and rightest pixels of the block is |
| 312 |
while ((x1 - 3 < ImageWidth_) || (x1 + 2 < ImageWidth_)) |
| 313 |
{ |
| 314 |
CNLLBlock Block(*this, InputImage, x1, y1, OutputImage); |
| 315 |
|
| 316 |
Block.FillBlock(); |
| 317 |
Block.OutputBlock(); |
| 318 |
|
| 319 |
|
| 320 |
// advance to next block |
| 321 |
x1 += 6; |
| 322 |
} // for blocks in the row |
| 323 |
} // for rows |
| 324 |
} |
| 325 |
|
| 326 |
/* |
| 327 |
void |
| 328 |
CNLLHalftoning::ProcessImage(const std::vector<buffer_t>& InputImage, std::vector<buffer_t>& OutputImage) |
| 329 |
{ |
| 330 |
OutputImage.clear(); |
| 331 |
if (InputImage.size() == 0) return; |
| 332 |
|
| 333 |
ImageWidth_ = CalcImageWidth(InputImage[0]); |
| 334 |
ImageHeight_ = InputImage.size(); |
| 335 |
|
| 336 |
// create an empty output image |
| 337 |
buffer_t EmptyLine; |
| 338 |
EmptyLine.resize(ImageWidth_ / 8 + 1, 0); |
| 339 |
OutputImage.resize(ImageHeight_, EmptyLine); |
| 340 |
|
| 341 |
// split the image to 18-pixels block |
| 342 |
size_t x1 = 0; |
| 343 |
size_t y1 = 0; |
| 344 |
while (ProcessDiagonal(InputImage, OutputImage, x1, y1)) |
| 345 |
{ |
| 346 |
//x1 += 5; |
| 347 |
//y1 += 1; |
| 348 |
x1 += 2; |
| 349 |
y1 += 4; |
| 350 |
} |
| 351 |
} |
| 352 |
*/ |
| 353 |
|
| 354 |
bool |
| 355 |
CNLLHalftoning::ProcessDiagonal( |
| 356 |
const std::vector<buffer_t>& InputImage, std::vector<buffer_t>& OutputImage, size_t& x1, size_t& y1) |
| 357 |
{ |
| 358 |
//fprintf(stderr, "ProcessDiagonal(%i, %i)\n", x1, y1); |
| 359 |
|
| 360 |
bool Result = false; |
| 361 |
bool HasDownBlocks = false; |
| 362 |
bool HasUpBlocks = false; |
| 363 |
size_t UpperDownX = 0; |
| 364 |
size_t UpperDownY = 0; |
| 365 |
|
| 366 |
// go down |
| 367 |
size_t x = x1 - 3; |
| 368 |
size_t y = y1 + 3; |
| 369 |
while (true) |
| 370 |
{ |
| 371 |
CNLLBlock Block(*this, InputImage, x, y, OutputImage); |
| 372 |
|
| 373 |
if (Block.IsInImage()) |
| 374 |
{ |
| 375 |
//fprintf(stderr, "down Block (%i, %i)\n", x, y); |
| 376 |
Block.FillBlock(); |
| 377 |
Block.OutputBlock(); |
| 378 |
Result = true; |
| 379 |
|
| 380 |
if (!HasDownBlocks) |
| 381 |
{ |
| 382 |
UpperDownX = x; |
| 383 |
UpperDownY = y; |
| 384 |
HasDownBlocks = true; |
| 385 |
} |
| 386 |
|
| 387 |
x -= 3; |
| 388 |
y += 3; |
| 389 |
} |
| 390 |
else |
| 391 |
break; |
| 392 |
} // go down |
| 393 |
|
| 394 |
// go up |
| 395 |
x = x1; |
| 396 |
y = y1; |
| 397 |
while (true) |
| 398 |
{ |
| 399 |
CNLLBlock Block(*this, InputImage, x, y, OutputImage); |
| 400 |
|
| 401 |
if (Block.IsInImage()) |
| 402 |
{ |
| 403 |
//fprintf(stderr, "up Block (%i, %i)\n", x, y); |
| 404 |
|
| 405 |
Block.FillBlock(); |
| 406 |
Block.OutputBlock(); |
| 407 |
Result = true; |
| 408 |
HasUpBlocks = true; |
| 409 |
|
| 410 |
x1 = x; |
| 411 |
y1 = y; |
| 412 |
x += 3; |
| 413 |
y -= 3; |
| 414 |
} |
| 415 |
else |
| 416 |
break; |
| 417 |
} // go up |
| 418 |
|
| 419 |
if (Result && !HasUpBlocks) |
| 420 |
{ |
| 421 |
x1 = UpperDownX; |
| 422 |
y1 = UpperDownY; |
| 423 |
} |
| 424 |
//fprintf(stderr, "ProcessDiagonal returns (%i, %i)\n", x1, y1); |
| 425 |
|
| 426 |
return Result; |
| 427 |
} |
| 428 |
|
| 429 |
int |
| 430 |
CNLLHalftoning::GetThreshold() |
| 431 |
{ |
| 432 |
return Threshold_; |
| 433 |
} |
| 434 |
|
| 435 |
//////////////////////////////////////////////////////////////////////// |
| 436 |
// Block class methods |
| 437 |
//////////////////////////////////////////////////////////////////////// |
| 438 |
|
| 439 |
CNLLBlock::CNLLBlock( |
| 440 |
CNLLHalftoning& Parent, const CHalftoneFilter::image_buffer_t& Image, int x1, int y1, CHalftoneFilter::image_buffer_t& OutputImage): |
| 441 |
Parent_(Parent), Image_(Image), OutputImage_(OutputImage), x1_(x1), y1_(y1), Pixels_(18, 0), Classes_(18, 0) |
| 442 |
{ |
| 443 |
ImageWidth_ = Parent_.CalcImageWidth(Image_[0]); |
| 444 |
ImageHeight_ = Image_.size(); |
| 445 |
} |
| 446 |
|
| 447 |
void |
| 448 |
CNLLBlock::FillBlock() |
| 449 |
{ |
| 450 |
for (size_t i = 0; i < Pixels_.size(); ++i) |
| 451 |
FillPixel(i + 1, x1_ + PixelOffsets_[i].x, y1_ + PixelOffsets_[i].y); |
| 452 |
|
| 453 |
ReduceClasses(); |
| 454 |
|
| 455 |
} |
| 456 |
|
| 457 |
void |
| 458 |
CNLLBlock::ReduceClasses() |
| 459 |
{ |
| 460 |
//ReduceClasses(1, 2); |
| 461 |
//ReduceClasses(5, 4); |
| 462 |
} |
| 463 |
|
| 464 |
void |
| 465 |
CNLLBlock::ReduceClasses(size_t ClassFrom, size_t ClassTo) |
| 466 |
{ |
| 467 |
for (size_t i = 0; i < 8; ++i) |
| 468 |
{ |
| 469 |
if ((Classes_[Squares_[i].p1 - 1] == ClassFrom) |
| 470 |
&& (Classes_[Squares_[i].p2 - 1] == ClassFrom) |
| 471 |
&& (Classes_[Squares_[i].p3 - 1] == ClassFrom) |
| 472 |
&& (Classes_[Squares_[i].p4 - 1] == ClassFrom)) |
| 473 |
{ |
| 474 |
Classes_[Squares_[i].p1 - 1] = ClassTo; |
| 475 |
Classes_[Squares_[i].p3 - 1] = ClassTo; |
| 476 |
} |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
void |
| 481 |
CNLLBlock::FillPixel(size_t PixelNo, int x, int y) |
| 482 |
{ |
| 483 |
// fill pixels |
| 484 |
Pixels_[PixelNo - 1] = GetPixelGray(x, y); |
| 485 |
|
| 486 |
// fill classes |
| 487 |
int NLL = GetNLL(x, y); |
| 488 |
int Threshold = Parent_.GetThreshold(); |
| 489 |
|
| 490 |
// we added to new classes to those described in the papers |
| 491 |
// 0 - (same as 1) - it is set for black pixels, those should remeined black |
| 492 |
// 6 - (same as 5) - it is set for white pixels, those should remeined white |
| 493 |
|
| 494 |
if (Pixels_[PixelNo - 1] == 0) |
| 495 |
Classes_[PixelNo - 1] = 0; |
| 496 |
else if (Pixels_[PixelNo - 1] == 255) |
| 497 |
Classes_[PixelNo - 1] = 6; |
| 498 |
else // as in papers |
| 499 |
if (NLL < -Threshold) |
| 500 |
Classes_[PixelNo - 1] = 1; |
| 501 |
else if (NLL > Threshold) |
| 502 |
Classes_[PixelNo - 1] = 5; |
| 503 |
else |
| 504 |
Classes_[PixelNo - 1] = 3; |
| 505 |
|
| 506 |
} |
| 507 |
|
| 508 |
int |
| 509 |
CNLLBlock::GetPixelGray(int x, int y) |
| 510 |
{ |
| 511 |
if (IsInImage(x, y)) |
| 512 |
{ |
| 513 |
byte R, G, B; |
| 514 |
Parent_.ExtractRGB(Image_[y], x, R, G, B); |
| 515 |
return Parent_.RGBToGrayScale(R, G, B); |
| 516 |
} |
| 517 |
else |
| 518 |
return 255; // white |
| 519 |
} |
| 520 |
|
| 521 |
int |
| 522 |
CNLLBlock::GetNLL(int x, int y) |
| 523 |
{ |
| 524 |
int A = |
| 525 |
GetPixelGray(x, y) |
| 526 |
- ( GetPixelGray(x - 1, y - 1) |
| 527 |
+ GetPixelGray(x + 1, y - 1) |
| 528 |
+ GetPixelGray(x - 1, y + 1) |
| 529 |
+ GetPixelGray(x + 1, y + 1)) / 4; |
| 530 |
|
| 531 |
int B = |
| 532 |
GetPixelGray(x, y) |
| 533 |
- ( GetPixelGray(x - 0, y - 1) |
| 534 |
+ GetPixelGray(x + 0, y + 1) |
| 535 |
+ GetPixelGray(x - 1, y + 0) |
| 536 |
+ GetPixelGray(x + 1, y + 0)) / 4; |
| 537 |
|
| 538 |
if ((A > 0) && (B > 0)) |
| 539 |
return std::min(A, B); |
| 540 |
|
| 541 |
if ((A < 0) && (B < 0)) |
| 542 |
return -std::min(abs(A), abs(B)); |
| 543 |
|
| 544 |
return 0; |
| 545 |
} |
| 546 |
|
| 547 |
|
| 548 |
size_t |
| 549 |
CNLLBlock::GetBlockIntenseValue() |
| 550 |
{ |
| 551 |
size_t Intense = 128; |
| 552 |
for (size_t i = 0; i < Pixels_.size(); ++i) |
| 553 |
Intense += Pixels_[i]; |
| 554 |
|
| 555 |
size_t result = 18 - std::min(Intense / 255, size_t(18)); |
| 556 |
//fprintf(stderr, "GetBlockIntenseValue() = %d\n", result); |
| 557 |
return result; |
| 558 |
} |
| 559 |
|
| 560 |
void |
| 561 |
CNLLBlock::OutputBlock() |
| 562 |
{ |
| 563 |
size_t RemainedPixels = GetBlockIntenseValue(); |
| 564 |
size_t PixelCount = 0; |
| 565 |
|
| 566 |
// output all pixels for class 0 |
| 567 |
PixelCount = OutputClass(0, 18); |
| 568 |
|
| 569 |
if (PixelCount < RemainedPixels) |
| 570 |
{ |
| 571 |
RemainedPixels -= PixelCount; |
| 572 |
RemainedPixels -= OutputClass(1, RemainedPixels); |
| 573 |
RemainedPixels -= OutputClass(2, RemainedPixels); |
| 574 |
RemainedPixels -= OutputClass(3, RemainedPixels); |
| 575 |
RemainedPixels -= OutputClass(4, RemainedPixels); |
| 576 |
} |
| 577 |
} |
| 578 |
|
| 579 |
size_t |
| 580 |
CNLLBlock::OutputClass(size_t ClassNo, size_t MaxPixelsToOutput) |
| 581 |
{ |
| 582 |
//fprintf(stderr, "OutputClass(%i, %i)\n", ClassNo, MaxPixelsToOutput); |
| 583 |
|
| 584 |
if (MaxPixelsToOutput == 0) |
| 585 |
return 0; |
| 586 |
|
| 587 |
std::vector<size_t> Pixels; |
| 588 |
|
| 589 |
// collect all pixels of a class |
| 590 |
for (size_t i = 0; i < Classes_.size(); ++i) |
| 591 |
if (Classes_[i] == ClassNo) |
| 592 |
Pixels.push_back(i + 1); |
| 593 |
|
| 594 |
// sort pixels, so output it in order |
| 595 |
//std::sort(Pixels.begin(), Pixels.end()); |
| 596 |
//std::random_shuffle(Pixels.begin(), Pixels.end()); |
| 597 |
|
| 598 |
// output pixels |
| 599 |
size_t Result = 0; |
| 600 |
for (size_t i = 0; i < Pixels.size(); ++i) |
| 601 |
if (Result < MaxPixelsToOutput) |
| 602 |
{ |
| 603 |
OutputPixel(Pixels[i]); |
| 604 |
++Result; |
| 605 |
} |
| 606 |
else |
| 607 |
break; |
| 608 |
|
| 609 |
return Result; |
| 610 |
} |
| 611 |
|
| 612 |
void |
| 613 |
CNLLBlock::OutputPixel(size_t PixelNo) |
| 614 |
{ |
| 615 |
OutputPixel(x1_ + PixelOffsets_[PixelNo - 1].x, y1_ + PixelOffsets_[PixelNo - 1].y); |
| 616 |
} |
| 617 |
|
| 618 |
void |
| 619 |
CNLLBlock::OutputPixel(int x, int y) |
| 620 |
{ |
| 621 |
if (IsInImage(x, y)) |
| 622 |
Parent_.SetPixelBW(OutputImage_[y], x, 1); |
| 623 |
} |
| 624 |
|
| 625 |
bool |
| 626 |
CNLLBlock::IsInImage() |
| 627 |
{ |
| 628 |
for (size_t i = 0; i < Pixels_.size(); ++i) |
| 629 |
if (IsInImage(x1_ + PixelOffsets_[i].x, y1_ + PixelOffsets_[i].y)) |
| 630 |
return true; |
| 631 |
|
| 632 |
return false; |
| 633 |
} |
| 634 |
|
| 635 |
bool |
| 636 |
CNLLBlock::IsInImage(int x, int y) |
| 637 |
{ |
| 638 |
return (x >= 0) && (size_t(x) < ImageWidth_) && (y >= 0) && (size_t(y) < ImageHeight_); |
| 639 |
} |
| 640 |
|
| 641 |
|
| 642 |
} // namespace |
| 643 |
|
| 644 |
/* |
| 645 |
* End of "$Id: NonLinearLaplacianHalftoning.cpp 4759 2008-06-19 19:02:27Z vbuzuev $". |
| 646 |
*/ |