Get dimensions of JPEG in C++
Asked Answered
A

6

7

I need to get the image dimensions of a JPEG in C++. I'm looking for either a fairly simple way to do it or a smallish library that provides that functionality. I'm working in C++ on OpenVMS, so any external libraries may have to be adapted to compile on our systems - so please don't post me links to big, closed source libraries!

Has anyone come across anything that might do the trick, or understand the JPEG file format (I think I probably mean the JFIF file format here) to tell me how I might go about rolling my own solution?

Abnormality answered 25/11, 2008 at 12:12 Comment(0)
S
15

You have this C function which may extract the relevant data for you.

This is a C routine but should compile fine with C++.
Pass it a normal FILE pointer (from fopen) to the beginning of a jpeg file and two int pointers to be set with the image height and width.

Or you may find in the Boost library a jpeg class which has the right function (From Adobe Generic Image Library).

jpeg_read_dimensions

boost::gil::jpeg_read_dimensions (const char *filename)

Returns the width and height of the JPEG file at the specified location. Throws std::ios_base::failure if the location does not correspond to a valid JPEG file.

Splashdown answered 25/11, 2008 at 12:16 Comment(5)
Sorry for the delay - just disappeared off to get married! Back at my desk now :-(. I opted for the C routine (though the #defines at the top are HORRIBLE - rewrote those as functions).Abnormality
Congratulation to both of you!Splashdown
return Many Happy return Many Happy!Douceur
@Splashdown first link is dead, but here's another, looks the same? carnage-melon.tom7.org/stuff/jpegsize.htmlBarry
@Steve Thank you. The first link is fixed.Splashdown
S
3

libjpeg is reasonably small, open source and available on OpenVMS. It's probably quicker to install it than to handle JPEG yourself.

Sobersided answered 25/11, 2008 at 12:20 Comment(0)
M
2

Maybe libjpeg?

Mays answered 25/11, 2008 at 12:15 Comment(0)
C
1

You should be able to use this jpeg lib with this patch for OpenVMS

Confirmand answered 25/11, 2008 at 12:18 Comment(0)
C
1

No need for full libjpeg library just to get this information (unless you need to do something else with the images). ImageInfo might help you. It is a Java class, but there are ports for other languages, including C++.
As pointed out, Exif might change these information (eg. with orientation setting).

Coexecutor answered 25/11, 2008 at 12:27 Comment(0)
S
0

You may want to try GDAL library which serves as an abstraction layer for large number of raster data formats, mostly used in geospatial applications for GIS/RS.

GDAL provides number of APIs, for C, C++ and scripting languages. Of course, it supports JPEG images and its variants like JPEG2000 and more.

Here is a very simple example how to open JPEG image and query its dimensions:

#include <gdal_priv.h>

GDALAllRegister(); // call ones in your application

GDALDataset* ds = (GDALDataset*)GDALOpen("my.jpeg", GA_ReadOnly);
int width  = ds->GetRasterXSize();
int height = ds->GetRasterYSize(),
int nbands = ds->GetRasterCount();

Check GDAL API tutorial for more complete example.

Synecdoche answered 20/1, 2010 at 13:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.