int AlgorithmShiftNoise::DoYourJob(Magick::Image& image) { srand(time(NULL)); image.modifyImage(); Magick::Pixels pixelCache(image); Magick::PixelPacket* pixels=pixelCache.get(0,0,image.columns(), image.rows()); for (int fromX=0; fromX<image.columns(); fromX++) for(int fromY=0; fromY<image.rows(); fromY++) { int luck=rand()%100; if (luck>Percent) continue; int toX=rand()%image.columns(); int toY=rand()%image.rows(); Magick::PixelPacket tmp=pixels[fromX+fromY*image.columns()]; pixels[fromX+fromY*image.columns()]=pixels[toX+toY*image.columns()]; pixels[toX+toY*image.columns()]=tmp; } pixelCache.sync(); return 0; }
int DetailedAlgorithm::DoYourJob(Magick::Image& image) { image.modifyImage(); Magick::Pixels pixelCache(image); for(int x=0;x<image.columns();x++) for(int y=0;y<image.rows();y++) OperationPerPixel(pixelCache.get(x,y,1,1),x,y); pixelCache.sync(); return 0; }
int SimpleAlgorithm::DoYourJob(Magick::Image& image) { //"Note: The main benefit of the 'Pixels' class is that it provides efficient access to raw image pixels" image.modifyImage(); Magick::Pixels pixelCache(image); for(int x=0;x<image.columns();x++) for(int y=0;y<image.rows();y++) OperationPerPixel(pixelCache.get(x,y,1,1)); pixelCache.sync(); return 0; }
static Magick::Image* ImageCtor_layer(iTJSDispatch2* lay) { unsigned int w, h; unsigned char *p; long s; if (!lay || TJS_FAILED(lay->IsInstanceOf(0, 0, 0, TJS_W("Layer"), lay))) TVPThrowExceptionMessage(TJS_W("Magick::Image: _layer method needs Layer-instance param.")); // レイヤサイズ取得 tTJSVariant tmp[2]; lay->PropGet(0, TJS_W("imageWidth"), 0, &tmp[0], lay); lay->PropGet(0, TJS_W("imageHeight"), 0, &tmp[1], lay); w = static_cast<unsigned int>((tTVInteger)tmp[0]); h = static_cast<unsigned int>((tTVInteger)tmp[1]); // バッファ取得 lay->PropGet(0, TJS_W("mainImageBuffer"), 0, &tmp[0], lay); lay->PropGet(0, TJS_W("mainImageBufferPitch"), 0, &tmp[1], lay); p = reinterpret_cast<unsigned char*>((tTVInteger)tmp[0]); s = static_cast<long >((tTVInteger)tmp[1]); typedef Magick::PixelPacket PixelT; typedef Magick::Color ColorT; Magick::Image *image = new Magick::Image(Magick::Geometry(w, h), ColorT()); image->modifyImage(); image->type(Magick::TrueColorMatteType); // コピー if (sizeof(Magick::Quantum) == 1) { // 8bit quantum 専用 for (unsigned int y = 0; y < h; y++, p+=s) { unsigned char *cp = p; PixelT *q = image->getPixels(0, y, w, 1); for (int i = w; i > 0; i--, cp+=4) { *q++ = ColorT(static_cast<Magick::Quantum>(cp[2]), static_cast<Magick::Quantum>(cp[1]), static_cast<Magick::Quantum>(cp[0]), ~static_cast<Magick::Quantum>(cp[3])); } image->syncPixels(); } } else { // それ以外はdouble経由なので重い for (unsigned int y = 0; y < h; y++, p+=s) { unsigned char *cp = p; PixelT *q = image->getPixels(0, y, w, 1); for (int i = w; i > 0; i--, cp+=4) { Magick::ColorRGB col(cp[2]/255.0, cp[1]/255.0, cp[0]/255.0); col.alpha(cp[3]/255.0); *q++ = col; } image->syncPixels(); } } return image; }
int SingleMaskConvolutionAlgorithm::DoYourJob(Magick::Image& image) { image.modifyImage(); Magick::Pixels pixelCache(image); Magick::PixelPacket* pixels; for(int x=0;x<image.columns();x++) { int rectangleWidth=ConvolutionMask.Width; int maskX=x-ConvolutionMask.Width/2; if(maskX<0) { rectangleWidth+=maskX; maskX=0; } if(maskX+rectangleWidth>image.columns()) rectangleWidth=image.columns()-maskX; for(int y=0;y<image.rows();y++) { int rectangleHeight=ConvolutionMask.Height; int maskY=y-ConvolutionMask.Height/2; if(maskY<0) { rectangleHeight+=maskY; maskY=0; } if(maskY+rectangleHeight>image.rows()) rectangleHeight=image.rows()-maskY; pixels=pixelCache.get(maskX,maskY,rectangleWidth,rectangleHeight); Magick::PixelPacket maskResult; maskResult.red=maskResult.green=maskResult.blue=0; for(int mx=0;mx<rectangleWidth;mx++) for(int my=0;my<rectangleHeight;my++) { maskResult.red+=ConvolutionMask[mx][my]*pixels[mx+rectangleWidth*my].red; maskResult.green+=ConvolutionMask[mx][my]*pixels[mx+rectangleWidth*my].green; maskResult.blue+=ConvolutionMask[mx][my]*pixels[mx+rectangleWidth*my].blue; } maskResult.red/=ConvolutionMask.Weight; maskResult.green/=ConvolutionMask.Weight; maskResult.blue/=ConvolutionMask.Weight; OperationPerPixel(pixels,x,y,&maskResult); } } pixelCache.sync(); return 0; }
/*---------------------------------------------------------------------------- Reads image data from a file into the raw data with dimensions ----------------------------------------------------------------------------*/ void read_img( string filename, Magick::Blob &blob, int &width, int &height ) { Magick::Image *magick; /* ImageMagick object */ /* load the file into an ImageMagick object */ magick = new Magick::Image( filename.c_str() ); /* get image dimensions from ImageMagick */ width = magick->columns(); height = magick->rows(); /* convert image to grayscale, 8-bit depth and get raw data */ magick->modifyImage(); magick->write( &blob, "GRAY", 8 ); /* enough magick for now */ delete magick; }
void scan_image (const char *filename) { scanner.reset(); // normally scanner would reset associated decoder, // but this debug program connects them manually // (to make intermediate state more readily available) // so decoder must also be reset manually decoder.reset(); Magick::Image image; image.read(filename); string file = image.baseFilename(); size_t baseidx = file.rfind('/'); if(baseidx != string::npos) file = file.substr(baseidx + 1, file.length() - baseidx - 1); ofstream svg((file + ".svg").c_str()); unsigned inwidth = image.columns(); unsigned flush1 = inwidth / 32; unsigned flush0 = 2; unsigned width = inwidth + flush1 + flush0; unsigned height = image.rows(); unsigned midy = (height + 1) / 2 + 2; image.crop(Magick::Geometry(inwidth, 1, 0, midy)); image.size(Magick::Geometry(width, 1, 0, 0)); svg << "<?xml version='1.0'?>" << endl << "<!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.1//EN'" << " 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'>" << endl << "<svg version='1.1' id='top'" << " width='10in' height='6in' preserveAspectRatio='xMinYMid slice'" << " overflow='visible' viewBox='0,0 " << width * 2 << ",384'" << " xmlns:xlink='http://www.w3.org/1999/xlink'" << " xmlns='http://www.w3.org/2000/svg'>" << endl << "<defs><style type='text/css'><![CDATA[" << endl << " * { stroke-linejoin: round; stroke-linecap: round;" << " stroke-width: .1; text-anchor: middle;" << " image-rendering: optimizeSpeed;" << " font-size: 6; font-weight: bold }" << endl << " path { fill: none }" << endl << " #zero { stroke: #00f }" << endl << " #edges { stroke: #f00 }" << endl << " #cur-edge { stroke: #f44 }" << endl << " #raw { stroke: orange }" << endl << " #y0 { stroke: yellow }" << endl << " #y1 { stroke: #0c0 }" << endl << " #y2 { stroke: #0aa }" << endl << " .y1thr { stroke: #f0f }" << endl << " rect.bar { fill: black }" << endl << " text.bar { fill: white }" << endl << " rect.space { fill: white }" << endl << " text.space { fill: black }" << endl << " text.data { fill: #44f; font-size: 16 }" << endl << "]]></style></defs>" << endl << "<image width='" << width * 2 << "' height='384'" << " preserveAspectRatio='none'" << " xlink:href='" << file << ".png'/>" << endl << "<g transform='translate(1,384) scale(2,-.5)'>" << endl; // brute force unsigned raw[width]; { // extract scan from image pixels image.modifyImage(); Magick::Pixels view(image); Magick::PixelPacket *pxp = view.get(0, 0, width, 1); Magick::ColorYUV y; double max = 0; svg << "<path id='raw' d='M"; unsigned i; for(i = 0; i < inwidth; i++, pxp++) { y = *pxp; if(max < y.y()) max = y.y(); raw[i] = (unsigned)(y.y() * 0x100); svg << ((i != 1) ? " " : " L ") << i << "," << raw[i]; y.u(0); y.v(0); *pxp = y; } y.y(max); /* flush scan FIXME? */ for(; i < inwidth + flush1; i++) { raw[i] = (unsigned)(y.y() * 0x100); svg << " " << i << "," << raw[i]; *pxp++ = y; } y.y(0); for(; i < width; i++) { raw[i] = (unsigned)(y.y() * 0x100); svg << " " << i << "," << raw[i]; *pxp++ = y; } view.sync(); svg << "'/>" << endl << "</g>" << endl; } image.depth(8); image.write(file + ".png"); // process scan and capture calculated values unsigned cur_edge[width], last_edge[width]; int y0[width], y1[width], y2[width], y1_thr[width]; svg << "<g transform='translate(-3)'>" << endl; for(unsigned i = 0; i < width; i++) { int edge = scanner.scan_y(raw[i]); unsigned x; zbar_scanner_get_state(scanner.get_c_scanner(), &x, &cur_edge[i], &last_edge[i], &y0[i], &y1[i], &y2[i], &y1_thr[i]); #ifdef DEBUG_SCANNER cerr << endl; #endif cur_edge[i] += i - x; if(edge) { last_edge[i] += i - x; unsigned w = scanner.get_width(); svg << "<rect x='" << (2. * (last_edge[i] - w) / ZBAR_FRAC) << "' width='" << (w * 2. / ZBAR_FRAC) << "' height='32' class='" << (scanner.get_color() ? "space" : "bar") << "'/>" << endl << "<text transform='translate(" << ((2. * last_edge[i] - w) / ZBAR_FRAC) - 3 << ",16) rotate(90)' class='" << (scanner.get_color() ? "space" : "bar") << "'>" << endl << w << "</text>" << endl; zbar_symbol_type_t sym = decoder.decode_width(w); if(sym > ZBAR_PARTIAL) { svg << "<text transform='translate(" << (2. * (last_edge[i] + w) / ZBAR_FRAC) << ",208) rotate(90)' class='data'>" << decoder.get_data_string() << "</text>" << endl; } } else last_edge[i] = 0; } svg << "</g>" << endl << "<g transform='translate(-3,384) scale(2,-.5)'>" << endl << "<path id='edges' d='"; for(unsigned i = 0; i < width; i++) if(last_edge[i]) svg << " M" << ((double)last_edge[i] / ZBAR_FRAC) << ",0v768"; svg << "'/>" << endl << "</g>" << endl << "<g transform='translate(-1,384) scale(2,-.5)'>" << endl << "<path id='y0' d='M"; for(unsigned i = 0; i < width; i++) svg << ((i != 1) ? " " : " L ") << i << "," << y0[i]; svg << "'/>" << endl << "</g>" << endl; svg << "<g transform='translate(-1,128) scale(2,-1)'>" << endl << "<line id='zero' x2='" << width << "'/>" << endl << "<path id='cur-edge' d='"; for(unsigned i = 1; i < width - 1; i++) if(!last_edge[i + 1] && (cur_edge[i] != cur_edge[i + 1])) svg << " M" << ((double)cur_edge[i] / ZBAR_FRAC) - 1 << ",-32v64"; svg << "'/>" << endl << "<path class='y1thr' d='M"; for(unsigned i = 0; i < width; i++) svg << ((i != 1) ? " " : " L ") << i << "," << y1_thr[i]; svg << "'/>" << endl << "<path class='y1thr' d='M"; for(unsigned i = 0; i < width; i++) svg << ((i != 1) ? " " : " L ") << i << "," << -y1_thr[i]; svg << "'/>" << endl << "<path id='y1' d='M"; for(unsigned i = 0; i < width; i++) svg << ((i != 1) ? " " : " L ") << (i - 0.5) << "," << y1[i]; svg << "'/>" << endl << "<path id='y2' d='M"; for(unsigned i = 0; i < width; i++) svg << ((i != 1) ? " " : " L ") << i << "," << y2[i]; svg << "'/>" << endl << "</g>" << endl; svg << "</svg>" << endl; }