C++: binarizer updates
[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     row[x] = (unsigned char)((306 * ((int)p->red >> 8) + 601 * ((int)p->green >> 8) + 117 * ((int)p->blue >> 8)) >> 10);
56   }
57   return row;
58
59 }
60
61 /** This is a more efficient implementation. */
62 unsigned char* MagickBitmapSource::getMatrix() {
63   int width = getWidth();
64   int height =  getHeight();
65   unsigned char* matrix = new unsigned char[width*height];
66   unsigned char* m = matrix;
67   const Magick::PixelPacket* p = pixel_cache;
68   for (int y = 0; y < height; y++) {
69     for (int x = 0; x < width; x++) {
70       *m = (unsigned char)((306 * ((int)p->red >> 8) + 601 * ((int)p->green >> 8) + 117 * ((int)p->blue >> 8)) >> 10);
71       m++;
72       p++;
73     }
74   }
75   return matrix;
76 }
77
78 bool MagickBitmapSource::isRotateSupported() const {
79   return false;
80 }
81
82 Ref<LuminanceSource> MagickBitmapSource::rotateCounterClockwise() {
83     //TODO(flyashi): add rotated image support.
84   /* this segfaults. I tried a few things, none seemed to work. Perhaps the problem is elsewhere? */
85   /*
86   Magick::Image rotated(image_);
87   rotated.modifyImage();
88   rotated.rotate(90); // Image::rotate takes CCW degrees as an argument
89   rotated.syncPixels();
90   return Ref<MagickBitmapSource> (new MagickBitmapSource(rotated));
91   */
92   return Ref<MagickBitmapSource> (NULL);
93 }
94
95 }
96