-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinvert_RGB_image.c
148 lines (111 loc) · 3.18 KB
/
invert_RGB_image.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
/*
* use libjpeg to read and write jpeg format file.
*
* usage:
* gcc -o jpeg_sample jpeg_sample.c -ljpeg
* ./jpeg_sample input.jpg output.jpg
*
* mm mmmm
* mmmm mmm m"# # #
* #" "# # " #" # "mmmm"
* # # """m #mmm#m # "#
* ##m#" "mmm" # "#mmm"
* #
* "
*
*/
#include<stdio.h>
#include<jpeglib.h>
#include<stdlib.h>
#include<time.h>
unsigned char *raw_image = NULL;
int width;
int height;
int bytes_per_pixel; /* or 1 for GRACYSCALE images */
int color_space = JCS_RGB; /* or JCS_GRAYSCALE for grayscale images */
int read_jpeg_file( char *filename )
{
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
JSAMPROW row_pointer[1];
FILE *infile = fopen( filename, "rb" );
unsigned long location = 0;
int i = 0;
if ( !infile )
{
printf("Error opening jpeg file %s\n!", filename );
return -1;
}
cinfo.err = jpeg_std_error( &jerr );
jpeg_create_decompress( &cinfo );
jpeg_stdio_src( &cinfo, infile );
jpeg_read_header( &cinfo, TRUE );
width=cinfo.image_width;
height=cinfo.image_height;
bytes_per_pixel=cinfo.num_components;
jpeg_start_decompress( &cinfo );
raw_image = (unsigned char*)malloc( cinfo.output_width*cinfo.output_height*cinfo.num_components );
row_pointer[0] = (unsigned char *)malloc( cinfo.output_width*cinfo.num_components );
while( cinfo.output_scanline < cinfo.image_height )
{
jpeg_read_scanlines( &cinfo, row_pointer, 1 );
for( i=0; i<cinfo.image_width*cinfo.num_components;i++)
raw_image[location++] = (char)(255-((int)row_pointer[0][i]));
}
jpeg_finish_decompress( &cinfo );
jpeg_destroy_decompress( &cinfo );
free( row_pointer[0] );
fclose( infile );
return 1;
}
int write_jpeg_file( char *filename )
{
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
JSAMPROW row_pointer[1];
FILE *outfile = fopen( filename, "wb" );
if ( !outfile )
{
printf("Error opening output jpeg file %s\n!", filename );
return -1;
}
cinfo.err = jpeg_std_error( &jerr );
jpeg_create_compress(&cinfo);
jpeg_stdio_dest(&cinfo, outfile);
cinfo.image_width = width;
cinfo.image_height = height;
cinfo.input_components = bytes_per_pixel;
cinfo.in_color_space = color_space;
jpeg_set_defaults( &cinfo );
jpeg_start_compress( &cinfo, TRUE );
while( cinfo.next_scanline < cinfo.image_height )
{
row_pointer[0] = &raw_image[ cinfo.next_scanline * cinfo.image_width * cinfo.input_components];
jpeg_write_scanlines( &cinfo, row_pointer, 1 );
}
jpeg_finish_compress( &cinfo );
jpeg_destroy_compress( &cinfo );
fclose( outfile );
return 1;
}
int main( int argc, char *argv[] )
{
clock_t tStart = clock();
if(argc!=3)
{
printf("\n\nError : Change input in this form: %s file1.jpg file2.jpg \n\n",argv[0]);
return -1;
}
char *infilename = argv[1], *outfilename = argv[2];
int x;
if( read_jpeg_file(infilename) == 1 )
{
printf("The image was read successfully. \n");
}
if(write_jpeg_file(outfilename)==1)
{
printf("The jpeg file was inverted successfully. \n");
}
printf("Time taken: %lf s\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
return 0;
}