2 * MagickBitmapSource.cpp
5 * Copyright 2010 ZXing authors All rights reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include "MagickBitmapSource.h"
24 using namespace Magick;
28 MagickBitmapSource::MagickBitmapSource(Image& image) : image_(image) {
29 width = image.columns();
30 height = image.rows();
32 pixel_cache = image.getConstPixels(0, 0, width, height);
35 MagickBitmapSource::~MagickBitmapSource() {
39 int MagickBitmapSource::getWidth() const {
43 int MagickBitmapSource::getHeight() const {
47 unsigned char* MagickBitmapSource::getRow(int y, unsigned char* row) {
48 int width = getWidth();
50 row = new unsigned char[width];
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);
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);
81 bool MagickBitmapSource::isRotateSupported() const {
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? */
89 Magick::Image rotated(image_);
90 rotated.modifyImage();
91 rotated.rotate(90); // Image::rotate takes CCW degrees as an argument
93 return Ref<MagickBitmapSource> (new MagickBitmapSource(rotated));
95 return Ref<MagickBitmapSource> (NULL);