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
|
/*
* Copyright (c) 1994 Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the Network Research
* Group at Lawrence Berkeley Laboratory.
* 4. Neither the name of the University nor of the Laboratory may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static char rcsid[] =
"@(#) $Header: /cs/research/mice/starship/src/local/CVS_repository/vic/mkcube.cc,v 1.1.1.1 1998/02/18 18:03:36 ucacsva Exp $ (LBL)";
#endif
#include <stdio.h>
#ifndef WIN32
#include <unistd.h>
#endif
#include <stdlib.h>
#include <string.h>
#ifdef sgi
#include <getopt.h>
#endif
#ifdef WIN32
extern "C" {
int getopt(int, char * const *, const char *);
extern int optind;
extern char *optarg;
}
#endif
struct color {
short r;
short g;
short b;
short y;
short u;
short v;
};
int yuv_to_rgb(struct color* c)
{
double y = c->y;
double u = c->u - 128.;
double v = c->v - 128.;
int r = int(y + 1.402 * v + 0.5);
int g = int(y - 0.34414 * u - 0.71414 * v + 0.5);
int b = int(y + 1.772 * u + 0.5);
int valid = 1;
#define CHECK(v) \
if (v > 255) { \
v = 255; \
valid = 0; \
} else if (v < 0) { \
v = 0; \
valid = 0; \
}
CHECK(r)
c->r = r;
CHECK(g)
c->g = g;
CHECK(b)
c->b = b;
return (valid);
}
void
rgb()
{
int r, g, b;
printf("P3\n%d 1\n255\n", 4 * 8 * 4);
for (b = 0; b < 4; ++b) {
for (g = 0; g < 8; ++g) {
for (r = 0; r < 4; ++r) {
int red = r * 16384 + 16383;
int green = g * 8192 + 8191;
int blue = b * 16384 + 16383;
printf("%d %d %d\n", red >> 8,
green >> 8, blue >> 8);
}
}
}
}
void
cklimit(int n)
{
if (n >= 256) {
fprintf(stderr, "mkcube: too many colors\n");
exit(1);
}
}
void
yuv(int ystep, int ustep, int vstep, int Fflag)
{
int i;
int ncolor = 0;
struct color colors[256];
color c;
/*
* uv grids - start from gray levels and work outward until
* we hit infeasible colors.
*/
int v;
for (v = 128; v < 256; v += vstep) {
c.v = v;
int u;
for (u = 128; u < 256; u += ustep) {
c.u = u;
for (int y = 0; y < 256; y += ystep) {
c.y = y;
if (yuv_to_rgb(&c)) {
cklimit(ncolor);
colors[ncolor++] = c;
}
}
}
for (u = 128 - ustep; u >= 0; u -= ustep) {
c.u = u;
for (int y = 0; y < 256; y += ystep) {
c.y = y;
if (yuv_to_rgb(&c)) {
cklimit(ncolor);
colors[ncolor++] = c;
}
}
}
}
for (v = 128 - vstep; v >= 0; v -= vstep) {
c.v = v;
int u;
for (u = 128; u < 256; u += ustep) {
c.u = u;
for (int y = 0; y < 256; y += ystep) {
c.y = y;
if (yuv_to_rgb(&c)) {
cklimit(ncolor);
colors[ncolor++] = c;
}
}
}
for (u = 128 - ustep; u >= 0; u -= ustep) {
c.u = u;
for (int y = 0; y < 256; y += ystep) {
c.y = y;
if (yuv_to_rgb(&c)) {
cklimit(ncolor);
colors[ncolor++] = c;
}
}
}
}
if (Fflag) {
/* some extra flesh tones */
for (v = 140; v <= 190; v += 15) {
c.v = v;
for (int u = 80; u <= 110; u += 15) {
c.u = u;
/* note y starts on grid for YBITS=3 or 4 */
for (int y = 96; y < 192; y += ystep) {
c.y = y;
if (yuv_to_rgb(&c)) {
cklimit(ncolor);
colors[ncolor++] = c;
}
}
}
}
}
printf("P3\n%d 1\n255\n", ncolor);
for (i = 0; i < ncolor; ++i)
printf("%d %d %d\n", colors[i].r, colors[i].g, colors[i].b);
}
int
ystep_to_ybits(int step)
{
if (step > 0 && step < 256) {
int ybits = 8;
while ((step & 1) == 0) {
step >>= 1;
--ybits;
}
if ((step >> 1) == 0)
return (ybits);
}
fprintf(stderr, "mkcube: -y arg must be power of 2 in [2,128]\n");
exit(1);
return (0);
}
void
usage()
{
fprintf(stderr,
"usage: mkcube [-y step] [-u step] [-v step] rgb|yuv\n");
exit(1);
}
extern char *optarg;
extern int optind;
int
main(int argc, char **argv)
{
int udelta = 40;
int vdelta = 40;
int ybits = 3;
int Fflag = 0;
int op;
while ((op = getopt(argc, argv, "FU:V:Y:")) != -1) {
switch (op) {
case 'F':
++Fflag;
break;
case 'U':
udelta = atoi(optarg);
break;
case 'V':
vdelta = atoi(optarg);
break;
case 'Y':
ybits = atoi(optarg);
break;
case '?':
usage();
}
}
char* which = 0;
if (optind < argc && argc > 1) {
if (argc - optind > 1)
usage();
which = argv[optind];
} else
usage();
if (strcmp(which, "rgb") == 0)
rgb();
else if (strcmp(which, "yuv") == 0)
yuv(1 << (8 - ybits), udelta, vdelta, Fflag);
else
usage();
return (0);
}
|