pair< unsigned char*, int >
Image::getBuffer() {
   size_t size = 0;
   ::ImageInfo info;
   GetImageInfo( &info );
   ExceptionInfo exception;
   GetExceptionInfo( &exception );

   unsigned char* blob = ImageToBlob( &info, m_image, &size,
                                      &exception );
   if ( blob == NULL ) {
      MC2String exceptionString = "Reason: ";
      exceptionString += exception.reason;
      exceptionString += "Description: ";
      exceptionString += exception.description;

      DestroyExceptionInfo( &exception );

      throw GSystem::Exception( "[ImageMagick]" + exceptionString );
   }

   DestroyExceptionInfo( &exception );

   return make_pair( blob, size );
}
Example #2
0
void* magickminify(void* src, ssize_t src_len, ssize_t* dst_len){
  Image *image, *resize;
  ImageInfo image_info;
  ExceptionInfo *exception;
  size_t len;
  void *ans;

  GetImageInfo(&image_info);
  exception = AcquireExceptionInfo();

  image = BlobToImage(&image_info, src, src_len, exception);

  if (exception->severity != UndefinedException)
    CatchException(exception);
  if (image == (Image *) NULL)
    exit(1);

  resize = MinifyImage(image, exception);

  if (exception->severity != UndefinedException)
    CatchException(exception);
  if (image == (Image *) NULL)
    exit(1);

  ans = ImageToBlob(&image_info, resize, &len, exception);
  
  if(dst_len != NULL)
    *dst_len = len;

  DestroyImage(image);
  DestroyImage(resize);
  DestroyExceptionInfo(exception);

  return ans;
}
Example #3
0
/**
 * Convert an image to a blob and the blob to a String.
 *
 * No Ruby usage (internal function)
 *
 * Notes:
 *   - Returns Qnil if there is no image
 *
 * @param image the Image to convert
 * @return Ruby string representation of image
 * @see str_to_image
 */
static VALUE
image_to_str(Image *image)
{
    VALUE dimg = Qnil;
    unsigned char *blob;
    size_t length;
    Info *info;
    ExceptionInfo *exception;

    if (image)
    {
        info = CloneImageInfo(NULL);
        exception = AcquireExceptionInfo();
        blob = ImageToBlob(info, image, &length, exception);
        DestroyImageInfo(info);
        CHECK_EXCEPTION();
        DestroyExceptionInfo(exception);
        dimg = rb_str_new((char *)blob, (long)length);
        magick_free((void*)blob);
    }

    RB_GC_GUARD(dimg);

    return dimg;
}
Example #4
0
static
ERL_NIF_TERM exmagick_image_dump_blob (ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
  void *blob;
  size_t size;
  exm_resource_t *resource;

  EXM_INIT;
  ErlNifResourceType *type = (ErlNifResourceType *) enif_priv_data(env);

  if (0 == enif_get_resource(env, argv[0], type, (void **) &resource))
  { EXM_FAIL(ehandler, "invalid handle"); }

  blob = ImageToBlob(resource->i_info, resource->image, &size, &resource->e_info);
  if (NULL == blob)
  {
    CatchException(&resource->e_info);
    EXM_FAIL(ehandler, resource->e_info.reason);
  }

  return(enif_make_tuple2(env,
                          enif_make_atom(env, "ok"),
                          enif_make_resource_binary(env, resource, blob, size)));
ehandler:
  return(enif_make_tuple2(env, enif_make_atom(env, "error"), exmagick_make_utf8str(env, errmsg)));
}
Example #5
0
int ImageWrite( struct ImageLibrary *im, Image *img, File *rootDev, const char *path )
{
	FHandler *fh = rootDev->f_FSys;
	File *rfp = (File *)fh->FileOpen( rootDev, path, "wb" );
	if( rfp != NULL )
	{
		char *buffer = NULL;
		size_t length = 0;

		
		ExceptionInfo *ei=AcquireExceptionInfo();
        ImageInfo *ii=CloneImageInfo((ImageInfo *) NULL);
								
		buffer =  ImageToBlob( ii, img, &length, ei );
		
		if( buffer == NULL )
		{
			ERROR("Cannot image to data\n");
			DestroyExceptionInfo( ei );
			DestroyImageInfo( ii );
			fh->FileClose( rootDev, rfp );
			
			return 2;
		}
		else
		{
			fh->FileWrite( rfp, buffer, length );
		}
		
		DestroyExceptionInfo( ei );
		DestroyImageInfo( ii );

		
		fh->FileClose( rootDev, rfp );
	}
	else
	{
		ERROR("Cannot open file: %s to write\n", path );
		return 1;
	}
	return 0;
}
Example #6
0
/*
    Static:     image_to_str
    Purpose:    Convert an image to a blob and the blob to a String
    Notes:      Returns Qnil if there is no image
*/
static VALUE
image_to_str(Image *image)
{
    volatile VALUE dimg = Qnil;
    unsigned char *blob;
    size_t length;
    Info *info;
    ExceptionInfo exception;

    if (image)
    {
        info = CloneImageInfo(NULL);
        GetExceptionInfo(&exception);
        blob = ImageToBlob(info, image, &length, &exception);
        DestroyImageInfo(info);
        CHECK_EXCEPTION();
        DestroyExceptionInfo(&exception);
        dimg = rb_str_new((char *)blob, (long)length);
        magick_free((void*)blob);
    }

    return dimg;
}
Example #7
0
static MagickBooleanType Huffman2DEncodeImage(const ImageInfo *image_info,
  Image *image,Image *inject_image)
{
  Image
    *group4_image;

  ImageInfo
    *write_info;

  MagickBooleanType
    status;

  size_t
    length;

  unsigned char
    *group4;

  status=MagickTrue;
  write_info=CloneImageInfo(image_info);
  (void) CopyMagickString(write_info->filename,"GROUP4:",MaxTextExtent);
  (void) CopyMagickString(write_info->magick,"GROUP4",MaxTextExtent);
  group4_image=CloneImage(inject_image,0,0,MagickTrue,&image->exception);
  if (group4_image == (Image *) NULL)
    return(MagickFalse);
  group4=(unsigned char *) ImageToBlob(write_info,group4_image,&length,
    &image->exception);
  group4_image=DestroyImage(group4_image);
  if (group4 == (unsigned char *) NULL)
    return(MagickFalse);
  write_info=DestroyImageInfo(write_info);
  if (WriteBlob(image,length,group4) != (ssize_t) length)
    status=MagickFalse;
  group4=(unsigned char *) RelinquishMagickMemory(group4);
  return(status);
}
Example #8
0
static MagickBooleanType WriteXTRNImage(const ImageInfo *image_info,Image *image)
{
  Image *
    p;

  ImageInfo
    *clone_info;

  int
    scene;

  MagickBooleanType
    status;

  void
    *param1,
    *param2,
    *param3;

  param1 = param2 = param3 = (void *) NULL;
  if (LocaleCompare(image_info->magick,"XTRNFILE") == 0)
    {
      clone_info=CloneImageInfo(image_info);
      status=WriteImage(image_info,image);
      if (status == MagickFalse)
        CatchImageException(image);
      clone_info=DestroyImageInfo(clone_info);
    }
  else if (LocaleCompare(image_info->magick,"XTRNIMAGE") == 0)
    {
      Image
        **image_ptr;

      ImageInfo
        **image_info_ptr;

      clone_info=CloneImageInfo(image_info);
      if (clone_info->filename[0])
        {
          (void) sscanf(clone_info->filename,"%lx,%lx",&param1,&param2);
          image_info_ptr=(ImageInfo **) param1;
          image_ptr=(Image **) param2;
          if ((image_info_ptr != (ImageInfo **) NULL) &&
              (image_ptr != (Image **) NULL))
            {
              *image_ptr=CloneImage(image,0,0,MagickFalse,&(image->exception));
              *image_info_ptr=clone_info;
            }
        }
    }
  else if (LocaleCompare(image_info->magick,"XTRNBLOB") == 0)
    {
      char
        **blob_data;

      ExceptionInfo
        exception;

      size_t
        *blob_length;

      char
        filename[MaxTextExtent];

      clone_info=CloneImageInfo(image_info);
      if (clone_info->filename[0])
        {
          (void) sscanf(clone_info->filename,"%lx,%lx,%s",
            &param1,&param2,&filename);

          blob_data=(char **) param1;
          blob_length=(size_t *) param2;

          scene = 0;
          (void) CopyMagickString(clone_info->filename,filename,MaxTextExtent);
          for (p=image; p != (Image *) NULL; p=GetNextImageInList(p))
          {
            (void) CopyMagickString(p->filename,filename,MaxTextExtent);
            p->scene=scene++;
          }
          SetImageInfo(clone_info,MagickTrue,&image->exception);
          (void) CopyMagickString(image->magick,clone_info->magick,
            MaxTextExtent);
          GetExceptionInfo(&exception);
          if (*blob_length == 0)
            *blob_length=8192;
          *blob_data=(char *) ImageToBlob(clone_info,image,blob_length,
            &exception);
          if (*blob_data == NULL)
            status=MagickFalse;
          if (status == MagickFalse)
            CatchImageException(image);
        }
      clone_info=DestroyImageInfo(clone_info);
    }
  else if (LocaleCompare(image_info->magick,"XTRNSTREAM") == 0)
    {
      size_t
        (*fifo)(const Image *,const void *,const size_t);

      char
        filename[MaxTextExtent];

      clone_info=CloneImageInfo(image_info);
      if (clone_info->filename[0])
        {
          (void) sscanf(clone_info->filename,"%lx,%lx,%s",
            &param1,&param2,&filename);

          fifo=(size_t (*)(const Image *,const void *,const size_t)) param1;
          image->client_data=param2;

          scene=0;
          (void) CopyMagickString(clone_info->filename,filename,MaxTextExtent);
          for (p=image; p != (Image *) NULL; p=GetNextImageInList(p))
          {
            (void) CopyMagickString(p->filename,filename,MaxTextExtent);
            p->scene=scene++;
          }
          SetImageInfo(clone_info,MagickTrue,&image->exception);
          (void) CopyMagickString(image->magick,clone_info->magick,
            MaxTextExtent);
          status=WriteStream(clone_info,image,fifo);
          if (status == MagickFalse)
            CatchImageException(image);
        }
      clone_info=DestroyImageInfo(clone_info);
    }
  else if (LocaleCompare(image_info->magick,"XTRNARRAY") == 0)
    {
      char
        filename[MaxTextExtent];

      clone_info=CloneImageInfo(image_info);
      if (clone_info->filename[0])
        {
          (void) sscanf(clone_info->filename,"%lx,%s",
            &param1,&filename);

          image->client_data=param1;

          scene = 0;
          (void) CopyMagickString(clone_info->filename,filename,MaxTextExtent);
          for (p=image; p != (Image *) NULL; p=GetNextImageInList(p))
          {
            (void) CopyMagickString(p->filename,filename,MaxTextExtent);
            p->scene=scene++;
          }
          SetImageInfo(clone_info,MagickTrue,&image->exception);
          (void) CopyMagickString(image->magick,clone_info->magick,
            MaxTextExtent);
          status=WriteStream(clone_info,image,SafeArrayFifo);
          if (status == MagickFalse)
            CatchImageException(image);
        }
      clone_info=DestroyImageInfo(clone_info);
    }
  return(MagickTrue);
}
Example #9
0
static unsigned int WriteXTRNImage(const ImageInfo *image_info,Image *image)
{
  Image *
    p;

  ImageInfo
    *clone_info;

  int
    scene;

  void
    *param1,
    *param2,
    *param3;

  unsigned int
    status;

  param1 = param2 = param3 = (void *) NULL;
  if (LocaleCompare(image_info->magick,"XTRNFILE") == 0)
    {
      clone_info=CloneImageInfo(image_info);
      status=WriteImage(image_info,image);
      if (status == False)
        CatchImageException(image);
      DestroyImageInfo(clone_info);
    }
  else if (LocaleCompare(image_info->magick,"XTRNIMAGE") == 0)
    {
      Image
        **image_ptr;

      ImageInfo
        **image_info_ptr;

      clone_info=CloneImageInfo(image_info);
      if (clone_info->filename[0])
        {
          (void) sscanf(clone_info->filename,"%lx,%lx",&param1,&param2);
          image_info_ptr=(ImageInfo **) param1;
          image_ptr=(Image **) param2;
          if ((image_info_ptr != (ImageInfo **) NULL) &&
              (image_ptr != (Image **) NULL))
            {
              *image_ptr=CloneImage(image,0,0,False,&(image->exception));
              *image_info_ptr=clone_info;
            }
        }
    }
  else if (LocaleCompare(image_info->magick,"XTRNBLOB") == 0)
    {
      char
        **blob_data;

      ExceptionInfo
        exception;

      size_t
        *blob_length;

      char
        filename[MaxTextExtent];

      clone_info=CloneImageInfo(image_info);
      if (clone_info->filename[0])
        {
          (void) sscanf(clone_info->filename,"%lx,%lx,%s",
			&param1,&param2,&filename);

          blob_data=(char **) param1;
          blob_length=(size_t *) param2;

          scene = 0;
          (void) strcpy(clone_info->filename, filename);
          for (p=image; p != (Image *) NULL; p=p->next)
	    {
	      (void) strcpy(p->filename, filename);
	      p->scene=scene++;
	    }
          SetImageInfo(clone_info,SETMAGICK_WRITE,&image->exception);
          (void) strcpy(image->magick,clone_info->magick);
          GetExceptionInfo(&exception);
          if (*blob_length == 0)
            *blob_length=8192;
          *blob_data=(char *) ImageToBlob(clone_info,image,blob_length,&exception);
          if (*blob_data == NULL)
            status=False;
          if (status == False)
            CatchImageException(image);
        }
      DestroyImageInfo(clone_info);
    }
  else if (LocaleCompare(image_info->magick,"XTRNARRAY") == 0)
    {
      char
        *blob_data;

      ExceptionInfo
        exception;

      size_t
        blob_length;

      char
        filename[MaxTextExtent];

      clone_info=CloneImageInfo(image_info);
      if (clone_info->filename[0])
        {
          (void) sscanf(clone_info->filename,"%lx,%s",
			&param1,&filename);

          image->client_data=param1;

          scene = 0;
	  blob_length = 0;
          (void) strcpy(clone_info->filename, filename);
          for (p=image; p != (Image *) NULL; p=p->next)
	    {
	      (void) strcpy(p->filename, filename);
	      p->scene=scene++;
	    }
          SetImageInfo(clone_info,SETMAGICK_WRITE,&image->exception);
          (void) strcpy(image->magick,clone_info->magick);
          GetExceptionInfo(&exception);
          blob_data=(char *) ImageToBlob(clone_info,image,&blob_length,&exception);
          if (blob_data == NULL)
            status=False;
          else
            SafeArrayFifo(image,blob_data,blob_length);

          if (status == False)
            CatchImageException(image);
        }
      DestroyImageInfo(clone_info);
    }
  return(True);
}
Example #10
0
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%   W r i t e I N L I N E I m a g e                                           %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  WriteINLINEImage() writes an image to a file in INLINE format (Base64).
%
%  The format of the WriteINLINEImage method is:
%
%      MagickBooleanType WriteINLINEImage(const ImageInfo *image_info,
%        Image *image,ExceptionInfo *exception)
%
%  A description of each parameter follows.
%
%    o image_info: the image info.
%
%    o image:  The image.
%
%    o exception: return any errors or warnings in this structure.
%
*/
static MagickBooleanType WriteINLINEImage(const ImageInfo *image_info,
  Image *image,ExceptionInfo *exception)
{
  char
    *base64,
    message[MagickPathExtent];

  const MagickInfo
    *magick_info;

  Image
    *write_image;

  ImageInfo
    *write_info;

  MagickBooleanType
    status;

  size_t
    blob_length,
    encode_length;

  unsigned char
    *blob;

  /*
    Convert image to base64-encoding.
  */
  assert(image_info != (const ImageInfo *) NULL);
  assert(image_info->signature == MagickCoreSignature);
  assert(image != (Image *) NULL);
  assert(image->signature == MagickCoreSignature);
  if (image->debug != MagickFalse)
    (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
  write_info=CloneImageInfo(image_info);
  (void) SetImageInfo(write_info,1,exception);
  if (LocaleCompare(write_info->magick,"INLINE") == 0)
    (void) CopyMagickString(write_info->magick,image->magick,MagickPathExtent);
  magick_info=GetMagickInfo(write_info->magick,exception);
  if ((magick_info == (const MagickInfo *) NULL) ||
      (GetMagickMimeType(magick_info) == (const char *) NULL))
    ThrowWriterException(CorruptImageError,"ImageTypeNotSupported");
  (void) CopyMagickString(image->filename,write_info->filename,
    MagickPathExtent);
  blob_length=2048;
  write_image=CloneImage(image,0,0,MagickTrue,exception);
  if (write_image == (Image *) NULL)
    {
      write_info=DestroyImageInfo(write_info);
      return(MagickTrue);
    }
  blob=(unsigned char *) ImageToBlob(write_info,write_image,&blob_length,
    exception);
  write_image=DestroyImage(write_image);
  write_info=DestroyImageInfo(write_info);
  if (blob == (unsigned char *) NULL)
    return(MagickFalse);
  encode_length=0;
  base64=Base64Encode(blob,blob_length,&encode_length);
  blob=(unsigned char *) RelinquishMagickMemory(blob);
  if (base64 == (char *) NULL)
    ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
  /*
    Write base64-encoded image.
  */
  status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
  if (status == MagickFalse)
    {
      base64=DestroyString(base64);
      return(status);
    }
  (void) FormatLocaleString(message,MagickPathExtent,"data:%s;base64,",
    GetMagickMimeType(magick_info));
  (void) WriteBlobString(image,message);
  (void) WriteBlobString(image,base64);
  base64=DestroyString(base64);
  return(MagickTrue);
}
Example #11
0
std::string
imageproc::ChangeImage(const std::string & inPath,
                       const std::string & tmpDir,
                       Type outputFormat,
                       const bp::List & transformations,
                       int quality,
                       unsigned int & x, unsigned int & y, 
                       unsigned int & orig_x, unsigned int & orig_y, 
                       std::string & oError)
{
    ExceptionInfo exception;
    Image *images;
    ImageInfo *image_info;

    orig_x = orig_y = x = y = 0;
    
    GetExceptionInfo(&exception);
    image_info = CloneImageInfo((ImageInfo *) NULL);

    // first we read the image
    if (exception.severity != UndefinedException)
    {
		if (exception.reason)
            g_bpCoreFunctions->log(BP_ERROR, "after: %s\n",
                                   exception.reason);
		if (exception.description)
            g_bpCoreFunctions->log(BP_ERROR, "after: %s\n",
                                   exception.description);
		CatchException(&exception);
    }


	(void) strcpy(image_info->filename, inPath.c_str());
    images = IP_ReadImageFile(image_info, inPath, &exception);
    
    if (exception.severity != UndefinedException)
    {
		if (exception.reason)
            g_bpCoreFunctions->log(BP_ERROR, "after: %s\n",
                                   exception.reason);
		if (exception.description)
            g_bpCoreFunctions->log(BP_ERROR, "after: %s\n",
                                   exception.description);
		CatchException(&exception);
    }
    
    if (!images)
    {
        oError.append("couldn't read image");
        DestroyImageInfo(image_info);
        image_info = NULL;
        DestroyExceptionInfo(&exception);
        return std::string();
    }

	g_bpCoreFunctions->log(
        BP_INFO, "Image contains %lu frames, type: %s\n",
        GetImageListLength(images),
        images->magick);

    // set quality
    if (quality > 100) quality = 100;
    if (quality < 0) quality = 0;
    image_info->quality = quality;

    g_bpCoreFunctions->log(
        BP_INFO, "Quality set to %d (0-100, worst-best)", quality);

    // execute 'actions' 
    images = runTransformations(images, transformations, quality, oError);

    // was all that successful?
    if (!images)
    {
        DestroyImageInfo(image_info);
        image_info = NULL;
        DestroyExceptionInfo(&exception);
        return std::string();
    } 

    // set the output size
    orig_x = images->magick_columns;
    orig_y = images->magick_rows;
    x = images->columns;
    y = images->rows;

    // let's set the output format correctly (default to input format)
    std::string name;
    if (outputFormat == UNKNOWN) name.append(ft::basename(inPath));
    else {
        name.append("img.");
        name.append(typeToExt(outputFormat));
        (void) sprintf(images->magick, outputFormat);
        g_bpCoreFunctions->log(BP_INFO, "Output to format: %s", outputFormat);
    }
    
    // Now let's go directly from blob to file.  We bypass
    // GM to-file functions so that we can handle wide filenames
    // safely on win32 systems.  A superior solution would
    // be to use GM stream facilities (if they exist)
    
    // upon success, will hold path to output file and will be returned to
    // client
    std::string rv;
    
    {
        size_t l = 0;
        void * blob = NULL;
        blob = ImageToBlob( image_info, images, &l, &exception );

        if (exception.severity != UndefinedException)
        {
            oError.append("ImageToBlob failed.");
            CatchException(&exception);
        }
        else
        {
            g_bpCoreFunctions->log(BP_INFO, "Writing %lu bytes to %s",
                                   l, name.c_str());

            if (!ft::mkdir(tmpDir, false)) {
                oError.append("Couldn't create temp dir");
            } else {
                std::string outpath = ft::getPath(tmpDir, name);
                FILE * f = ft::fopen_binary_write(outpath);
                if (f == NULL) { 
                    g_bpCoreFunctions->log(
                        BP_ERROR, "Couldn't open '%s' for writing!",
                        outpath.c_str());
                    oError.append("Error saving output image");
                } else {
                    size_t wt;
                    wt = fwrite(blob, sizeof(char), l, f);
                    fclose(f);

                    if (wt != l) {
                        g_bpCoreFunctions->log(
                            BP_ERROR,
                            "Partial write (%lu/%lu) when writing resultant "
                            "image '%s'",
                            wt, l, outpath.c_str());
                        oError.append("Error saving output image");
                    } else {
                        // success!
                        rv = outpath;
                    }
                }
            }
        }
    }
    
    DestroyImage(images);
    DestroyImageInfo(image_info);
    image_info = NULL;
    DestroyExceptionInfo(&exception);

    return rv;
}
Example #12
0
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%   W r i t e E P T I m a g e                                                 %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  WriteEPTImage() writes an image in the Encapsulated Postscript format
%  with a TIFF preview.
%
%  The format of the WriteEPTImage method is:
%
%      MagickBooleanType WriteEPTImage(const ImageInfo *image_info,
%        Image *image,ExceptionInfo *exception)
%
%  A description of each parameter follows.
%
%    o image_info: the image info.
%
%    o image:  The image.
%
%    o exception: return any errors or warnings in this structure.
%
*/
static MagickBooleanType WriteEPTImage(const ImageInfo *image_info,Image *image,
  ExceptionInfo *exception)
{
  char
     filename[MagickPathExtent];

  EPTInfo
    ept_info;

  Image
    *write_image;

  ImageInfo
    *write_info;

  MagickBooleanType
    status;

  /*
    Write EPT image.
  */
  assert(image_info != (const ImageInfo *) NULL);
  assert(image_info->signature == MagickCoreSignature);
  assert(image != (Image *) NULL);
  assert(image->signature == MagickCoreSignature);
  if (image->debug != MagickFalse)
    (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
  assert(exception != (ExceptionInfo *) NULL);
  assert(exception->signature == MagickCoreSignature);
  status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
  if (status == MagickFalse)
    return(status);
  write_image=CloneImage(image,0,0,MagickTrue,exception);
  if (write_image == (Image *) NULL)
    return(MagickFalse);
  write_info=CloneImageInfo(image_info);
  (void) CopyMagickString(write_info->magick,"EPS",MagickPathExtent);
  if (LocaleCompare(image_info->magick,"EPT2") == 0)
    (void) CopyMagickString(write_info->magick,"EPS2",MagickPathExtent);
  if (LocaleCompare(image_info->magick,"EPT3") == 0)
    (void) CopyMagickString(write_info->magick,"EPS3",MagickPathExtent);
  (void) ResetMagickMemory(&ept_info,0,sizeof(ept_info));
  ept_info.magick=0xc6d3d0c5ul;
  ept_info.postscript=(unsigned char *) ImageToBlob(write_info,write_image,
    &ept_info.postscript_length,exception);
  write_image=DestroyImage(write_image);
  write_info=DestroyImageInfo(write_info);
  if (ept_info.postscript == (void *) NULL)
    return(MagickFalse);
  write_image=CloneImage(image,0,0,MagickTrue,exception);
  if (write_image == (Image *) NULL)
    return(MagickFalse);
  write_info=CloneImageInfo(image_info);
  *write_info->magick='\0';
  (void) CopyMagickString(write_info->magick,"TIFF",MagickPathExtent);
  (void) FormatLocaleString(filename,MagickPathExtent,"tiff:%s",
    write_info->filename); 
  (void) CopyMagickString(write_info->filename,filename,MagickPathExtent);
  (void) TransformImage(&write_image,(char *) NULL,"512x512>",exception);
  if ((write_image->storage_class == DirectClass) ||
      (write_image->colors > 256))
    {
      QuantizeInfo
        quantize_info;

      /*
        EPT preview requires that the image is colormapped.
      */
      GetQuantizeInfo(&quantize_info);
      quantize_info.dither_method=IdentifyPaletteImage(write_image,
        exception) == MagickFalse ? RiemersmaDitherMethod : NoDitherMethod;
      (void) QuantizeImage(&quantize_info,write_image,exception);
    }
  write_info->compression=NoCompression;
  ept_info.tiff=(unsigned char *) ImageToBlob(write_info,write_image,
    &ept_info.tiff_length,exception);
  write_image=DestroyImage(write_image);
  write_info=DestroyImageInfo(write_info);
  if (ept_info.tiff == (void *) NULL)
    {
      ept_info.postscript=(unsigned char *) RelinquishMagickMemory(
        ept_info.postscript);
      return(MagickFalse);
    }
  /*
    Write EPT image.
  */
  (void) WriteBlobLSBLong(image,(unsigned int) ept_info.magick);
  (void) WriteBlobLSBLong(image,30);
  (void) WriteBlobLSBLong(image,(unsigned int) ept_info.postscript_length);
  (void) WriteBlobLSBLong(image,0);
  (void) WriteBlobLSBLong(image,0);
  (void) WriteBlobLSBLong(image,(unsigned int) ept_info.postscript_length+30);
  (void) WriteBlobLSBLong(image,(unsigned int) ept_info.tiff_length);
  (void) WriteBlobLSBShort(image,0xffff);
  (void) WriteBlob(image,ept_info.postscript_length,ept_info.postscript);
  (void) WriteBlob(image,ept_info.tiff_length,ept_info.tiff);
  /*
    Relinquish resources.
  */
  ept_info.postscript=(unsigned char *) RelinquishMagickMemory(
    ept_info.postscript);
  ept_info.tiff=(unsigned char *) RelinquishMagickMemory(ept_info.tiff);
  (void) CloseBlob(image);
  return(MagickTrue);
}
Example #13
0
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%   V a l i d a t e I m a g e F o r m a t s I n M e m o r y                   %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  ValidateImageFormatsInMemory() validates the ImageMagick image formats in
%  memory and returns the number of validation tests that passed and failed.
%
%  The format of the ValidateImageFormatsInMemory method is:
%
%      unsigned long ValidateImageFormatsInMemory(ImageInfo *image_info,
%        const char *reference_filename,const char *output_filename,
%        unsigned long *fail,ExceptionInfo *exception)
%
%  A description of each parameter follows:
%
%    o image_info: the image info.
%
%    o reference_filename: the reference image filename.
%
%    o output_filename: the output image filename.
%
%    o fail: return the number of validation tests that pass.
%
%    o exception: return any errors or warnings in this structure.
%
*/
static unsigned long ValidateImageFormatsInMemory(ImageInfo *image_info,
  const char *reference_filename,const char *output_filename,
  unsigned long *fail,ExceptionInfo *exception)
{
  char
    size[MaxTextExtent];

  const MagickInfo
    *magick_info;

  double
    distortion,
    fuzz;

  Image
    *difference_image,
    *reference_image,
    *reconstruct_image;

  MagickBooleanType
    status;

  register long
    i,
    j;

  size_t
    length;

  unsigned char
    *blob;

  unsigned long
    test;

  test=0;
  (void) fprintf(stdout,"validate image formats in memory:\n");
  for (i=0; reference_formats[i].magick != (char *) NULL; i++)
  {
    magick_info=GetMagickInfo(reference_formats[i].magick,exception);
    if ((magick_info == (const MagickInfo *) NULL) ||
        (magick_info->decoder == (DecodeImageHandler *) NULL) ||
        (magick_info->encoder == (EncodeImageHandler *) NULL))
      continue;
    for (j=0; reference_types[j].type != UndefinedType; j++)
    {
      /*
        Generate reference image.
      */
      CatchException(exception);
      (void) fprintf(stdout,"  test %lu: %s/%s/%lu-bits",test++,
        reference_formats[i].magick,MagickOptionToMnemonic(MagickTypeOptions,
        reference_types[j].type),reference_types[j].depth);
      (void) CopyMagickString(image_info->filename,reference_filename,
        MaxTextExtent);
      reference_image=ReadImage(image_info,exception);
      if (reference_image == (Image *) NULL)
        {
          (void) fprintf(stdout,"... fail @ %s/%s/%lu.\n",GetMagickModule());
          (*fail)++;
          continue;
        }
      /*
        Write reference image.
      */
      (void) FormatMagickString(size,MaxTextExtent,"%lux%lu",
        reference_image->columns,reference_image->rows);
      (void) CloneString(&image_info->size,size);
      image_info->depth=reference_types[j].depth;
      (void) FormatMagickString(reference_image->filename,MaxTextExtent,"%s:%s",
        reference_formats[i].magick,output_filename);
      status=SetImageType(reference_image,reference_types[j].type);
      InheritException(exception,&reference_image->exception);
      if (status == MagickFalse)
        {
          (void) fprintf(stdout,"... fail @ %s/%s/%lu.\n",GetMagickModule());
          (*fail)++;
          reference_image=DestroyImage(reference_image);
          continue;
        }
      status=SetImageDepth(reference_image,reference_types[j].depth);
      InheritException(exception,&reference_image->exception);
      if (status == MagickFalse)
        {
          (void) fprintf(stdout,"... fail @ %s/%s/%lu.\n",GetMagickModule());
          (*fail)++;
          reference_image=DestroyImage(reference_image);
          continue;
        }
      status=WriteImage(image_info,reference_image);
      InheritException(exception,&reference_image->exception);
      reference_image=DestroyImage(reference_image);
      if (status == MagickFalse)
        {
          (void) fprintf(stdout,"... fail @ %s/%s/%lu.\n",GetMagickModule());
          (*fail)++;
          continue;
        }
      /*
        Read reference image.
      */
      (void) FormatMagickString(image_info->filename,MaxTextExtent,"%s:%s",
        reference_formats[i].magick,output_filename);
      reference_image=ReadImage(image_info,exception);
      if (reference_image == (Image *) NULL)
        {
          (void) fprintf(stdout,"... fail @ %s/%s/%lu.\n",GetMagickModule());
          (*fail)++;
          continue;
        }
      /*
        Write reference image.
      */
      (void) FormatMagickString(reference_image->filename,MaxTextExtent,"%s:%s",
        reference_formats[i].magick,output_filename);
      (void) CopyMagickString(image_info->magick,reference_formats[i].magick,
        MaxTextExtent);
      reference_image->depth=reference_types[j].depth;
      length=8192;
      blob=ImageToBlob(image_info,reference_image,&length,exception);
      if (blob == (unsigned char *) NULL)
        {
          (void) fprintf(stdout,"... fail @ %s/%s/%lu.\n",GetMagickModule());
          (*fail)++;
          reference_image=DestroyImage(reference_image);
          continue;
        }
      /*
        Read reconstruct image.
      */
      (void) FormatMagickString(image_info->filename,MaxTextExtent,"%s:%s",
        reference_formats[i].magick,output_filename);
      reconstruct_image=BlobToImage(image_info,blob,length,exception);
      blob=(unsigned char *) RelinquishMagickMemory(blob);
      if (reconstruct_image == (Image *) NULL)
        {
          (void) fprintf(stdout,"... fail @ %s/%s/%lu.\n",GetMagickModule());
          (*fail)++;
          reference_image=DestroyImage(reference_image);
          continue;
        }
      /*
        Compare reference to reconstruct image.
      */
      fuzz=0.0;
      if (reference_formats[i].fuzz != 0.0)
        fuzz=reference_formats[i].fuzz;
#if defined(MAGICKCORE_HDRI_SUPPORT)
      fuzz+=0.003;
#endif
      if (reference_image->colorspace != RGBColorspace)
        fuzz+=0.3;
      fuzz+=MagickEpsilon;
      difference_image=CompareImageChannels(reference_image,reconstruct_image,
        AllChannels,MeanSquaredErrorMetric,&distortion,exception);
      reconstruct_image=DestroyImage(reconstruct_image);
      reference_image=DestroyImage(reference_image);
      if (difference_image == (Image *) NULL)
        {
          (void) fprintf(stdout,"... fail @ %s/%s/%lu.\n",GetMagickModule());
          (*fail)++;
          continue;
        }
      difference_image=DestroyImage(difference_image);
      if ((distortion/QuantumRange) > fuzz)
        {
          (void) fprintf(stdout,"... fail (with distortion %g).\n",distortion/
            QuantumRange);
          (*fail)++;
          continue;
        }
      (void) fprintf(stdout,"... pass.\n");
    }
  }
  (void) fprintf(stdout,"  summary: %lu subtests; %lu passed; %lu failed.\n",
    test,test-(*fail),*fail);
  return(test);
}
Example #14
0
static MagickBooleanType WriteXTRNImage(const ImageInfo *image_info,
  Image *image,ExceptionInfo *exception)
{
  Image *
    p;

  ImageInfo
    *clone_info;

  int
    scene;

  MagickBooleanType
    status;

  void
    *param1,
    *param2,
    *param3;

  param1 = param2 = param3 = (void *) NULL;
  status=MagickTrue;
  if (LocaleCompare(image_info->magick,"XTRNFILE") == 0)
    {
      clone_info=CloneImageInfo(image_info);
      *clone_info->magick='\0';
      status=WriteImage(clone_info,image,exception);
      if (status == MagickFalse)
        CatchImageException(image);
      clone_info=DestroyImageInfo(clone_info);
    }
  else if (LocaleCompare(image_info->magick,"XTRNIMAGE") == 0)
    {
      Image
        **image_ptr;

      ImageInfo
        **image_info_ptr;

      clone_info=CloneImageInfo(image_info);
      if (clone_info->filename[0])
        {
          (void) sscanf(clone_info->filename,"%lx,%lx",&param1,&param2);
          image_info_ptr=(ImageInfo **) param1;
          image_ptr=(Image **) param2;
          if ((image_info_ptr != (ImageInfo **) NULL) &&
              (image_ptr != (Image **) NULL))
            {
              *image_ptr=CloneImage(image,0,0,MagickFalse,exception);
              *image_info_ptr=clone_info;
            }
        }
    }
  else if (LocaleCompare(image_info->magick,"XTRNBLOB") == 0)
    {
      char
        **blob_data;

      size_t
        *blob_length;

      char
        filename[MagickPathExtent];

      clone_info=CloneImageInfo(image_info);
      if (clone_info->filename[0])
        {
          (void) sscanf(clone_info->filename,"%lx,%lx,%2048s",
            &param1,&param2,filename);

          blob_data=(char **) param1;
          blob_length=(size_t *) param2;
          scene = 0;
          (void) CopyMagickString(clone_info->filename,filename,
            MagickPathExtent);
          for (p=image; p != (Image *) NULL; p=GetNextImageInList(p))
          {
            (void) CopyMagickString(p->filename,filename,MagickPathExtent);
            p->scene=scene++;
          }
          SetImageInfo(clone_info,1,exception);
          (void) CopyMagickString(image->magick,clone_info->magick,
            MagickPathExtent);
          if (*blob_length == 0)
            *blob_length=8192;
          *blob_data=(char *) ImageToBlob(clone_info,image,blob_length,
            exception);
          if (*blob_data == NULL)
            status=MagickFalse;
          if (status == MagickFalse)
            CatchImageException(image);
        }
      clone_info=DestroyImageInfo(clone_info);
    }
  else if (LocaleCompare(image_info->magick,"XTRNARRAY") == 0)
    {
      char
        filename[MagickPathExtent];

      size_t
        blob_length;

      unsigned char
        *blob_data;

      clone_info=CloneImageInfo(image_info);
      if (*clone_info->filename != '\0')
        {
          (void) sscanf(clone_info->filename,"%lx,%2048s",&param1,filename);
          image->client_data=param1;
          scene=0;
          (void) CopyMagickString(clone_info->filename,filename,
            MagickPathExtent);
          for (p=image; p != (Image *) NULL; p=GetNextImageInList(p))
          {
            (void) CopyMagickString(p->filename,filename,MagickPathExtent);
            p->scene=scene++;
          }
          SetImageInfo(clone_info,1,exception);
          (void) CopyMagickString(image->magick,clone_info->magick,
            MagickPathExtent);
          blob_data=ImageToBlob(clone_info,image,&blob_length,
            exception);
          if (blob_data == (unsigned char *) NULL)
            status=MagickFalse;
          else
            SafeArrayFifo(image,blob_data,blob_length);
          if (status == MagickFalse)
            CatchImageException(image);
        }
      clone_info=DestroyImageInfo(clone_info);
    }
  return(MagickTrue);
}
Example #15
0
static MagickBooleanType WriteCALSImage(const ImageInfo *image_info,
  Image *image)
{
  char
    header[129];

  Image
    *group4_image;

  ImageInfo
    *write_info;

  MagickBooleanType
    status;

  register ssize_t
    i;

  size_t
    density,
    length,
    orient_x,
    orient_y;

  ssize_t
    count;

  unsigned char
    *group4;

  /*
    Open output image file.
  */
  assert(image_info != (const ImageInfo *) NULL);
  assert(image_info->signature == MagickSignature);
  assert(image != (Image *) NULL);
  assert(image->signature == MagickSignature);
  if (image->debug != MagickFalse)
    (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
  status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
  if (status == MagickFalse)
    return(status);
  /*
    Create standard CALS header.
  */
  count=WriteCALSRecord(image,"srcdocid: NONE");
  (void) count;
  count=WriteCALSRecord(image,"dstdocid: NONE");
  count=WriteCALSRecord(image,"txtfilid: NONE");
  count=WriteCALSRecord(image,"figid: NONE");
  count=WriteCALSRecord(image,"srcgph: NONE");
  count=WriteCALSRecord(image,"docls: NONE");
  count=WriteCALSRecord(image,"rtype: 1");
  orient_x=0;
  orient_y=0;
  switch (image->orientation)
  {
    case TopRightOrientation:
    {
      orient_x=180;
      orient_y=270;
      break;
    }
    case BottomRightOrientation:
    {
      orient_x=180;
      orient_y=90;
      break;
    }
    case BottomLeftOrientation:
    {
      orient_y=90;
      break;
    }
    case LeftTopOrientation:
    {
      orient_x=270;
      break;
    }
    case RightTopOrientation:
    {
      orient_x=270;
      orient_y=180;
      break;
    }
    case RightBottomOrientation:
    {
      orient_x=90;
      orient_y=180;
      break;
    }
    case LeftBottomOrientation:
    {
      orient_x=90;
      break;
    }
    default:
    {
      orient_y=270;
    }
  }
  (void) FormatLocaleString(header,MaxTextExtent,"rorient: %03ld,%03ld",
    (long) orient_x,(long) orient_y);
  count=WriteCALSRecord(image,header);
  (void) FormatLocaleString(header,MaxTextExtent,"rpelcnt: %06lu,%06lu",
    (unsigned long) image->columns,(unsigned long) image->rows);
  count=WriteCALSRecord(image,header);  
  density=200;
  if (image_info->density != (char *) NULL)
    {
      GeometryInfo
        geometry_info;

      (void) ParseGeometry(image_info->density,&geometry_info);
      density=(size_t) floor(geometry_info.rho+0.5);
    }
  (void) FormatLocaleString(header,MaxTextExtent,"rdensty: %04lu",
    (unsigned long) density);
  count=WriteCALSRecord(image,header);
  count=WriteCALSRecord(image,"notes: NONE");
  (void) ResetMagickMemory(header,' ',128);
  for (i=0; i < 5; i++)
    (void) WriteBlob(image,128,(unsigned char *) header);
  /*
    Write CALS pixels.
  */
  write_info=CloneImageInfo(image_info);
  (void) CopyMagickString(write_info->filename,"GROUP4:",MaxTextExtent);
  (void) CopyMagickString(write_info->magick,"GROUP4",MaxTextExtent);
  group4_image=CloneImage(image,0,0,MagickTrue,&image->exception);
  if (group4_image == (Image *) NULL)
    {
      (void) CloseBlob(image);
      return(MagickFalse);
    }
  group4=(unsigned char *) ImageToBlob(write_info,group4_image,&length,
    &image->exception);
  group4_image=DestroyImage(group4_image);
  if (group4 == (unsigned char *) NULL)
    {
      (void) CloseBlob(image);
      return(MagickFalse);
    }
  write_info=DestroyImageInfo(write_info);
  if (WriteBlob(image,length,group4) != (ssize_t) length)
    status=MagickFalse;
  group4=(unsigned char *) RelinquishMagickMemory(group4);
  (void) CloseBlob(image);
  return(status);
}
static ngx_int_t
ngx_screenshot_handler(ngx_http_request_t *r)
{
    size_t                              len;
    u_char                             *p;
    ngx_buf_t                          *b;
    ngx_chain_t                         cl;
    u_char                              path[1024];
    ExceptionInfo                      *exception;
    Image                              *image = 0, *resized = 0;
    ImageInfo                          *image_info = 0;
    unsigned char                      *image_data;
    ngx_str_t                           size;
    ngx_int_t                           width = -1, height = -1;
    ngx_screenshot_sizes_conf_t        *screenshot_sizes_conf = ngx_event_get_conf(ngx_cycle->conf_ctx, ngx_screenshot_module);

    ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "TEST %d", screenshot_sizes_conf->sizes.nelts);
    
    //TODO: generate unique name
    system("avconv -analyzeduration 1000 -i \"rtmp://127.0.0.1:1935\" -vframes 1 -q:v 2 -f image2 /tmp/output.png -loglevel quiet");

    MagickCoreGenesis("", MagickTrue);
    exception = AcquireExceptionInfo();
    
    ngx_memzero(path, sizeof(path));
    
    strcpy((char *)path, "/tmp/output.png");
    
    image_info = CloneImageInfo((ImageInfo *)NULL);
    strcpy(image_info->filename, (const char*)path);
    
    image = ReadImage(image_info, exception);
    
    if (image == 0) {
        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Failed to open screenshot file");
        goto error;
    }
    
    if (ngx_http_arg(r, (u_char *) "size", sizeof("size") - 1, &size) == NGX_OK) {
        
        if (size.len > 0) {
            if (size.data[0] == 'l' && size.data[1] == 'o' && size.data[2] == 'w') {
                width = 320;
                height = 180;
            }
            else if (size.data[0] == 'm' && size.data[1] == 'e' && size.data[2] == 'd') {
                width = 640;
                height = 360;
            }
            else if (size.data[0] == 'h' && size.data[1] == 'i') {
                width = 960;
                height = 540;
            }
            else if (size.data[0] == 'h' && size.data[1] == 'd') {
                width = 1280;
                height = 720;
            }
        }
    }
    
    if (width < 1 || width > (ngx_int_t)image->columns) {
        width = image->columns;
    }
    
    if (height < 1 || height > (ngx_int_t)image->rows) {
        height = image->rows;
    }
    
    if (width == (ngx_int_t)image->columns && height == (ngx_int_t)image->rows) {
        resized = image;
    }
    else {
        resized = ResizeImage(image, width, height, SincFilter, 1.0, exception);
    }
    
    if (resized == 0) {
        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Failed to resize screenshot file");
        goto error;
    }
    
    image_data = ImageToBlob(image_info, resized, &len, exception);
    
    p = ngx_palloc(r->connection->pool, len);
    if (p == NULL) {
        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Failed to allocate memory for response");
        goto error;
    }
    
    ngx_memcpy(p, image_data, len);
    
    if (resized != image) {
        DestroyImage(resized);
    }
    
    DestroyImage(image);
    MagicComponentTerminus();
    
    ngx_str_set(&r->headers_out.content_type, "image/png");
    r->headers_out.status = NGX_HTTP_OK;
    r->headers_out.content_length_n = len;
    
    b = ngx_calloc_buf(r->pool);
    if (b == NULL) {
        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Failed to allocate memory for response");
        goto error;
    }
    
    b->start = b->pos = p;
    b->end = b->last = p + len;
    b->temporary = 1;
    b->last_buf = 1;
    
    ngx_memzero(&cl, sizeof(cl));
    cl.buf = b;
    
    ngx_http_send_header(r);
    
    return ngx_http_output_filter(r, &cl);
    
error:
    if (resized && resized != image) DestroyImage(resized);
    if (image) DestroyImage(image);
    
    MagicComponentTerminus();
    
    return NGX_HTTP_INTERNAL_SERVER_ERROR;
}