Yeah, I know that clips not resizes. But the original Resize function also clipped, it just didn't let you choose which part of the image to clip from. The syntax of the load function and the Resize function is very similar, yet for some reason, I can't get the code functioning properly in the load function. There's probably some stupid thing I'm missing, maybe you could take a look at it and figure it out. Giving a value of 0 for the Width or the Height gives the image the original width and height.
////////////////////////////////////////////////////////////
/// Load pixels from an image file in memory
////////////////////////////////////////////////////////////
bool ImageLoader::LoadImageFromMemory(const char* Data, std::size_t SizeInBytes, std::vector<Color>& Pixels, unsigned int X, unsigned int Y, unsigned int& Width, unsigned int& Height)
{
// Clear the array (just in case)
Pixels.clear();
// Load the image and get a pointer to the pixels in memory
const unsigned char* Buffer = reinterpret_cast<const unsigned char*>(Data);
int Size = static_cast<int>(SizeInBytes);
int ImgWidth, ImgHeight, ImgChannels;
unsigned int uimgWidth,uimgHeight;
unsigned char* PixelsPtr = SOIL_load_image_from_memory(Buffer, Size, &ImgWidth, &ImgHeight, &ImgChannels, SOIL_LOAD_RGBA);
if (PixelsPtr)
{
// Assign the image properties
uimgWidth=ImgWidth, uimgHeight=ImgHeight;
if(Width==0) Width = ImgWidth-X;
if(Height==0) Height = ImgHeight-Y;
// Copy the loaded pixels to the pixel buffer
Pixels.reserve(Width * Height);
for (unsigned int x = X; x < std::min<unsigned int>(Width+X, uimgWidth); ++x)
for (unsigned int y = Y; y < std::min<unsigned int>(Height+Y, uimgHeight); ++y)
{
Uint8* Pixel = PixelsPtr + (y + x * uimgHeight) * 4;
Pixels.push_back(Color(Pixel[0], Pixel[1], Pixel[2], Pixel[3]));
}
// Free the loaded pixels (they are now in our own pixel buffer)
SOIL_free_image_data(PixelsPtr);
return true;
}
else
{
// Error, failed to load the image
std::cerr << "Failed to load image from memory. Reason : " << SOIL_last_result() << std::endl;
return false;
}
}
In my old engine, which was DirectX based, I made a class that did basically what you described. It was a separate class from the regular Picture class and it would load an image of any size and split it into an Array of pictures, which could then be drawn.