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 row[x] = (unsigned char)((306 * ((int)p->red >> 8) + 601 * ((int)p->green >> 8) + 117 * ((int)p->blue >> 8)) >> 10);
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);
78 bool MagickBitmapSource::isRotateSupported() const {
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? */
86 Magick::Image rotated(image_);
87 rotated.modifyImage();
88 rotated.rotate(90); // Image::rotate takes CCW degrees as an argument
90 return Ref<MagickBitmapSource> (new MagickBitmapSource(rotated));
92 return Ref<MagickBitmapSource> (NULL);