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
|
/******************************************************************
Copyright (C) 1996 by Brian Scearce
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
and/or distribute copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the following conditions:
1. The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
2. Redistribution for profit requires the express, written permission of
the author.
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 BRIAN SCEARCE 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.
******************************************************************/
/** Fixdark
Routine to repair dark current artifacts in qcam output.
Basic idea: the Qcam CCD suffers from "dark current";
that is, some of the CCD pixels will leak current under
long exposures, even if they're in the dark, and this
shows up as ugly speckling on images taken in low light.
Fortunately, the leaky pixels are the same from shot to
shot. So, we can figure out which pixels are leaky by
taking some establishing shots in the dark, and try to
fix those pixels on subsequent shots. The dark
establishing shots need only be done once per camera.
*/
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include <unistd.h>
#include "qcam.h"
#define FNAME "qcam.darkfile"
#define TRUE 1
#define FALSE 0
static unsigned char master_darkmask[MAX_HEIGHT][MAX_WIDTH];
static
int
write_darkmask()
{
char darkfile[BUFSIZ], *p;
FILE *fp;
strcpy(darkfile, CONFIG_FILE);
if ( (p = strrchr(darkfile, '/'))) {
strcpy(p+1, FNAME);
} else {
strcpy(darkfile, FNAME);
}
if (!(fp = fopen(darkfile, "w"))) {
#ifdef DEBUG
fprintf(stderr, "Can't open darkfile %s\n", darkfile);
#endif
return 0;
}
if (fwrite(master_darkmask, sizeof(unsigned char), MAX_WIDTH*MAX_HEIGHT, fp) !=
MAX_WIDTH*MAX_HEIGHT) {
#ifdef DEBUG
fprintf(stderr, "Error reading darkfile\n");
#endif
return 0;
}
fclose(fp);
return 1;
}
#define MAX_SHADES 64
static
void
mode_average(scanbuf *scan, int pixels[MAX_SHADES], int *mode, int *average)
{
int i;
int pixval;
int modeindex = 32;
int modeval = 0;
int pixtotal = 0;
for (i = 0; i < MAX_SHADES; i++) pixels[i] = 0;
for (i = 0; i < MAX_WIDTH * MAX_HEIGHT; i++) {
pixval = scan[i];
assert(pixval < MAX_SHADES);
pixels[pixval]++;
pixtotal += pixval;
}
for (i = 0; i < MAX_SHADES; i++) {
if (pixels[i] > modeval) {
modeval = pixels[i];
modeindex = i;
}
}
*mode = modeindex;
*average = pixtotal / (MAX_WIDTH * MAX_HEIGHT);
}
#define MAX_TRIES 10
int
main(int ac, char *av[])
{
struct qcam *q;
scanbuf *scan;
int pixels[MAX_SHADES];
int again, tries;
int countdown;
int toohigh;
int pixelcount;
int whitebal, brightness, contrast;
int whitebal_low, whitebal_high;
int contrast_low, contrast_high;
int mode, average;
int total;
int x, y;
int i;
printf("Your QuickCam must be in the dark for this to work.\n");
printf("Do not run this program with the QuickCam exposed to light!\n");
printf("You have ten seconds to kill this program before it proceeds.\n");
sleep(10);
printf("Running...\n");
for (y = 0; y < MAX_HEIGHT; y++)
for (x = 0; x < MAX_WIDTH; x++)
master_darkmask[y][x] = 255;
if(geteuid()) {
fprintf(stderr,"%s: Not installed SUID or run as root. Exiting.\n",
av[0]);
exit(1);
}
q=qc_init();
qc_initfile(q, 0);
qc_setbitdepth(q, 6);
qc_settransfer_scale(q, 1);
qc_settop(q, 1);
qc_setleft(q, 2);
qc_setresolution(q, MAX_WIDTH, MAX_HEIGHT);
whitebal = qc_getwhitebal(q);
contrast = qc_getcontrast(q);
qc_setbrightness(q, 254);
brightness = qc_getbrightness(q);
if (brightness != 254) {
fprintf(stderr, "Brightness not correctly set");
exit(1);
}
if (qc_open(q)) {
fprintf(stderr,"Cannot open QuickCam; exiting.\n");
exit(1);
}
setuid(getuid());
countdown = 15;
for (brightness = 254; countdown-- > 0 && brightness > 0; brightness--) {
qc_setbrightness(q, brightness);
qc_set(q);
scan = qc_scan(q);
mode_average(scan, pixels, &mode, &average);
/* Part One: adjust white balance so mode is middle of range */
whitebal_low = 0; whitebal_high = 255; tries = 0;
do {
#ifdef DEBUG
fprintf(stderr, "Whitebal try %d, whitebal = %d, mode = %d\n",
tries, whitebal, mode);
#endif
again = FALSE;
if (mode < MAX_SHADES/2 - 2) { /* too low */
whitebal_low = whitebal;
whitebal = (whitebal_low + whitebal_high) / 2;
qc_setwhitebal(q, whitebal);
again = 1;
}
if (mode > MAX_SHADES/2 + 2) { /* too high */
assert(!again); /* can't be too bright AND too dark */
whitebal_high = whitebal;
whitebal = (whitebal_low + whitebal_high) / 2;
qc_setwhitebal(q, whitebal);
again = 1;
}
if (again) {
free(scan);
qc_set(q);
scan = qc_scan(q);
mode_average(scan, pixels, &mode, &average);
}
} while (tries++ < MAX_TRIES && again && whitebal_low < whitebal_high-1);
/* Part 2: set contrast */
contrast_low = 0; contrast_high = 255; tries = 0;
do {
again = FALSE;
for (total = 0, i = mode - MAX_SHADES/16; i < mode + MAX_SHADES/16; i++)
total += pixels[i];
#ifdef DEBUG
fprintf(stderr, "Contrast try %d, contrast = %d, midrange = %d%%\n",
tries, contrast, 100 * total/(MAX_WIDTH * MAX_HEIGHT) );
#endif
if (total > 7 * MAX_WIDTH * MAX_HEIGHT / 8) { /* too high */
contrast_low = contrast;
contrast = (contrast_low + contrast_high) / 2;
qc_setcontrast(q, contrast);
again = 1;
}
if (total < 3 * MAX_WIDTH * MAX_HEIGHT / 4) { /* too low */
assert(!again); /* can't be too wide AND too narrow */
contrast_high = contrast;
contrast = (contrast_low + contrast_high) / 2;
qc_setcontrast(q, contrast);
again = 1;
}
if (again) {
free(scan);
qc_set(q);
scan = qc_scan(q);
mode_average(scan, pixels, &mode, &average);
}
} while (tries++ < MAX_TRIES && again && contrast_low < contrast_high-1);
again = FALSE;
toohigh = mode + MAX_SHADES/4;
pixelcount = 0;
for (y = 0; y < MAX_HEIGHT; y++) {
for (x = 0; x < MAX_WIDTH; x++) {
if (scan[y*MAX_WIDTH+x] > toohigh) {
master_darkmask[y][x] = brightness;
pixelcount++;
again = TRUE;
}
}
}
#ifdef DEBUG
#define CHARTHEIGHT 15
for (i = CHARTHEIGHT; i > 0; i--) {
int j;
for (j = 0; j < MAX_SHADES; j++) {
if (CHARTHEIGHT * pixels[j] >= i*pixels[mode]) {
if (j == toohigh) fprintf(stderr, "+");
else fprintf(stderr, ".");
} else if (j == toohigh) fprintf(stderr, "|");
else fprintf(stderr, " ");
}
fprintf(stderr, "\n");
}
for (i = 0; i < MAX_SHADES; i++) fprintf(stderr, "%01d", i / 10);
fprintf(stderr, "\n");
for (i = 0; i < MAX_SHADES; i++) fprintf(stderr, "%01d", i % 10);
fflush(stderr);
#endif
printf("Brightness %3d gives whitebal %3d, contrast %3d, %5d bad pixel%s\n",
brightness, whitebal, contrast, pixelcount, (pixelcount==1)?"":"s");
free(scan);
if (again) countdown = 5;
}
qc_close(q);
write_darkmask();
return 0;
}
|