File: bgsubtract.c

package info (click to toggle)
astrometry.net 0.93%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 19,372 kB
  • sloc: ansic: 163,192; python: 18,357; makefile: 1,522; sh: 138; cpp: 78; pascal: 67; awk: 56; perl: 9
file content (81 lines) | stat: -rw-r--r-- 1,738 bytes parent folder | download | duplicates (5)
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
/*
 # This file is part of the Astrometry.net suite.
 # Licensed under a 3-clause BSD style license - see LICENSE
 */

#include <unistd.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <stdint.h>

#include "ctmf.h"
#include "cairoutils.h"

const char* OPTIONS = "h";


int main(int argc, char *args[]) {
    int argchar;
    //char* progname = args[0];
    char *infn = NULL, *outfn = NULL;
    unsigned char* img;
    int W, H;
    unsigned char* bg;
    int i, j;
    int halfbox = 10;

    while ((argchar = getopt (argc, args, OPTIONS)) != -1)
        switch (argchar) {
            //case '?':
            //case 'h':
            //printHelp(progname);
            //return 0;
        default:
            return -1;
        }

    if (optind != argc-2) {
        //printHelp(progname);
        exit(-1);
    }

    infn = args[optind];
    outfn = args[optind+1];

    printf("Reading input %s...\n", infn);
    img = cairoutils_read_jpeg(infn, &W, &H);
    if (!img) {
        fprintf(stderr, "Failed to read input image %s.\n", infn);
        exit(-1);
    }
    printf("Image is %i x %i.\n", W, H);

    bg = malloc(W * H * 4);

    printf("Filtering...");
    for (i=0; i<3; i++) {
        printf(" %c...", "RGB"[i]);
        fflush(stdout);
        ctmf(img + i, bg + i, W, H, 4*W, 4*W, halfbox, 4, 512*1024);
    }
    printf("\n");

    for (i=0; i<(W*H); i++)
        for (j=0; j<3; j++) {
            int ind = i*4 + j;
            if (img[ind] <= bg[ind])
                img[ind] = 0;
            else
                img[ind] -= bg[ind];
        }

    printf("Writing output...\n");
    cairoutils_write_jpeg(outfn, img, W, H);

    free(img);
    free(bg);

    printf("Done!\n");
    return 0;
}