Пример #1
0
int main() {
    int method, i;

    for(method = GD_BELL; method <= GD_TRIANGLE; method++) {   /* GD_WEIGHTED4 is unsupported. */
        gdImagePtr im[2];

        // printf("Method = %d\n", method);
        im[0] = gdImageCreateTrueColor(X, Y);
        im[1] = gdImageCreatePalette(X, Y);

        for (i = 0; i < 2; i++) {
            gdImagePtr result;

            // printf("    %s\n", i == 0 ? "truecolor" : "palette");

            gdImageFilledRectangle(im[i], 0, 0, X-1, Y-1,
                                   gdImageColorExactAlpha(im[i], 255, 255, 255, 0));

            gdImageSetInterpolationMethod(im[i], method);
            gdTestAssert(im[i]->interpolation_id == method); /* No getter yet. */

            result = gdImageScale(im[i], NX, NY);
            gdTestAssert(result != NULL);
            gdTestAssert(result != im[i]);
            gdTestAssert(result->sx == NX && result->sy == NY);

            gdImageDestroy(result);
            gdImageDestroy(im[i]);
        }/* for */
    }/* for*/


    return gdNumFailures();
}/* main*/
Пример #2
0
int main()
{
	gdImagePtr im, im2;
	int error = 0;

	im = gdImageCreateTrueColor(100, 100);

	if (im == NULL) {
		gdTestErrorMsg("gdImageCreateTruecolor failed\n");
		error = 1;
		goto exit;
	}
	gdImageColorTransparent(im, -1);
	gdImageTrueColorToPalette(im, 1, 3);
	gdImageColorTransparent(im, 9);
	im2 = gdImageScale(im, 1, 65535);
	if (im2 == NULL) {
		error = 1;
		goto freeim;
	} else {
		gdImageDestroy(im2);
	}

freeim:
	gdImageDestroy(im);
exit:
	return error;
}
Пример #3
0
void do_test(int x, int y, int nx, int ny)
{
	gdImagePtr im, imref, tmp;
    gdImagePtr same, same2;

	im = mkwhite(x, y);
    imref = mkwhite(x, y);

    same = gdImageScale(im, x, y);

    /* Trivial resize should be a straight copy. */
    gdTestAssert(im != same);
    gdTestAssert(gdMaxPixelDiff(im, same) == 0);
    gdTestAssert(gdMaxPixelDiff(imref, same) == 0);

    /* Ensure that modifying im doesn't modify same (i.e. see if we
     * can catch them accidentally sharing the same pixel buffer.) */
    mkblack(im);
    gdTestAssert(gdMaxPixelDiff(imref, same) == 0);

    gdImageDestroy(same);
    gdImageDestroy(im);

    /* Scale horizontally, vertically and both. */
    scaletest(x, y, nx, y);
    scaletest(x, y, x, ny);
    scaletest(x, y, nx, ny);
}
Пример #4
0
int
main( int argc, char **argv )
{
	FILE *fp;
	gdImagePtr original, cropped,resized;
	if( argc != 3 ) {
		printf( "usage: %s in-jpeg out-jpeg\n", argv[0] );
		exit( 1 );
	}

	if( !(fp = fopen( argv[1], "r" )) ) {
		printf( "unable to open \"%s\"\n", argv[1] );
		exit( 1 );
	}
	if( !(original = gdImageCreateFromJpeg( fp )) ) {
		printf( "unable to load \"%s\"\n", argv[1] );
		exit( 1 );
	}
	fclose( fp );


  gdRect crop;
  crop.x = 100;
  crop.y = 100;
  crop.width = original->sx - 200;
  crop.height = original->sy - 200;
  cropped = gdImageCrop(original, &crop);
  gdImageDestroy( original );
  original = 0;

  if( !(cropped) ) {
    printf( "unable to crop image\n" ); 
    exit( 1 );
  }
  

  resized = gdImageScale(cropped, crop.width * 0.9, crop.height * 0.9);
  gdImageDestroy( cropped );
  cropped = 0;

  if( !(resized) ) {
    printf( "unable to resize image\n" ); 
    exit( 1 );
  }

  //gdImageSharpen is extremely slow
	//gdImageSharpen( resized, 75 );

	if( !(fp = fopen( argv[2], "w" )) ) {
		printf( "unable to open \"%s\"\n", argv[2] );
		exit( 1 );
	}
	gdImageJpeg( resized, fp, -1 );
	fclose( fp );

	gdImageDestroy( resized );

	return( 0 ); 
}
Пример #5
0
int main (int argc, char *argv[]) {
	FILE *fp;
	gdImagePtr in, out;
	int w, h;

	/* Help */
	if (argc<=4) {
		printf("%s  input.jpg  output.jpg  width  height\n", argv[0]);
		return 1;
	}

	/* Size */
	w = atoi(argv[3]);
	h = atoi(argv[4]);
	if (w<=0 || h<=0) {
		fprintf(stderr, "Bad size %dx%d\n", h, w);
		return 2;
	}

	/* Input */
	fp = fopen(argv[1], "rb");
	if (!fp) {
		fprintf(stderr, "Can't read image %s\n", argv[1]);
		return 3;
	}
	in = gdImageCreateFromJpeg(fp);
	fclose(fp);
	if (!in) {
		fprintf(stderr, "Can't create image from %s\n", argv[1]);
		return 4;
	}

	/* Resize */
	gdImageSetInterpolationMethod(in, GD_BILINEAR_FIXED);
	out = gdImageScale(in, w, h);
	if (!out) {
		fprintf(stderr, "gdImageScale fails\n");
		return 5;
	}

	/* Output */
	fp = fopen(argv[2], "wb");
	if (!fp) {
		fprintf(stderr, "Can't save image %s\n", argv[2]);
		return 6;
	}
	gdImageJpeg(out, fp, 90);
	fclose(fp);

	/* Cleanups */
	gdImageDestroy(in);
	gdImageDestroy(out);

	return 0;
}
Пример #6
0
void scaletest(int x, int y, int nx, int ny)
{
    gdImagePtr im, imref, tmp, same;

	imref = mkwhite(x, y);
    im = mkwhite(x, y);
    tmp = gdImageScale(im, nx, ny);
    same = gdImageScale(tmp, x, y);

    /* Test the result to insure that it's close enough to the
     * original. */
    gdTestAssert(gdMaxPixelDiff(im, same) < CLOSE_ENOUGH);

    /* Modify the original and test for a change again.  (I.e. test
     * for accidentally shared memory.) */
    mkblack(tmp);
    gdTestAssert(gdMaxPixelDiff(imref, same) < CLOSE_ENOUGH);

    gdImageDestroy(im);
    gdImageDestroy(tmp);
    gdImageDestroy(same);
}/* scaletest*/
Пример #7
0
void storeImageTarget(gdImagePtr image, const char *fileExtension,
				const char *dir, const char *fileName, const char *groupName)
{
	char directoryFinal[64];
	char fileNameFinal[64];
	char redisIndex[4];
	long long index;
	
	strcpy(directoryFinal, "");
	strcat(directoryFinal, dir);
	strcat(directoryFinal, "/");
	strcat(directoryFinal, dir);
	
	// Get current image count in database
	redisReply *setIncrement = redisCommand(Context, "INCR %s", GRP_TARGET_COUNTER);
	
	index = setIncrement->integer;
	
	snprintf(redisIndex, 4, "%lld", index);
	
	freeReplyObject(setIncrement);
	
	strcpy(fileNameFinal, "");
	strcat(fileNameFinal, dir);
	strcat(fileNameFinal, redisIndex);
	
	// Regardless store the filename
	redisReply *setReply = redisCommand(Context, "SADD %s %s", groupName, fileNameFinal);
	freeReplyObject(setReply);
	
	strcat(directoryFinal, redisIndex);
	strcat(directoryFinal, fileExtension);
	
	// Scale the image
	double imageHeight = TARGET_WIDTH*((double)image->sx/(double)image->sy);
	int imageHeightRounded = (int)imageHeight;
	image = gdImageScale(image, TARGET_WIDTH, imageHeightRounded);
	
	// Add transparency
	image = blendTransparency(image, image->sy);
	
	FILE *output = fopen(directoryFinal, "w");
	gdImagePng(image, output);
	fclose(output);
}
Пример #8
0
void storeImageBackground(gdImagePtr image, const char *fileExtension,
				const char *dir, const char *fileName, const char *groupName)
{
	char directoryFinal[64];
	char redisIndex[4];
	long long index;
	
	strcpy(directoryFinal, "");
	strcat(directoryFinal, dir);
	strcat(directoryFinal, "/");
	strcat(directoryFinal, dir);
	
	// Get current image count in database
	redisReply *getReply = redisCommand(Context, "SCARD %s", groupName);
	
	index = getReply->integer;
	
	// Get the next
	index++;
	
	snprintf(redisIndex, 4, "%lld", index);
	
	freeReplyObject(getReply);
	
	// Regardless store the filename
	redisReply *setReply = redisCommand(Context, "SADD %s %s", groupName, fileName);
	freeReplyObject(setReply);
	
	strcat(directoryFinal, redisIndex);
	strcat(directoryFinal, fileExtension);
	
	// Scale the image
	double imageHeight = BACKGROUND_WIDTH*((double)image->sx/(double)image->sy);
	int imageHeightRounded = (int)imageHeight;
	image = gdImageScale(image, BACKGROUND_WIDTH, imageHeightRounded);
	
	FILE *output = fopen(directoryFinal, "w");
	gdImageJpeg(image, output, 100);
	fclose(output);
}
Пример #9
0
int main()
{
	gdImagePtr im, im2;

	im = gdImageCreate(1,1);
	if (im == NULL) {
		printf("gdImageCreate failed\n");
		return 1;
	}
	gdImageSetInterpolationMethod(im, GD_BELL);
	
	/* here the call may pass if the system has enough memory (physical or swap)
	   or fails (overflow check or alloc fails.
	   in both cases the tests pass */
	im2 = gdImageScale(im,0x15555556, 1);
	if (im2 == NULL) {
		printf("gdImageScale failed, expected (out of memory or overflow validation\n");
		return 0;
	}
	gdImageDestroy(im);
	gdImageDestroy(im2);

	return 0;
}