コード例 #1
0
ファイル: pict2png.c プロジェクト: brianwells/pict2png
void save_image(ConvertContext *context) {
    int result = RESULT_OK;
    char *error_desc;
    ExceptionType error_type;
	
    // get pixel data
    if (context->hasAlphaChannel == MagickTrue) {
        if (MagickImportImagePixels(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;
        }
    }
    
    // convert image to PNG
    if (result == RESULT_OK) {
        if (MagickSetImageFormat(context->mw,"PNG") == MagickFalse) {
            error_desc = MagickGetException(context->mw, &error_type);
			asprintf(&context->results.message,"Error converting image (%s): %s\n",error_desc,context->src_path);
            error_desc = (char *)MagickRelinquishMemory(error_desc);
            result += RESULT_ERROR;
        }
    }
	
	// activate/deactivate alpha channel
	MagickSetImageAlphaChannel(context->mw, (context->results.alpha_type == ALPHA_TYPE_NONE ? DeactivateAlphaChannel : ActivateAlphaChannel));

    // make sure image is saved as RGB and not crunched down to grayscale
	MagickSetType(context->mw, (context->results.alpha_type == ALPHA_TYPE_NONE ? TrueColorType : TrueColorMatteType));
	
	// save image to disk
    if (result == RESULT_OK) {
        if (MagickWriteImage(context->mw, context->dst_path) == MagickFalse) {
            error_desc = MagickGetException(context->mw, &error_type);
            asprintf(&context->results.message, "Error saving image (%s): %s\n",error_desc,context->dst_path);
            error_desc = (char *)MagickRelinquishMemory(error_desc);
            result += RESULT_ERROR;
        }
    }
	
	if (result == RESULT_OK && context->options.delete_original != 0) {
		if (unlink(context->src_path) == -1) {
			asprintf(&context->results.message, "Unable to delete original image (%s): %s\n", strerror(errno), context->src_path);
			result += RESULT_ERROR;
		}
    }
    
	// cleanup and report results
	context->results.result = result;
	dispatch_group_async_f(context->conv_group, dispatch_get_main_queue(), context, (void (*)(void *))finish_image);
}
コード例 #2
0
ファイル: magick.c プロジェクト: kulve/pwgallery
/*
 * Apply image modifications (rotate, gamma, etc) to the
 * image. Resizing to web image or thumbnail is done afterwards
 */
static gboolean _apply_modifications(struct data *data, 
                                     MagickWand *wand, 
                                     struct image *image)
{
    gchar *desc;
    ExceptionType severity;

    g_debug("in _apply_modifications");

    g_assert(data != NULL);
    g_assert(wand != NULL);
    g_assert(image != NULL);
    
    /* rotate image */
    if (image->rotate) {
        PixelWand *px;

        px = NewPixelWand();
        g_assert(px);

        PixelSetColor(px, "blue");

        if( !MagickRotateImage(wand, px, image->rotate)) {
            desc = MagickGetException(wand, &severity);
            /* FIXME: popup */
            g_warning("_apply_modifications: "
                      "error rotating image: %s\n", desc);
            ClearPixelWand(px);
            desc = (char *) MagickRelinquishMemory(desc);
            return FALSE;
        }
        DestroyPixelWand( px );
    }

    /* Apply gamma, if over changed over 0.01 */
    if (image->gamma <= 0.99 || image->gamma >= 1.01) {
        if (!MagickGammaImage(wand, image->gamma)) {
            desc = MagickGetException(wand, &severity);
            
            /* FIXME: popup */
            g_warning("_apply_modifications: "
                      "error setting gamma (%.2f) of image: %s\n",
                      image->gamma, desc);
            desc = (char *) MagickRelinquishMemory(desc);
            return FALSE;
        }
    }

    return TRUE;
}
コード例 #3
0
ファイル: pict2png.c プロジェクト: brianwells/pict2png
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);
	}
}
コード例 #4
0
ファイル: magick.c プロジェクト: kulve/pwgallery
/*
 * Load image to image magic
 */
static gboolean _load_image(struct data *data, 
                            MagickWand *wand, 
                            struct image *image)
{
    gchar *desc;
    ExceptionType severity;
    guchar *img_data;
    gsize img_len;

    g_debug("in _load_image");

    g_assert(data != NULL);
    g_assert(wand != NULL);
    g_assert(image != NULL);

    /* Read image from file to memory */
    vfs_read_file(data, image->uri, &img_data, &img_len);
    
    /* Read image to image magick */
    if (!MagickReadImageBlob(wand, img_data, img_len)) {
        desc = MagickGetException(wand, &severity) ;
        /* FIXME: popup */
        g_warning("_load_image: error reading image: %s\n", desc);
        g_free(img_data);
        desc = (char *) MagickRelinquishMemory(desc);
        return FALSE;
    }

    g_free(img_data);

    return TRUE;
}    
コード例 #5
0
ファイル: magick.c プロジェクト: kulve/pwgallery
/*
 * Resize image. 
 */
static gboolean _resize(struct data *data,
                        MagickWand *wand, 
                        struct image *image,
                        gint width,
                        gint height)
{
    gchar *desc;
    ExceptionType severity;

    g_debug("in _resize");

    g_assert(data != NULL);
    g_assert(wand != NULL);
    g_assert(image != NULL);
    
    /* CHECKME: 1.0 ok? LanczosFilter ok? */
    if (!MagickResizeImage(wand, width, height, LanczosFilter, 1.0 ) )
    {
        desc = MagickGetException(wand, &severity);

        g_warning("_resize: error resizing image: %s\n", desc);
        desc = (char *) MagickRelinquishMemory(desc);
        return FALSE;
    }

    /* FIXME: MagickUnsharpMaskImage */

    return TRUE;
}
コード例 #6
0
ファイル: magick.c プロジェクト: kulve/pwgallery
/* save image to file */
static gboolean _save(struct data *data, 
                      MagickWand *wand, 
                      const gchar *uri)
{
    gchar *desc;
    guchar *img_data;
    gsize img_len;
    ExceptionType severity;

    g_debug("in _save");

    g_assert(data != NULL);
    g_assert(wand != NULL);
    g_assert(uri != NULL);
 
    if (data->gal->remove_exif && !MagickStripImage(wand))
    {
        desc = MagickGetException(wand, &severity);
        g_warning("_save: error stripping image: %s\n", desc);
        desc = (char *) MagickRelinquishMemory(desc);
    }

    img_data = MagickGetImagesBlob(wand, &img_len);

    vfs_write_file(data, uri, img_data, img_len);

    MagickRelinquishMemory(img_data);

    return TRUE;
}
コード例 #7
0
ファイル: ext_gmagick.cpp プロジェクト: cdnewbee/hiphop-php
/* Check the integer and raise an exception if the result is not MagickTrue */
void c_Gmagick::checkResult(int result) {
  if (MagickTrue == result) 
    return;
  ExceptionType magick_severity;
  char * magick_message = MagickGetException(magick_wand, &magick_severity);
  throwException(magick_message, magick_severity);
}
コード例 #8
0
ファイル: magick.c プロジェクト: BobPyron/calibre
// magick_set_exception {{{
PyObject* magick_set_exception(MagickWand *wand) {
    ExceptionType ext;
    char *desc = MagickGetException(wand, &ext);
    PyErr_SetString(PyExc_Exception, desc);
    MagickClearException(wand);
    desc = MagickRelinquishMemory(desc);
    return NULL;
}
コード例 #9
0
void ThrowWandException(MagickWand *wand) {
    char *description; 
    ExceptionType severity; 
    description = MagickGetException(wand, &severity); 
    (void) fprintf(stderr,"%s %s %lu %s\n",GetMagickModule(),description); 
    description=(char *) MagickRelinquishMemory(description); 
    exit(-1); 
}
コード例 #10
0
ファイル: sample.c プロジェクト: 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;
}
コード例 #11
0
ファイル: gmimp.cpp プロジェクト: kitech/nullimp
int GmImp::get_errno()
{
    char *err;
    ExceptionType et;

    err = MagickGetException(this->wand, &et);
    std::cout<<"err:"<<err<<",eno:"<<et<<std::endl;
    return et;    
}
コード例 #12
0
ファイル: image.c プロジェクト: acinonyx/upsat-iac-software
void iac_image_exception(const MagickWand *wand)
{
    char *desc;
    ExceptionType severity;

    desc = MagickGetException(wand, &severity);
    fprintf(stderr, "%s %s %ld %s\n", GetMagickModule(), desc);
    desc = (char *) MagickRelinquishMemory(desc);

}
コード例 #13
0
ファイル: magick.c プロジェクト: waebbl/ledcat
/**
 * ImageMagick error-handler
 */
void im_error(MagickWand *wand)
{
#if HAVE_IMAGEMAGICK == 1
    char *description;
    ExceptionType  severity;

    description = MagickGetException(wand, &severity);
    NFT_LOG(L_ERROR, "%s %s %lu %s\n", GetMagickModule(), description);
    description = (char *) MagickRelinquishMemory(description);
#endif
}
コード例 #14
0
ファイル: dhash.c プロジェクト: moravianlibrary/dhash
void set_mw_err(MagickWand* magick_wand, dhash_err* error) {
    fprintf(stderr, "called set_mw_err");
    ExceptionType severity;
    char* description;
    char* err_description;
    description = MagickGetException(magick_wand, &severity);
    err_description = (char*) malloc(sizeof(char) * strlen(description) + 1);
    strcpy(err_description, description);
    description = (char *) MagickRelinquishMemory(description);
    error->err_type = IMAGE_MAGICK_ERROR;
    if (error->description) {
        free(error->description);
    }
    error->description = err_description;
}
コード例 #15
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;
}
コード例 #16
0
ファイル: zbarimg.c プロジェクト: jebai0521/zbar-code-1.2
static inline int dump_error(MagickWand *wand)
{
    char *desc;
    ExceptionType severity;
    desc = MagickGetException(wand, &severity);

    if(severity >= FatalErrorException)
        exit_code = 2;
    else if(severity >= ErrorException)
        exit_code = 1;
    else
        exit_code = 0;

    static const char *sevdesc[] = { "WARNING", "ERROR", "FATAL" };
    fprintf(stderr, "%s: %s\n", sevdesc[exit_code], desc);

    MagickRelinquishMemory(desc);
    return(exit_code);
}
コード例 #17
0
ファイル: zbarcode.c プロジェクト: ARoddis/php-zbarcode
# else 
#  include "gd.h"
# endif
#endif

#define PHP_ZBARCODE_RESOLUTION	1
#define PHP_ZBARCODE_ENHANCE	2
#define PHP_ZBARCODE_SHARPEN	4

static
void s_throw_image_exception (MagickWand *magick_wand, const char *message TSRMLS_DC)
{
	ExceptionType severity;
	char *magick_msg;

	magick_msg = MagickGetException (magick_wand, &severity);
	MagickClearException (magick_wand);

	if (magick_msg && strlen (magick_msg) > 0) {
		zend_throw_exception(php_zbarcode_exception_class_entry, magick_msg, 1 TSRMLS_CC);
		magick_msg = MagickRelinquishMemory(magick_msg);
		return;
	}

	if (magick_msg) {
		MagickRelinquishMemory(magick_msg);
	}

	zend_throw_exception(php_zbarcode_exception_class_entry, message, 1 TSRMLS_CC);
}
コード例 #18
0
ファイル: ptImage_GMC.cpp プロジェクト: divyang4481/photivo
// Open Image
ptImage* ptImage::ptGMCOpenImage(const char*        FileName,
                                 short              ColorSpace,
                                 short              Intent,
                                 short              ScaleFactor,
                                 bool               IsRAW,
                                 TImage8RawData*    ImgData,
                                 bool&              Success)
{
  Success = 0;

  MagickWand* image = NewMagickWand();
  ExceptionType MagickExcept;

  if (IsRAW) {
    MagickReadImageBlob(image, (const uchar*)ImgData->data(), (const size_t)ImgData->size());
  } else {
    if (!QFile::exists(QString::fromLocal8Bit(FileName))) return this;
    MagickReadImage(image, FileName);
  }

  MagickGetException(image, &MagickExcept);
  if (MagickExcept != UndefinedException) {
    return this;
  }

  Success = 1;

  // Get the embedded profile
  cmsHPROFILE InProfile = NULL;

  if (MagickGetImageType(image) != GrayscaleType) {
    ulong ProfileLength = 0;
    uchar* IccProfile = MagickGetImageProfile(image, "ICC", &ProfileLength);

    if (ProfileLength > 0) {
      InProfile = cmsOpenProfileFromMem(IccProfile, ProfileLength);
    }
  }
  if (!InProfile) {
    InProfile = cmsCreate_sRGBProfile();
  }

  MagickSetImageType(image, TrueColorType);
  MagickSetImageFormat(image, "RGB");
  MagickSetImageDepth(image, 16);

  uint16_t NewWidth = MagickGetImageWidth(image);
  uint16_t NewHeight = MagickGetImageHeight(image);

  // Buffer for the data from Magick
  std::vector<std::array<float, 3> > ImageBuffer;
  ImageBuffer.resize((size_t) NewWidth*NewHeight);

  MagickGetImagePixels(image, 0, 0, NewWidth, NewHeight, "RGB", FloatPixel, (uchar*)ImageBuffer.data());

  m_Width = NewWidth;
  DestroyMagickWand(image);

  // Image before color transform, may be binned
  std::vector<std::array<float, 3> > NewImage;

  if (ScaleFactor != 0) {
    // Binning
    NewHeight >>= ScaleFactor;
    NewWidth >>= ScaleFactor;

    short Step = 1 << ScaleFactor;
    int Average = pow(2,2 * ScaleFactor);

    NewImage.resize((size_t)NewWidth*NewHeight);

#pragma omp parallel for schedule(static)
    for (uint16_t Row=0; Row < NewHeight*Step; Row+=Step) {
      for (uint16_t Col=0; Col < NewWidth*Step; Col+=Step) {
        float  PixelValue[3] = {0.0f,0.0f,0.0f};
        for (uint8_t sRow=0; sRow < Step; sRow++) {
          for (uint8_t sCol=0; sCol < Step; sCol++) {
            int32_t index = (Row+sRow)*m_Width+Col+sCol;
            for (short c=0; c < 3; c++) {
              PixelValue[c] += ImageBuffer[index][c];
            }
          }
        }
        for (short c=0; c < 3; c++) {
          NewImage[Row/Step*NewWidth+Col/Step][c]
            = PixelValue[c] / Average;
        }
      }
    }
  } else {
コード例 #19
0
ファイル: main.c プロジェクト: MaxLeiter/kimg
int main(int argc, char **argv) {
    init_context();

    int _;
    if ((_ = parse_arguments(argc, argv))) {
        return _;
    }

    FILE *infile;
    FILE *outfile;

    infile = fopen(context.infile, "r");
    if (infile == NULL) {
        fprintf(stderr, "No such file or directory: %s\n", context.infile);
        return 1;
    }

    outfile = fopen(context.outfile, "wb");
    if (outfile == NULL) {
        fprintf(stderr, "Please provide an outfile. See `man kimg` for more details.");
        return 1;
    }

    MagickWand *input;

    MagickWandGenesis();
    input = NewMagickWand();

    MagickBooleanType mwstatus = MagickReadImageFile(input, infile);
    if (mwstatus == MagickFalse) {
        ExceptionType type;
        fprintf(stderr, "Error reading image file: %s\n", context.infile);
        fprintf(stderr, "ImageMagick says: %s\n", MagickGetException(input, &type));
        return 1;
    }

    unsigned long height, width;
    unsigned short bytewidth;

    height = MagickGetImageHeight(input);
    width = MagickGetImageWidth(input);
    bytewidth = width / 8;
    if (width % 8 != 0) {
        bytewidth++;
    }

    if (!context.color) {
        MagickSetImageType(input, GrayscaleType);
        MagickSetImageColorspace(input, GRAYColorspace);
    }

    uint8_t _version = KIMG_VERSION;
    uint8_t _format = 0; // TODO: format flags
    uint16_t _height = (uint16_t)height;
    uint16_t _width = (uint16_t)width;

    if (!context.bare) {
        fwrite("KIMG",    4, 1, outfile); // 0x00: Magic Header
        fwrite(&_version, 1, 1, outfile); // 0x04: Format Version
        fwrite(&_format,  1, 1, outfile); // 0x05: Format Flags
        fwrite(&_height,  2, 1, outfile); // 0x06: Image Height
        fwrite(&_width,   2, 1, outfile); // 0x08: Image Width
        // 0x09: Palette/Image Data...
    }

    PixelIterator *iter;
    PixelWand **row;
    MagickPixelPacket pixel;
    uint8_t mask, byte;

    iter = NewPixelIterator(input);

    unsigned long x, y;
    for (y = 0; y < height; y++) {
        mask = 0x80;
        byte = 0;

        row = PixelGetNextIteratorRow(iter, &width);
        for (x = 0; x < width; x++) {
            PixelGetMagickColor(row[x], &pixel);

            if (context.color) {
                // TODO: color support
            } else {
                if (pixel.red < 32767) {
                    byte |= mask;
                }
            }

            mask >>= 1;

            if (mask == 0) { // TODO: build in memory
                fwrite(&byte, sizeof(uint8_t), 1, outfile);
                mask = 0x80;
                byte = 0;
            }
        }
        if (mask != 0x80) {
            fwrite(&byte, sizeof(uint8_t), 1, outfile);
        }
    }

    iter = DestroyPixelIterator(iter);
    input = DestroyMagickWand(input);
    MagickWandTerminus();
    fclose(outfile);

    return 0;
}