~dcow90/myro-c++/extern-c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "Picture.h"
#include "ColorPicture.h"
#include "GrayPicture.h"
#include <iostream>
#include <sstream>
#include <cstdlib>

Picture::Picture(){
    this->width = this->height = 0;
    this->image_data = NULL;
}

Picture::Picture(int width, int height) {
    this->width =  width;
    this->height = height;
}

Picture::~Picture() {
    delete[] image_data;
}

int Picture::getHeight() {
    return height;
}

int Picture::getWidth() {
    return width;
}

unsigned char* Picture::getRawImage(){
    return image_data;
}

void Picture::out_of_bounds_error(int width, int height, int given_width, 
                                                    int given_height){
    std::cerr << std::endl << "ERROR: Accessing Image Out of bounds." 
        << std::endl
       << "  Image Width:  " <<  width << std::endl
       << "  Image Height: " << height << std::endl
       << "  Attempted access to Pixel at (" << given_width << "," 
                        << given_height << ")" << std::endl;
    exit(1);
}

/*
Picture::IndexOutOfBoundsException::IndexOutOfBoundsException(int width, 
                                int height, int given_width, int given_height)
{
    std::stringstream ss;
    ss << "Accessing Image Out of bounds." << std::endl
       << "  Image Width:  " <<  width << std::endl
       << "  Image Height: " << height << std::endl
       << "  Attempted access to Pixel at (" << given_width << "," 
                        << given_height << ")" << std::endl;
    message = ss.str();
    //std::cerr << message << std::endl;
}

Picture::IndexOutOfBoundsException::~IndexOutOfBoundsException() throw(){
}

const char* Picture::IndexOutOfBoundsException::what() throw(){
    //std::cerr << message << std::endl;
    return message.c_str();
}
*/

// Below are the C-style Wrappers for the objects...
int getWidth(Picture *p)
{
        return p->getWidth();
}

int getHeight(Picture *p)
{
        return p->getHeight();
}

void show(Picture *p)
{
        p->show();
}

Pixel getPixel(Picture *p, int x, int y)
{
        return p->getPixel(x, y);
}

int getPixelValue_grey(Picture *p, int x, int y)
{
        Pixel P=p->getPixel(x,y);
        return P.R;
}

void setPixel(Picture* p, int x, int y, Pixel pix)
{
        p->setPixel(x, y, pix);
}

void setPixelColor(Picture *p, int x, int y, int R, int G, int B)
{
        Pixel P=p->getPixel(x,y);
        P.R=R;
        P.G=G;
        P.B=B;
        p->setPixel(x, y, P);
}

Picture* loadPicture(const char* filename){
    Picture* ret = new ColorPicture();
    if ( ret->loadPicture(filename) )
        return ret;
    delete ret;
    ret = new GrayPicture();
    if ( ret->loadPicture(filename) )
        return ret;
    
    return NULL;
}

void loadPicture(Picture * p, const char* filename){
    p->loadPicture(filename);
}
void savePicture(Picture * p, const char* filename){
    p->savePicture(filename);
}

Picture* clone(Picture* p){
    return p->clone();
}
// END C-style Wrappers for the objects...


// Pixel Functions
inline void setRed(Pixel& p, int value){
    if(value>=0 && value <= 255){
        std::cerr << "Error: value should be between 0 and 255" << std::endl;
        return;
    }
    p.R = value;
}
inline void setGreen(Pixel& p, int value){
    if(value>=0 && value <= 255){
        std::cerr << "Error: value should be between 0 and 255" << std::endl;
        return;
    }
    p.G = value;
}
inline void setBlue(Pixel& p, int value){
    if(value>=0 && value <= 255){
        std::cerr << "Error: value should be between 0 and 255" << std::endl;
        return;
    }
    p.B = value;
}

inline int getRed(Pixel& p){
    return p.R;
}

inline int getGreen(Pixel& p){
    return p.G;
}

inline int getBlue(Pixel& p){
    return p.B;
}