Beispiel #1
0
int ImageWrite( struct ImageLibrary *im, File *rootDev, gdImagePtr img, const char *path )
{
	FHandler *fh = rootDev->f_FSys;
	File *rfp = (File *)fh->FileOpen( rootDev, path, "wb" );
	if( rfp != NULL )
	{
		char *buffer = NULL;
		int length = 0;
		int psize = strlen( path );
		
		if( psize > 3 )		//we are checking if file have extension
		{
			char ext[ 4 ];
			strcpy( ext, &(path[ psize-4 ]) );
			ext[ 0 ] = toupper( ext[ 0 ] );
			ext[ 1 ] = toupper( ext[ 1 ] );
			ext[ 2 ] = toupper( ext[ 2 ] );
			
			if( CHECK_EXT( ext,  'P','N','G' ) )
			{
				buffer = gdImagePngPtr( img, &length );
			}else if( CHECK_EXT( ext,  'J','P','G' ) ){
				buffer = gdImageJpegPtr( img, &length, 100 );
			}else if( CHECK_EXT( ext,  'G','I','F' ) ){
				buffer = gdImageGifPtr( img, &length );
			}else if( CHECK_EXT( ext,  'W','E','B' ) ){
				buffer = gdImageWebpPtr( img, &length );
			}else if( CHECK_EXT( ext,  'I','I','F' ) ){
				buffer = gdImageTiffPtr( img, &length );
			}else if( CHECK_EXT( ext,  'B','M','P' ) ){
				buffer = gdImageBmpPtr( img, &length, 100 );
			}
			
			if( buffer == NULL )
			{
				fh->FileClose( rootDev, rfp );
				ERROR("Cannot save picture, GD couldnt create buffer from image\n");
			
				return 2;
			}
			else
			{
				fh->FileWrite( rfp, buffer, length );
			}
		}
		else
		{
			ERROR("Extension name is too short, file format not recognized\n");
		}

		fh->FileClose( rootDev, rfp );
	}
	else
	{
		ERROR("Cannot open file: %s to write\n", path );
		return 1;
	}
	return 0;
}
Beispiel #2
0
result_t Image::getData(int32_t format, int32_t quality,
                        obj_ptr<Buffer_base> &retVal, AsyncEvent *ac)
{
    if (!m_image)
        return CHECK_ERROR(CALL_E_INVALID_CALL);

    if (!ac)
        return CHECK_ERROR(CALL_E_NOSYNC);

    int32_t size = 0;
    void *data = NULL;
    int32_t sx = gdImageSX(m_image);
    int32_t sy = gdImageSY(m_image);
    int32_t i, j;
    int32_t trans = -1;

    if (gdImageTrueColor(m_image))
    {
        for (i = 0; i < sx && trans == -1; i ++)
            for (j = 0; j < sy && trans == -1; j++)
                if ((gdImageGetPixel(m_image, i, j) & 0xff000000) != 0)
                    trans = 0;
    }
    else
        trans = gdImageGetTransparent(m_image);

    gdImagePtr nowImage = m_image;

    if (trans != -1)
    {
        if (format != gd_base::_PNG)
        {
            if (gdImageTrueColor(m_image))
                nowImage = gdImageCreateTrueColor(sx, sy);
            else
            {
                nowImage = gdImageCreate(sx, sy);
                gdImagePaletteCopy(nowImage, m_image);
            }

            gdImageFilledRectangle(nowImage, 0, 0, sx, sy, gdImageColorAllocate(nowImage, 255, 255, 255));
            gdImageCopy(nowImage, m_image, 0, 0, 0, 0, sx, sy);
        }
        else if (gdImageTrueColor(m_image))
            gdImageSaveAlpha(m_image, 1);
    }

    switch (format)
    {
    case gd_base::_GIF:
        data = gdImageGifPtr(nowImage, &size);
        break;
    case gd_base::_PNG:
        data = gdImagePngPtr(nowImage, &size);
        break;
    case gd_base::_JPEG:
    {
        unsigned char *ed_data = NULL;
        uint32_t  ed_size = 0;

        data = gdImageJpegPtr(nowImage, &size, quality, ed_data, ed_size);

        if (ed_data)
            free(ed_data);

        break;
    }
    case gd_base::_TIFF:
        data = gdImageTiffPtr(nowImage, &size);
        break;
    case gd_base::_BMP:
        data = gdImageBmpPtr(nowImage, &size, 1);
        break;
    case gd_base::_WEBP:
        data = gdImageWebpPtrEx(nowImage, &size, quality);
        break;
    }

    if (nowImage != m_image)
        gdImageDestroy(nowImage);

    if (data == NULL)
        return CHECK_ERROR(CALL_E_INVALIDARG);

    retVal = new Buffer(std::string((char *) data, size));
    gdFree(data);

    return 0;
}
Beispiel #3
0
int main()
{
	gdImagePtr src, dst;
	int r, res_x, res_y;
	void *p;
	int size = 0;
	int status = 0;

	src = gdImageCreate(100, 100);
	if (src == NULL) {
		printf("could not create src\n");
		return 1;
	}
	r = gdImageColorAllocate(src, 0xFF, 0, 0);
	gdImageFilledRectangle(src, 0, 0, 99, 99, r);

	// gd default DPI is 96; libtiff default is 72.  
	// Use something else so we know the value has been
	// written / read correctly.
	res_x = 100;
	res_y = 200;
	src->res_x = res_x;
	src->res_y = res_y;

#define OUTPUT_TIFF(name) do {							\
		FILE *fp;										\
														\
		fp = fopen("tiff_dpi_" #name ".tiff", "wb");	\
		if (fp) {										\
			gdImageTiff(name, fp);						\
			fclose(fp);									\
		}												\
	} while (0)

	OUTPUT_TIFF(src);
	p = gdImageTiffPtr(src, &size);
	if (p == NULL) {
		status = 1;
		printf("p is null\n");
		goto door0;
	}
	if (size <= 0) {
		status = 1;
		printf("size is non-positive\n");
		goto door1;
	}

	dst = gdImageCreateFromTiffPtr(size, p);
	if (dst == NULL) {
		status = 1;
		printf("could not create dst\n");
		goto door1;
	}

	if (dst->res_x != res_x) {
		status = 1;
		printf("mismatch in res_x (got %d, expected %d)", dst->res_x, res_x);
	}

	if (dst->res_y != res_y) {
		status = 1;
		printf("mismatch in res_y (got %d, expected %d)", dst->res_y, res_y);
	}

	gdImageDestroy(dst);
door1:
	gdFree(p);
door0:
	gdImageDestroy(src);
	return status;
}