Image::Image( uint32 width, uint32 height, const char* map, StorageType storage, const void* pixels ) throw (GSystem::Exception): m_image( NULL ) { // initialize exception ExceptionInfo exception; GetExceptionInfo( &exception ); m_image = ConstituteImage( width, height, map, storage, pixels, &exception ); if ( m_image == NULL ) { MC2String exceptionString = "Reason: "; exceptionString += exception.reason; exceptionString += "Description: "; exceptionString += exception.description; DestroyExceptionInfo( &exception ); throw GSystem::Exception( "[ImageMagick]" + exceptionString ); } DestroyExceptionInfo( &exception ); GetQuantizeInfo(&m_quantizeInfo); }
bool draw_forest(enum tree_state * forest, size_t width, size_t height) { static uint32_t counter = 0; bool ret = false; MagickCoreGenesis(NULL,MagickTrue); { uint8_t * pixels = malloc( width * height * sizeof(uint8_t) * 4); // channel count forest_to_intensity(forest, pixels, width * height); const char name[128]; sprintf((char *)name, "out_%04d.gif", counter); ExceptionInfo * exception = AcquireExceptionInfo(); Image * image = ConstituteImage(width,height,"RGBP",CharPixel,pixels,exception); ImageInfo * info = AcquireImageInfo(); strncpy(image->filename, name, sizeof(image->filename)); ret = WriteImage(info, image); DestroyImage(image); DestroyExceptionInfo(exception); DestroyImageInfo(info); free(pixels); } counter++; MagickCoreTerminus(); return ret; }
//============================================================== void f0r_update(f0r_instance_t instance, double time, const uint32_t* inframe, uint32_t* outframe) { Tinstance *in; in=(Tinstance*)instance; ExceptionInfo *exception; /* Generamos la excepción */ exception = AcquireExceptionInfo(); /* Transformamos el mapa de imagen de entrada para imagemagick */ Image *im = ConstituteImage(in->size.width, in->size.height, "RGBA", CharPixel, inframe, exception); if (in->preview) { minify(&im, (int)in->preview, exception); } /* Generamos la imagen de salida */ Image *out = MotionBlurImage(im, in->radius, in->deviation, in->angle, exception); if (in->preview) { magnify(&out, (int)in->preview, exception); } /* Volvemos a coger el contenido de la imagen para frei0r */ ExportImagePixels(out, 0, 0, in->size.width, in->size.height, "RGBA", CharPixel, outframe, exception); /* Liberamos memoria */ exception = DestroyExceptionInfo(exception); im = DestroyImage(im); out = DestroyImage(out); }
Image *get_threshold_image(Image *img, c_threshold *c) { Image *threshold_img; int size_x, size_y; int i = 0; int j = 0; char *string_img = malloc(img->rows * img->columns * sizeof(*string_img)); char *temp_string_img; PixelPacket *px; ExceptionInfo exception; GetExceptionInfo(&exception); px = GetImagePixels(img, 0, 0, img->columns, img->rows); size_x = (int)img->columns; size_y = (int)img->rows; while (i < size_y) { j=0; while (j < size_x) { string_img[(size_x * i) + j] = (char)px[(size_x * i) + j].green; j++; } i++; } temp_string_img = otsu_th(size_x, size_y, string_img, c); threshold_img = ConstituteImage(size_x, size_y, "I", CharPixel, temp_string_img, &exception); free(temp_string_img); free(string_img); DestroyImage(img); SyncImagePixels(threshold_img); return (threshold_img); }
Image *get_blur_maked(Image *img, c_image *c) { Image *maked_img; static unsigned short color = 0; unsigned int x = 0; unsigned int y = 0; unsigned char *pixel_map = malloc(img->columns * img->rows * sizeof(*pixel_map)); c->label_map = malloc(img->columns * img->rows * sizeof(*c->label_map)); int width = (int)img->columns; int height = (int)img->rows; PixelPacket *px; ExceptionInfo exception; GetExceptionInfo(&exception); px = GetImagePixels(img, 0, 0, img->columns, img->rows); while (y < height) { x = 0; while (x < width) { pixel_map[(width * y) + x] = (char)px[(width * y) + x].green; x++; } y++; } y = 0; while (y < height){ x = 0; while (x < width){ if (pixel_map[(width * y) + x] != WHITE){ c->label_map[(width * y) + x] = 1; pixel_map[(width * y) + x] = get_neighborpixcel(pixel_map, x, y, width, height, color, c); } else{ c->label_map[(width * y) + x] = 0; } if(color >= 255){ color = 0; } x++; } y++; } maked_img = ConstituteImage(width, height, "I", CharPixel, pixel_map, &exception); free(c->label_map); free(pixel_map); DestroyImage(img); SyncImagePixels(maked_img); return (maked_img); }
PIXM_PyImage pixmagick_convert_py_pix(PyObject *self, PyObject *args) { PyObject *pyArg1; TCAX_Pix pix; Image *pImage; if (PyTuple_GET_SIZE(args) < 1) { PyErr_SetString(PyExc_RuntimeError, "pmgToImage error, too less parameters - `(pix)'\n"); return NULL; } pyArg1 = PyTuple_GET_ITEM(args, 0); if (!PyTuple_Check(pyArg1)) { PyErr_SetString(PyExc_RuntimeError, "pmgToImage error, the 1st param should be a tuple - `PIX'\n"); return NULL; } tcaxlib_convert_py_pix((const TCAX_PyPix)pyArg1, &pix); pImage = ConstituteImage(pix.width, pix.height, "RGBA", CharPixel, pix.buf, NULL); free(pix.buf); return PyLong_FromUnsignedLong((unsigned long)pImage); /* real content are still kept in memory, not a real leak, but will be deallocated by pmgToPix function */ }
Image *crop_area_selected(Image *img) { Image *final_images; unsigned char *pixel_map = malloc(img->columns * img->rows * sizeof(*pixel_map)); unsigned int x = 0; unsigned int y = 0; int width = (int)img->columns; int height = (int)img->rows; PixelPacket *px; ExceptionInfo exception; GetExceptionInfo(&exception); px = GetImagePixels(img, 0, 0, img->columns, img->rows); while (y < height) { x = 0; while (x < width) { pixel_map[(width * y) + x] = (char)px[(width * y) + x].green; x++; } y++; } y = 0; while (y < height){ x = 0; while (x < width){ if (pixel_map[(width * y) + x] == BLACK){ if (check_srounding(pixel_map, x, y, width, height) == TRUE){ pixel_map[(width * y) + x] = OUTLINE; } } if (pixel_map[(width * y) + x] !=FILL_COLOR && pixel_map[(width * y) + x] != OUTLINE) pixel_map[(width * y) + x] = WHITE; x++; } y++; } final_images = ConstituteImage(width, height, "I", CharPixel, pixel_map, &exception); free(pixel_map); DestroyImage(img); return (final_images); }
struct fp_img *fpi_im_resize(struct fp_img *img, unsigned int factor) { Image *mimg; Image *resized; ExceptionInfo exception; MagickBooleanType ret; int new_width = img->width * factor; int new_height = img->height * factor; struct fp_img *newimg; /* It is possible to implement resizing using a simple algorithm, however * we use ImageMagick because it applies some kind of smoothing to the * result, which improves matching performances in my experiments. */ if (!IsMagickInstantiated()) InitializeMagick(NULL); GetExceptionInfo(&exception); mimg = ConstituteImage(img->width, img->height, "I", CharPixel, img->data, &exception); GetExceptionInfo(&exception); resized = ResizeImage(mimg, new_width, new_height, 0, 1.0, &exception); newimg = fpi_img_new(new_width * new_height); newimg->width = new_width; newimg->height = new_height; newimg->flags = img->flags; GetExceptionInfo(&exception); ret = ExportImagePixels(resized, 0, 0, new_width, new_height, "I", CharPixel, newimg->data, &exception); if (ret != MagickTrue) { fp_err("export failed"); return NULL; } DestroyImage(mimg); DestroyImage(resized); return newimg; }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % I m p o r t I m a g e P i x e l s % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % ImportImagePixels() accepts pixel data and stores in the image at the % location you specify. The method returns True on success otherwise False % if an error is encountered. The pixel data can be either char, short int, % int, long, float, or double in the order specified by map. % % Suppose your want want to upload the first scanline of a 640x480 image from % character data in red-green-blue order: % % ImportImagePixels(image,0,0,640,1,"RGB",CharPixel,pixels); % % The format of the ImportImagePixels method is: % % unsigned int ImportImagePixels(Image *image,const long x_offset, % const long y_offset,const unsigned long columns, % const unsigned long rows,const char *map,const StorageType type, % const void *pixels) % % A description of each parameter follows: % % o image: The image. % % o x_offset, y_offset: Offset (from top left) on base image on which % to composite image data. % % o map: This string reflects the expected ordering of the pixel array. % It can be any combination or order of R = red, G = green, B = blue, % A = alpha (same as Transparency), O = Opacity, T = Transparency, % C = cyan, Y = yellow, M = magenta, K = black, or I = intensity % (for grayscale). Specify "P" = pad, to skip over a quantum which is % intentionally ignored. Creation of an alpha channel for CMYK images % is currently not supported. % % o type: Define the data type of the pixels. Float and double types are % normalized to [0..1] otherwise [0..MaxRGB]. Choose from these types: % CharPixel, ShortPixel, IntegerPixel, LongPixel, FloatPixel, or % DoublePixel. % % o pixels: This array of values contain the pixel components as defined by % map and type. You must preallocate this array where the expected % length varies depending on the values of width, height, map, and type. % % */ WandExport unsigned int ImportImagePixels(Image *image,const long x_offset, const long y_offset,const unsigned long columns,const unsigned long rows, const char *map,const StorageType type,const void *pixels) { Image *constitute_image; assert(image != (Image *) NULL); assert(image->signature == MagickSignature); constitute_image= ConstituteImage(columns,rows,map,type,pixels,&image->exception); if (constitute_image) { (void) CompositeImage(image,CopyCompositeOp,constitute_image,x_offset, y_offset); DestroyImage(constitute_image); return (image->exception.severity == UndefinedException); } return (False); }
Image *get_green_grayscale_image(Image *img) { unsigned int i = 0; unsigned int j = 0; PixelPacket *px_original; Image *new_img; ExceptionInfo exception; unsigned char *new_raw_image; GetExceptionInfo(&exception); if ((px_original = GetImagePixelsEx(img, 0, 0, img->columns, img->rows, &exception)) == NULL) { CatchException(&exception); return (NULL); } new_raw_image = malloc((img->rows * img->columns) * sizeof(*new_raw_image)); while (i < img->rows) { j = 0; while (j < img->columns) { new_raw_image[(img->columns * i) + j] = px_original[(img->columns * i) + j].green; j++; } i++; } if ((new_img = ConstituteImage(img->columns, img->rows, "I", CharPixel, new_raw_image, &exception)) == NULL) { CatchException(&exception); return (NULL); } free(new_raw_image); return (new_img); }
/********************************************************* * processing of the video frame (RGB codec) * processes the actual frame depending on the * selected mode * @param buffer video buffer * width video width * height video height * instance filter instance * @return void nothing *********************************************************/ static void work_with_rgb_frame(char *buffer, int width, int height, int instance) { int row, col, i; int xdistance, ydistance, distance_west, distance_north; unsigned char hcalc, vcalc; int buf_off, packet_off, buf_off_xpos, buf_off_width, buf_off_ypos, buf_off_height; int alpha_hori, alpha_vert; uint8_t px; if(data[instance]->dump) { // DUMP for(row=data[instance]->ypos; row<data[instance]->height; ++row) { for(col=data[instance]->xpos; col<data[instance]->width; ++col) { packet_off = ((row-data[instance]->ypos)*(data[instance]->width-data[instance]->xpos)+(col-data[instance]->xpos)) * 3; buf_off = ((height-row)*width+col) * 3; /* R */ data[instance]->dump_buf[packet_off +0] = buffer[buf_off +0]; /* G */ data[instance]->dump_buf[packet_off +1] = buffer[buf_off +1]; /* B */ data[instance]->dump_buf[packet_off +2] = buffer[buf_off +2]; } } data[instance]->dumpimage = ConstituteImage(data[instance]->width-data[instance]->xpos, data[instance]->height-data[instance]->ypos, "RGB", CharPixel, data[instance]->dump_buf, &data[instance]->exception_info); tc_snprintf (data[instance]->dumpimage->filename, MaxTextExtent, "dump[%d].png", instance); WriteImage(data[instance]->dumpimage_info, data[instance]->dumpimage); } switch(data[instance]->mode) { case 0: // NONE break; case 1: // SOLID for(row=data[instance]->ypos; row<data[instance]->height; ++row) { for(col=data[instance]->xpos; col<data[instance]->width; ++col) { buf_off = ((height-row)*width+col) * 3; packet_off = (row-data[instance]->ypos) * (data[instance]->width-data[instance]->xpos) + (col-data[instance]->xpos); /* R */ px = (uint8_t)ScaleQuantumToChar(data[instance]->pixel_packet[packet_off].red); buffer[buf_off +0] = data[instance]->alpha?(alpha_blending( buffer[buf_off +0], data[instance]->rcolor, px )):data[instance]->rcolor; /* G */ px = (uint8_t)ScaleQuantumToChar(data[instance]->pixel_packet[packet_off].green); buffer[buf_off +1] = data[instance]->alpha?(alpha_blending( buffer[buf_off +1], data[instance]->gcolor, px )):data[instance]->gcolor; /* B */ px = (uint8_t)ScaleQuantumToChar(data[instance]->pixel_packet[packet_off].blue); buffer[buf_off +2] = data[instance]->alpha?(alpha_blending( buffer[buf_off +2], data[instance]->bcolor, px )):data[instance]->bcolor; } } break; case 2: // XY xdistance = 256 / (data[instance]->width - data[instance]->xpos); ydistance = 256 / (data[instance]->height - data[instance]->ypos); for(row=data[instance]->ypos; row<data[instance]->height; ++row) { distance_north = data[instance]->height - row; alpha_vert = ydistance * distance_north; buf_off_xpos = ((height-row)*width+data[instance]->xpos) * 3; buf_off_width = ((height-row)*width+data[instance]->width) * 3; for(col=data[instance]->xpos; col<data[instance]->width; ++col) { distance_west = data[instance]->width - col; alpha_hori = xdistance * distance_west; buf_off_ypos = ((height-data[instance]->ypos)*width+col) * 3; buf_off_height = ((height-data[instance]->height)*width+col) * 3; buf_off = ((height-row)*width+col) * 3; packet_off = (row-data[instance]->ypos) * (data[instance]->width-data[instance]->xpos) + (col-data[instance]->xpos); /* R */ hcalc = alpha_blending( buffer[buf_off_xpos +0], buffer[buf_off_width +0], alpha_hori ); vcalc = alpha_blending( buffer[buf_off_ypos +0], buffer[buf_off_height +0], alpha_vert ); px = (uint8_t)ScaleQuantumToChar(data[instance]->pixel_packet[packet_off].red); buffer[buf_off +0] = data[instance]->alpha?alpha_blending( buffer[buf_off +0], (hcalc*data[instance]->xweight + vcalc*data[instance]->yweight)/100, px):((hcalc*data[instance]->xweight + vcalc*data[instance]->yweight)/100); /* G */ hcalc = alpha_blending( buffer[buf_off_xpos +1], buffer[buf_off_width +1], alpha_hori ); vcalc = alpha_blending( buffer[buf_off_ypos +1], buffer[buf_off_height +1], alpha_vert ); /* sic */ buffer[buf_off +1] = data[instance]->alpha?alpha_blending( buffer[buf_off +1], (hcalc*data[instance]->xweight + vcalc*data[instance]->yweight)/100, px):((hcalc*data[instance]->xweight + vcalc*data[instance]->yweight)/100); /* B */ hcalc = alpha_blending( buffer[buf_off_xpos +2], buffer[buf_off_width +2], alpha_hori ); vcalc = alpha_blending( buffer[buf_off_ypos +2], buffer[buf_off_height +2], alpha_vert ); /* sic */ buffer[buf_off +2] = data[instance]->alpha?alpha_blending( buffer[buf_off +2], (hcalc*data[instance]->xweight + vcalc*data[instance]->yweight)/100, px):((hcalc*data[instance]->xweight + vcalc*data[instance]->yweight)/100); } } break; case 3: // SHAPE xdistance = 256 / (data[instance]->width - data[instance]->xpos); ydistance = 256 / (data[instance]->height - data[instance]->ypos); for(row=data[instance]->ypos; row<data[instance]->height; ++row) { distance_north = data[instance]->height - row; alpha_vert = ydistance * distance_north; for(col=data[instance]->xpos; col<data[instance]->width; ++col) { distance_west = data[instance]->width - col; alpha_hori = xdistance * distance_west; buf_off = ((height-row)*width+col) * 3; packet_off = (row-data[instance]->ypos) * (data[instance]->width-data[instance]->xpos) + (col-data[instance]->xpos); buf_off_xpos = ((height-row)*width+data[instance]->xpos) * 3; buf_off_width = ((height-row)*width+data[instance]->width) * 3; buf_off_ypos = ((height-data[instance]->ypos)*width+col) * 3; buf_off_height = ((height-data[instance]->height)*width+col) * 3; i=0; px = (uint8_t)ScaleQuantumToChar(data[instance]->pixel_packet[packet_off-i].red); while( (px != 255) && (col-i>data[instance]->xpos) ) i++; buf_off_xpos = ((height-row)*width + col-i) * 3; i=0; px = (uint8_t)ScaleQuantumToChar(data[instance]->pixel_packet[packet_off+i].red); while( (px != 255) && (col+i<data[instance]->width) ) i++; buf_off_width = ((height-row)*width + col+i) * 3; i=0; px = (uint8_t)ScaleQuantumToChar(data[instance]->pixel_packet[packet_off-i*(data[instance]->width-data[instance]->xpos)].red); while( (px != 255) && (row-i>data[instance]->ypos) ) i++; buf_off_ypos = (height*width*3)-((row-i)*width - col) * 3; i=0; px = (uint8_t)ScaleQuantumToChar(data[instance]->pixel_packet[packet_off+i*(data[instance]->width-data[instance]->xpos)].red); while( (px != 255) && (row+i<data[instance]->height) ) i++; buf_off_height = (height*width*3)-((row+i)*width - col) * 3; /* R */ hcalc = alpha_blending( buffer[buf_off_xpos +0], buffer[buf_off_width +0], alpha_hori ); vcalc = alpha_blending( buffer[buf_off_ypos +0], buffer[buf_off_height +0], alpha_vert ); px = (uint8_t)ScaleQuantumToChar(data[instance]->pixel_packet[packet_off].red); buffer[buf_off +0] = data[instance]->alpha?alpha_blending( buffer[buf_off +0], (hcalc*data[instance]->xweight + vcalc*data[instance]->yweight)/100, px):((hcalc*data[instance]->xweight + vcalc*data[instance]->yweight)/100); /* G */ hcalc = alpha_blending( buffer[buf_off_xpos +1], buffer[buf_off_width +1], alpha_hori ); vcalc = alpha_blending( buffer[buf_off_ypos +1], buffer[buf_off_height +1], alpha_vert ); /* sic */ buffer[buf_off +1] = data[instance]->alpha?alpha_blending( buffer[buf_off +1], (hcalc*data[instance]->xweight + vcalc*data[instance]->yweight)/100, px):((hcalc*data[instance]->xweight + vcalc*data[instance]->yweight)/100); /* B */ hcalc = alpha_blending( buffer[buf_off_xpos +2], buffer[buf_off_width +2], alpha_hori ); vcalc = alpha_blending( buffer[buf_off_ypos +2], buffer[buf_off_height +2], alpha_vert ); /* sic */ buffer[buf_off +2] = data[instance]->alpha?alpha_blending( buffer[buf_off +2], (hcalc*data[instance]->xweight + vcalc*data[instance]->yweight)/100, px):((hcalc*data[instance]->xweight + vcalc*data[instance]->yweight)/100); } } break; } if(data[instance]->border) { // BORDER for(row=data[instance]->ypos; row<data[instance]->height; ++row) { if((row == data[instance]->ypos) || (row==data[instance]->height-1)) { for(col=data[instance]->xpos*3; col<data[instance]->width*3; ++col) if(col&1) buffer[((height-row)*width*3+col)] = 255 & 0xff; } if(row&1) { buf_off = ((height-row)*width+data[instance]->xpos)*3; buffer[buf_off +0] = 255; buffer[buf_off +1] = 255; buffer[buf_off +2] = 255; } if(row&1) { buf_off = ((height-row)*width+data[instance]->width)*3; buffer[buf_off +0] = 255; buffer[buf_off +1] = 255; buffer[buf_off +2] = 255; } } } }
void ImageProcessorDlg::Impl::test(ViewPane& view) { #if 0 CImageDecoderPtr d= photos_[0]->GetDecoder(); Dib bmp; CSize size(500,500); if (d->DecodeImg(bmp, size, false) != IS_OK) return; size = CSize(bmp.GetWidth(), bmp.GetHeight()); ExceptionInfo exc; GetExceptionInfo(&exc); Image* img= ConstituteImage(bmp.GetWidth(), bmp.GetHeight(), "RGB", CharPixel, bmp.GetBuffer(), &exc); if (img) { double radius= 10.0; double sigma= 1.0; double amount= 1.0; double threshold= 0.01; // Image* sharp= UnsharpMaskImage(img, radius, sigma, amount, threshold, &exc); int w= size.cy * 16 / 9; Image* sharp= LiquidRescaleImage(img, w, size.cy, 1.0, 0.0, &exc); if (sharp) { //ImageInfo ii; //GetImageInfo(&ii); //strcpy(sharp->filename, "c:\\sharp.img"); size.cx = sharp->columns; size.cy = sharp->rows; const PixelPacket* pix= AcquireImagePixels(sharp, 0, 0, size.cx, size.cy, &exc); Dib b(size.cx, size.cy, 24); for (int y= size.cy - 1; y >= 0; --y) { BYTE* line= b.LineBuffer(y); for (int x= 0; x < size.cx; ++x) { line[0] = pix->red; line[1] = pix->green; line[2] = pix->blue; line += 3; pix++; } } b.Swap(bmp); DestroyImage(sharp); } DestroyImage(img); } view.DisplayBitmap(bmp); bmp.Save(L"c:\\resized.bmp"); #endif }