MP 2
Image Manipulation II
Scene Class Reference

Represents an arrangement of many images that is output as a single image when the user calls drawscene(). More...

#include "scene.h"

Public Member Functions

 Scene (int max)
 Initializes this Scene object to be able to hold "max" number of images with indices 0 through max-1. More...
 
 ~Scene ()
 Frees all space that was dynamically allocated by this Scene. More...
 
 Scene (const Scene &source)
 The copy constructor makes this Scene an independent copy of the source. More...
 
const Sceneoperator= (const Scene &source)
 The assignment operator for the Scene class. More...
 
void changemaxlayers (int newmax)
 Modifies the size of the array of Image pointers without changing their indices. More...
 
void addpicture (const char *FileName, int index, int x, int y)
 This function will add a picture to the scene, by placing it in the array cell corresponding to the given index, and storing its x coordinate and y coordinate. More...
 
void changelayer (int index, int newindex)
 Moves an Image from one layer to another. More...
 
void translate (int index, int xcoord, int ycoord)
 Changes the x and y coordinates of the Image in the specified layer. More...
 
void deletepicture (int index)
 Deletes the Image at the given index. More...
 
Imagegetpicture (int index) const
 This function will return a pointer to the Image at the specified index, not a copy of it. More...
 
Image drawscene () const
 Draws the whole scene on one Image and returns that Image by value. More...
 

Detailed Description

Represents an arrangement of many images that is output as a single image when the user calls drawscene().

Constructor & Destructor Documentation

Scene::Scene ( int  max)

Initializes this Scene object to be able to hold "max" number of images with indices 0 through max-1.

This function will set up the Scene to hold "max" Images by (1) dynamically allocating an array of Image pointers of size "max", (2) initializing all of them to NULL, (3) creating storage for the coordinates, and (4) storing "max" as a member variable.

Parameters
maxThe maximum number of layers in this Scene
Note
You may assume that max > 0.
Scene::~Scene ( )

Frees all space that was dynamically allocated by this Scene.

Scene::Scene ( const Scene source)

The copy constructor makes this Scene an independent copy of the source.

Hint: Create a copy() helper function and call it here.

Parameters
sourceThe other Scene object to copy data from

Member Function Documentation

const Scene & Scene::operator= ( const Scene source)

The assignment operator for the Scene class.

It does the following: (1) checks for self assignment, (2) then deletes everything this Scene has allocated, (3) then makes this Scene an independent copy of the source, (4) then returns a reference to the current instance.

Hint: Create copy() and clear() helper functions and call them here.

Parameters
sourceThe other Scene object to copy data from
Returns
a constant Scene reference
void Scene::changemaxlayers ( int  newmax)

Modifies the size of the array of Image pointers without changing their indices.

The new set of valid indices will be from 0 to (newmax - 1).

A new array of Image pointers of size newmax will be allocated, and the non-null pointers in the old array will be copied over before the old array is deleted. If this cannot be done because there are non-null pointers outside the range [0, newmax-1], use the following code to print an error message:

cout << "invalid newmax" << endl;

In the case of an error this function call should not cause any change to the Scene.

Parameters
newmaxThe new value for the maximum number of Images in the Scene.
Note
You may assume newmax is greater than zero.
void Scene::addpicture ( const char *  FileName,
int  index,
int  x,
int  y 
)

This function will add a picture to the scene, by placing it in the array cell corresponding to the given index, and storing its x coordinate and y coordinate.

If there is already an Image in cell location index it should be replaced by the new Image. If the index is not within the range [0, max-1], use the following code to print an error message:

cout << "index out of bounds" << endl;

Hint: Make use of the PNG member function (which was inherited by the Image class) for loading an Image from a file.

Parameters
FileNameThe name of the image file to read in
indexWhich "layer" this png resides in
xThe x location of the new png
yThe y location of the new png
Note
You may assume that x and y are integers in the range [0, MAXINT].
void Scene::changelayer ( int  index,
int  newindex 
)

Moves an Image from one layer to another.

You are not allowed to dynamically allocate anything in this function. Rather, just move the pointer.

If the new index is the same as the old index, do nothing and return. If the destination is already occupied, delete the image there. The source index should be marked vacant by making it value NULL. If either index is invalid, do nothing and use the following code to print an error message:

cout << "invalid index" << endl;

Parameters
indexThe source index location
newindexThe destination index location

Note that we do not prevent the user from requesting that a NULL Image pointer be moved to an occupied spot.

void Scene::translate ( int  index,
int  xcoord,
int  ycoord 
)

Changes the x and y coordinates of the Image in the specified layer.

If the index is invalid or if it contains a NULL pointer, do nothing and use the following code to print an error message:

cout << "invalid index" << endl;

Parameters
indexWhich layer to operate on
xcoordThe x coordinate value to move the png to
ycoordThe y coordinate value to move the png to
Note
You may assume that x and y are integers in the range [0, MAXINT].
void Scene::deletepicture ( int  index)

Deletes the Image at the given index.

If the index is invalid or if it contains a NULL pointer, do nothing and use the following code to print an error message:

cout << "invalid index" << endl;

Parameters
indexThe layer in which to delete the png
Image * Scene::getpicture ( int  index) const

This function will return a pointer to the Image at the specified index, not a copy of it.

That way, the user can modify the Image. If the index is invalid, return NULL and print an error message:

cout << "invalid index" << endl;

Returns
a pointer to a specific Image in the Scene
Image Scene::drawscene ( ) const

Draws the whole scene on one Image and returns that Image by value.

Images in the scene will be drawn in order from index 0 to index (max-1) at the stored coordinates. Do not use the alpha channel or any other transparency.

Hint: The x and y coordinates of an Image are the coordinates of its upper-left corner pixel in the output of this function.

Hint: First determine the minimum width and height necessary to ensure that none of the images go off of the bottom or the right edge of the returned image, and make sure your returned image has that width and height.

Returns
an Image object representing the drawn scene

The documentation for this class was generated from the following files: