Esempio n. 1
0
void main(void)
{
	clock_t begin, end;
	double time_spent;
	double imageAspectRatio = 0;
	double originalWidth;
	double originalHeight;
	double masterAspectRatio = 1.77; 

	MagickWand *m_wand = NULL;
	
	int width,height;
	
	MagickWandGenesis();
	
	m_wand = NewMagickWand();

	MagickReadImage(m_wand,"mona-lisa.JPG");
	begin = clock();	
	
	originalWidth = MagickGetImageWidth(m_wand);
	originalHeight = MagickGetImageHeight(m_wand);
	
	imageAspectRatio = originalWidth/originalHeight;
	if(imageAspectRatio <= masterAspectRatio)
	{

		MagickCropImage(m_wand,originalWidth,(int)(originalWidth/masterAspectRatio),0,(originalHeight-(int)(originalWidth/masterAspectRatio))/2 );
		printf("%d X %d ",MagickGetImageWidth(m_wand) , MagickGetImageHeight(m_wand));
	}
	else
	{
		MagickCropImage(m_wand,(int)(originalHeight * masterAspectRatio),originalHeight,(originalWidth - (int)(originalHeight * masterAspectRatio))/2,0);
	}
	if (originalWidth > 1920)
		MagickResizeImage(m_wand,1920,1080,LanczosFilter,1);
	

	end = clock();
	time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
	printf("Time taken is %f\n",time_spent);

	
//	MagickCropImage(m_wand,originalWidth/2,originalHeight/2,0,0);
	// Set the compression quality to 95 (high quality = low compression)
	//MagickSetImageCompressionQuality(m_wand,95);
	
	/* Write the new image */
	MagickWriteImage(m_wand,"master.jpg");
	
	/* Clean up */
	if(m_wand)m_wand = DestroyMagickWand(m_wand);
	
	MagickWandTerminus();}
Esempio n. 2
0
int barcode_to_png (char *image_name) {
  MagickWand *magick_wand;
  MagickBooleanType status;

  int width, height, pad, half_pad;

  /* read a barcode image */
  MagickWandGenesis();
  magick_wand = NewMagickWand();
  MagickSetResolution(magick_wand, 300, 300);
  status = MagickReadImage(magick_wand, image_name);
  if (status == MagickFalse) ThrowWandException(magick_wand, 1);

  /* trim the image, resample it, and pad it by [10% of the long side] per side */
  MagickTrimImage(magick_wand, 10);
  width = MagickGetImageWidth(magick_wand);
  height = MagickGetImageHeight(magick_wand);
  pad = determine_padding(width, height);
  half_pad = round(pad/2);
  MagickExtentImage(magick_wand, width+pad, height+pad, -half_pad, -half_pad);
  
  /* write image (a PNG version and a formatted PS version) */
  status=MagickWriteImage(magick_wand, chop_path(image_name, ".png"));
  if (status == MagickFalse) ThrowWandException(magick_wand, 2);
  status=MagickWriteImage(magick_wand, chop_path(image_name, ".ps"));
  if (status == MagickFalse) ThrowWandException(magick_wand, 2);

  /* clean up */
  magick_wand=DestroyMagickWand(magick_wand);
  MagickWandTerminus();

  return 0;
}
Esempio n. 3
0
/**
* @brief add_info Added image info to the request
*
* @param im The image struct
* @param req The evhtp request
*/
void add_info(MagickWand *im, evhtp_request_t *req)
{
    MagickSizeType size = MagickGetImageSize(im);
    unsigned long width = MagickGetImageWidth(im);
    unsigned long height = MagickGetImageHeight(im);
    size_t quality = MagickGetImageCompressionQuality(im);
    quality = (quality == 0 ? 100 : quality);
    char *format = MagickGetImageFormat(im);

    //{"ret":true,"info":{"size":195135,"width":720,"height":480,"quality":75,"format":"JPEG"}}
    cJSON *j_ret = cJSON_CreateObject();
    cJSON *j_ret_info = cJSON_CreateObject();
    cJSON_AddBoolToObject(j_ret, "ret", 1);
    cJSON_AddNumberToObject(j_ret_info, "size", size);
    cJSON_AddNumberToObject(j_ret_info, "width", width);
    cJSON_AddNumberToObject(j_ret_info, "height", height);
    cJSON_AddNumberToObject(j_ret_info, "quality", quality);
    cJSON_AddStringToObject(j_ret_info, "format", format);
    cJSON_AddItemToObject(j_ret, "info", j_ret_info);
    char *ret_str_unformat = cJSON_PrintUnformatted(j_ret);
    LOG_PRINT(LOG_DEBUG, "ret_str_unformat: %s", ret_str_unformat);
    evbuffer_add_printf(req->buffer_out, "%s", ret_str_unformat);
    cJSON_Delete(j_ret);
    free(ret_str_unformat);
    free(format);
}
Esempio n. 4
0
/**
 * Legacy API support.
 */
apr_status_t
dims_legacy_crop_operation (dims_request_rec *d, char *args, char **err) {
    MagickStatusType flags;
    RectangleInfo rec;
    ExceptionInfo ex_info;
    long width, height;
    int x, y;

    flags = ParseGravityGeometry(GetImageFromMagickWand(d->wand), args, &rec, &ex_info);

    if(!(flags & AllValues)) {
        *err = "Parsing crop geometry failed";
        return DIMS_FAILURE;
    }

    width = MagickGetImageWidth(d->wand);
    height = MagickGetImageHeight(d->wand);
    x = (width / 2) - (rec.width / 2);
    y = (height / 2) - (rec.height / 2);

    ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, d->r, 
        "legacy_crop will crop to %ldx%ld+%d+%d", 
        rec.width, rec.height, x, y);

    MAGICK_CHECK(MagickCropImage(d->wand, rec.width, rec.height, x, y), d);

    return DIMS_SUCCESS;
}
Esempio n. 5
0
static zbar_image_t *_php_zbarcode_get_page(MagickWand *wand) 
{
	unsigned long width, height;
	unsigned char *image_data;
	size_t image_size;
	
	if (MagickSetImageDepth(wand, 8) == MagickFalse) {
		return NULL;
	}

	if (MagickSetImageFormat(wand, "GRAY") == MagickFalse) {
		return NULL;
	}
	
	width  = MagickGetImageWidth(wand);
	height = MagickGetImageHeight(wand);

	image_data = malloc(width * height);
	
	if (!MagickExportImagePixels(wand, 0, 0, width, height, "I", CharPixel, image_data)) {
		return NULL;
	}
	
	return _php_zbarcode_image_create(width, height, image_data);
}
Esempio n. 6
0
apr_status_t
dims_extent_operation (dims_request_rec *d, char *args, char **err) {
    MagickStatusType flags;
    RectangleInfo rec;
    
    flags = ParseAbsoluteGeometry(args, &rec);
    if(!(flags & AllValues)) {
        *err = "Parsing extent geometry failed";
        return DIMS_FAILURE;
    }
    
    PixelWand *p_wand = NewPixelWand();
    long w,h;
    int x, y;
    
    PixelSetColor(p_wand, "white");
    
    w = MagickGetImageWidth(d->wand);
    h = MagickGetImageHeight(d->wand);
    
    MagickSetImageBackgroundColor(d->wand,p_wand);
    
    x = (w - rec.width) / 2;
    y = (h - rec.height) / 2;
    MAGICK_CHECK(MagickExtentImage(d->wand,rec.width, rec.height, x, y), d);
    
    return DIMS_SUCCESS;   
} 
Esempio n. 7
0
apr_status_t
dims_resize_operation (dims_request_rec *d, char *args, char **err) {
    MagickStatusType flags;
    RectangleInfo rec;

    flags = ParseSizeGeometry(GetImageFromMagickWand(d->wand), args, &rec);
    if(!(flags & AllValues)) {
        *err = "Parsing thumbnail geometry failed";
        return DIMS_FAILURE;
    }

    if (d->optimize_resize) {
        size_t orig_width;
        size_t orig_height;

        RectangleInfo sampleRec = rec;
        sampleRec.width *= d->optimize_resize;
        sampleRec.height *= d->optimize_resize;

        orig_width = MagickGetImageWidth(d->wand);
        orig_height = MagickGetImageHeight(d->wand);

        if(sampleRec.width < orig_width && sampleRec.height < orig_height) {
            ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, d->r, "Sampling image down to %dx%d before resizing.", sampleRec.width, sampleRec.height);
            MAGICK_CHECK(MagickSampleImage(d->wand, sampleRec.width, sampleRec.height), d);
        }
    }

    MAGICK_CHECK(MagickScaleImage(d->wand, rec.width, rec.height), d);

    return DIMS_SUCCESS;
}
Esempio n. 8
0
bool c_Gmagick::adjust_dimensions(bool bestfit, long desired_width, long desired_height, 
                                  long *new_width, long *new_height) {
  long orig_width, orig_height;

  orig_width  = MagickGetImageWidth(magick_wand); 
  orig_height = MagickGetImageHeight(magick_wand);

  if ((orig_width == desired_width) && (orig_height == desired_height)) { 
    *new_width  = desired_width; 
    *new_height = desired_height;
    return true;
  }

  if (bestfit) { 
    double ratio_x, ratio_y; 

    if (desired_width <= 0 || desired_height <= 0) {
      return false;
    }

    ratio_x = (double)desired_width / (double)orig_width; 
    ratio_y = (double)desired_height / (double)orig_height; 

    if (ratio_x < ratio_y) { 
      *new_width  = desired_width; 
      *new_height = ratio_x * (double)orig_height; 
    } else { 
      *new_height = desired_height; 
      *new_width  = ratio_y * (double)orig_width; 
    } 

    *new_width  = (*new_width < 1)  ? 1 : *new_width; 
    *new_height = (*new_height < 1) ? 1 : *new_height; 
  } else { 
    double ratio; 

    if (desired_width <= 0 && desired_height <= 0) { 
      return false;
    }

    if (desired_width <= 0 || desired_height <= 0) {		 
      if (desired_width <= 0) { 
        ratio = (double)orig_height / (double)desired_height; 
        *new_width  = orig_width / ratio;
        *new_height = desired_height;
      } else { 
        ratio = (double)orig_width / (double)desired_width; 
        *new_height = orig_height / ratio;
        *new_width  = desired_width;
      }
    } else {
      *new_width  = desired_width; 
      *new_height = desired_height;
    }
  }
  return true;
}
Esempio n. 9
0
caddr_t bif_im_DeepZoom4to1 (caddr_t * qst, caddr_t * err, state_slot_t ** args)
  {
  im_env_t env;
  caddr_t res;
  int fmt_is_set = 0;
  int image_ctr;
  im_init (&env, qst, args, "IM DeepZoom4to1");
  im_set_background (&env, "#000000");
  env.ime_target_magick_wand = NewMagickWand ();
  if (MagickFalse == MagickNewImage (env.ime_target_magick_wand, 256, 256, env.ime_background))
    im_leave_with_error (&env, "22023", "IM001", "Can not make new image");
  if (MagickFalse == MagickSetImageType (env.ime_target_magick_wand, TrueColorType))
    im_leave_with_error (&env, "22023", "IM001", "Can not set image type");
  if (MagickFalse == MagickSetImageDepth (env.ime_target_magick_wand, 16))
    im_leave_with_error (&env, "22023", "IM001", "Can not set image depth");
  if (MagickFalse == MagickSetImageExtent (env.ime_target_magick_wand, 256, 256))
    im_leave_with_error (&env, "22023", "IM001", "Can not set image extent");
  if (MagickFalse == MagickSetImageBackgroundColor (env.ime_target_magick_wand, env.ime_background))
    im_leave_with_error (&env, "22023", "IM001", "Can not set image background");
  image_ctr = BOX_ELEMENTS (args) / 2;
  if (image_ctr > 4)
    image_ctr = 4;
  while (0 < image_ctr--)
    {
      if (DV_DB_NULL == DV_TYPE_OF (bif_arg (qst, args, image_ctr*2, "IM DeepZoom4to1")))
        continue;
      im_env_set_input_blob (&env, image_ctr * 2);
      /*im_env_set_blob_ext (&env, 2);*/
      im_read (&env);
      MagickResetIterator (env.ime_magick_wand);
      while (MagickNextImage (env.ime_magick_wand) != MagickFalse)
        {
          unsigned long v_size, h_size;
          if (!fmt_is_set)
            {
              if (MagickFalse == MagickSetImageFormat (env.ime_target_magick_wand, MagickGetImageFormat (env.ime_magick_wand)))
                im_leave_with_error (&env, "22023", "IM001", "Can not set image format");
              fmt_is_set = 1;
            }
          h_size = MagickGetImageWidth (env.ime_magick_wand);
          v_size = MagickGetImageHeight (env.ime_magick_wand);
          if ((256 < h_size) || (256 < v_size))
            continue;
          MagickResizeImage (env.ime_magick_wand, h_size/2, v_size/2, BoxFilter, 1.0);
          if (MagickFalse == MagickCompositeImage (env.ime_target_magick_wand, env.ime_magick_wand, OverCompositeOp, (image_ctr & 1) * 128, (image_ctr & 2) * 64))
            im_leave_with_error (&env, "22023", "IM001", "Can not composite image");
        }
      im_reset_read (&env);
    }
  MagickProfileImage (env.ime_target_magick_wand, "*", NULL, 0);
  DestroyMagickWand (env.ime_magick_wand);
  env.ime_magick_wand = env.ime_target_magick_wand;
  env.ime_target_magick_wand = NULL;
  res = im_write (&env);
  im_leave (&env);
  return res;
}
int convert_crop (MagickWand *input, convert_t *opts) {
  unsigned long crop_width = opts->crop_width;
  unsigned long crop_height = opts->crop_height;

  if (!crop_width && !opts->crop_x && !crop_height && !opts->crop_y) return MagickPass;
  if (!crop_width) crop_width = MagickGetImageWidth(input);
  if (!crop_height) crop_height = MagickGetImageHeight(input);

  return MagickCropImage(input, crop_width, crop_height, opts->crop_x, opts->crop_y);
}
struct bufferAndSize pngQuant(MagickWand *output){

      unsigned long wid = MagickGetImageWidth(output);
      unsigned long hei = MagickGetImageHeight(output);
      unsigned char *bmp_buffer;
      bmp_buffer = (unsigned char*) malloc(wid*hei*4);
      MagickGetImagePixels(output,0,0,wid,hei,"RGBA",CharPixel,bmp_buffer);

      liq_attr *attr = liq_attr_create();
      liq_image *qimage = liq_image_create_rgba(attr, bmp_buffer, wid, hei, 0);
      liq_set_max_colors(attr, 255);
      liq_set_speed(attr, 10);
      liq_result *res = liq_quantize_image(attr, qimage);

      int png_buffer_size = wid*hei;
      unsigned char *png_buffer = malloc(png_buffer_size);

      liq_write_remapped_image(res, qimage, png_buffer, png_buffer_size);
      const liq_palette *pal = liq_get_palette(res);

      LodePNGState state;

      lodepng_state_init(&state);
      /*optionally customize the state*/
      state.info_png.color.bitdepth = 8;
      state.info_png.color.colortype = LCT_PALETTE;
      state.info_raw.colortype = LCT_PALETTE;
      state.info_raw.bitdepth = 8;
      state.encoder.auto_convert = 0;
      state.encoder.add_id = 0;
      state.encoder.zlibsettings.nicematch = 258;
      int i = 0;
      for (i=0; i < pal->count; ++i)
      {
        lodepng_palette_add(&state.info_png.color, pal->entries[i].r , pal->entries[i].g, pal->entries[i].b, pal->entries[i].a);
        lodepng_palette_add(&state.info_raw, pal->entries[i].r , pal->entries[i].g, pal->entries[i].b, pal->entries[i].a);
      }
      unsigned char *data;
      size_t size = 0;
      lodepng_encode(&data, &size, png_buffer, wid, hei, &state);
      // writtendata = io_write(size, data);
      lodepng_state_cleanup(&state);
      free(bmp_buffer);
      free(png_buffer);
      liq_attr_destroy(attr);
      liq_image_destroy(qimage);
      liq_result_destroy(res);

      lodepng_state_cleanup(&state);
      struct bufferAndSize bs;
      bs.data = data;
      bs.size = size;
      return bs;
}
Esempio n. 12
0
int raster(MagickWand *image_wand, char **outBuffer)
{

	char *localPointer;
	PixelIterator *iterator;
	PixelWand **pixels;
	register long x;
	long y;
	unsigned long columns, rows;
	int bufcnt = 0;

	/*
	 * allocate buffer
	 */

	columns = MagickGetImageHeight(image_wand);
	rows = MagickGetImageWidth(image_wand);

	fprintf(stderr,"Image size : %ldx%ld\n", columns, rows);
	int outSize = 9 * rows * columns * sizeof(char);
	localPointer = (char*) malloc(outSize);
	if (localPointer == NULL)
	{
		perror("malloc");
		return 0;
	}

	/*
	 * convert wand into a buffer
	 */

	iterator = NewPixelIterator(image_wand);
	if (iterator == (PixelIterator *) NULL)
		ThrowWandException(image_wand);
	for (y = 0; y < (long) columns; y++)
	{
		pixels = PixelGetNextIteratorRow(iterator, &rows);
		if (pixels == (PixelWand **) NULL)
			break;
		for (x = 0; x < (long) rows; x++)
			bufcnt += sprintf(localPointer + bufcnt, "defb &%2.2X\n",
					cpc2raster(pixel2cpc(pixels[x])));
	}

	if (y < (long) columns)
		ThrowWandException(image_wand);
	iterator = DestroyPixelIterator(iterator);
	image_wand = DestroyMagickWand(image_wand);
	MagickWandTerminus();

	*outBuffer = localPointer;
	return bufcnt;
}
Esempio n. 13
0
/**
 * @brief crop crop an image
 *
 * @param im the image
 * @param x position x
 * @param y position y
 * @param cols target width
 * @param rows target height
 *
 * @return 0 for OK and -1 for fail
 */
static int crop(MagickWand *im, int x, int y, int cols, int rows)
{
    int ret = -1;
    unsigned long im_cols = MagickGetImageWidth(im);
    unsigned long im_rows = MagickGetImageHeight(im);
    if (x < 0) x = 0;
    if (y < 0) y = 0;
    if (x >= im_cols || y >= im_rows) return -1;
    if (cols == 0 || im_cols < x + cols) cols = im_cols - x;
    if (rows == 0 || im_rows < y + rows) rows = im_rows - y;
    LOG_PRINT(LOG_DEBUG, "wi_crop(im, %d, %d, %d, %d)", x, y, cols, rows);
    ret = MagickCropImage(im, cols, rows, x, y);
    return ret;
}
Esempio n. 14
0
/**
 * Prints count lines on output with background bg. Lines can be scaled by using factor.
 * NOTE: This performs some filtering removing lines passing through origin
 */
void print_lines(char* output, char* bg, LINE_TYPE* lines, sizep_t count, double factor) {
	MagickWand *mw = NULL;
	DrawingWand *dw = NULL;
	PixelWand *pmw = NULL;
	unsigned long width, semi_width, height, semi_height;

	MagickWandGenesis();
	mw = NewMagickWand();
	dw = NewDrawingWand();
	pmw = NewPixelWand();

	MagickReadImage(mw, bg);
	width = MagickGetImageWidth(mw);
	semi_width = ceil(width/2.0);
	height = MagickGetImageHeight(mw);
	semi_height = ceil(height/2.0);
	PixelSetColor(pmw,"red");
	DrawSetStrokeColor(dw, pmw);
	DrawSetStrokeWidth(dw, .5*factor);
	DrawSetStrokeAntialias(dw, 0);

	sizep_t n;
	for(n=0; n<count; n++) {
		LINE_TYPE cline = lines[n];

		double m = -cos(cline.t)/sin(cline.t);
		double b = cline.r/sin(cline.t);

		if ((-0.5 < m && m < 0.5) || (-1 < b && b < 1)) continue; // remove lines too horizontal

		double x0 = - ((long) semi_width); double y0 = x0*m+b;
		double x1 = ((long) semi_width); double y1 = x1*m+b;

		// Apply factor
		x0 *= factor; y0 *= factor; x1 *= factor; y1 *= factor;

		// Fix coordinates and plot over the image
		DrawLine(dw, x0+semi_width, height-y0-semi_height, x1+semi_width, height-y1-semi_height);
	}

	MagickDrawImage(mw,dw);
	MagickWriteImage(mw, output);

	pmw = DestroyPixelWand(pmw);
	mw = DestroyMagickWand(mw);
	dw = DestroyDrawingWand(dw);

	MagickWandTerminus();
}
Esempio n. 15
0
void load_image(ConvertContext *context) {
    int result = RESULT_OK;
    char *error_desc;
    ExceptionType error_type;

	// wait for resources to be available
	dispatch_semaphore_wait(context->conv_semaphore, DISPATCH_TIME_FOREVER);
	
	// load image
	if (MagickReadImage(context->mw, context->src_path) == MagickFalse) {
		// deal with error
        error_desc = MagickGetException(context->mw, &error_type);
        asprintf(&context->results.message,"Error loading image (%s): %s\n",error_desc,context->src_path);
        error_desc = (char *)MagickRelinquishMemory(error_desc);
        result += RESULT_ERROR;
    }
    if (result == RESULT_OK) {
        // get image info
        context->hasAlphaChannel = MagickGetImageAlphaChannel(context->mw);
        if  (context->hasAlphaChannel == MagickTrue) {
            context->imageWidth = MagickGetImageWidth(context->mw);
            context->imageHeight = MagickGetImageHeight(context->mw);

            // get pixel data
            context->pixel_count = context->imageWidth * context->imageHeight;
            context->pixels = malloc(context->pixel_count * sizeof(PixelData));
            if (context->pixels == NULL) {
                asprintf(&context->results.message, "Error allocating memory for pixel data: %s\n",context->src_path);
                result += RESULT_ERROR;
            } else {
                if (MagickExportImagePixels(context->mw, 0, 0, context->imageWidth, context->imageHeight, "ARGB", CharPixel, context->pixels) == MagickFalse) {
                    error_desc = MagickGetException(context->mw, &error_type);
                    asprintf(&context->results.message, "Error exporting pixel data (%s): %s\n",error_desc,context->src_path);
                    error_desc = (char *)MagickRelinquishMemory(error_desc);
                    result += RESULT_ERROR;
                }
            }
        }
    }
    if (result != RESULT_OK) {
        // clean up mess
		context->results.result = result;
		dispatch_group_async_f(context->conv_group, dispatch_get_main_queue(), context, (void (*)(void *))finish_image);
    } else {
		// move to next step
		dispatch_group_async_f(context->conv_group, context->conv_queue, context, (void (*)(void *))conv_image);
	}
}
Esempio n. 16
0
int main (int argc,char **argv)
 {
	FILE *fp;
	unsigned char *polje;
	char format[5] = {0};
	int len;
	size_t width, height;

	MagickWand	*image_wand;
	MagickBooleanType status;


	//ucitavanje slike
	if( (fp = fopen(argv[1],"rb")) == NULL)
		printf("nemoze otvorit sliku\n");

	fseek(fp,0,SEEK_END);
	len = ftell(fp);
	fseek(fp,0,SEEK_SET);

	polje = (unsigned char*) malloc (len);
	fread(polje,1,len,fp);

	//ucitavanje u imagemagick

	MagickWandGenesis();
	image_wand = NewMagickWand();

	status = MagickReadImageBlob(image_wand, polje, len);
	
	free(polje);
	if (status == MagickFalse)
					{printf("\nnevalja status\n"); exit(-1);}

	strcpy(format,MagickGetImageFormat(image_wand));
	width = MagickGetImageWidth(image_wand);
	height = MagickGetImageHeight(image_wand);

	printf("width = %d\nheight = %d\nformat = %s\n",width,height,format);

	fclose(fp);

	image_wand = DestroyMagickWand(image_wand); 
	MagickWandTerminus();

	system("pause");
    return 0;
 }
Esempio n. 17
0
// Given a pattern name (which MUST have a leading #) and a pattern file,
// set up a pattern URL for later reference in the specified drawing wand
// Currently only used in Text Effect 2 
void set_tile_pattern(DrawingWand *d_wand,char *pattern_name,char *pattern_file)
{
	MagickWand *t_wand;
	long w,h;

	t_wand=NewMagickWand();  
	MagickReadImage(t_wand,pattern_file);
	// Read the tile's width and height
	w = MagickGetImageWidth(t_wand);
	h = MagickGetImageHeight(t_wand);

	DrawPushPattern(d_wand, pattern_name+1, 0, 0, w, h);
	DrawComposite(d_wand, SrcOverCompositeOp, 0, 0, 0, 0, t_wand);
	DrawPopPattern(d_wand);
	DrawSetFillPatternURL(d_wand, pattern_name);
}
void to_convert_info (MagickWand *input, convert_info_t *res) {
  res->width = MagickGetImageWidth(input);
  res->height = MagickGetImageHeight(input);
  res->format = NOOP;

  res->pages = MagickGetNumberImages(input);

  char *fmt = MagickGetImageFormat(input);
  int i;

  for (i = JPEG; i <= PDF; i++) {
    if (strcmp(fmt, formats[i])) continue;
    res->format = i;
    break;
  }
}
Esempio n. 19
0
/**
 * Extracts from a binary image white points and consider them as information
 * filling sample array and setting size structure.
 *
 * NOTE: When extracting points origin is moved to the center of the image:
 *
 *                     +semi_height
 *     Image Origin
 *              +-----------|
 *              |           |
 *              |           |
 *              |           | Hough Origin
 *  -semi_width ------------+------------- +semi_width
 *                          |
 *                          |
 *                          |
 *                          |
 *
 *                     -semi_height
 */
sizep_t get_points(char* name, POINT_TYPE **sample, SIZE_TYPE *size) {
	MagickWand *mw = NULL;
	PixelWand **pmw = NULL;
	PixelIterator *imw = NULL;
	MagickWandGenesis();

	mw = NewMagickWand();
	MagickReadImage(mw, name);

	unsigned long width, semi_width, height, semi_height;
    width = MagickGetImageWidth(mw);
    semi_width = ceil(width/2.0);
    height = MagickGetImageHeight(mw);
    semi_height = ceil(height/2.0);
    imw = NewPixelIterator(mw);

    sizep_t count = 0;
    POINT_TYPE *aux = (POINT_TYPE*) malloc(sizeof(POINT_TYPE)*width*height);

    // Extract white points
    int y, x;
    for (y=0; y<height; y++) {
    	pmw = PixelGetNextIteratorRow(imw, &width);
    	for (x=0; x< (long) width; x++) {
    		if (PixelGetBlack(pmw[x])) {
    			aux[count].x = x-semi_width;
    			aux[count].y = (height-y)-semi_height;
    			count++;
    		}
    	}
    }

	POINT_TYPE* output = (POINT_TYPE*) malloc(sizeof(POINT_TYPE)*count);
	memcpy(output, aux, sizeof(POINT_TYPE)*count);
	free(aux); aux = NULL;

	if(mw)
		mw = DestroyMagickWand(mw);

	MagickWandTerminus();

	(*sample) = output;
	size->width = width;
	size->height = height;
	return count;
}
Esempio n. 20
0
File: sample.c Progetto: qtkmz/dojo
int main(int argc, char *argv[])
{
	MagickWand *wand = NULL;
	MagickBooleanType ret;
	char *description;
	ExceptionType excep;

	if (argc < 2) {
		fprintf(stderr, "Usage: %s file\n", argv[0]);
		return 1;
	}

	MagickWandGenesis();

	wand = NewMagickWand();
	if (wand == NULL) {
		description = MagickGetException(wand, &excep);
		fprintf(stderr, "%s; %s\n", "NewMagickWand() failed", description);
		MagickRelinquishMemory(description);
		return 1;
	}

	ret = MagickReadImage(wand, argv[1]);
	if (ret != MagickTrue) {
		description = MagickGetException(wand, &excep);
		fprintf(stderr, "%s; %s\n", "MagickReadImage() failed", description);
		MagickRelinquishMemory(description);
		return 1;
	}

	printf("Format: %s\n", MagickGetImageFormat(wand));
	printf("Filename: %s\n", MagickGetImageFilename(wand));
	printf("Compression Quality: %ld\n", MagickGetCompressionQuality(wand));
	printf("Signature: %s\n", MagickGetImageSignature(wand));
	printf("Width: %ld\n", MagickGetImageWidth(wand));
	printf("Height: %ld\n", MagickGetImageHeight(wand));

	if (wand != NULL) {
		DestroyMagickWand(wand);
	}

	MagickWandTerminus();

	return 0;
}
Esempio n. 21
0
Handle<Value> MerlinImage::ImageInfo(const Arguments& args) {
    HandleScope scope;
    MagickWand* wand = MerlinImage::ReadImage(  ObjectWrap::Unwrap<MerlinImage>(args.This()) );

    Local<Object> info = Object::New();
    info->Set(String::NewSymbol("width"), Number::New(MagickGetImageWidth(wand)));
    info->Set(String::NewSymbol("height"), Number::New(MagickGetImageHeight(wand)));
    info->Set(String::NewSymbol("size"), Number::New(MagickGetImageSize(wand)));
    double x;
    double y;
    MagickGetImageResolution(wand, &x, &y);
    Local<Object> resolution = Object::New();
    resolution->Set(String::NewSymbol("x"), Number::New(x));
    resolution->Set(String::NewSymbol("y"), Number::New(y));
    info->Set(String::NewSymbol("resolution"), resolution);

    return scope.Close(info);
}
Esempio n. 22
0
void probe_im(info_t *ipipe)
{
    MagickWand *wand = NULL;
    MagickBooleanType status;

    MagickWandGenesis();
    wand = NewMagickWand();

    if (wand == NULL) {
        tc_log_error(__FILE__, "cannot create magick wand");
        ipipe->error = 1;
        return;
    }

    status = MagickReadImage(wand, ipipe->name);
    if (status == MagickFalse) {
        ExceptionType severity;
        const char *description = MagickGetException(wand, &severity);

        tc_log_error(__FILE__, "%s", description);

        MagickRelinquishMemory((void*)description);
        ipipe->error = 1;
        return;
    }
    MagickSetLastIterator(wand);


	/* read all video parameter from input file */
	ipipe->probe_info->width = MagickGetImageWidth(wand);
	ipipe->probe_info->height = MagickGetImageHeight(wand);

	/* slide show? */
	ipipe->probe_info->frc = 9;   /* FRC for 1 fps */
	ipipe->probe_info->fps = 1.0;

	ipipe->probe_info->magic = ipipe->magic;
	ipipe->probe_info->codec = TC_CODEC_RGB24;

    DestroyMagickWand(wand);
    MagickWandTerminus();

	return;
}
Esempio n. 23
0
apr_status_t
dims_legacy_thumbnail_operation (dims_request_rec *d, char *args, char **err) {
    MagickStatusType flags;
    RectangleInfo rec;
    long width, height;
    int x, y;
    char *resize_args = apr_psprintf(d->pool, "%s^", args);

    flags = ParseSizeGeometry(GetImageFromMagickWand(d->wand), resize_args, &rec);
    if(!(flags & AllValues)) {
        *err = "Parsing thumbnail (resize) geometry failed";
        return DIMS_FAILURE;
    }

    if(rec.width < 200 && rec.height < 200) {
        MAGICK_CHECK(MagickThumbnailImage(d->wand, rec.width, rec.height), d);
    } else {
        MAGICK_CHECK(MagickScaleImage(d->wand, rec.width, rec.height), d);
    }

    ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, d->r, 
        "legacy_thumbnail will resize to %ldx%ld", rec.width, rec.height);

    flags = ParseAbsoluteGeometry(args, &rec);
    if(!(flags & AllValues)) {
        *err = "Parsing thumbnail (crop) geometry failed";
        return DIMS_FAILURE;
    }

    width = MagickGetImageWidth(d->wand);
    height = MagickGetImageHeight(d->wand);
    x = (width / 2) - (rec.width / 2);
    y = (height / 2) - (rec.height / 2);

    ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, d->r, 
        "legacy_thumbnail will crop to %ldx%ld+%d+%d", rec.width, rec.height, x, y);

    MAGICK_CHECK(MagickCropImage(d->wand, rec.width, rec.height, x, y), d);

    return DIMS_SUCCESS;
}
Esempio n. 24
0
int resize_image(char* path, char* req_path, size_t size, char* mode)
{
	// Create new image
	MagickWand* magick_wand = NewMagickWand();
	if((uintptr_t)magick_wand < 1)
		error("Could not initialize ImageMagick (magick_wand is NULL)", EXIT_FAILURE);

	// Read the Image
	bool status = MagickReadImage(magick_wand, path);
	if (status == false)
		return -1;

	size_t width = MagickGetImageWidth(magick_wand);
	size_t height = MagickGetImageHeight(magick_wand);

	// If the size is bigger than both height and width, redirect to original
	if(size >= width)
		return 1;

	size_t new_height = size;
	size_t new_width = size;

	// Unless a square image is requested, calculcate proper aspect ratio
	if(size != 1 && strcmp(mode, "square"))
		new_height = height * size / width;

	/* Turn the image into a thumbnail sequence (for animated GIFs)
	 * Automatically switches to a less cpu intense filter for animated GIFs.
	 */
	MagickResetIterator(magick_wand);
	for(int i = 0; MagickNextImage(magick_wand) != false && i < 250; i++)
		MagickResizeImage(magick_wand, new_width, new_height, !i ? LanczosFilter : TriangleFilter, 1);

	// Write the image
	status = MagickWriteImages(magick_wand, req_path, true);
	if (status == MagickFalse)
		error("Could not write image", EXIT_FAILURE)

	DestroyMagickWand(magick_wand);
	return 0;
}
Esempio n. 25
0
  char *get_thumbnail(char *full_filename, char *thumbnail_str, size_t *thumbnail_size)
 {
	
	char image_data = NULL;
	if(full_filename == NULL || thumbnail_str == NULL)
		return NULL;
	MagickBooleanType  status;
   	MagickWand  *magick_wand, *tmp_magick_wand;
	magick_wand=NewMagickWand();
	status=MagickReadImage(magick_wand, full_filename);
	if (status == MagickFalse)
  	  ThrowWandException(magick_wand);
	size_t height = MagickGetImageHeight(magick_wand);
	size_t width = MagickGetImageWidth(magick_wand);
	ssize_t i, j;
	ParseMetaGeometry(thumbnail_str, &i, &j, &width, &height);
	if(width <= 0 || height <= 0)
	{
		logError("%s%s:Geometry  %s error\n", __FILE__, __func__, thumbnail_str);
	}
	else
	{
		/*
		if(is_gif(full_filename))
		{
			  tmp_magick_wand = magick_wand;
			  magick_wand = MagickCoalesceImages(tmp_magick_wand);
			  tmp_magick_wand =   DestroyMagickWand(tmp_magick_wand);
		}
		*/	
		 MagickResetIterator(magick_wand);
   		 while (MagickNextImage(magick_wand) != MagickFalse)
     		 {
     		 	MagickThumbnailImage(magick_wand,height,width);
   		 }
		image_data =  MagickGetImagesBlob(magick_wand, thumbnail_size);
	}
	
	magick_wand=DestroyMagickWand(magick_wand);
	return image_data;
}
Esempio n. 26
0
int main(int argc, char* argv[])
{
	if (argc < 3)
	{
		fprintf (stderr, "Usage : %s IN_FILE OUT_FILE\n", argv[0]);
		exit(1);
	}

	MagickWand* m_wand;
	int w, h;

	MagickWandGenesis ();
	m_wand = NewMagickWand ();

	if (MagickReadImage (m_wand, argv[1]) == MagickFalse)
	{
		fprintf(stderr, "Cannot read image : %s\n", argv[1]);
		exit(1);
	}

	w = MagickGetImageWidth (m_wand);
	h = MagickGetImageHeight (m_wand);

	MagickResizeImage (m_wand, w/2, h/2, LanczosFilter, 1.0);
	MagickSetImageCompressionQuality (m_wand, QUALITY);

	if (MagickWriteImage (m_wand, argv[2]) == MagickFalse)
	{
		fprintf (stderr, "Cannot wright image : %s\n", argv[2]);
		exit(1);
	}

	if (m_wand)
	{
		m_wand = DestroyMagickWand (m_wnad);
	}
	MagickWandTerminus ();

	return 0;
}
Esempio n. 27
0
int main (int argc, char **argv) { 
    MagickWand *img = NULL;  
    imag_t p1, *p; p=&p1;
    char *res;
    int i;
    
    MagickWandGenesis();
    img = NewMagickWand();
    if (argc > 1 && MagickReadImage(img, argv[1]) == MagickFalse) 
        { fprintf(stderr, "Failed to read image!\n"); exit(EXIT_FAILURE); }
    
    /* Perform the required transform in image */
    //MagickTransformImageColorspace(img, GRAYColorspace); // To gray colors
    //MagickThresholdImage(img, 40000);    // Threshold 
    //MagickNegateImage(img, MagickFalse); // Negate or invert black/white 
    
    /* Extract image data */
    p->x = MagickGetImageWidth(img);
    p->y = MagickGetImageHeight(img); 
    p->p = malloc(p->x*p->y * sizeof(unsigned char));
    MagickExportImagePixels(img, 0, 0, p->x, p->y, "I", CharPixel, p->p);
     
    /* Gocr library functions *************************************************/
    gocr_ini();                       /* Init a gocr job */
    gocr_opts("0-9","#",NULL,0,0,0);  /* only digits, "#" for unrecognized char */
    res = gocr_call(p);               /* Call gocr */
    for (i=0; i < gocr_rows(); i++)  
        printf("%s\n", gocr_resp(i)); /* Print response */
    gocr_end();                       /* End a gocr job */
    /**************************************************************************/
    
    /* Write processed image */
    MagickWriteImage(img, "processed.png");
    DestroyMagickWand(img);
    MagickWandTerminus();
    
    exit(EXIT_SUCCESS);
}
int convert_scale (MagickWand *input, convert_t *opts) {
  unsigned long scale_width = opts->scale_width;
  unsigned long scale_height = opts->scale_height;

  if (!scale_height && !scale_width) return MagickPass;

  unsigned long wid = MagickGetImageWidth(input);
  unsigned long hei = MagickGetImageHeight(input);
  double ratio = (double)wid / (double)hei;

  unsigned long new_wid = 0;
  unsigned long new_hei = 0;

  if (opts->scale_options & FIXED) {
    new_wid = scale_width;
    new_hei = scale_height;
  } else {
    new_wid = MIN(scale_width, wid);
    new_hei = MIN(scale_height, hei);

    if (new_hei && new_wid) {
      double new_ratio = (double)new_wid / (double)new_hei;

      if (opts->scale_options & COVER) {
        if (new_ratio > ratio) new_hei = 0;
        else new_wid = 0;
      }
      if (opts->scale_options & CONTAIN) {
        if (new_ratio > ratio) new_wid = 0;
        else new_hei = 0;
      }
    }
  }
  if (!new_wid) new_wid = ratio * (double)new_hei;
  if (!new_hei) new_hei = (double)new_wid / ratio;
  return MagickScaleImage(input, new_wid, new_hei);
}
Esempio n. 29
0
apr_status_t
dims_save_operation(dims_request_rec *d, char *args, char **err) {
	apr_status_t status;
	if (d->config->save_allowed) {
		MagickStatusType myflags;
			RectangleInfo myrec;
			myflags = ParseAbsoluteGeometry(args, &myrec);

			if (myrec.width < MagickGetImageWidth(d->wand)
                            || myrec.height < MagickGetImageHeight(d->wand)) {
				dims_resize_operation(d, args, err);
			}

			if (MagickWriteImage(d->wand, d->r->filename) == MagickTrue) {
				status = DIMS_SAVED;
			} else {
				status = DIMS_FAILURE;
			}
	} else {
		status = DIMS_NOT_ALLOWED;
	}

	return status;
}
Esempio n. 30
0
static int scan_image (const char *filename)
{
    if(exit_code == 3)
        return(-1);

    int found = 0;
    MagickWand *images = NewMagickWand();
    if(!MagickReadImage(images, filename) && dump_error(images))
        return(-1);

    unsigned seq, n = MagickGetNumberImages(images);
    for(seq = 0; seq < n; seq++) {
        if(exit_code == 3)
            return(-1);

        if(!MagickSetIteratorIndex(images, seq) && dump_error(images))
            return(-1);

        zbar_image_t *zimage = zbar_image_create();
        assert(zimage);
        zbar_image_set_format(zimage, zbar_fourcc('Y','8','0','0'));

        int width = MagickGetImageWidth(images);
        int height = MagickGetImageHeight(images);
        zbar_image_set_size(zimage, width, height);

        // extract grayscale image pixels
        // FIXME color!! ...preserve most color w/422P
        // (but only if it's a color image)
        size_t bloblen = width * height;
        unsigned char *blob = malloc(bloblen);
        zbar_image_set_data(zimage, blob, bloblen, zbar_image_free_data);

        if(!MagickExportImagePixels(images, 0, 0, width, height,
                                    "I", CharPixel, blob))
            return(-1);

        if(xmllvl == 1) {
            xmllvl++;
            printf("<source href='%s'>\n", filename);
        }

        zbar_process_image(processor, zimage);

        // output result data
        const zbar_symbol_t *sym = zbar_image_first_symbol(zimage);
        for(; sym; sym = zbar_symbol_next(sym)) {
            zbar_symbol_type_t typ = zbar_symbol_get_type(sym);
            unsigned len = zbar_symbol_get_data_length(sym);
            if(typ == ZBAR_PARTIAL)
                continue;
            else if(xmllvl <= 0) {
                if(!xmllvl)
                    printf("%s:", zbar_get_symbol_name(typ));
                if(len &&
                   fwrite(zbar_symbol_get_data(sym), len, 1, stdout) != 1) {
                    exit_code = 1;
                    return(-1);
                }
            }
            else {
                if(xmllvl < 3) {
                    xmllvl++;
                    printf("<index num='%u'>\n", seq);
                }
                zbar_symbol_xml(sym, &xmlbuf, &xmlbuflen);
                if(fwrite(xmlbuf, xmlbuflen, 1, stdout) != 1) {
                    exit_code = 1;
                    return(-1);
                }
            }
            printf("\n");
            found++;
            num_symbols++;
        }
        if(xmllvl > 2) {
            xmllvl--;
            printf("</index>\n");
        }
        fflush(stdout);

        zbar_image_destroy(zimage);

        num_images++;
        if(zbar_processor_is_visible(processor)) {
            int rc = zbar_processor_user_wait(processor, -1);
            if(rc < 0 || rc == 'q' || rc == 'Q')
                exit_code = 3;
        }
    }

    if(xmllvl > 1) {
        xmllvl--;
        printf("</source>\n");
    }

    if(!found)
        notfound++;

    DestroyMagickWand(images);
    return(0);
}