unsigned long long int* compute_integral_image(const Image *in_img, const int squared) { unsigned int num_rows = in_img->rows; unsigned int num_cols = in_img->columns; unsigned long long int *out_data; Image *out_img; unsigned char *in_data; int ir, ic; ExceptionInfo exception; in_data = (unsigned char*)malloc(num_cols * num_rows * sizeof(unsigned char)); GetExceptionInfo(&exception); if (MagickFail == DispatchImage(in_img, 0, 0, num_cols, num_rows, "I", CharPixel, in_data, &exception)) { fprintf(stderr, "There was a problem extracting image pixels. Exception=%s\n", exception.description); exit(EXIT_FAILURE); } /* Allocate space for the output image */ out_data = (unsigned long long int*)malloc(num_cols * num_rows * sizeof(unsigned long long int)); if (!squared) { for (ir = 0; ir < num_rows; ir++) for (ic = 0; ic < num_cols; ic++) { if (ir == 0 && ic == 0) out_data[0] = in_data[0]; else if (ir == 0) out_data[ic] = in_data[ic] + out_data[ic-1]; else if (ic == 0) out_data[ir*num_cols] = in_data[ir*num_cols] + out_data[(ir-1)*num_cols]; else out_data[ir*num_cols+ic] = in_data[ir*num_cols+ic] + out_data[(ir-1)*num_cols+ic] + out_data[ir*num_cols+ic-1] - out_data[(ir-1)*num_cols+ic-1]; } } else { for (ir = 0; ir < num_rows; ir++) for (ic = 0; ic < num_cols; ic++) { if (ir == 0 && ic == 0) out_data[0] = pow(in_data[0], 2); else if (ir == 0) out_data[ic] = pow(in_data[ic], 2) + out_data[ic-1]; else if (ic == 0) out_data[ir*num_cols] = pow(in_data[ir*num_cols], 2) + out_data[(ir-1)*num_cols]; else out_data[ir*num_cols+ic] = pow(in_data[ir*num_cols+ic], 2) + out_data[(ir-1)*num_cols+ic] + out_data[ir*num_cols+ic-1] - out_data[(ir-1)*num_cols+ic-1]; } } DestroyExceptionInfo(&exception); free(in_data); return out_data; }
dt_imageio_retval_t dt_imageio_open_gm( dt_image_t *img, const char *filename, dt_mipmap_cache_allocator_t a) { int err = DT_IMAGEIO_FILE_CORRUPTED; float *buf = NULL; ExceptionInfo exception; Image *image = NULL; ImageInfo *image_info = NULL; uint32_t width, height, orientation; if(!_supported_image(filename)) return DT_IMAGEIO_FILE_CORRUPTED; if(!img->exif_inited) (void) dt_exif_read(img, filename); GetExceptionInfo(&exception); image_info=CloneImageInfo((ImageInfo *) NULL); g_strlcpy(image_info->filename,filename,sizeof(image_info->filename)); image=ReadImage(image_info,&exception); if (exception.severity != UndefinedException) CatchException(&exception); if (!image) { fprintf(stderr, "[GraphicsMagick_open] image `%s' not found\n", img->filename); err = DT_IMAGEIO_FILE_NOT_FOUND; goto error; } width = image->columns; height = image->rows; orientation = image->orientation; if(orientation & 4) { img->width = height; img->height = width; } else { img->width = width; img->height = height; } img->bpp = 4*sizeof(float); float *mipbuf = (float *)dt_mipmap_cache_alloc(img, DT_MIPMAP_FULL, a); if(!mipbuf) { fprintf(stderr, "[GraphicsMagick_open] could not alloc full buffer for image `%s'\n", img->filename); err = DT_IMAGEIO_CACHE_FULL; goto error; } buf = (float *)dt_alloc_align(16, width*img->bpp); if(!buf) goto error; const int ht2 = orientation & 4 ? img->width : img->height; // pretend unrotated, rotate in write_pos const int wd2 = orientation & 4 ? img->height : img->width; for (uint32_t row = 0; row < height; row++) { int ret = DispatchImage(image, 0, row, width, 1, "RGBP", FloatPixel, (void *)buf, &exception); if (exception.severity != UndefinedException) CatchException(&exception); if(ret != MagickPass) { fprintf(stderr, "[GraphicsMagick_open] error reading image `%s'\n", img->filename); err = DT_IMAGEIO_FILE_CORRUPTED; goto error; } for(uint32_t i=0; i<width; i++) for(int k=0; k<4; k++) mipbuf[4*dt_imageio_write_pos(i, row, wd2, ht2, wd2, ht2, orientation) + k] = buf[4*i + k]; } if(buf) dt_free_align(buf); if(image) DestroyImage(image); if(image_info) DestroyImageInfo(image_info); DestroyExceptionInfo(&exception); img->filters = 0; img->flags &= ~DT_IMAGE_RAW; img->flags &= ~DT_IMAGE_HDR; img->flags |= DT_IMAGE_LDR; return DT_IMAGEIO_OK; error: if(buf) dt_free_align(buf); if(image) DestroyImage(image); if(image_info) DestroyImageInfo(image_info); DestroyExceptionInfo(&exception); return err; }
// load a full-res thumbnail: int dt_imageio_large_thumbnail(const char *filename, uint8_t **buffer, int32_t *width, int32_t *height, dt_colorspaces_color_profile_type_t *color_space) { int res = 1; uint8_t *buf = NULL; char *mime_type = NULL; size_t bufsize; // get the biggest thumb from exif if(dt_exif_get_thumbnail(filename, &buf, &bufsize, &mime_type)) goto error; if(strcmp(mime_type, "image/jpeg") == 0) { // Decompress the JPG into our own memory format dt_imageio_jpeg_t jpg; if(dt_imageio_jpeg_decompress_header(buf, bufsize, &jpg)) goto error; *buffer = (uint8_t *)malloc((size_t)sizeof(uint8_t) * jpg.width * jpg.height * 4); if(!*buffer) goto error; *width = jpg.width; *height = jpg.height; // TODO: check if the embedded thumbs have a color space set! currently we assume that it's always sRGB *color_space = DT_COLORSPACE_SRGB; if(dt_imageio_jpeg_decompress(&jpg, *buffer)) { free(*buffer); *buffer = NULL; goto error; } res = 0; } else { #ifdef HAVE_GRAPHICSMAGICK ExceptionInfo exception; Image *image = NULL; ImageInfo *image_info = NULL; GetExceptionInfo(&exception); image_info = CloneImageInfo((ImageInfo *)NULL); image = BlobToImage(image_info, buf, bufsize, &exception); if(exception.severity != UndefinedException) CatchException(&exception); if(!image) { fprintf(stderr, "[dt_imageio_large_thumbnail GM] thumbnail not found?\n"); goto error_gm; } *width = image->columns; *height = image->rows; *color_space = DT_COLORSPACE_SRGB; // FIXME: this assumes that embedded thumbnails are always srgb *buffer = (uint8_t *)malloc((size_t)sizeof(uint8_t) * image->columns * image->rows * 4); if(!*buffer) goto error_gm; for(uint32_t row = 0; row < image->rows; row++) { uint8_t *bufprt = *buffer + (size_t)4 * row * image->columns; int gm_ret = DispatchImage(image, 0, row, image->columns, 1, "RGBP", CharPixel, bufprt, &exception); if(exception.severity != UndefinedException) CatchException(&exception); if(gm_ret != MagickPass) { fprintf(stderr, "[dt_imageio_large_thumbnail GM] error_gm reading thumbnail\n"); free(*buffer); *buffer = NULL; goto error_gm; } } // fprintf(stderr, "[dt_imageio_large_thumbnail GM] successfully decoded thumbnail\n"); res = 0; error_gm: if(image) DestroyImage(image); if(image_info) DestroyImageInfo(image_info); DestroyExceptionInfo(&exception); if(res) goto error; #else fprintf(stderr, "[dt_imageio_large_thumbnail] error: The thumbnail image is not in JPEG format, but DT " "was built without GraphicsMagick. Please rebuild DT with GraphicsMagick support " "enabled.\n"); #endif } if(res) { fprintf( stderr, "[dt_imageio_large_thumbnail] error: Not a supported thumbnail image format or broken thumbnail: %s\n", mime_type); goto error; } error: free(mime_type); free(buf); return res; }