int main() { gdImagePtr im, exp; char path[2048]; const char *file_im = "gdimagerotate/php_bug_64898.png"; const char *file_exp = "gdimagerotate/php_bug_64898_exp.png"; FILE *fp; int color; sprintf(path, "%s/%s", GDTEST_TOP_DIR, file_im); fp = fopen(path, "rb"); if (!fp) { gdTestErrorMsg("opening PNG %s for reading failed.\n", path); return 1; } im = gdImageCreateTrueColor(141, 200); if (!im) { gdTestErrorMsg("loading %s failed.\n", path); return 1; } gdImageFilledRectangle(im, 0, 0, 140, 199, 0x00ffffff); /* Try default interpolation method, but any non-optimized fails */ /* gdImageSetInterpolationMethod(im, GD_BICUBIC_FIXED); */ exp = gdImageRotateInterpolated(im, 45, 0x0); if (!exp) { gdTestErrorMsg("rotating image failed.\n"); gdImageDestroy(im); return 1; } sprintf(path, "%s/%s", GDTEST_TOP_DIR, file_exp); if (!gdAssertImageEqualsToFile(path, exp)) { printf("comparing rotated image to %s failed.\n", path); gdImageDestroy(im); gdImageDestroy(exp); return 1; } gdImageDestroy(exp); gdImageDestroy(im); return 0; }
static VALUE fastimage_native_resize( VALUE self, VALUE rb_in, VALUE rb_out, VALUE rb_w, VALUE rb_h, VALUE rb_image_type, VALUE rb_jpeg_quality, VALUE rb_orientation ) { char *filename_in = StringValuePtr(rb_in); char *filename_out = StringValuePtr(rb_out); int w = NUM2INT(rb_w); int h = NUM2INT(rb_h); int image_type = NUM2INT(rb_image_type); int jpeg_quality = NUM2INT(rb_jpeg_quality); int orientation = NUM2INT(rb_orientation); gdImagePtr im_in, im_out; FILE *in, *out; int trans = 0, x = 0, y = 0, f = 0; in = fopen(filename_in, "rb"); if (!in) return Qnil; switch(image_type) { case 0: im_in = gdImageCreateFromJpeg(in); break; case 1: im_in = gdImageCreateFromPng(in); break; case 2: im_in = gdImageCreateFromGif(in); trans = gdImageGetTransparent(im_in); /* find a transparent pixel, then turn off transparency so that it copies correctly */ if (trans >= 0) { for (x=0; x<gdImageSX(im_in); x++) { for (y=0; y<gdImageSY(im_in); y++) { if (gdImageGetPixel(im_in, x, y) == trans) { f = 1; break; } } if (f) break; } gdImageColorTransparent(im_in, -1); if (!f) trans = -1; /* no transparent pixel found */ } break; } if (!im_in) { fclose(in); return Qnil; } /* Handle orientation */ if (orientation == 5 || orientation == 6) { im_in = gdImageRotateInterpolated(im_in, 270.0, 0); } if (orientation == 7 || orientation == 8) { im_in = gdImageRotateInterpolated(im_in, 90.0, 0); } if (!im_in) { fclose(in); return Qnil; } if (orientation == 2 || orientation == 5 || orientation == 7) { gdImageFlipHorizontal(im_in); } if (orientation == 3) { gdImageFlipBoth(im_in); } if (orientation == 4) { gdImageFlipVertical(im_in); } /* Compute target size */ if (w == 0 || h == 0) { int originalWidth = gdImageSX(im_in); int originalHeight = gdImageSY(im_in); if (h != 0) { w = (int)(h * originalWidth / originalHeight); } else if (w != 0) { h = (int)(w * originalHeight / originalWidth); } else { w = originalWidth; h = originalHeight; } } im_out = gdImageCreateTrueColor(w, h); /* must be truecolor */ if (im_out) { if (image_type == 1) { gdImageAlphaBlending(im_out, 0); /* handle transparency correctly */ gdImageSaveAlpha(im_out, 1); } fclose(in); } else { fclose(in); return Qnil; } /* Now copy the original */ gdImageCopyResampled(im_out, im_in, 0, 0, 0, 0, gdImageSX(im_out), gdImageSY(im_out), gdImageSX(im_in), gdImageSY(im_in)); out = fopen(filename_out, "wb"); if (out) { switch(image_type) { case 0: gdImageJpeg(im_out, out, jpeg_quality); break; case 1: gdImagePng(im_out, out); break; case 2: gdImageTrueColorToPalette(im_out, 0, 256); if (trans >= 0) { trans = gdImageGetPixel(im_out, x, y); /* get the color index of our transparent pixel */ gdImageColorTransparent(im_out, trans); /* may not always work as hoped */ } gdImageGif(im_out, out); break; } fclose(out); } gdImageDestroy(im_in); gdImageDestroy(im_out); return Qnil; }