Skip to content

Commit

Permalink
Merge pull request #2 from LebedevRI/exiv2-thubms-upd
Browse files Browse the repository at this point in the history
Cleanup of several broken bits of the exiv2 thumbnail code
  • Loading branch information
pedrocr committed Dec 31, 2014
2 parents 2645b56 + f191597 commit 7915afa
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/common/camera_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -933,8 +933,8 @@ int _camctl_recursive_get_previews(const dt_camctl_t *c, dt_camera_preview_flags
char fullpath[512];
snprintf(fullpath, sizeof(fullpath), "%s/%s/%s", c->active_camera->port + 5, path, filename);
uint8_t *jpg; // gphoto takes care of freeing img eventually
uint32_t size;
if (dt_exif_get_thumbnail(fullpath, &jpg, &size))
size_t size;
if(!dt_exif_get_thumbnail(fullpath, &jpg, &size))
gp_file_set_data_and_size(preview, (char *) jpg, size);
}
}
Expand Down
20 changes: 12 additions & 8 deletions src/common/exif.cc
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,8 @@ int dt_exif_read_from_blob(dt_image_t *img, uint8_t *blob, const int size)
/**
* Get the largest possible thumbnail from the image
*/
int dt_exif_get_thumbnail(const char *path, uint8_t **buffer, uint32_t *size) {
int dt_exif_get_thumbnail(const char *path, uint8_t **buffer, size_t *size)
{
try
{
Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(path);
Expand All @@ -847,25 +848,28 @@ int dt_exif_get_thumbnail(const char *path, uint8_t **buffer, uint32_t *size) {
// Get the selected preview image
Exiv2::PreviewImage preview = loader.getPreviewImage(selected);
const unsigned char *tmp = preview.pData();
uint32_t size = preview.size();
size_t _size = preview.size();

// Workaround the fact that exiv2 sometimes returns prepended garbage
while (size > 4 && (tmp[0] != 0xff || tmp[1] != 0xd8)) {
// Workaround the fact that exiv2 sometimes returns prepended garbage
while(_size > 4 && (tmp[0] != 0xff || tmp[1] != 0xd8))
{
tmp++;
size--;
_size--;
}
if (size == 0) {
if(_size == 0)
{
std::cerr << "[exiv2] thumbnail doesn't seem to be a JPG " << path << std::endl;
return 1;
}

*buffer = (uint8_t *)malloc((size_t) size);
*size = _size;
*buffer = (uint8_t *)malloc(_size);
if(!*buffer) {
std::cerr << "[exiv2] couldn't allocate memory for thumbnail for" << path << std::endl;
return 1;
}
memcpy(*buffer, tmp, size);
//std::cerr << "[exiv2] "<< path << ": found thumbnail "<< preview.width() << "x" << preview.height() << std::endl;
memcpy(*buffer, tmp, _size);


return 0;
Expand Down
2 changes: 1 addition & 1 deletion src/common/exif.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ int dt_exif_xmp_attach(const int imgid, const char *filename);
int dt_exif_xmp_read(dt_image_t *img, const char *filename, const int history_only);

/** fetch largest exif thumbnail jpg bytestream into buffer*/
int dt_exif_get_thumbnail(const char *path, uint8_t **buffer, uint32_t *size);
int dt_exif_get_thumbnail(const char *path, uint8_t **buffer, size_t *size);

/** thread safe init and cleanup. */
void dt_exif_init();
Expand Down
22 changes: 15 additions & 7 deletions src/common/imageio.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,27 +61,35 @@
// load a full-res thumbnail:
int dt_imageio_large_thumbnail(const char *filename, uint8_t **buffer, int32_t *width, int32_t *height)
{
int res = 1;

// Get the JPG embedded in the raw
uint8_t *jpgbuffer;
uint32_t jpgbuffersize;
uint8_t *jpgbuffer = NULL;
size_t jpgbuffersize;

if(dt_exif_get_thumbnail(filename, &jpgbuffer, &jpgbuffersize)) return 1;

// Decompress the JPG into our own memory format
dt_imageio_jpeg_t jpg;
if(dt_imageio_jpeg_decompress_header(jpgbuffer, jpgbuffersize, &jpg))
return 1;
if(dt_imageio_jpeg_decompress_header(jpgbuffer, jpgbuffersize, &jpg)) goto error;

*buffer = (uint8_t *)malloc((size_t)sizeof(uint8_t) * jpg.width * jpg.height * 4);
if(!*buffer) return 1;
if(!*buffer) goto error;

*width = jpg.width;
*height = jpg.height;
if(dt_imageio_jpeg_decompress(&jpg, *buffer))
{
free(*buffer);
*buffer = 0;
return 1;
goto error;
}
return 0;

res = 0;

error:
free(jpgbuffer);
return res;
}

void dt_imageio_flip_buffers(char *out, const char *in, const size_t bpp, const int wd, const int ht,
Expand Down
2 changes: 1 addition & 1 deletion src/libs/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ static void _lib_import_update_preview(GtkFileChooser *file_chooser, gpointer da
if(!have_preview)
{
uint8_t *buffer;
uint32_t size;
size_t size;
if (!dt_exif_get_thumbnail(filename, &buffer, &size)) {
// Scale the image to the correct size
GdkPixbuf *tmp;
Expand Down

0 comments on commit 7915afa

Please sign in to comment.