Merge pull request #879 from openmv/jpeg_loading

Support loading JPEG images
This commit is contained in:
Ibrahim Abd Elkader 2020-08-12 22:12:24 +02:00 committed by GitHub
commit 6718121800
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 11 deletions

View File

@ -954,7 +954,9 @@ bool imlib_read_geometry(FIL *fp, image_t *img, const char *path, img_read_setti
file_buffer_on(fp); // REMEMBER TO TURN THIS OFF LATER!
vflipped = bmp_read_geometry(fp, img, path, &rs->bmp_rs);
} else {
ff_unsupported_format(NULL);
rs->format = FORMAT_JPG;
file_read_open(fp, path);
jpeg_read_geometry(fp, img, path, &rs->jpg_rs);
}
imblib_parse_extension(img, path); // Enforce extension!
return vflipped;

View File

@ -972,6 +972,12 @@ typedef struct ppm_read_settings {
uint8_t ppm_fmt;
} ppm_read_settings_t;
typedef struct jpg_read_settings {
int32_t jpg_w;
int32_t jpg_h;
int32_t jpg_size;
} jpg_read_settings_t;
typedef enum save_image_format {
FORMAT_DONT_CARE,
FORMAT_BMP,
@ -981,10 +987,10 @@ typedef enum save_image_format {
} save_image_format_t;
typedef struct img_read_settings {
union
{
union {
bmp_read_settings_t bmp_rs;
ppm_read_settings_t ppm_rs;
jpg_read_settings_t jpg_rs;
};
save_image_format_t format;
} img_read_settings_t;
@ -1171,7 +1177,7 @@ void bmp_read_pixels(FIL *fp, image_t *img, int n_lines, bmp_read_settings_t *rs
void bmp_read(image_t *img, const char *path);
void bmp_write_subimg(image_t *img, const char *path, rectangle_t *r);
bool jpeg_compress(image_t *src, image_t *dst, int quality, bool realloc);
void jpeg_read_geometry(FIL *fp, image_t *img, const char *path);
void jpeg_read_geometry(FIL *fp, image_t *img, const char *path, jpg_read_settings_t *rs);
void jpeg_read_pixels(FIL *fp, image_t *img);
void jpeg_read(image_t *img, const char *path);
void jpeg_write(image_t *img, const char *path, int quality);

View File

@ -1288,7 +1288,7 @@ jpeg_overflow:
#endif //defined OMV_HARDWARE_JPEG
// This function inits the geometry values of an image.
void jpeg_read_geometry(FIL *fp, image_t *img, const char *path)
void jpeg_read_geometry(FIL *fp, image_t *img, const char *path, jpg_read_settings_t *rs)
{
for (;;) {
uint16_t header;
@ -1316,9 +1316,9 @@ void jpeg_read_geometry(FIL *fp, image_t *img, const char *path)
uint16_t height;
read_word(fp, &height);
height = IM_SWAP16(height);
img->w = width;
img->h = height;
img->bpp = f_size(fp);
rs->jpg_w = img->w = width;
rs->jpg_h = img->h = height;
rs->jpg_size = img->bpp = f_size(fp);
return;
} else {
file_seek(fp, f_tell(fp) + size - 2);
@ -1339,12 +1339,18 @@ void jpeg_read_pixels(FIL *fp, image_t *img)
void jpeg_read(image_t *img, const char *path)
{
FIL fp;
jpg_read_settings_t rs;
file_read_open(&fp, path);
// Do not use file_buffer_on() here.
jpeg_read_geometry(&fp, img, path);
if (!img->pixels) img->pixels = xalloc(img->bpp);
jpeg_read_geometry(&fp, img, path, &rs);
if (!img->pixels) {
img->pixels = xalloc(img->bpp);
}
jpeg_read_pixels(&fp, img);
// Do not use file_buffer_off() here.
file_close(&fp);
}