Scaling up an image
This does not segment fault, but it is not reading the pixels of the "original" file.
Image Image::scaleUp(int numTimes) const
{
Image newImage(width*numTimes, height*numTimes);
newImage.createImage(width*numTimes, height*numTimes);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
for(int inner_x = 0; inner_x < numTimes; inner_x++){
for (int inner_y = 0; inner_y < numTimes; inner_y++){
newImage.pixelData[x*numTimes+inner_x][y*numTimes+inner_y] = pixelData[x][y];
}
}
}
}
return newImage;
}
Solved now * (*except my image comes out in b开发者_Python百科/w)
Your code seems to make the colors brighter (multiplying them by numTimes
). Is this what you want to do ?
If you want to return a copy of the image, you should make a copy before applying your transformation, then return the reference to that image. Perhaps your Image class already has a copy constructor you can use to get the copy (if not, you will have to add it or have another way of constructing the object).
I think this is what you want:
Image Image::scaleUp(int numTimes) const
{
Image newImage = new Image(); // You might have a constructor to specify size so data is pre-allocated ?
// Your copy-and-scale code, but set data in newImage ....
// Using copyAll here should be avoided, since it is just copying data
// that you will need to set again when doing the scaling.
return newImage;
}
All methods in C++ have a pointer to the current object named this
. You can return a reference to the object with the following:
// Note that we're now returning a reference with Image&
const Image& Image::scaleUp(int numTimes) const
{
// ...
return *this;
}
Ok, so I think what you are trying to do is apply a contrast filter (multiply each pixel by a value). If you want a brightness algorithm, instead of multiply, just sum. On basic way to do it, as long you read each pixel is:
float contrast = 1.10; //Apply 10% contrast
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++)
{
int temp = pixelData[x][y] * contrast;
temp = temp > 255 ? 255 : temp; //it is always good to check if the result is in the range 0..255
pixelData[x][y] = temp;
}
}
In the code above, I'm doing in in place, in the original image. These steps will be performed for each pixel. I'm also assuming you have 8bits pixel (0..255) for each channel (RGB).
I'm also assuming that x (column) points to one element of RGB space, say R, while x+1 points to G and x+2 points to B. This is a case where all pixels channel are adjacent in the memory
精彩评论