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 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
|
#include <stdio.h>
#include <debug.h>
#include <cmdline.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#ifndef max
#define max(a,b) ({typeof(a) _a = (a); typeof(b) _b = (b); _a > _b ? _a : _b; })
#define min(a,b) ({typeof(a) _a = (a); typeof(b) _b = (b); _a < _b ? _a : _b; })
#endif
#define CONVERT_TYPE_PPM 0
#define CONVERT_TYPE_RGB 1
#define CONVERT_TYPE_ARGB 2
/*
YUV 4:2:0 image with a plane of 8 bit Y samples followed by an interleaved
U/V plane containing 8 bit 2x2 subsampled chroma samples.
except the interleave order of U and V is reversed.
H V
Y Sample Period 1 1
U (Cb) Sample Period 2 2
V (Cr) Sample Period 2 2
*/
typedef struct rgb_context {
unsigned char *buffer;
int width;
int height;
int rotate;
int i;
int j;
int size; /* for debugging */
} rgb_context;
typedef void (*rgb_cb)(
unsigned char r,
unsigned char g,
unsigned char b,
rgb_context *ctx);
const int bytes_per_pixel = 2;
static void color_convert_common(
unsigned char *pY, unsigned char *pUV,
int width, int height,
unsigned char *buffer,
int size, /* buffer size in bytes */
int gray,
int rotate,
rgb_cb cb)
{
int i, j;
int nR, nG, nB;
int nY, nU, nV;
rgb_context ctx;
ctx.buffer = buffer;
ctx.size = size; /* debug */
ctx.width = width;
ctx.height = height;
ctx.rotate = rotate;
if (gray) {
for (i = 0; i < height; i++) {
for (j = 0; j < width; j++) {
nB = *(pY + i * width + j);
ctx.i = i;
ctx.j = j;
cb(nB, nB, nB, &ctx);
}
}
} else {
// YUV 4:2:0
for (i = 0; i < height; i++) {
for (j = 0; j < width; j++) {
nY = *(pY + i * width + j);
nV = *(pUV + (i/2) * width + bytes_per_pixel * (j/2));
nU = *(pUV + (i/2) * width + bytes_per_pixel * (j/2) + 1);
// Yuv Convert
nY -= 16;
nU -= 128;
nV -= 128;
if (nY < 0)
nY = 0;
// nR = (int)(1.164 * nY + 2.018 * nU);
// nG = (int)(1.164 * nY - 0.813 * nV - 0.391 * nU);
// nB = (int)(1.164 * nY + 1.596 * nV);
nB = (int)(1192 * nY + 2066 * nU);
nG = (int)(1192 * nY - 833 * nV - 400 * nU);
nR = (int)(1192 * nY + 1634 * nV);
nR = min(262143, max(0, nR));
nG = min(262143, max(0, nG));
nB = min(262143, max(0, nB));
nR >>= 10; nR &= 0xff;
nG >>= 10; nG &= 0xff;
nB >>= 10; nB &= 0xff;
ctx.i = i;
ctx.j = j;
cb(nR, nG, nB, &ctx);
}
}
}
}
static void rgb16_cb(
unsigned char r,
unsigned char g,
unsigned char b,
rgb_context *ctx)
{
unsigned short *rgb16 = (unsigned short *)ctx->buffer;
*(rgb16 + ctx->i * ctx->width + ctx->j) = b | (g << 5) | (r << 11);
}
static void common_rgb_cb(
unsigned char r,
unsigned char g,
unsigned char b,
rgb_context *ctx,
int alpha)
{
unsigned char *out = ctx->buffer;
int offset = 0;
int bpp;
int i = 0;
switch(ctx->rotate) {
case 0: /* no rotation */
offset = ctx->i * ctx->width + ctx->j;
break;
case 1: /* 90 degrees */
offset = ctx->height * (ctx->j + 1) - ctx->i;
break;
case 2: /* 180 degrees */
offset = (ctx->height - 1 - ctx->i) * ctx->width + ctx->j;
break;
case 3: /* 270 degrees */
offset = (ctx->width - 1 - ctx->j) * ctx->height + ctx->i;
break;
default:
FAILIF(1, "Unexpected roation value %d!\n", ctx->rotate);
}
bpp = 3 + !!alpha;
offset *= bpp;
FAILIF(offset < 0, "point (%d, %d) generates a negative offset.\n", ctx->i, ctx->j);
FAILIF(offset + bpp > ctx->size, "point (%d, %d) at offset %d exceeds the size %d of the buffer.\n",
ctx->i, ctx->j,
offset,
ctx->size);
out += offset;
if (alpha) out[i++] = 0xff;
out[i++] = r;
out[i++] = g;
out[i] = b;
}
static void rgb24_cb(
unsigned char r,
unsigned char g,
unsigned char b,
rgb_context *ctx)
{
return common_rgb_cb(r,g,b,ctx,0);
}
static void argb_cb(
unsigned char r,
unsigned char g,
unsigned char b,
rgb_context *ctx)
{
return common_rgb_cb(r,g,b,ctx,1);
}
static void convert(const char *infile,
const char *outfile,
int height,
int width,
int gray,
int type,
int rotate)
{
void *in, *out;
int ifd, ofd, rc;
int psz = getpagesize();
static char header[1024];
int header_size;
size_t outsize;
int bpp = 3;
switch (type) {
case CONVERT_TYPE_PPM:
PRINT("encoding PPM\n");
if (rotate & 1)
header_size = snprintf(header, sizeof(header), "P6\n%d %d\n255\n", height, width);
else
header_size = snprintf(header, sizeof(header), "P6\n%d %d\n255\n", width, height);
break;
case CONVERT_TYPE_RGB:
PRINT("encoding raw RGB24\n");
header_size = 0;
break;
case CONVERT_TYPE_ARGB:
PRINT("encoding raw ARGB\n");
header_size = 0;
bpp = 4;
break;
}
outsize = header_size + width * height * bpp;
outsize = (outsize + psz - 1) & ~(psz - 1);
INFO("Opening input file %s\n", infile);
ifd = open(infile, O_RDONLY);
FAILIF(ifd < 0, "open(%s) failed: %s (%d)\n",
infile, strerror(errno), errno);
INFO("Opening output file %s\n", outfile);
ofd = open(outfile, O_RDWR | O_CREAT, 0664);
FAILIF(ofd < 0, "open(%s) failed: %s (%d)\n",
outfile, strerror(errno), errno);
INFO("Memory-mapping input file %s\n", infile);
in = mmap(0, width * height * 3 / 2, PROT_READ, MAP_PRIVATE, ifd, 0);
FAILIF(in == MAP_FAILED, "could not mmap input file: %s (%d)\n",
strerror(errno), errno);
INFO("Truncating output file %s to %d bytes\n", outfile, outsize);
FAILIF(ftruncate(ofd, outsize) < 0,
"Could not truncate output file to required size: %s (%d)\n",
strerror(errno), errno);
INFO("Memory mapping output file %s\n", outfile);
out = mmap(0, outsize, PROT_WRITE, MAP_SHARED, ofd, 0);
FAILIF(out == MAP_FAILED, "could not mmap output file: %s (%d)\n",
strerror(errno), errno);
INFO("PPM header (%d) bytes:\n%s\n", header_size, header);
FAILIF(write(ofd, header, header_size) != header_size,
"Error wrinting PPM header: %s (%d)\n",
strerror(errno), errno);
INFO("Converting %dx%d YUV 4:2:0 to RGB24...\n", width, height);
color_convert_common(in, in + width * height,
width, height,
out + header_size, outsize - header_size,
gray, rotate,
type == CONVERT_TYPE_ARGB ? argb_cb : rgb24_cb);
}
int verbose_flag;
int quiet_flag;
int main(int argc, char **argv) {
char *infile, *outfile, *type;
int height, width, gray, rotate;
int cmdline_error = 0;
/* Parse command-line arguments. */
int first = get_options(argc, argv,
&outfile,
&height,
&width,
&gray,
&type,
&rotate,
&verbose_flag);
if (first == argc) {
ERROR("You must specify an input file!\n");
cmdline_error++;
}
if (!outfile) {
ERROR("You must specify an output file!\n");
cmdline_error++;
}
if (height < 0 || width < 0) {
ERROR("You must specify both image height and width!\n");
cmdline_error++;
}
FAILIF(rotate % 90, "Rotation angle must be a multiple of 90 degrees!\n");
rotate /= 90;
rotate %= 4;
if (rotate < 0) rotate += 4;
if (cmdline_error) {
print_help(argv[0]);
exit(1);
}
infile = argv[first];
INFO("input file: [%s]\n", infile);
INFO("output file: [%s]\n", outfile);
INFO("height: %d\n", height);
INFO("width: %d\n", width);
INFO("gray only: %d\n", gray);
INFO("encode as: %s\n", type);
INFO("rotation: %d\n", rotate);
/* Convert the image */
int conv_type;
if (!type || !strcmp(type, "ppm"))
conv_type = CONVERT_TYPE_PPM;
else if (!strcmp(type, "rgb"))
conv_type = CONVERT_TYPE_RGB;
else if (!strcmp(type, "argb"))
conv_type = CONVERT_TYPE_ARGB;
else FAILIF(1, "Unknown encoding type %s.\n", type);
convert(infile, outfile,
height, width, gray,
conv_type,
rotate);
free(outfile);
return 0;
}
|