File: image2ps.c

package info (click to toggle)
libforms 1.0.93sp1-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 11,548 kB
  • ctags: 9,107
  • sloc: ansic: 97,227; sh: 9,236; makefile: 858
file content (202 lines) | stat: -rw-r--r-- 5,004 bytes parent folder | download | duplicates (2)
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
/*
 * This file is part of XForms.
 *
 * XForms is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1, or
 * (at your option) any later version.
 *
 * XForms is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with XForms. If not, see <http://www.gnu.org/licenses/>.
 */


/**
 * \file image2ps.c
 *
 *  This file is part of XForms package
 *  Copyright (c) 1997-2000  by T.C. Zhao
 *  All rights reserved.
 *
 * Turn a colormapped image into PostScript
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include "fd2ps.h"

static const char *hexdigits = "0123456789abcdef";
#define LINELENGTH  37


/***************************************
 ***************************************/

int
image2colorps( short      * pixels,
               int          w,
               int          h,
               fd2psCMAP *  map,
               int          ncol  FL_UNUSED_ARG,
               const char * cmt)
{
    int x,
        y,
        k,
        r,
        g,
        b;
    int *rb,
        *gb,
        *bb;
    char pscmd[ 128 ];
    short *p;

    strcpy( pscmd, ps_literal( ( cmt && *cmt ) ? cmt : "startColorImage" ) );
    ps_output( "/redstring %d string def\n", w );
    ps_output( "/grnstring %d string def\n", w );
    ps_output( "/blustring %d string def\n", w );

    ps_output( "/%s\n", pscmd );
    ps_output( "{%d %d 8 [ %d 0 0 -%d 0 %d]\n", w, h, w, h, h );
    ps_output( " {currentfile redstring readhexstring pop}\n" );
    ps_output( " {currentfile grnstring readhexstring pop}\n" );
    ps_output( " {currentfile blustring readhexstring pop}\n" );
    ps_output( " true 3 colorimage\n } bind def\n");

    /* Start */

    ps_output( "%d %d scale\n", w, h );

    ps_verbatim( "%s\n", pscmd );

    rb = malloc( w * sizeof *rb );
    gb = malloc( w * sizeof *gb );
    bb = malloc( w * sizeof *bb );

    for ( p = pixels, k = y = 0; y < h; y++ )
    {
        for ( x = 0; x < w; x++, p++ )
        {
            rb[ x ] = map[ *p ].red;
            gb[ x ] = map[ *p ].green;
            bb[ x ] = map[ *p ].blue;
        }

        for ( x = 0; x < w; x++ )
        {
            r = rb[ x ];
            ps_output( "%c%c", hexdigits[ ( r >> 4 ) & 15 ],
                       hexdigits[ r & 15 ] );
            if ( ++k % LINELENGTH == 0 )
                ps_output( "\n" );
        }

        for ( x = 0; x < w; x++ )
        {
            g = gb[ x ];
            ps_output( "%c%c", hexdigits[ ( g >> 4 ) & 15 ],
                       hexdigits[ g & 15 ] );
            if ( ++k % LINELENGTH == 0 )
                ps_output( "\n" );
        }

        for ( x = 0; x < w; x++ )
        {
            b = bb[ x ];
            ps_output( "%c%c", hexdigits[ ( b >> 4 ) & 15 ],
                       hexdigits[ b & 15 ] );
            if ( ++k % LINELENGTH == 0 )
                ps_output( "\n" );
        }
    }

    free( rb );
    free( gb );
    free( bb );

    return 0;
}


/***************************************
 ***************************************/

int
image2grayps( short      * pixels,
              int          w,
              int          h,
              fd2psCMAP  * map,
              int          ncol,
              const char * cmt)
{
    int x,
        y,
        k,
        r;
    char pscmd[ 128 ];
    short *p;

    strcpy( pscmd, ps_literal( ( cmt && *cmt ) ? cmt : "startGrayImage" ) );
    ps_output( "/graystring %d string def\n", w );

    ps_output( "/%s\n", pscmd );
    ps_output( "{%d %d 8 [ %d 0 0 -%d 0 %d]\n", w, h, w, h, h );
    ps_output( " {currentfile graystring readhexstring pop}\n" );
    ps_output( " image \n} bind def\n" );
    ps_output( "%d %d scale\n", w, h );
    ps_verbatim( "%s\n", pscmd );

    /* convert colormap to grayscale */

    for ( x = 0; x < ncol; x++ )
        map[ x ].red = rgb2gray( map[ x ].red, map[ x ].green, map[ x ].blue );

    for ( p = pixels, k = y = 0; y < h; y++ )
    {
        for ( x = 0; x < w; x++, p++ )
        {
            r = map[ *p ].red;
            ps_output( "%c%c", hexdigits[ ( r >> 4 ) & 15 ],
                       hexdigits[ r & 15 ] );
            if ( ++k % LINELENGTH == 0 )
                ps_output( "\n" );
        }
    }

    return 0;
}


/***************************************
 ***************************************/

char *
ps_literal( const char * s )
{
    static char buf[ 1024 ];
    char *p = buf;

    for ( *p = '\0'; s && *s; s++ )
        *p++ = ( PS_SPECIAL( *s ) ) ? '$' : *s;

    *p = '\0';
    return buf;
}


/*
 * Local variables:
 * tab-width: 4
 * indent-tabs-mode: nil
 * End:
 */