forked from elijahparker/node-jpeg-lum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjpeg-lum.cc
228 lines (196 loc) · 6.45 KB
/
jpeg-lum.cc
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
221
222
223
224
225
226
227
228
#include <stdio.h>
#include <jpeglib.h>
#include <stdlib.h>
#include <unistd.h>
#include <cstring>
#include <math.h>
unsigned int size;
unsigned int histogram[3][256];
unsigned int width;
unsigned int height;
unsigned long count;
double luminance;
double pixel;
double clipped;
#define LUT_LENGTH 33
struct lut_t {
double x;
double y;
};
lut_t lut[LUT_LENGTH] =
{
{4.793865/4, -7},
{4.793865/2, -6},
{4.793865, -5},
{6.056875, -4.66666666666667},
{7.697362, -4.33333333333333},
{9.780035, -4},
{12.092661, -3.66666666666667},
{15.089602, -3.33333333333333},
{18.683953, -3},
{23.421734, -2.66666666666667},
{28.643862, -2.33333333333333},
{35.019192, -2},
{42.607219, -1.66666666666667},
{52.126612, -1.33333333333333},
{63.956853, -1},
{76.885797, -0.666666666666667},
{89.6882, -0.333333333333333},
{103.382354, 0},
{118.336503, 0.333333333333333},
{135.244366, 0.666666666666667},
{147.969053, 1},
{162.615242, 1.33333333333333},
{182.654258, 1.66666666666667},
{194.224312, 2},
{206.764022, 2.33333333333333},
{213.891809, 2.66666666666667},
{230.007117, 3},
{238.483315, 3.33333333333333},
{246.258409, 3.66666666666667},
{249.447248, 4},
{249.495413, 4.33333333333333},
{252, 5},
{253, 6}
};
double lum(double x)
{
int i;
if(x < lut[0].x) return lut[0].y - 1;
if(x > lut[LUT_LENGTH-1].x) return lut[LUT_LENGTH-1].y + 1;
for( i = 0; i < LUT_LENGTH-1; i++ )
{
if ( lut[i].x <= x && lut[i+1].x >= x )
{
double diffx = x - lut[i].x;
double diffn = lut[i+1].x - lut[i].x;
return lut[i].y + ( lut[i+1].y - lut[i].y ) * diffx / diffn;
}
}
printf("error - not found\n");
return 0; // Not in Range
}
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 int i = 0;
int component = 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);
jpeg_start_decompress(&cinfo);
width = cinfo.output_width;
height = cinfo.output_height;
luminance = 0.0;
size = cinfo.output_width*cinfo.output_height*cinfo.num_components*sizeof(unsigned int);
memset(histogram, 0, sizeof(int)*256*3);
row_pointer[0] = (unsigned char *)malloc(cinfo.output_width*cinfo.num_components);
count = 0;
while (cinfo.output_scanline < cinfo.image_height) {
jpeg_read_scanlines( &cinfo, row_pointer, 1 );
for (i=0; i<cinfo.image_width;i+=cinfo.num_components) {
pixel = 0.0;
for(component=0;component<cinfo.num_components;component++) {
if(component < 3) {
pixel = (double) row_pointer[0][i + component];
pixel = lum(pixel);
if(pixel > 4) clipped++;
luminance += pixel;
count++;
histogram[component][(int)pixel]++;
}
}
}
}
luminance /= (double)count;
clipped /= (double)count;
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
free(row_pointer[0]);
fclose(infile);
return 1;
}
#ifndef BUILDING_NODE_EXTENSION
#define BUILDING_NODE_EXTENSION
#endif
#include <node.h>
using namespace v8;
Handle<Value> CreateObject(const FunctionCallbackInfo<Value>& info) {
Isolate* isolate;
isolate = info.GetIsolate();
Local<Object> obj = Object::New(isolate);
Local<Array> histArray = Array::New(isolate, 256);
unsigned int maxPeak = 0;
for (unsigned int i = 0; i < 256; i++) {
for(unsigned int j = 0; j < 3; j++) {
if(histogram[j][i] > maxPeak) maxPeak = histogram[j][i];
}
}
for (unsigned int i = 0; i < 256; i++) {
unsigned int max = 0;
for(unsigned int j = 0; j < 3; j++) {
if(histogram[j][i] > max) max = histogram[j][i];
}
max = (max*16)/((maxPeak*16)/256); // compress to 256 max, integer math for speed
histArray->Set(i, Number::New(isolate, max));
}
obj->Set(String::NewFromUtf8(isolate, "histogram", String::kInternalizedString), histArray);
obj->Set(String::NewFromUtf8(isolate, "luminance", String::kInternalizedString), Number::New(isolate, luminance));
obj->Set(String::NewFromUtf8(isolate, "clipped", String::kInternalizedString), Number::New(isolate, clipped));
obj->Set(String::NewFromUtf8(isolate, "width", String::kInternalizedString), Number::New(isolate, width));
obj->Set(String::NewFromUtf8(isolate, "height", String::kInternalizedString), Number::New(isolate, height));
return obj;
}
void Read(const FunctionCallbackInfo<Value>& info) {
Isolate* isolate;
isolate = info.GetIsolate();
//HandleScope scope;
//Persistent<Function> callback = Persistent<Function>::New(info[1]);;
Local<Function> callback = Local<Function>::Cast(info[1]);
if (info.Length() < 2) {
Local<Value> err = Exception::Error(String::NewFromUtf8(isolate, "Specify an image filename to read"));
Local<Value> argv[] = { err };
callback->Call(isolate->GetCurrentContext()->Global(), 1, argv);
info.GetReturnValue().SetUndefined();
return;
}
Local<v8::String> string = Local<String>::Cast(info[0]);
const int length = string->Utf8Length() + 1;
uint8_t *filename = new uint8_t[length];
string->WriteOneByte(filename, /* start */ 0, length);
//char *filename = (char *) malloc(length + 1);
//strcpy(filename, string);
if (read_jpeg_file((char *)(filename))) {
Handle<Value> value = CreateObject(info);
Local<Value> argv[] = {
Local<Value>::New(isolate, Null(isolate)),
Local<Value>::New(isolate, value),
};
callback->Call(isolate->GetCurrentContext()->Global(), 2, argv);
info.GetReturnValue().Set(value);
return;
}
else {
Local<Value> err = Exception::Error(String::NewFromUtf8(isolate, "Error reading image file"));
Local<Value> argv[] = { err };
callback->Call(isolate->GetCurrentContext()->Global(), 1, argv);
info.GetReturnValue().SetUndefined();
return;
}
info.GetReturnValue().Set(Local<Object>::Cast(CreateObject(info)));
return;
}
void init(Handle<Object> exports) {
Isolate* isolate = exports->GetIsolate();
exports->Set(String::NewFromUtf8(isolate, "read", String::kInternalizedString),
FunctionTemplate::New(isolate, Read)->GetFunction());
}
NODE_MODULE(jpeglum, init)