-
Notifications
You must be signed in to change notification settings - Fork 1
/
seam_carver_functions.c
188 lines (162 loc) · 6.4 KB
/
seam_carver_functions.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
LICENSE - Public Domain (www.unlicense.org)
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
software, either in source code form or as a compiled binary, for any purpose,
commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of this
software dedicate any and all copyright interest in the software to the public
domain. We make this dedication for the benefit of the public at large and to
the detriment of our heirs and successors. We intend this dedication to be an
overt act of relinquishment in perpetuity of all present and future rights to
this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdlib.h>
#include <stdio.h>
#include "seam_carver.h"
#include "cuda_kernels.h"
uchar4 *build_pixels(const unsigned char *imgv, int w, int h){
uchar4 *pixels = (uchar4*)malloc(w*h*sizeof(uchar4));
int i, j;
uchar4 pix;
for(i = 0; i < h; i++){
for(j = 0; j < w; j++){
pix.x = imgv[i*3*w + 3*j];
pix.y = imgv[i*3*w + 3*j + 1];
pix.z = imgv[i*3*w + 3*j + 2];
pixels[i*w + j] = pix;
}
}
return pixels;
}
unsigned char *flatten_pixels(uchar4 *pixels, int w, int h, int new_w){
unsigned char *flattened = (unsigned char*)malloc(3*new_w*h*sizeof(unsigned char));
int i, j;
uchar4 pix;
for(i = 0; i < h; i++){
for(j = 0; j < new_w; j++){
pix = pixels[i*w + j];
flattened[3*i*new_w + 3*j] = pix.x;
flattened[3*i*new_w + 3*j + 1] = pix.y;
flattened[3*i*new_w + 3*j + 2] = pix.z;
}
}
return flattened;
}
void seam_carver_init(seam_carver *sc, seam_carver_mode mode, unsigned char* imgv, int w, int h){
sc->w = w;
sc->h = h;
sc->current_w = w;
sc->mode = mode;
sc->h_pixels = build_pixels(imgv, w, h);
cudaMalloc((void**)&sc->d_pixels, w*h*sizeof(uchar4));
cudaMalloc((void**)&sc->d_pixels_swap, w*h*sizeof(uchar4));
if(sc->mode != SEAM_CARVER_APPROX_MODE){
cudaMalloc((void**)&(sc->d_costs.left), w*h*sizeof(short));
cudaMalloc((void**)&(sc->d_costs.up), w*h*sizeof(short));
cudaMalloc((void**)&(sc->d_costs.right), w*h*sizeof(short));
}
//printf("%lu \n",sizeof(short));
if(sc->mode == SEAM_CARVER_UPDATE_MODE){
cudaMalloc((void**)&(sc->d_costs_swap.left), w*h*sizeof(short));
cudaMalloc((void**)&(sc->d_costs_swap.up), w*h*sizeof(short));
cudaMalloc((void**)&(sc->d_costs_swap.right), w*h*sizeof(short));
}
cudaMalloc((void**)&sc->d_M, w*h*sizeof(int));
if(sc->mode == SEAM_CARVER_APPROX_MODE)
sc->reduce_row = &(sc->d_M[0]); //first row
else
sc->reduce_row = &(sc->d_M[w*(h-1)]); //last row
if(sc->mode == SEAM_CARVER_APPROX_MODE){
cudaMalloc((void**)&sc->d_index_map, w*h*sizeof(int));
cudaMalloc((void**)&sc->d_offset_map, w*h*sizeof(int));
//cudaMallocHost((void**)&sc->h_index_map, w*h*sizeof(int));
//cudaMallocHost((void**)&sc->h_seam, h*sizeof(int));
//cudaStreamCreate(&sc->kernel_stream);
//cudaStreamCreate(&sc->copy_stream);
}
//alloc on device for indices
cudaMalloc((void**)&sc->d_indices, w*sizeof(int));
cudaMalloc((void**)&sc->d_indices_ref, w*sizeof(int));
cudaMalloc((void**)&sc->d_seam, h*sizeof(int));
}
void seam_carver_resize(seam_carver *sc, int seams_to_remove){
uchar4* pixels_tmp;
cost_data costs_tmp;
int num_iterations;
//copy image pixels from host to device
cudaMemcpy(sc->d_pixels, sc->h_pixels, sc->w*sc->h*sizeof(uchar4), cudaMemcpyHostToDevice);
int* indices = (int*)malloc(sc->w*sizeof(int));
for(int i = 0; i < sc->w; i++){
indices[i] = i;
}
//copy indices reference to device
cudaMemcpy(sc->d_indices_ref, indices, sc->w*sizeof(int), cudaMemcpyHostToDevice);
if(sc->mode == SEAM_CARVER_UPDATE_MODE){
compute_costs(*sc);
}
num_iterations = 0;
while(num_iterations < seams_to_remove){
if(sc->mode == SEAM_CARVER_STANDARD_MODE){
compute_costs(*sc);
}
if(sc->mode != SEAM_CARVER_APPROX_MODE){
compute_M(*sc);
find_min_index(*sc);
find_seam(*sc);
}
else{
approx_setup(*sc);
approx_M(*sc);
find_min_index(*sc);
approx_seam(*sc);
}
remove_seam(*sc);
//swap pixels
pixels_tmp = sc->d_pixels;
sc->d_pixels = sc->d_pixels_swap;
sc->d_pixels_swap = pixels_tmp;
if(sc->mode == SEAM_CARVER_UPDATE_MODE){
update_costs(*sc);
//swap costs
costs_tmp = sc->d_costs;
sc->d_costs = sc->d_costs_swap;
sc->d_costs_swap = costs_tmp;
}
sc->current_w = sc->current_w - 1;
num_iterations = num_iterations + 1;
}
cudaMemcpy(sc->h_pixels, sc->d_pixels, sc->w*sc->h*sizeof(uchar4), cudaMemcpyDeviceToHost);
sc->output = flatten_pixels(sc->h_pixels, sc->w, sc->h, sc->current_w);
free(indices);
}
void seam_carver_destroy(seam_carver *sc){
cudaFree(sc->d_pixels);
cudaFree(sc->d_pixels_swap);
if(sc->mode != SEAM_CARVER_APPROX_MODE){
cudaFree(sc->d_costs.left);
cudaFree(sc->d_costs.up);
cudaFree(sc->d_costs.right);
}
if(sc->mode == SEAM_CARVER_UPDATE_MODE){
cudaFree(sc->d_costs_swap.left);
cudaFree(sc->d_costs_swap.up);
cudaFree(sc->d_costs_swap.right);
}
cudaFree(sc->d_M);
cudaFree(sc->d_indices);
cudaFree(sc->d_indices_ref);
cudaFree(sc->d_seam);
if(sc->mode == SEAM_CARVER_APPROX_MODE){
cudaFree(sc->d_index_map);
cudaFree(sc->d_offset_map);
}
free(sc->h_pixels);
free(sc->output);
}