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 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include "readers.h"
#include "op.h"
#include "filter.h"
/* ----------------------------------------------------------------------- */
/* functions */
static char op_none_data;
static void
op_flip_vert(struct ida_image *src, struct ida_rect *rect,
unsigned char *dst, int line, void *data)
{
char *scanline;
scanline = src->data + (src->i.height - line - 1) * src->i.width * 3;
memcpy(dst,scanline,src->i.width*3);
}
static void
op_flip_horz(struct ida_image *src, struct ida_rect *rect,
unsigned char *dst, int line, void *data)
{
char *scanline;
unsigned int i;
scanline = src->data + (line+1) * src->i.width * 3;
for (i = 0; i < src->i.width; i++) {
scanline -= 3;
dst[0] = scanline[0];
dst[1] = scanline[1];
dst[2] = scanline[2];
dst += 3;
}
}
static void*
op_rotate_init(struct ida_image *src, struct ida_rect *rect,
struct ida_image_info *i, void *parm)
{
*i = src->i;
i->height = src->i.width;
i->width = src->i.height;
i->dpi = src->i.dpi;
return &op_none_data;
}
static void
op_rotate_cw(struct ida_image *src, struct ida_rect *rect,
unsigned char *dst, int line, void *data)
{
char *pix;
unsigned int i;
pix = src->data + src->i.width * src->i.height * 3 + line * 3;
for (i = 0; i < src->i.height; i++) {
pix -= src->i.width * 3;
dst[0] = pix[0];
dst[1] = pix[1];
dst[2] = pix[2];
dst += 3;
}
}
static void
op_rotate_ccw(struct ida_image *src, struct ida_rect *rect,
unsigned char *dst, int line, void *data)
{
char *pix;
unsigned int i;
pix = src->data + (src->i.width-line-1) * 3;
for (i = 0; i < src->i.height; i++) {
dst[0] = pix[0];
dst[1] = pix[1];
dst[2] = pix[2];
pix += src->i.width * 3;
dst += 3;
}
}
static void
op_invert(struct ida_image *src, struct ida_rect *rect,
unsigned char *dst, int line, void *data)
{
unsigned char *scanline;
int i;
scanline = src->data + line * src->i.width * 3;
memcpy(dst,scanline,src->i.width * 3);
if (line < rect->y1 || line >= rect->y2)
return;
dst += 3*rect->x1;
scanline += 3*rect->x1;
for (i = rect->x1; i < rect->x2; i++) {
dst[0] = 255-scanline[0];
dst[1] = 255-scanline[1];
dst[2] = 255-scanline[2];
scanline += 3;
dst += 3;
}
}
static void*
op_crop_init(struct ida_image *src, struct ida_rect *rect,
struct ida_image_info *i, void *parm)
{
if (rect->x2 - rect->x1 == src->i.width &&
rect->y2 - rect->y1 == src->i.height)
return NULL;
*i = src->i;
i->width = rect->x2 - rect->x1;
i->height = rect->y2 - rect->y1;
return &op_none_data;
}
static void
op_crop_work(struct ida_image *src, struct ida_rect *rect,
unsigned char *dst, int line, void *data)
{
unsigned char *scanline;
int i;
scanline = src->data + (line+rect->y1) * src->i.width * 3 + rect->x1 * 3;
for (i = rect->x1; i < rect->x2; i++) {
dst[0] = scanline[0];
dst[1] = scanline[1];
dst[2] = scanline[2];
scanline += 3;
dst += 3;
}
}
static void*
op_autocrop_init(struct ida_image *src, struct ida_rect *unused,
struct ida_image_info *i, void *parm)
{
static struct op_3x3_parm filter = {
f1: { -1, -1, -1 },
f2: { -1, 8, -1 },
f3: { -1, -1, -1 },
};
struct ida_rect rect;
struct ida_image img;
int x,y,limit;
unsigned char *line;
void *data;
/* detect edges */
rect.x1 = 0;
rect.x2 = src->i.width;
rect.y1 = 0;
rect.y2 = src->i.height;
data = desc_3x3.init(src, &rect, &img.i, &filter);
img.data = malloc(img.i.width * img.i.height * 3);
for (y = 0; y < (int)img.i.height; y++)
desc_3x3.work(src, &rect, img.data+3*img.i.width*y, y, data);
desc_3x3.done(data);
limit = 64;
/* y border */
for (y = 0; y < (int)img.i.height; y++) {
line = img.data + img.i.width*y*3;
for (x = 0; x < (int)img.i.width; x++)
if (line[3*x+0] > limit ||
line[3*x+1] > limit ||
line[3*x+2] > limit)
break;
if (x != (int)img.i.width)
break;
}
rect.y1 = y;
for (y = (int)img.i.height-1; y > rect.y1; y--) {
line = img.data + img.i.width*y*3;
for (x = 0; x < (int)img.i.width; x++)
if (line[3*x+0] > limit ||
line[3*x+1] > limit ||
line[3*x+2] > limit)
break;
if (x != (int)img.i.width)
break;
}
rect.y2 = y+1;
/* x border */
for (x = 0; x < (int)img.i.width; x++) {
for (y = 0; y < (int)img.i.height; y++) {
line = img.data + (img.i.width*y+x) * 3;
if (line[0] > limit ||
line[1] > limit ||
line[2] > limit)
break;
}
if (y != (int)img.i.height)
break;
}
rect.x1 = x;
for (x = (int)img.i.width-1; x > rect.x1; x--) {
for (y = 0; y < (int)img.i.height; y++) {
line = img.data + (img.i.width*y+x) * 3;
if (line[0] > limit ||
line[1] > limit ||
line[2] > limit)
break;
}
if (y != (int)img.i.height)
break;
}
rect.x2 = x+1;
free(img.data);
if (debug)
fprintf(stderr,"y: %d-%d/%u -- x: %d-%d/%u\n",
rect.y1, rect.y2, img.i.height,
rect.x1, rect.x2, img.i.width);
if (0 == rect.x2 - rect.x1 || 0 == rect.y2 - rect.y1)
return NULL;
*unused = rect;
*i = src->i;
i->width = rect.x2 - rect.x1;
i->height = rect.y2 - rect.y1;
return &op_none_data;
}
/* ----------------------------------------------------------------------- */
static char op_none_data;
void* op_none_init(struct ida_image *src, struct ida_rect *sel,
struct ida_image_info *i, void *parm)
{
*i = src->i;
return &op_none_data;
}
void op_none_done(void *data) {}
void op_free_done(void *data) { free(data); }
/* ----------------------------------------------------------------------- */
struct ida_op desc_flip_vert = {
name: "flip-vert",
init: op_none_init,
work: op_flip_vert,
done: op_none_done,
};
struct ida_op desc_flip_horz = {
name: "flip-horz",
init: op_none_init,
work: op_flip_horz,
done: op_none_done,
};
struct ida_op desc_rotate_cw = {
name: "rotate-cw",
init: op_rotate_init,
work: op_rotate_cw,
done: op_none_done,
};
struct ida_op desc_rotate_ccw = {
name: "rotate-ccw",
init: op_rotate_init,
work: op_rotate_ccw,
done: op_none_done,
};
struct ida_op desc_invert = {
name: "invert",
init: op_none_init,
work: op_invert,
done: op_none_done,
};
struct ida_op desc_crop = {
name: "crop",
init: op_crop_init,
work: op_crop_work,
done: op_none_done,
};
struct ida_op desc_autocrop = {
name: "autocrop",
init: op_autocrop_init,
work: op_crop_work,
done: op_none_done,
};
|