C/C++ Image Loading

C/C++ Image Loading

I'm always a fan of CImg. It's very easy to use. Another user liked the answer as well. I'll post the same example I posted in the answer so you can see how easy it is to access pixels and dimension info.

CImg<unsigned char> src("image.jpg");
int width = src.width();
int height = src.height();
unsigned char* ptr = src.data(10,10); // get pointer to pixel @ 10,10
unsigned char pixel = *ptr;

Simple image loading libraries

devIL and SDL_Image supports a good deal of formats.
Derelict provides their bindings.

My own code for using these and have a raw buffer:

  • devIL: http://codepad.org/tKonvsJ0

  • SDL_Image: http://codepad.org/jLJDNstw

Loading image from CoreData at cellForRowAtIndexPath slows down scrolling

To keep your scrolling smooth regardless of where your data comes from, you need to fetch your data on a separate thread and only update the UI when you have the data in memory. Grand Central Despatch is the way to go. Here's a skeleton which assume you have a self.photos dictionary with a text reference to an image file. The image thumbnail may or may not be loaded into a live dictionary; may or may not be in a filesystem cache; otherwise is fetched from an online store. It could use Core Data, but the key to smooth scrolling is that you don't wait around for the data wherever it comes from.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Photo Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//identify the data you are after
id photo = [self.photos objectAtIndex:indexPath.row];
// Configure the cell based on photo id

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//move to an asynchronous thread to fetch your image data
UIImage* thumbnail = //get thumbnail from photo id dictionary (fastest)
if (!thumbnail) { //if it's not in the dictionary
thumbnail = //get it from the cache (slower)
// update the dictionary
if (!thumbnail) { //if it's not in the cache
thumbnail = //fetch it from the (online?) database (slowest)
// update cache and dictionary
}
}
}
if (thumbnail) {
dispatch_async(dispatch_get_main_queue(), ^{
//return to the main thread to update the UI
if ([[tableView indexPathsForVisibleRows] containsObject:indexPath]) {
//check that the relevant data is still required
UITableViewCell * correctCell = [self.tableView cellForRowAtIndexPath:indexPath];
//get the correct cell (it might have changed)
[[correctCell imageView] setImage:thumbnail];
[correctCell setNeedsLayout];
}
});
}
});
return cell;
}

If you are using some kind of singleton image store manager, you would expect the manager to deal with the details of cache / database access, which simplifies this example.

This part

            UIImage* thumbnail = //get thumbnail from photo id dictionary (fastest)
if (!thumbnail) { //if it's not in the dictionary
thumbnail = //get it from the cache (slower)
// update the dictionary
if (!thumbnail) { //if it's not in the cache
thumbnail = //fetch it from the (online?) database (slowest)
// update cache and dictionary
}
}

would be replaced with something like

      UIImage* thumbnail = [[ImageManager singleton] getImage];

(you wouldn't use a completion block as you are effectively providing one in GCD when you return to the main queue)

How to open an image using C?

As others have mentioned, since images are binary files you have to know how to "interpret" the data in them after reading them. Luckily, for most formats you can find libraries that do it for you like libpng or libjpeg.

How can I load an image in C++ using SFML library?

Try something like:

sf::Texture texture;
if (!texture.loadFromFile("path/to/myTexture.png")) {
perror("Couldn't load texture \"myTexture\".");
return;
}

You can then put it on spirte:

sf::Sprite sprite;
sprite.setTexture(texture);

and finally display it:

sprite.setPosition(x,y);
window.draw(sprite);

Win32 C/C++ Load Image from memory buffer

Nevermind, I found my solution! Here's the initializing code:

std::ifstream is;
is.open("Image.bmp", std::ios::binary);
is.seekg (0, std::ios::end);
length = is.tellg();
is.seekg (0, std::ios::beg);
pBuffer = new char [length];
is.read (pBuffer,length);
is.close();

tagBITMAPFILEHEADER bfh = *(tagBITMAPFILEHEADER*)pBuffer;
tagBITMAPINFOHEADER bih = *(tagBITMAPINFOHEADER*)(pBuffer+sizeof(tagBITMAPFILEHEADER));
RGBQUAD rgb = *(RGBQUAD*)(pBuffer+sizeof(tagBITMAPFILEHEADER)+sizeof(tagBITMAPINFOHEADER));

BITMAPINFO bi;
bi.bmiColors[0] = rgb;
bi.bmiHeader = bih;

char* pPixels = (pBuffer+bfh.bfOffBits);

char* ppvBits;

hBitmap = CreateDIBSection(NULL, &bi, DIB_RGB_COLORS, (void**) &ppvBits, NULL, 0);
SetDIBits(NULL, hBitmap, 0, bih.biHeight, pPixels, &bi, DIB_RGB_COLORS);

GetObject(hBitmap, sizeof(BITMAP), &cBitmap);

How can I read/load images in C++?

Personally, I prefer the ImageMagick library.

There are many available graphics processing libraries, and there is not a single choice that stands out as clearly superior to the others. My advice is to make a short list of 3 or 4, take a look at the documentation for each, and try to write a simple half-page program with each. Use whichever one you personally find easiest to use.



Related Topics



Leave a reply



Submit