`
lobin
  • 瀏覽: 229085 次
  • 性別: Icon_minigender_1
  • 來自: 上海
社區版塊
存檔分類
最新評論

C: Linux C 編程 - GraphicsMagick圖像處理

 
閱讀更多

圖像處理

?

GraphicsMagick

?

GraphicsMagick Core C API

?

圖片縮放

#include<string.h>
#include<magick/api.h>

// gm2.0 --rate=0.444444 --input=huacao.jpg --output=huacao-new.jpg
// gm2.0 --width=400 --input=huacao.jpg --output=huacao-new.jpg
// gm2.0 --height=200 --input=huacao.jpg --output=huacao-new.jpg
// gm2.0 --width=400 --height=200 --input=huacao.jpg --output=huacao-new.jpg
int main(int argc, char **argv)
{
  char *filename = NULL;
  char *out = NULL;

  ExceptionInfo exception;
  ImageInfo *image_info;
  Image *image, *resize_image;
  
  unsigned long new_width = 0, new_height = 0;
  double rate = 0.0;

  int i;
  for (i = 1; i < argc; i++)
  {
    char* arg = argv[i];
    if (strstr(arg, "--rate=") != NULL)
    {
      arg += 7;
      sscanf(arg, "%lf", &rate);
    }
	else if (strstr(arg, "--width=") != NULL)
	{
      arg += 8;
      sscanf(arg, "%lu", &new_width);
	}
	else if (strstr(arg, "--height=") != NULL)
	{
      arg += 9;
      sscanf(arg, "%lu", &new_height);
	}
	else if (strstr(arg, "--input=") != NULL)
	{
      filename = arg + 8;
	  
	  if (strcmp(filename, "") == 0)
	  {
        filename = NULL;
	  }
	}
	else if (strstr(arg, "--output=") != NULL)
	{
      out = arg + 9;
	  
	  if (strcmp(out, "") == 0)
	  {
        out = NULL;
	  }
	}
  }
  

  if (filename == NULL)
  {
    fprintf(stderr, "no input ...");
    return 1;
  }
  if (rate == 0.0 && new_width == 0 && new_height == 0)
  {
    fprintf(stderr, "no size: rate, new width or height ...");
    return 1;
  }
  else if (rate != 0.0 && (new_width != 0 || new_height != 0))
  {
    fprintf(stderr, "invalid size: rate, new width or height ...");
    return 1;
  }

  
  if (out == NULL)
  {
    out = "out.jpg";
  }

  printf("filename=%s ...\n", filename);
  printf("rate=%f ...\n", rate);
  printf("output=%s ...\n", out);
  printf("new width=%ld, height=%ld ...\n", new_width, new_height);

  InitializeMagick(*argv);
  GetExceptionInfo(&exception);
  image_info = CloneImageInfo((ImageInfo *) NULL);

  strcpy(image_info->filename, filename);
  printf("Reading %s ...\n", image_info->filename);

  image = ReadImage(image_info, &exception);

  printf("width=%ld,height=%ld ...\n", image->columns, image->rows);
  printf("width=%ld,height=%ld ...\n", image->magick_columns, image->magick_rows);


  if (rate != 0.0)
  {
    new_width = image->columns * rate;
    new_height = image->rows * rate;
  }
  else if (new_width != 0 || new_height != 0)
  {
	if (new_width != 0 && new_height == 0)
	{
      rate = (double) new_width / image->columns;
	  new_height = image->rows * rate;
	}
	else if (new_width == 0 && new_height != 0)
	{
      rate = (double) new_height / image->rows;
	  new_width = image->columns * rate;
	}
  }
  printf("new width=%ld, height=%ld ...\n", new_width, new_height);

  resize_image = ResizeImage(image, new_width, new_height, LanczosFilter, 1.0, &exception);
  if (resize_image == (Image *) NULL)
  {
    fprintf(stderr, "resize err ...");
    return 1;
  }

  strcpy(resize_image->filename, out);
  printf("Writing %s ...\n", resize_image->filename);
  WriteImage(image_info, resize_image);

  DestroyImage(image);
  DestroyImageInfo(image_info);
  DestroyExceptionInfo(&exception);
  DestroyMagick();
  return 0;
}

gcc -c -I D:\sbin\usr\include\GraphicsMagick magick_test.c -o gm2.0.o

gcc -I D:\sbin\usr\include\GraphicsMagick magick_test.c -o gm2.0 -lGraphicsMagick

?

gm2.0 --rate=0.444444 --input=huacao.jpg --output=huacao-new.jpg

gm2.0 --width=400 --input=huacao.jpg --output=huacao-new.jpg

gm2.0 --height=200 --input=huacao.jpg --output=huacao-new.jpg

gm2.0 --width=400 --height=200 --input=huacao.jpg --output=huacao-new.jpg

?

0
1
分享到:
評論
Global site tag (gtag.js) - Google Analytics 开心农场种蔬菜赚钱 滨化股份股票行情 近期新股申购一览表 浙江省11选5五码走势图 个人理财规划 河南快三网站 pk10预测计划 辽宁35选7开奖结果29号 买股票主要看什么数 江西快3推荐一定牛 阿里巴巴股票行情走势 快乐12辽宁的基本走势 体彩排列5走势图 江西11选5赢钱技巧 地产基金配资比例 秒速赛车基本走势图 幸运赛车怎么算中