-
Notifications
You must be signed in to change notification settings - Fork 1
/
image.hpp
220 lines (154 loc) · 5.26 KB
/
image.hpp
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <stdio.h>
#include <stdlib.h>
#include <jpeglib.h>
#include <typeinfo>
#include <string.h>
#include <unistd.h>
#include <bits/stdc++.h>
#define GRAYSCALE JCS_GRAYSCALE
#define RGB JCS_RGB
using namespace std;
/* High level class Image */
enum save_flags {SAVE_FIRST, DO_NOT_SAVE};
class Image
{
public:
int width;
int height;
int components;
unsigned char *image; //contains image pixels in form R,G,B,R,G,B,... in case of RGB image
char *filename;
Image()
{
filename = NULL;
width = 0;
height = 0;
components = 0;
}
Image(const char *name) //constructor takes filepath as parameter
{
int size = strlen(name);
filename = new char[size];
strcpy(filename, name);
read_jpeg();
}
Image(int wid , int hei, int component, unsigned char *img){
width = wid;
height = hei;
components = component;
filename = NULL;
int size = width*height*components;
image = new unsigned char[size];
// copy(img,img+size, image);
int i;
for(i=0; i<size; ++i)
image[i] = img[i];
}
Image(int wid , int hei, int component){
width = wid;
height = hei;
components = component;
filename = NULL;
image = new unsigned char[width*height*components];
}
Image(const Image &src)
{
width = src.width;
height = src.height;
components = src.components;
int len = strlen(src.filename);
if(len>0)
{
filename = new char[len];
strcpy(filename, src.filename);
}
int size = width*height*components;
image = new unsigned char[size];
// copy(src.image,src.image+size, image);
int i;
for(i=0; i<size; ++i)
image[i] = src.image[i];
}
unsigned char **read_jpeg() //puts jpeg filename in image
{
struct jpeg_decompress_struct cinfo;
FILE * infile;
JSAMPARRAY buffer;
infile = fopen(filename, "rb");
struct jpeg_error_mgr jerr;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
jpeg_stdio_src(&cinfo, infile);
jpeg_read_header(&cinfo, TRUE);
jpeg_start_decompress(&cinfo);
width = static_cast<int>(cinfo.output_width);
height = static_cast<int>(cinfo.output_height);
components = cinfo.output_components;
// printf("Height %d\nWidth %d\nComponents %d\n", height, width, components);
// image = new unsigned char*[cinfo.output_components];
image = new unsigned char[height * width * components];
// int i;
// for(i=0; i<cinfo.output_components; ++i)
// image[i] = new unsigned char[height * width];
int row_stride = width * components;
int iter = 0;
while (cinfo.output_scanline < cinfo.output_height) {
(void) jpeg_read_scanlines(&cinfo, &image, 1);
// for(i=0; i<row_stride; ++i)
// image[i%components][width*iter + i/components] = temp[i];
image += row_stride;
iter += 1;
}
image -= iter*row_stride;
// delete[] temp;
}
void write_jpeg(const char *savename, J_COLOR_SPACE colorspace = RGB, int quality=98) //writes image with name savename with passed quality (1-100)
{
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
FILE * outfile; /* target file */
JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */
int row_stride; /* physical row width in image buffer */
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
if ((outfile = fopen(savename, "wb")) == NULL) {
fprintf(stderr, "can't open %s\n", savename);
exit(1);
}
jpeg_stdio_dest(&cinfo, outfile);
cinfo.image_width = width; /* image width and height, in pixels */
cinfo.image_height = height;
cinfo.input_components = components; /* # of color components per pixel */
cinfo.in_color_space = colorspace; /* colorspace of input image */
jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */);
jpeg_start_compress(&cinfo, TRUE);
row_stride = width * components; /* JSAMPLEs per row in image */
while (cinfo.next_scanline < cinfo.image_height) {
row_pointer[0] = &image[cinfo.next_scanline * row_stride];
(void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
}
jpeg_finish_compress(&cinfo);
fclose(outfile);
jpeg_destroy_compress(&cinfo);
}
void display(const char *savename, int save_flag=DO_NOT_SAVE, J_COLOR_SPACE colorspace=RGB) //function to display image after saving at path savename
{
if(save_flag == SAVE_FIRST)
write_jpeg(savename, colorspace); //saves jpeg first
/* eog <savename> & - displays image on linux machines in background
so that execution can continue */
char eog[] = "eog ";
char *command = new char[10+strlen(savename)];
strcpy(command, eog);
strcat(command, savename);
char temp[] = " &";
strcat(command, temp);
system(command);
}
~Image()
{
delete[] image;
delete[] filename;
}
};