static MagickBooleanType recolor(WandView *view, const ssize_t y, const int id, void *context){ RectangleInfo extent; MagickPixelPacket pixel; PixelWand **pixels; register ssize_t x; register float t; extent = GetWandViewExtent(view); pixels = GetWandViewPixels(view); for (x = 0; x < (ssize_t) (extent.width - extent.x); x++){ PixelGetMagickColor(pixels[x], &pixel); t = ScaleQuantumToChar(pixel.red); pixel.red = get_color(t, 0); pixel.green = get_color(t, 1); pixel.blue = get_color(t, 2); PixelSetMagickColor(pixels[x], &pixel); } return(MagickTrue); }
xcolor_image_t *image_to_xterm(const char *path) { struct winsize ws; ioctl(0, TIOCGWINSZ, &ws); MagickWand *wand; MagickBooleanType status; MagickPixelPacket pixel; PixelIterator *iterator; PixelWand **pixels; MagickWandGenesis(); wand = NewMagickWand(); status = MagickReadImage(wand, path); if(status == MagickFalse) { exit(EXIT_FAILURE); } int **xterm_colors = NULL; MagickResetIterator(wand); size_t row_width = 0; size_t im_height = 0; size_t im_width = 0; size_t width = 0; size_t height = 0; while(MagickNextImage(wand) != MagickFalse) { // Get the actual width and height of the original image im_height = MagickGetImageHeight(wand); im_width = MagickGetImageWidth(wand); // Calculate the new width and height of the image width = min(im_width, (size_t) ws.ws_col); height = ((im_height * width) / im_width) / 2; MagickAdaptiveResizeImage(wand, width, height); // iterate through all the pixels iterator = NewPixelIterator(wand); if((xterm_colors = calloc(height, sizeof(int *))) == NULL) { perror("calloc"); exit(EXIT_FAILURE); } for(size_t y = 0; y < height; y++) { pixels = PixelGetNextIteratorRow(iterator, &row_width); if(pixels == (PixelWand **) NULL) { break; } if((xterm_colors[y] = calloc(row_width, sizeof(int))) == NULL) { perror("calloc"); exit(EXIT_FAILURE); } for(size_t x = 0; x < row_width; x++) { PixelGetMagickColor(pixels[x], &pixel); int xc = rgb_to_xterm((int)(pixel.red / 255) , (int)(pixel.green / 255), (int)(pixel.blue / 255)); xterm_colors[y][x] = xc; } } iterator = DestroyPixelIterator(iterator); break; } // We don't need imagemagick anymore, shut it down wand = DestroyMagickWand(wand); xcolor_image_t *xcolor_image; if((xcolor_image = malloc(sizeof(xcolor_image_t))) == NULL) { perror("malloc"); exit(EXIT_FAILURE); } xcolor_image->x = row_width; xcolor_image->y = height; xcolor_image->pixels = xterm_colors; return xcolor_image; }
int parseImage(void) { int x,y; MagickBooleanType status; MagickWand *magick_wand; //PixelWand *color; MagickPixelPacket pixel; PixelWand **pixels; MagickWandGenesis(); magick_wand=NewMagickWand(); status=MagickReadImage(magick_wand,"matti.png"); if (status == MagickFalse) printf("ERROR\n"); size_t width=MagickGetImageWidth(magick_wand); size_t height=MagickGetImageHeight(magick_wand); printf("X:%d, Y:%d\n",width, height); int image_width = MAX_WIDTH; int image_height = MAX_HEIGHT; if(width < MAX_WIDTH) image_width=width; if(height < MAX_HEIGHT) image_height=height; // PixelIterator* iterator = NewPixelIterator(magick_wand); //PixelWand **pixels = PixelGetNextIteratorRow(iterator,&number_wands); for (y=0; y < image_height; y++) // for (y=0; y < 1; y++) { pixels=PixelGetNextIteratorRow(iterator,&width); for (x=0; x < image_width; x++) { PixelGetMagickColor(pixels[x],&pixel); //printf("R/G/B %g/%g/%g\n",round(pixel.red),round(pixel.green),round(pixel.blue)); int real_y= image_height-y-1; if (real_y < 0) { real_y = 0; printf("ERRRORRR\n"); } else { /*if (round(pixel.red)==0&&round(pixel.green)==0&&round(pixel.blue)==0) image_in[x][real_y]=255; if (round(pixel.red)<4095) image_in[x][real_y]=255;*/ image_in[x][real_y] = 65535 - round(pixel.red); } } PixelSyncIterator(iterator); } PixelSyncIterator(iterator); iterator=DestroyPixelIterator(iterator); ClearMagickWand(magick_wand); printf("Parsed bitmap\n"); magick_wand=DestroyMagickWand(magick_wand); MagickWandTerminus(); }
int main(int argc, char **argv) { init_context(); int _; if ((_ = parse_arguments(argc, argv))) { return _; } FILE *infile; FILE *outfile; infile = fopen(context.infile, "r"); if (infile == NULL) { fprintf(stderr, "No such file or directory: %s\n", context.infile); return 1; } outfile = fopen(context.outfile, "wb"); if (outfile == NULL) { fprintf(stderr, "Please provide an outfile. See `man kimg` for more details."); return 1; } MagickWand *input; MagickWandGenesis(); input = NewMagickWand(); MagickBooleanType mwstatus = MagickReadImageFile(input, infile); if (mwstatus == MagickFalse) { ExceptionType type; fprintf(stderr, "Error reading image file: %s\n", context.infile); fprintf(stderr, "ImageMagick says: %s\n", MagickGetException(input, &type)); return 1; } unsigned long height, width; unsigned short bytewidth; height = MagickGetImageHeight(input); width = MagickGetImageWidth(input); bytewidth = width / 8; if (width % 8 != 0) { bytewidth++; } if (!context.color) { MagickSetImageType(input, GrayscaleType); MagickSetImageColorspace(input, GRAYColorspace); } uint8_t _version = KIMG_VERSION; uint8_t _format = 0; // TODO: format flags uint16_t _height = (uint16_t)height; uint16_t _width = (uint16_t)width; if (!context.bare) { fwrite("KIMG", 4, 1, outfile); // 0x00: Magic Header fwrite(&_version, 1, 1, outfile); // 0x04: Format Version fwrite(&_format, 1, 1, outfile); // 0x05: Format Flags fwrite(&_height, 2, 1, outfile); // 0x06: Image Height fwrite(&_width, 2, 1, outfile); // 0x08: Image Width // 0x09: Palette/Image Data... } PixelIterator *iter; PixelWand **row; MagickPixelPacket pixel; uint8_t mask, byte; iter = NewPixelIterator(input); unsigned long x, y; for (y = 0; y < height; y++) { mask = 0x80; byte = 0; row = PixelGetNextIteratorRow(iter, &width); for (x = 0; x < width; x++) { PixelGetMagickColor(row[x], &pixel); if (context.color) { // TODO: color support } else { if (pixel.red < 32767) { byte |= mask; } } mask >>= 1; if (mask == 0) { // TODO: build in memory fwrite(&byte, sizeof(uint8_t), 1, outfile); mask = 0x80; byte = 0; } } if (mask != 0x80) { fwrite(&byte, sizeof(uint8_t), 1, outfile); } } iter = DestroyPixelIterator(iter); input = DestroyMagickWand(input); MagickWandTerminus(); fclose(outfile); return 0; }
void Resize(MagickWand *imagen, MagickWand *imagen2, int proporcion) { int ancho, largo, factor_conversion; register ssize_t x; size_t width, width2; ssize_t y; int ity, itx, index = 0; MagickPixelPacket pixel, pixel2; PixelIterator *iterator, *iterator2; PixelWand **pixels, **pixels2; PixelWand *bgw1 = NewPixelWand(); factor_conversion = abs(proporcion); if(proporcion >= 0){ ancho = MagickGetImageWidth(imagen)*factor_conversion; largo = MagickGetImageHeight(imagen)*factor_conversion; }else{ ancho = (MagickGetImageWidth(imagen)/factor_conversion)+1; largo = (MagickGetImageHeight(imagen)/factor_conversion)+1; } MagickNewImage(imagen2,ancho,largo,bgw1); iterator = NewPixelIterator(imagen); iterator2 = NewPixelIterator(imagen2); if(proporcion >= 0){ //****************** ITERACION PIXEL POR PIXEL *********************** for (y=0; y < (ssize_t) MagickGetImageHeight(imagen); y++) { pixels = PixelGetNextIteratorRow(iterator,&width); if (pixels == (PixelWand **) NULL) break; for(ity=0; ity < factor_conversion; ity++){ pixels2 = PixelGetNextIteratorRow(iterator2,&width2); for (x=0; x < (ssize_t) width; x++) { PixelGetMagickColor(pixels[x],&pixel); for(itx=0;itx<factor_conversion;itx++){ PixelGetMagickColor(pixels2[factor_conversion*x+itx],&pixel2); pixel2.red = pixel.red; pixel2.green = pixel.green; pixel2.blue = pixel.blue; PixelSetMagickColor(pixels2[factor_conversion*x+itx],&pixel2); } } (void) PixelSyncIterator(iterator2); } (void) PixelSyncIterator(iterator); } //****************************************************************** }else{ //****************** ITERACION PIXEL POR PIXEL *********************** for (y=0; y < (ssize_t) MagickGetImageHeight(imagen); y++) { pixels = PixelGetNextIteratorRow(iterator,&width); if (pixels == (PixelWand **) NULL) break; pixels2 = PixelGetNextIteratorRow(iterator2,&width2); for (x=0; x < (ssize_t) width; x++) { if(x%factor_conversion == 0){ PixelGetMagickColor(pixels[x],&pixel); PixelGetMagickColor(pixels2[index],&pixel2); pixel2.red = pixel.red; pixel2.green = pixel.green; pixel2.blue = pixel.blue; PixelSetMagickColor(pixels2[index],&pixel2); index++; } } index = 0; for(ity=1;ity<factor_conversion;ity++){ pixels = PixelGetNextIteratorRow(iterator,&width); } (void) PixelSyncIterator(iterator2); (void) PixelSyncIterator(iterator); } //****************************************************************** } iterator = DestroyPixelIterator(iterator); iterator = DestroyPixelIterator(iterator2); }
int main(int argc, char **argv) { printf("Hello World\n"); #define QuantumScale ((MagickRealType) 1.0/(MagickRealType) QuantumRange) #define SigmoidalContrast(x) (QuantumRange*(1.0/(1+exp(10.0*(0.5-QuantumScale*x)))-0.0066928509)*1.0092503) #define ThrowWandException(wand)\ {\ char *description;\ ExceptionType\ severity;\ \ description=MagickGetException(wand,&severity);\ (void)fprintf(stderr,"%s %s %lu %s\n",GetMagickModule(),description);\ exit(-1);\ } long y; MagickBooleanType status; MagickPixelPacket pixel; MagickWand *contrast_wand, *image_wand; PixelIterator *contrast_iterator,*iterator; PixelWand **contrast_pixels, **pixels; register long x; unsigned long width; if (argc != 3) { (void)fprintf(stdout,"Usage: %s image sigmoidal-image\n",argv[0]); exit(0); } /* Read an Image */ MagickWandGenesis(); image_wand = NewMagickWand(); status = MagickReadImage(image_wand,argv[1]); if(status == MagickFalse) ThrowWandException(image_wand); contrast_wand = CloneMagickWand(image_wand); /* Sigmoidal non-linearity contrast control. */ iterator = NewPixelIterator(image_wand); contrast_iterator=NewPixelIterator(contrast_wand); if((iterator == (PixelIterator*)NULL) || (contrast_iterator == (PixelIterator*)NULL)) ThrowWandException(image_wand); for(y=0; y<(long)MagickGetImageHeight(image_wand); y++) { pixels = PixelGetNextIteratorRow(iterator,&width); contrast_pixels = PixelGetNextIteratorRow(contrast_iterator,&width); if((pixels==(PixelWand**)NULL) || (contrast_pixels == (PixelWand**)NULL)) break; for(x = 0; x<(long)width; x++) { PixelGetMagickColor(pixels[x],&pixel); pixel.red = SigmoidalContrast(pixel.red); pixel.green = SigmoidalContrast(pixel.green); pixel.blue = SigmoidalContrast(pixel.blue); pixel.index = SigmoidalContrast(pixel.index); PixelSetMagickColor(contrast_pixels[x],&pixel); } (void)PixelSyncIterator(contrast_iterator); } if(y < (long)MagickGetImageHeight(image_wand)) ThrowWandException(image_wand); contrast_iterator = DestroyPixelIterator(contrast_iterator); iterator = DestroyPixelIterator(iterator); image_wand=DestroyMagickWand(image_wand); status = MagickWriteImages(contrast_wand,argv[2],MagickTrue); if(status == MagickFalse) ThrowWandException(image_wand); contrast_wand = DestroyMagickWand(contrast_wand); MagickWandTerminus(); return 0; }