int main() { gdImagePtr src, dst; int b; void *p; int size = 0; int status = 0; CuTestImageResult result = {0, 0}; src = gdImageCreate(100, 100); if (src == NULL) { printf("could not create src\n"); return 1; } gdImageColorAllocate(src, 0xFF, 0xFF, 0xFF); /* allocate white for background color */ b = gdImageColorAllocate(src, 0, 0, 0); gdImageRectangle(src, 20, 20, 79, 79, b); gdImageEllipse(src, 70, 25, 30, 20, b); #define OUTPUT_WBMP(name) do { \ FILE *fp = gdTestTempFp(); \ gdImageWBMP(name, 1, fp); \ fclose(fp); \ } while (0) OUTPUT_WBMP(src); p = gdImageWBMPPtr(src, &size, 1); 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 = gdImageCreateFromWBMPPtr(size, p); if (dst == NULL) { status = 1; printf("could not create dst\n"); goto door1; } OUTPUT_WBMP(dst); gdTestImageDiff(src, dst, NULL, &result); if (result.pixels_changed > 0) { status = 1; printf("pixels changed: %d\n", result.pixels_changed); } gdImageDestroy(dst); door1: gdFree(p); door0: gdImageDestroy(src); return status; }
gdImagePtr ImgLoadGd(const char *filename, unsigned int *filesize) { FILE *file; gdImagePtr img; char blah[4], *tmp; uint16_t sig16; uint32_t sig32; long filelen; if (!filename) return NULL; file = NULL; tmp = NULL; img = NULL; file = fopen(filename, "rb"); if (!file) return NULL; if (!fread(blah, 4, 1, file)) goto done; fseek(file, 0, SEEK_END); filelen = ftell(file); rewind(file); if (filesize) *filesize = filelen; tmp = malloc(filelen); if (!fread(tmp, filelen, 1, file)) goto done; sig16 = LE16(*(uint16_t *)blah); sig32 = LE32(*(uint32_t *)blah); if (sig16 == 0xD8FF) { img = gdImageCreateFromJpegPtr(filelen, tmp); } else if (sig32 == 'GNP\x89') { img = gdImageCreateFromPngPtr(filelen, tmp); } else if (sig32 == '8FIG') { img = gdImageCreateFromGifPtr(filelen, tmp); } else if (sig16 == 'MB') { img = gdImageCreateFromWBMPPtr(filelen, tmp); } else { fprintf(stderr, "WARNING: %s is an unsupported image format\n", filename); } done: if (file) fclose(file); if (tmp) free(tmp); return img; }
static int put_file_func (CameraFilesystem *fs, const char *folder, const char *name, CameraFileType type, CameraFile *file, void *data, GPContext *context) { #ifdef HAVE_GD Camera *camera = data; char *c, *in_name, *out_name, *filedata = NULL; int ret, in_width, in_height, in_x, in_y; size_t inc, outc; double aspect_in, aspect_out; #ifdef HAVE_ICONV char *in, *out; #endif unsigned long filesize = 0; gdImagePtr rotated, im_out, im_in = NULL; if (strcmp (folder, "/")) return GP_ERROR_DIRECTORY_NOT_FOUND; inc = strlen (name); in_name = strdup (name); outc = inc; out_name = malloc (outc + 1); if (!in_name || !out_name) { free (in_name); free (out_name); return GP_ERROR_NO_MEMORY; } /* Convert name to ASCII */ #ifdef HAVE_ICONV in = in_name; out = out_name; if (iconv (camera->pl->cd, &in, &inc, &out, &outc) == -1) { free (in_name); free (out_name); gp_log (GP_LOG_ERROR, "iconv", "Failed to convert filename to ASCII"); return GP_ERROR_OS_FAILURE; } outc = out - out_name; out_name[outc] = 0; #else for (i = 0; i < inc; i++) { if ((uint8_t)in_name[i] < 0x20 || (uint8_t)in_name[i] > 0x7E) out_name[i] = '?'; else out_name[i] = in_name[i]; } out_name[i] = 0; #endif free (in_name); /* Remove file extension, and if necessary truncate the name */ c = strrchr (out_name, '.'); if (c) *c = 0; if (outc > ST2205_FILENAME_LENGTH) out_name[ST2205_FILENAME_LENGTH] = 0; ret = gp_file_get_data_and_size (file, (const char **)&filedata, &filesize); if (ret < 0) { free (out_name); return ret; } /* Try loading the file using gd, starting with the most often used types first */ /* gdImageCreateFromJpegPtr is chatty on error, don't call it on non JPEG files */ if (filesize > 2 && (uint8_t)filedata[0] == 0xff && (uint8_t)filedata[1] == 0xd8) im_in = gdImageCreateFromJpegPtr(filesize, filedata); if (im_in == NULL) im_in = gdImageCreateFromPngPtr(filesize, filedata); if (im_in == NULL) im_in = gdImageCreateFromGifPtr(filesize, filedata); if (im_in == NULL) im_in = gdImageCreateFromWBMPPtr(filesize, filedata); if (im_in == NULL) { gp_log (GP_LOG_ERROR, "st2205", "Unrecognized file format for file: %s%s", folder, name); free (out_name); return GP_ERROR_BAD_PARAMETERS; } if (needs_rotation (camera)) { rotated = gdImageCreateTrueColor (im_in->sy, im_in->sx); if (rotated == NULL) { gdImageDestroy (im_in); free (out_name); return GP_ERROR_NO_MEMORY; } rotate90 (im_in, rotated); gdImageDestroy (im_in); im_in = rotated; } im_out = gdImageCreateTrueColor(camera->pl->width, camera->pl->height); if (im_out == NULL) { gdImageDestroy (im_in); free (out_name); return GP_ERROR_NO_MEMORY; } /* Keep aspect */ aspect_in = (double)im_in->sx / im_in->sy; aspect_out = (double)im_out->sx / im_out->sy; if (aspect_in > aspect_out) { /* Reduce in width (crop left and right) */ in_width = (im_in->sx / aspect_in) * aspect_out; in_x = (im_in->sx - in_width) / 2; in_height = im_in->sy; in_y = 0; } else { /* Reduce in height (crop top and bottom) */ in_width = im_in->sx; in_x = 0; in_height = (im_in->sy * aspect_in) / aspect_out; in_y = (im_in->sy - in_height) / 2; } gdImageCopyResampled (im_out, im_in, 0, 0, in_x, in_y, im_out->sx, im_out->sy, in_width, in_height); if (im_in->sx != im_out->sx || im_in->sy != im_out->sy) gdImageSharpen(im_out, 100); ret = st2205_write_file (camera, out_name, im_out->tpixels); if (ret >= 0) { /* Add to our filenames list */ ST2205_SET_FILENAME(camera->pl->filenames[ret], out_name, ret); /* And commit the changes to the device */ ret = st2205_commit(camera); } gdImageDestroy (im_in); gdImageDestroy (im_out); free (out_name); return ret; #else gp_log(GP_LOG_ERROR,"st2205", "GD compression not supported - no libGD present during build"); return GP_ERROR_NOT_SUPPORTED; #endif }
static int put_file_func (CameraFilesystem *fs, const char *folder, CameraFile *file, void *data, GPContext *context) { #ifdef HAVE_GD Camera *camera = data; char *filedata = NULL; const char *name; int ret, in_width, in_height, in_x, in_y; double aspect_in, aspect_out; unsigned long filesize = 0; gdImagePtr im_out, im_in = NULL; if (strcmp (folder, "/")) return GP_ERROR_DIRECTORY_NOT_FOUND; CHECK (gp_file_get_name (file, &name)) CHECK (gp_file_get_data_and_size (file, (const char **)&filedata, &filesize)) /* Try loading the file using gd, starting with the most often used types first */ /* gdImageCreateFromJpegPtr is chatty on error, don't call it on non JPEG files */ if (filesize > 2 && (uint8_t)filedata[0] == 0xff && (uint8_t)filedata[1] == 0xd8) im_in = gdImageCreateFromJpegPtr(filesize, filedata); if (im_in == NULL) im_in = gdImageCreateFromPngPtr(filesize, filedata); if (im_in == NULL) im_in = gdImageCreateFromGifPtr(filesize, filedata); if (im_in == NULL) im_in = gdImageCreateFromWBMPPtr(filesize, filedata); if (im_in == NULL) { gp_log (GP_LOG_ERROR, "tp6801", "Unrecognized file format for file: %s%s", folder, name); return GP_ERROR_BAD_PARAMETERS; } im_out = gdImageCreateTrueColor(camera->pl->width, camera->pl->height); if (im_out == NULL) { gdImageDestroy (im_in); return GP_ERROR_NO_MEMORY; } /* Keep aspect */ aspect_in = (double)im_in->sx / im_in->sy; aspect_out = (double)im_out->sx / im_out->sy; if (aspect_in > aspect_out) { /* Reduce in width (crop left and right) */ in_width = (im_in->sx / aspect_in) * aspect_out; in_x = (im_in->sx - in_width) / 2; in_height = im_in->sy; in_y = 0; } else { /* Reduce in height (crop top and bottom) */ in_width = im_in->sx; in_x = 0; in_height = (im_in->sy * aspect_in) / aspect_out; in_y = (im_in->sy - in_height) / 2; } gdImageCopyResampled (im_out, im_in, 0, 0, in_x, in_y, im_out->sx, im_out->sy, in_width, in_height); if (im_in->sx != im_out->sx || im_in->sy != im_out->sy) gdImageSharpen(im_out, 100); ret = tp6801_write_file (camera, im_out->tpixels); if (ret >= 0) { /* Commit the changes to the device */ ret = tp6801_commit(camera); } gdImageDestroy (im_in); gdImageDestroy (im_out); return ret; #else gp_log(GP_LOG_ERROR,"tp6801", "GD compression not supported - no libGD present during build"); return GP_ERROR_NOT_SUPPORTED; #endif }
gdImagePtr ImageRead( struct ImageLibrary *im, File *rootDev, const char *path ) { gdImagePtr img = NULL; FHandler *fh = rootDev->f_FSys; File *rfp = (File *)fh->FileOpen( rootDev, path, "rb" ); if( rfp != NULL ) { BufString *bs = BufStringNew( ); char buffer[ 20048 ]; int len = 0; while( ( len = fh->FileRead( rfp, buffer, 20048 ) ) > 0 ) { BufStringAddSize( bs, buffer, len ); } img = gdImageCreateFromJpegPtr( bs->bs_Size, (void *)bs->bs_Buffer ) ; if( img == NULL ) { if( img == NULL ) { img = gdImageCreateFromBmpPtr( bs->bs_Size, (void *)bs->bs_Buffer ) ; if( img == NULL ) { img = gdImageCreateFromGifPtr( bs->bs_Size, (void *)bs->bs_Buffer ) ; if( img == NULL ) { img = gdImageCreateFromPngPtr( bs->bs_Size, (void *)bs->bs_Buffer ) ; if( img == NULL ) { img = gdImageCreateFromTgaPtr( bs->bs_Size, (void *)bs->bs_Buffer ) ; if( img == NULL ) { img = gdImageCreateFromTiffPtr( bs->bs_Size, (void *)bs->bs_Buffer ) ; if( img == NULL ) { img = gdImageCreateFromWBMPPtr( bs->bs_Size, (void *)bs->bs_Buffer ) ; if( img == NULL ) { img = gdImageCreateFromWebpPtr( bs->bs_Size, (void *)bs->bs_Buffer ) ; } } } } } } } } if( img == NULL ) { ERROR("Graphics format not recognized\n"); } BufStringDelete( bs ); fh->FileClose( rootDev, rfp ); } else { ERROR("Cannot open file: %s to read\n", path ); } return img; }