imsz
imsz

Get image width and height reading as few bytes as possible.

This library provides C bindings for the imsz Rust library. (Rust reference)

See imsz.h for all provided functionality.

Example

const char *filename = "image.png";
int error = imsz(filename, &info);
switch (error) {
case IMSZ_OK:
printf("%s: %s, %" PRIu64 " x %" PRIu64 "\n", fname, imsz_format_name(info->format), info->width, info->height);
break;
fprintf(stderr, "%s: IO Error\n", fname);
break;
fprintf(stderr, "%s: Parser Error %s\n", fname, imsz_format_name(info->format));
break;
fprintf(stderr, "%s: Unsupported Format\n", fname);
break;
default:
// NOTE: Under Windows `error` is here a Windows error code and
// FormatMessage() has to be used instead of strerror().
fprintf(stderr, "%s: %s\n", fname, strerror(error));
break;
}

Compile

Build the static and dynamic C libraries by running cargo build in the c folder of the imsz project. (See also cargo build --release, cargo build --target=i686-pc-windows-gnu, cargo build --target=x86_64-pc-windows-gnu, etc.)

Build and link an example dynamically:

gcc -Ltarget/debug -limsz -I. examples/imsz.c -o imsz

Build and link an example pseudo-statically (POSIX GCC):

gcc -DIMSZ_STATIC -lpthread -ldl -I. examples/imsz.c target/debug/libimsz.a -o imsz

-DIMSZ_STATIC is only really needed for MSVC.

Sadly this requires linking libraries that aren't actually used by imsz, but that are used be the Rust standard library.

Then why do this pseudo-statically? Because when creating an actual static library we get warnings about certain functions in glibc (getpwuid_r and getaddrinfo) that require at runtime the shared libraries from the glibc version used for static linking:

gcc -static -DIMSZ_STATIC -I. examples/imsz.c -Ltarget/debug -limsz -lpthread -ldl -o imsz

Build and link an example statically (Windows MinGW):

i686-w64-mingw32-gcc -static -DIMSZ_STATIC -I. examples/imsz.c \
-Ltarget/i686-pc-windows-gnu/debug -limsz \
-lws2_32 -lbcrypt -luserenv -o imsz

Or:

x86_64-w64-mingw32-gcc -static -DIMSZ_STATIC -I. examples/imsz.c \
-Ltarget/x86_64-pc-windows-gnu/debug -limsz \
-lws2_32 -lbcrypt -luserenv -o imsz

Again, there are libraries that aren't actually used by imsz, but only by the Rust standard library.

imsz_format_name
const char * imsz_format_name(unsigned int format)
ImInfo::width
uint64_t width
Width of the image.
Definition: imsz.h:92
IMSZ_OK
@ IMSZ_OK
No error.
Definition: imsz.h:53
ImInfo::height
uint64_t height
Height of the image.
Definition: imsz.h:93
IMSZ_ERR_IO
@ IMSZ_ERR_IO
IO error happened, but no OS error (errno or WIndows error code) was reported. (Classic should never ...
Definition: imsz.h:54
ImInfo
The width, height and format of an image.
Definition: imsz.h:91
ImInfo::format
unsigned int format
Values from ImFormat.
Definition: imsz.h:103
IMSZ_ERR_PARSER
@ IMSZ_ERR_PARSER
File format was detected, but there was an error parsing the file. ImInfo::format will be set to the ...
Definition: imsz.h:55
IMSZ_ERR_UNSUPPORTED
@ IMSZ_ERR_UNSUPPORTED
File format is not supported.
Definition: imsz.h:56
IMSZ_INIT
#define IMSZ_INIT
Definition: imsz.h:88