| 1 |
// -*- C++ -*- |
| 2 |
// $Id: ErrorDiffusionHalftoning.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 "ErrorDiffusionHalftoning.h" |
| 22 |
#include <algorithm> |
| 23 |
|
| 24 |
//namespace dymo |
| 25 |
namespace DymoPrinterDriver |
| 26 |
{ |
| 27 |
|
| 28 |
|
| 29 |
CErrorDiffusionHalftoning::CErrorDiffusionHalftoning(image_t InputImageType, image_t OutputImageType, bool UsePrinterColorSpace): |
| 30 |
CHalftoneFilter(InputImageType, OutputImageType), ImageWidth_(0), Errors_(), GrayLine_(), UsePrinterColorSpace_(UsePrinterColorSpace) |
| 31 |
{ |
| 32 |
if (GetOutputImageType() != itBW) |
| 33 |
throw EHalftoneError(EHalftoneError::heUnsupportedImageType); |
| 34 |
} |
| 35 |
|
| 36 |
CErrorDiffusionHalftoning::~CErrorDiffusionHalftoning() |
| 37 |
{ |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
bool |
| 42 |
CErrorDiffusionHalftoning::IsProcessLineSupported() |
| 43 |
{ |
| 44 |
return true; |
| 45 |
} |
| 46 |
|
| 47 |
void |
| 48 |
CErrorDiffusionHalftoning::ProcessLine( |
| 49 |
const buffer_t& InputLine, buffer_t& OutputLine) |
| 50 |
{ |
| 51 |
int pixelValue = 0; |
| 52 |
int error = 0; |
| 53 |
size_t i = 0; |
| 54 |
|
| 55 |
// set image width |
| 56 |
if (!ImageWidth_) |
| 57 |
ImageWidth_ = CalcImageWidth(InputLine); |
| 58 |
|
| 59 |
// check buffer size |
| 60 |
OutputLine.resize(CalcOutputBufferSize(ImageWidth_)); |
| 61 |
std::fill(OutputLine.begin(), OutputLine.end(), byte(0)); |
| 62 |
|
| 63 |
// initialize halftone errors array and line buffer |
| 64 |
if (Errors_.size() == 0) |
| 65 |
Errors_.resize(ImageWidth_, 0); |
| 66 |
|
| 67 |
if (GrayLine_.size() == 0) |
| 68 |
GrayLine_.resize(ImageWidth_, 0); |
| 69 |
|
| 70 |
// convert from RGB to GrayScale |
| 71 |
for (i = 0; i < ImageWidth_; ++i) |
| 72 |
{ |
| 73 |
byte R, G, B; |
| 74 |
ExtractRGB(InputLine, i, R, G, B); |
| 75 |
GrayLine_[i] = RGBToGrayScale(R, G, B); |
| 76 |
} |
| 77 |
|
| 78 |
// apply errors from prev line |
| 79 |
for (i = 0; i < ImageWidth_; ++i) |
| 80 |
{ |
| 81 |
//fprintf(stderr, "%i ", Errors_[i]); |
| 82 |
// only if not black and white |
| 83 |
if ((GrayLine_[i] != 0) && (GrayLine_[i] != 255)) |
| 84 |
{ |
| 85 |
if (Errors_[i] + GrayLine_[i] >= 255) |
| 86 |
GrayLine_[i] = 255; |
| 87 |
else |
| 88 |
if (Errors_[i] + GrayLine_[i] <= 0) |
| 89 |
GrayLine_[i] = 0; |
| 90 |
else |
| 91 |
GrayLine_[i] += Errors_[i]; |
| 92 |
} |
| 93 |
|
| 94 |
Errors_[i] = 0; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
// compute output pixels and new errors |
| 99 |
for (i = 0; i < ImageWidth_; ++i) |
| 100 |
{ |
| 101 |
pixelValue = GrayLine_[i] >= 128; |
| 102 |
error = GrayLine_[i] - pixelValue * 255; |
| 103 |
|
| 104 |
if (UsePrinterColorSpace_) |
| 105 |
SetPixelBW(OutputLine, i, !pixelValue); |
| 106 |
else |
| 107 |
SetPixelBW(OutputLine, i, pixelValue); |
| 108 |
|
| 109 |
|
| 110 |
// disribute error |
| 111 |
if (i > 0) |
| 112 |
Errors_[i - 1] += (error * 3) / 16; |
| 113 |
|
| 114 |
Errors_[i] += (error * 5) / 16; |
| 115 |
|
| 116 |
if (i < ImageWidth_ - 1) |
| 117 |
{ |
| 118 |
Errors_[i + 1] += (error * 1) / 16; |
| 119 |
|
| 120 |
if ((GrayLine_[i + 1] != 0) && (GrayLine_[i + 1] != 255)) |
| 121 |
{ |
| 122 |
error = (error * 7) / 16; |
| 123 |
|
| 124 |
if (error + GrayLine_[i + 1] >= 255) |
| 125 |
GrayLine_[i + 1] = 255; |
| 126 |
else |
| 127 |
if (error + GrayLine_[i + 1] <= 0) |
| 128 |
GrayLine_[i + 1] = 0; |
| 129 |
else |
| 130 |
GrayLine_[i + 1] += error; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
} // for all pixels |
| 135 |
} |
| 136 |
|
| 137 |
void |
| 138 |
CErrorDiffusionHalftoning::ProcessImage( |
| 139 |
const void* ImageData, size_t ImageWidth, size_t ImageHeight, size_t LineDelta, std::vector<buffer_t>& OutputImage) |
| 140 |
{ |
| 141 |
OutputImage.clear(); |
| 142 |
|
| 143 |
buffer_t InputLine; |
| 144 |
size_t BufferSize = CalcBufferSize(ImageWidth); |
| 145 |
InputLine.resize(BufferSize, 0); |
| 146 |
|
| 147 |
for (size_t i = 0; i < ImageHeight; ++i) |
| 148 |
{ |
| 149 |
InputLine.assign( |
| 150 |
(byte*)ImageData + LineDelta*i, |
| 151 |
(byte*)ImageData + LineDelta*i + BufferSize); |
| 152 |
|
| 153 |
OutputImage.push_back(buffer_t()); |
| 154 |
ProcessLine(InputLine, OutputImage[OutputImage.size() - 1]); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
void |
| 159 |
CErrorDiffusionHalftoning::ProcessImage(const std::vector<buffer_t>& InputImage, std::vector<buffer_t>& OutputImage) |
| 160 |
{ |
| 161 |
OutputImage.clear(); |
| 162 |
|
| 163 |
buffer_t OutputLine; |
| 164 |
|
| 165 |
for (std::vector<buffer_t>::const_iterator i = InputImage.begin(); i < InputImage.end(); ++i) |
| 166 |
{ |
| 167 |
ProcessLine(*i, OutputLine); |
| 168 |
OutputImage.push_back(OutputLine); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
} // namespace |
| 174 |
|
| 175 |
/* |
| 176 |
* End of "$Id: ErrorDiffusionHalftoning.cpp 4759 2008-06-19 19:02:27Z vbuzuev $". |
| 177 |
*/ |