I get a memory access violation when I run the below code. void createNewImage(int width, int height); this function deletes any current image data and creates a new image with the specified width/height and allocates the needed number of pixels dynamically. The new image contains four blocks with different color, red, blue, green, and black. I'm really having a lot of trouble with this one I cant get my head around images with pointers.
There's lots of code there, and without showing declarations of things--the declarations aren't irrelevant details, they're absolutely essential.
Looking from the top I saw this error. You should be using the height version of the loop.
1 2 3 4 5 6 7 8
//provide the code for this function
pixel **p = new pixel*[width];
for (int i = 0; i < width; i++)
p[i] = new pixel[width];
//for (int j = 0; j < height; j++)
//p[j] = new pixel[height];
#ifndef IMAGE
#define IMAGE
#include <atlimage.h>
#include <string>
#include <cstdlib>
#include "pixel.h"
usingnamespace std;
typedefunsignedchar uchar;
class image {
public:
image(); //the image constructor (initializes everything)
image(string filename); //a image constructor that directly loads an image from disk
image(image &other);//copy constructor
~image(); //the image destructor (deletes the dynamically created pixel array)
pixel** getPixels(); //return the 2-dimensional pixels array
int getWidth(); //return the width of the image
int getHeight(); //return the height of the image
void createNewImage(int width, int height); //this function deletes any current image data and creates a new image
//with the specified width/height and allocates the needed number of pixels
//dynamically. The new image contains four blocks with different color, red, blue, green, and black.
//following two functions are provided.
bool loadImage(string filename); //load an image from the specified file path. Return true if it works, false if it is not a valid image.
//Note that we only accept images of the RGB 8bit colorspace!
void saveImage(string filename); //Save an image to the specified path
private:
pixel** pixels; // pixel data array for image
int width, height; // stores the image dimensions
void pixelsToCImage(CImage* myImage); //this function is called internally by the image class.
//it converts our pixel struct array to a standard BGR uchar array with word spacing.
//(Don't worry about what this does)
};
#endif
access violation here imageBuf[j*scanline+i*3] = pixels[j][i].blue;
and i changed it to this
1 2 3 4 5 6 7 8 9
//provide the code for this function
pixel **p = new pixel*[width];
// for (int i = 0; i < width; i++)
// p[i] = new pixel[width];
for (int j = 0; j < height; j++)
p[j] = new pixel[height];
//A POD class (Plain Old Data Class) to hold one pixel's worth of data. (Assuming the pixel is 24bpp and RGB)
//Note: Not having getters and setters for the members of a class is only acceptable if you are using it as a Plain Old Data Class.
//If your class has any constructors, destructors, static members, virtual functions, derivations, or anything that is not a public
//data member...then you should follow the conventions we have practiced in class. We are using a POD class in this case because it
//simplifies the usage of the pixel object as well as keeps it's memory footprint tiny (as we will have MANY of these classes loaded
//into memory at a given time.)
#ifndef PIXEL_H
#define PIXEL_H
class pixel
{
public:
unsignedchar red; //the red component
unsignedchar green; //the green component
unsignedchar blue; //the blue component
};
#endif
#include "image.h"
#include "pixel.h"
#include <iostream>
#include <string>
usingnamespace std;
void RotateClockWise(image *imageIn); //this function rotate the imageIn 90 degreee clockwise
void RotateAntiClockWise(image *imageIn); //this function rotate the imageIn 90 degree anticlockwise
int main()
{
//testing image 1
image* currImage=new image("testing1.jpg");
currImage->createNewImage(600,700); // create a new (4 blocks with different color) image with input size
currImage->saveImage("fourColor.jpg");
//testing image 2
currImage=new image("testing2.jpg");
RotateClockWise(currImage);
currImage->saveImage("test2RotateClock.jpg");
//testing image 3
currImage=new image("testing3.jpg");
RotateAntiClockWise(currImage);
currImage->saveImage("test3RotateAntiClock.jpg");
//testing copy concstructor using image 4
image a("testing4.jpg");
image b=a;
b.createNewImage(100,100);
b.saveImage("testing4color.jpg");
// a should remain the same, so testing4.jpg will not be changed.
system("pause");
return 0;
}
void RotateClockWise(image *imageIn)
{
//please provide the code for this function
}
void RotateAntiClockWise(image *imageIn)
{
//please provide code for this function
}