Example #1
0
/**
 * read frame using ImageMagick
 */
NftResult im_read_frame(struct Ledcat *c, size_t width, size_t height, char *buf)
{
#if HAVE_IMAGEMAGICK == 1

    /* is there an image from a previous read? */
    if(MagickHasNextImage(c->mw))
    {
        MagickNextImage(c->mw);
    }
    else
    {
        /* end-of-stream? */
        if(feof(c->file))
            return FALSE;

        /* read file */
        if(!MagickReadImageFile(c->mw, c->file))
        {
            im_error(c->mw);
            return FALSE;
        }

        /* reset iterator in case we read more than one file */
        //MagickResetIterator(c->mw);
    }


    /* turn possible alpha-channel black */
    /*PixelWand *pw;
    if(!(pw = NewPixelWand()))
            return FALSE;

    PixelSetColor(pw, "black");
    MagickSetImageBackgroundColor(c->mw, pw);
    DestroyPixelWand(pw);*/

    /* get raw-buffer from imagemagick */
    if(!(MagickExportImagePixels(c->mw, 0, 0, width, height, c->map, c->storage, buf)))
    {
        im_error(c->mw);
        return FALSE;
    }

    /* free resources */
    if(!MagickHasNextImage(c->mw))
        ClearMagickWand(c->mw);

#endif

    return TRUE;
}
Example #2
0
uint64_t dhash_compute_filedesc(FILE* file, dhash_err* error) {
    MagickBooleanType status;
    MagickWand *magick_wand;

    magick_wand = NewMagickWand();
    status = MagickReadImageFile(magick_wand, file);
    if (status == MagickFalse) {
        set_mw_err(magick_wand, error);
        magick_wand = DestroyMagickWand(magick_wand);
        return 0;
    }
    uint64_t hash = dhash_compute_internal(magick_wand, error);
    magick_wand = DestroyMagickWand(magick_wand);

    return hash;
}
Example #3
0
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;
}
Example #4
0
int php_imagick_read_image_using_php_streams(php_imagick_object *intern, int type, char *filename, int filename_len TSRMLS_DC)
{
	php_stream *stream;
	MagickBooleanType status;
	FILE *fp;
#if ZEND_MODULE_API_NO > 20060613 
	zend_error_handling error_handling;
#endif
	
#if ZEND_MODULE_API_NO > 20060613 
	zend_replace_error_handling(EH_THROW, php_imagick_exception_class_entry, &error_handling TSRMLS_CC);
#else
	php_set_error_handling(EH_THROW, php_imagick_exception_class_entry TSRMLS_CC);
#endif
	
	stream = php_stream_open_wrapper(filename, "rb", (ENFORCE_SAFE_MODE|IGNORE_PATH) & ~REPORT_ERRORS, NULL);

	if (!stream) {
		goto return_error;
	}
	
	if (php_stream_can_cast(stream, PHP_STREAM_AS_STDIO|PHP_STREAM_CAST_INTERNAL) == FAILURE) {
		goto return_error;
	}

	if (php_stream_cast(stream, PHP_STREAM_AS_STDIO|PHP_STREAM_CAST_INTERNAL, (void*)&fp, 0) == FAILURE) {
		goto return_error;
	}
	
#if ZEND_MODULE_API_NO > 20060613 
	zend_restore_error_handling(&error_handling TSRMLS_CC);
#else
	php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC);
#endif

	if (type == 1) {
		status = MagickReadImageFile(intern->magick_wand, fp);
	} else {
		status = MagickPingImageFile(intern->magick_wand, fp);
	}
	
	if (status == MagickFalse) {
		php_stream_close(stream);
		return IMAGICK_READ_WRITE_UNDERLYING_LIBRARY;
	}
	
	if (php_stream_is(stream, PHP_STREAM_IS_STDIO)) {
		char *absolute = expand_filepath(filename, NULL TSRMLS_CC);
		MagickSetImageFilename(intern->magick_wand, absolute);
		efree(absolute);
	} else {
		/* Set to empty filename, otherwise it will point to MAGICK_TEMP/magick-XXXXX */
		MagickSetImageFilename(intern->magick_wand, "");
	}
	php_stream_close(stream);

	if (status == MagickFalse) {
		return IMAGICK_READ_WRITE_UNDERLYING_LIBRARY;
	}
	
	IMAGICK_CORRECT_ITERATOR_POSITION(intern);
	return IMAGICK_READ_WRITE_NO_ERROR;
	
return_error:
#if ZEND_MODULE_API_NO > 20060613 
	zend_restore_error_handling(&error_handling TSRMLS_CC);
#else
	php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC);
#endif
	if (stream) php_stream_close(stream);
	return IMAGICK_READ_WRITE_UNDERLYING_LIBRARY;
}