-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.c
156 lines (140 loc) · 3.72 KB
/
block.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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <omp.h>
#include <time.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image/stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image/stb_image_write.h"
typedef struct PrimalBlock
{
int row;
int col;
}PrimalBlock;
uint8_t nearest_color(float in, uint8_t intervalLen)
{
in = fmin(in, 255);
in = fmax(in,0);
return round(((float)in)/intervalLen)*intervalLen;
}
PrimalBlock pb_finder(int M, int N, int a, int b, int itr)
{
PrimalBlock ans;
if (itr > 0 && itr <= ceil((float)N/b))
{
ans.row = 1;
ans.col = itr;
}
else
{
ans.row = 1 + ceil((itr - ceil((float)N/b))/2);
ans.col = ceil((float)N/b) - (int)(itr - ceil((float)N/b))%2;
}
return ans;
}
void dither(int height, int width, int channels, float** img, unsigned char* d_img, uint8_t intervalLen, int row, int col)
{
int i=row,j=col;
d_img[i*width*channels + j] = nearest_color(img[i][j], intervalLen);
if (channels == 2)
{
d_img[i*width*channels + j + 1] = img[i][j +1];
}
float err = img[i][j] - d_img[i*width*channels + j];
if (j+channels < width*channels)
{
img[i][j+channels] += (err*7)/16;
}
if (i + 1 < height)
{
if (j != 0)
{
img[i+1][j-channels] += (err*3)/16;
}
img[i+1][j] += (err*5)/16;
if (j + channels < width*channels)
{
img[i+1][j+channels] += err/16;
}
}
}
void ditherblock(int height, int width, int channels, float** img, unsigned char* d_img, uint8_t intervalLen, int row, int col, int a, int b)
{
row = a*row;
col = b*col;
for (int i=row;i<row+a && i<height;i++)
{
for (int j=(col==0)?col:(col+a-i+row); j<col + b + a - i + row && j<width; j++)
{
dither(height, width, channels, img, d_img, intervalLen, i, j*channels);
}
}
}
void block(int height, int width, int channels, float** img, unsigned char* d_img, uint8_t intervalLen, int a, int b)
{
int row = 0;
float col = 0;
for (int i=1; i <= 2*ceil((height-a)/(float)a) + ceil(width/(float)b); i++)
{
PrimalBlock pb = pb_finder(height, width, a, b, i);
row = pb.row-1;
col = pb.col-1;
int mini = fmin(height - row -1, col/2);
omp_set_num_threads(8);
#pragma omp parallel for
for(int k=0; k <= mini; k++)
{
ditherblock(height, width, channels, img, d_img, intervalLen, row+k, col-2*k, a, b);
}
}
}
int main(int argc, char* argv[])
{
int width, height, channels;
unsigned char *img = stbi_load(argv[2],&width, &height, &channels, 0);
uint8_t intervalLen = 255/(strtol(argv[1], NULL, 10)-1);
if (img == NULL)
{
printf("Error in loading the image");
exit(1);
}
printf("Loaded image with a width of %dpx, a height of %dpx and %d channels\n", width, height, channels);
if (channels > 2)
{
printf("Not a grayscale image\n");
exit(1);
}
size_t img_size = width*height*channels;
int block_size = (width*height)/16;
int a = sqrt(block_size), b = a;
unsigned char* d_img = malloc(img_size*sizeof(unsigned char));
float *temp = malloc(height*width*channels*sizeof(float));
float **pre;
pre = malloc(height*sizeof(float*));
if (d_img == NULL || temp == NULL || pre == NULL)
{
printf("Unable to allocate memory for image\n");
}
for(int i=0; i < height; i++)
{
pre[i] = temp + i*width*channels;
for (int j=0; j < width*channels; j++)
{
pre[i][j] = img[i*width*channels + j];
}
}
struct timespec start, finish;
double elapsed;
clock_gettime(CLOCK_MONOTONIC, &start);
block(height, width, channels, pre, d_img, intervalLen, a, b);
clock_gettime(CLOCK_MONOTONIC, &finish);
elapsed = (finish.tv_sec - start.tv_sec) * 1000;
elapsed += (finish.tv_nsec - start.tv_nsec) / 1000000.0;
printf("Time taken in milliseconds: %f\n", elapsed);
stbi_write_png(argv[3], width, height, channels, d_img, width*channels);
free(d_img);
free(temp);
free(pre);
return 0;
}