about summary refs log tree commit diff
path: root/converter/ppm/ppmtoascii.c
blob: b7c6669d78ee791f71c248b0b00ab020654cb483 (plain) (blame)
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
/*=============================================================================
                                  ppmtoascii
===============================================================================

  Convert a PPM image to ASCII with ANSI color graphics terminal controls

  Based on pbmtoascii.
  Copyright (C) 1988, 1992 by Jef Poskanzer.
  Copyright (C) 2010 by Frank Ch. Eigler.

  Permission to use, copy, modify, and distribute this software and its
  documentation for any purpose and without fee is hereby granted, provided
  that the above copyright notice appear in all copies and that both that
  copyright notice and this permission notice appear in supporting
  documentation.  This software is provided "as is" without express or
  implied warranty.
=============================================================================*/

#include "pm_c_util.h"
#include "mallocvar.h"
#include "shhopt.h"
#include "ppm.h"
#include "pm_gamma.h"

/* We use the same algorithm for converting many pixels into a character
   as Pbmtoascii.  See pbmtoascii.c for a full explanation.
*/


#define SQQ '\''
#define BSQ '\\'

/* Bit-map for 1x2 mode:
**     1
**     2
*/
static char const carr1x2[4] = {
/*   0    1    2    3   */
    ' ', '"', 'o', 'M' };

/* Bit-map for 2x4 mode (hex):
**     1  2
**     4  8
**     10 20
**     40 80
** The idea here is first to preserve geometry, then to show density.
*/
#define D08 'M'
#define D07 'H'
#define D06 '&'
#define D05 '$'
#define D04 '?'
static char const carr2x4[256] = {
/*0  1    2   3    4   5    6   7    8   9    A   B    C   D    E   F  */
' ',SQQ, '`','"', '-',SQQ, SQQ,SQQ, '-','`', '`','`', '-','^','^','"',/*00-0F*/
'.',':', ':',':', '|','|', '/',D04, '/','>', '/','>', '~','+','/','*',/*10-1F*/
'.',':', ':',':', BSQ,BSQ, '<','<', '|',BSQ, '|',D04, '~',BSQ,'+','*',/*20-2F*/
'-',':', ':',':', '~',D04, '<','<', '~','>', D04,'>', '=','b','d','#',/*30-3F*/
'.',':', ':',':', ':','!', '/',D04, ':',':', '/',D04, ':',D04,D04,'P',/*40-4F*/
',','i', '/',D04, '|','|', '|','T', '/',D04, '/','7', 'r','}','/','P',/*50-5F*/
',',':', ';',D04, '>',D04, 'S','S', '/',')', '|','7', '>',D05,D05,D06,/*60-6F*/
'v',D04, D04,D05, '+','}', D05,'F', '/',D05, '/',D06, 'p','D',D06,D07,/*70-7F*/
'.',':', ':',':', ':',BSQ, ':',D04, ':',BSQ, '!',D04, ':',D04,D04,D05,/*80-8F*/
BSQ,BSQ, ':',D04, BSQ,'|', '(',D05, '<','%', D04,'Z', '<',D05,D05,D06,/*90-9F*/
',',BSQ, 'i',D04, BSQ,BSQ, D04,BSQ, '|','|', '|','T', D04,BSQ,'4','9',/*A0-AF*/
'v',D04, D04,D05, BSQ,BSQ, D05,D06, '+',D05, '{',D06, 'q',D06,D06,D07,/*B0-BF*/
'_',':', ':',D04, ':',D04, D04,D05, ':',D04, D04,D05, ':',D05,D05,D06,/*C0-CF*/
BSQ,D04, D04,D05, D04,'L', D05,'[', '<','Z', '/','Z', 'c','k',D06,'R',/*D0-DF*/
',',D04, D04,D05, '>',BSQ, 'S','S', D04,D05, 'J',']', '>',D06,'1','9',/*E0-EF*/
'o','b', 'd',D06, 'b','b', D06,'6', 'd',D06, 'd',D07, '#',D07,D07,D08 /*F0-FF*/
};



static const char* rgb2x2x2fg[2][2][2] = {
  {{"\x1b[30m",   /* 000 black */
    "\x1b[34m"},  /* 001 blue */
   {"\x1b[32m",   /* 010 green */
    "\x1b[36m"}}, /* 011 cyan */
  {{"\x1b[31m",   /* 100 red */
    "\x1b[35m"},  /* 101 magenta */
   {"\x1b[33m",   /* 110 yellow */
    "\x1b[37m"}}, /* 111 white */
};



struct cmdlineInfo {
    /* All the information the user supplied in the command line,
       in a form easy for the program to use.
    */
    const char * inputFileName;  /* Name of input file */
    unsigned int cellWidth;
    unsigned int cellHeight;
    const char * carr;
};



static void
parseCommandLine(int argc, const char **argv,
                 struct cmdlineInfo * const cmdlineP) {

    optEntry * option_def;
    optStruct3 opt;

    unsigned int option_def_index;
    unsigned int dim1x2Spec, dim2x4Spec;

    MALLOCARRAY_NOFAIL(option_def, 100);

    option_def_index = 0;   /* incremented by OPTENTRY */
    OPTENT3(0, "1x2", OPT_FLAG, NULL, &dim1x2Spec, 0);
    OPTENT3(0, "2x4", OPT_FLAG, NULL, &dim2x4Spec, 0);

    opt.opt_table = option_def;
    opt.short_allowed = false;  /* We have no short (old-fashioned) options */
    opt.allowNegNum = false;  /* We have no parms that are negative numbers */

    pm_optParseOptions4(&argc, argv, opt, sizeof(opt), 0);
        /* Uses and sets argc, argv, and some of *cmdlineP and others. */

    if (dim1x2Spec && dim2x4Spec)
        pm_error("You cannot specify both -1x2 and -2x4");
    else if (dim2x4Spec) {
        cmdlineP->cellWidth  = 2;
        cmdlineP->cellHeight = 4;
        cmdlineP->carr       = carr2x4;
    } else {
        cmdlineP->cellWidth  = 1;
        cmdlineP->cellHeight = 2;
        cmdlineP->carr       = carr1x2;
    }

    if (argc-1 < 1)
        cmdlineP->inputFileName = "-";
    else {
        cmdlineP->inputFileName = argv[1];

        if (argc-1 > 1)
            pm_error("Too many arguments: %u.  The only possible argument "
                     "is the input file name", argc-1);
    }
}



static void
ppmtoascii(pixel * const *    const pixels,
           unsigned int       const cols,
           unsigned int       const rows,
           pixval             const maxval,
           struct cmdlineInfo const cmdline,
           FILE *             const ofP) {

    unsigned int const cellHeight = cmdline.cellHeight;
    unsigned int const cellWidth  = cmdline.cellWidth;
    const char * const carr       = cmdline.carr;
    unsigned int row;

    fprintf(ofP, "\x1b[0m"); /* Clear initial ansi attributes */
    /* TODO: set background to black */

    for (row = 0; row < rows; row += cellHeight) {
        unsigned int col;
        for (col = 0; col < cols; col += cellWidth)     {
            unsigned int const sumthresh = cellWidth * cellHeight * 1.0 / 2;

            float sumr, sumg, sumb;
                /* sum of intensity within cell so far, by component */
            unsigned int b;
            const char * colorstr;
            unsigned int subrow;

            sumr = sumg = sumb = 0;  /* initial value */
            b = 0; /* initial value */

            for (subrow = 0; subrow < cellHeight; ++subrow) {
                unsigned int subcol;
                for (subcol = 0; subcol < cellWidth; ++subcol) {
                    pixel color;
                    pixval value;

                    if (row + subrow < rows && col + subcol < cols)
                        color = pixels[row + subrow][col + subcol];
                    else
                        color = ppm_whitepixel (maxval);

                    sumr += pm_ungamma709((float)PPM_GETR(color)/maxval);
                    sumg += pm_ungamma709((float)PPM_GETG(color)/maxval);
                    sumb += pm_ungamma709((float)PPM_GETB(color)/maxval);

                    value = ppm_colorvalue(color);
                    b <<= 1;
                    if (value > maxval/2)
                        b |= 1;
                }
            }
            colorstr = rgb2x2x2fg
                [sumr >= sumthresh]
                [sumg >= sumthresh]
                [sumb >= sumthresh];
            fprintf(ofP, "%s%c", colorstr, carr[b]);
        }
        fprintf(ofP, "\n");
    }
    fprintf(ofP, "\x1b[0m"); /* Clear final ansi attributes */
}



int
main(int argc, const char ** argv) {

    struct cmdlineInfo cmdline;
    FILE * ifP;

    pixval maxval;
    pixel ** pixels;
    int rows, cols;

    pm_proginit(&argc, argv);

    parseCommandLine(argc, argv, &cmdline);

    ifP = pm_openr(cmdline.inputFileName);

    pixels = ppm_readppm(ifP, &cols, &rows, &maxval);

    pm_close(ifP);

    ppmtoascii(pixels, cols, rows, maxval, cmdline, stdout);

    return 0;
}