Differences between revisions 1 and 2
Revision 1 as of 2005-11-25 08:31:22
Size: 3303
Editor: hoxide
Comment:
Revision 2 as of 2005-11-25 08:33:40
Size: 522
Editor: hoxide
Comment:
Deletions are marked like this. Additions are marked like this.
Line 12: Line 12:
{{{
#!cplusplus


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <magick/ImageMagick.h>

int main(int argc,char **argv)
{
  ExceptionInfo
    exception;
  
  Image
    *image,
    *rgimage,
    *gbimage,
    *brimage;
  
  ImageInfo
    *image_info;
  char
    * infilename,
    * rgfilename,
    * gbfilename,
    * brfilename;
  PixelPacket * ipix, *rgpix,*gbpix, *brpix;
  
  unsigned char M[3][256*256];
  unsigned char r,g,b;

  infilename = argv[1];
  rgfilename = argv[2];
  gbfilename = argv[3];
  brfilename = argv[4];
  
  int i,j;
  
  /*
    Initialize the image info structure and read an image.
  */
  InitializeMagick(*argv);
  GetExceptionInfo(&exception);
  image_info=CloneImageInfo((ImageInfo *) NULL);
  
  (void) strcpy(image_info->filename, infilename);
  image=ReadImage(image_info,&exception);
  if (exception.severity != UndefinedException)
    CatchException(&exception);
  if (image == (Image *) NULL)
    exit(1);
  /*
    Turn the images into a thumbnail sequence.
  */
  
  fprintf(stderr, "\nImage Info:\n"
   "name: %s\n"
   "colorspace : %d\n"
   "type : %d\n"
   "columns: %ld rows : %ld\n"
   "depth: %ld colors: %ld\n"
   ,
   image_info->filename,
   image->colorspace,
   GetImageType(image, &exception),
   image->columns,
   image->rows,
   image->depth,
   image->colors
   );

  ipix = AcquireImagePixels(image, 0, 0,
       image->columns ,image->rows,
       &exception);
  fprintf(stderr, "\ncounting ... \n");
  memset(M,0,sizeof(M));
  for(i=0; i < image->columns*image->rows ; i++)
    {
      r = ScaleQuantumToChar(ipix[i].red);
      g = ScaleQuantumToChar(ipix[i].green);
      b = ScaleQuantumToChar(ipix[i].blue);
      //if(i%100<10) fprintf(stderr, "(%d,%d,%d)\n", r,g,b);
      M[0][r*256 + g] < 255 ? M[0][r*256 + g] += 1:0;
      M[1][g*256 + b] < 255 ? M[1][g*256 + b] += 1:0;
      M[2][b*256 + r] < 255 ? M[2][b*256 + r] += 1:0;
    }
  
  fprintf(stderr, "Writeing file...\n");
  rgimage = ConstituteImage(256,256,"I",CharPixel,M[0],&exception);
  gbimage = ConstituteImage(256,256,"I",CharPixel,M[1],&exception);
  brimage = ConstituteImage(256,256,"I",CharPixel,M[2],&exception);
  
  fprintf(stderr, "Writeing file: %s\n", rgfilename);
  (void) strcpy(rgimage->filename, rgfilename);
  WriteImage(image_info, rgimage);
  fprintf(stderr, "Writeing file: %s\n", gbfilename);
  (void) strcpy(gbimage->filename, gbfilename);
  WriteImage(image_info, gbimage);
  fprintf(stderr, "Writeing file: %s\n", brfilename);
  (void) strcpy(brimage->filename, brfilename);
  WriteImage(image_info, brimage);
  
  image_info=DestroyImageInfo(image_info);
  
  DestroyExceptionInfo(&exception);
  DestroyMagick();
  return(0);
}

}}}
 [[Inlcude(/rimg.c)]]
 
 
 [[Include(/Makefile)]]

C API

起因

  • 做生物图象处理, 先用python, 速度实在太慢, 用c 选图像库, ImageMagic看上去不错. 可惜, 文档实在太简洁了, 好在open source, 看着源码摸索着走.

开端

ImageMagic/CAPI (last edited 2009-12-25 07:12:44 by localhost)