Updated: October 28, 2024 |
To load an image, follow these steps:
First, you need a list of codecs that are installed, which you can retrieve by calling img_codec_list().
The image data has to come from a source, such as a file, TCP/IP socket, or memory buffer. This step involves establishing the origin of the data. This step may involve no work at all (that is, if it's a file already stored in memory), or it may involve opening a file or performing some other task.
The image library decoders need a conventional way to access the data, which is where the IO streams come in. Use io_open() to associate an io_stream_t with the data source from the previous step.
This step involves allowing the codecs you've enumerated to peek at the data to determine which one is capable of handling the data. You can do this with img_decode_validate(), which runs through the list of codecs and indicates which (if any) approved of the data. You can then use that codec to decode the data.
This step notifies the decoder of an imminent decode operation, and allows it to set up any resources it may require. Use img_decode_begin() to perform this step.
Decode frames using img_decode_frame() until you're finished or there are no more (when img_decode_frame() returns IMG_ERR_NODATA).
Call img_decode_finish() to allow the decoder to clean up after itself.
Although this process may seem complicated, there are two higher-level API calls that simplify the process:
Here's an example of using img_load_file():
int rc; img_t img; ... /* initialize an img_t by setting its flags to 0 */ img.flags = 0; /* if we want, we can preselect a format (ie force the image to be loaded in the format we specify) by enabling the following two lines */ img.format = IMG_FMT_PKLE_ARGB1555; img.flags |= IMG_FORMAT; /* likewise, we can 'clip' the loaded image by enabling the following */ img.w = 100; img.flags |= IMG_W; img.h = 100; img.flags |= IMG_H; if ((rc = img_load_file(ilib, argv[optind], NULL, &img)) != IMG_ERR_OK) { fprintf(stderr, "img_load_file(%s) failed: %d\n", argv[optind], rc); return -1; } fprintf(stdout, "img is %dx%dx%d\n", img.w, img.h, IMG_FMT_BPP(img.format)); /* for our purposes we're done with the img lib */ img_lib_detach(ilib);