Added rounding code to the C++ port as well.
[zxing.git] / cpp / magick / src / MagickBitmapSource.cpp
1 /*
2  *  MagickBitmapSource.cpp
3  *  zxing
4  *
5  *  Copyright 2010 ZXing authors All rights reserved.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 #include "MagickBitmapSource.h"
21
22 #include <iostream>
23
24 using namespace Magick;
25
26 namespace zxing {
27
28 MagickBitmapSource::MagickBitmapSource(Image& image) : image_(image) {
29   width = image.columns();
30   height = image.rows();
31
32   pixel_cache = image.getConstPixels(0, 0, width, height);
33 }
34
35 MagickBitmapSource::~MagickBitmapSource() {
36
37 }
38
39 int MagickBitmapSource::getWidth() const {
40   return width;
41 }
42
43 int MagickBitmapSource::getHeight() const {
44   return height;
45 }
46
47 unsigned char* MagickBitmapSource::getRow(int y, unsigned char* row) {
48   int width = getWidth();
49   if (row == NULL) {
50     row = new unsigned char[width];
51   }
52   for (int x = 0; x < width; x++) {
53     const PixelPacket* p = pixel_cache + y * width + x;
54     // We assume 16 bit values here
55     // 0x200 = 1<<9, half an lsb of the result to force rounding
56     row[x] = (unsigned char)((306 * ((int)p->red >> 8) + 601 * ((int)p->green >> 8) +
57         117 * ((int)p->blue >> 8) + 0x200) >> 10);
58   }
59   return row;
60
61 }
62
63 /** This is a more efficient implementation. */
64 unsigned char* MagickBitmapSource::getMatrix() {
65   int width = getWidth();
66   int height =  getHeight();
67   unsigned char* matrix = new unsigned char[width*height];
68   unsigned char* m = matrix;
69   const Magick::PixelPacket* p = pixel_cache;
70   for (int y = 0; y < height; y++) {
71     for (int x = 0; x < width; x++) {
72       *m = (unsigned char)((306 * ((int)p->red >> 8) + 601 * ((int)p->green >> 8) +
73           117 * ((int)p->blue >> 8) + 0x200) >> 10);
74       m++;
75       p++;
76     }
77   }
78   return matrix;
79 }
80
81 bool MagickBitmapSource::isRotateSupported() const {
82   return false;
83 }
84
85 Ref<LuminanceSource> MagickBitmapSource::rotateCounterClockwise() {
86     //TODO(flyashi): add rotated image support.
87   /* this segfaults. I tried a few things, none seemed to work. Perhaps the problem is elsewhere? */
88   /*
89   Magick::Image rotated(image_);
90   rotated.modifyImage();
91   rotated.rotate(90); // Image::rotate takes CCW degrees as an argument
92   rotated.syncPixels();
93   return Ref<MagickBitmapSource> (new MagickBitmapSource(rotated));
94   */
95   return Ref<MagickBitmapSource> (NULL);
96 }
97
98 }
99