summaryrefslogtreecommitdiff
path: root/tools/gslite/gslt_image_threads_test.c
blob: 431dba6c21753c26332262ceb1dfda5861ee1c17 (plain)
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
/* Copyright (C) 2006 artofcode LLC.
   All Rights Reserved.

   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied, modified
   or distributed except as expressly authorized under the terms of that
   license.  Refer to licensing information at http://www.artifex.com/
   or contact Artifex Software, Inc.,  7 Mt. Lassen Drive - Suite A-134,
   San Rafael, CA  94903, U.S.A., +1(415)492-9861, for further information.
*/

/* $Id: gslt_image_threads_test.c 2993 2007-12-18 23:28:31Z giles $ */
/* example client for the gslt image loading library */

#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>

typedef unsigned char byte;
#include "gslt.h"
#include "gslt_image.h"

extern void gs_erasepage(gs_state *gs);
extern void gs_moveto(gs_state *gs, double, double);
extern void gs_output_page(gs_state *gs, int, int);

gslt_image_t *
decode_image_file(gs_memory_t *mem, FILE *in)
{
    gslt_image_t *image;
    int len, bytes;
    byte *buf;

    /* get compressed data size */
    fseek(in, 0, SEEK_END);
    len = ftell(in);

    /* load the file into memory */
    fseek(in, 0, SEEK_SET);
    buf = malloc(len);
    if (buf == NULL) return NULL;

    bytes = fread(buf, 1, len, in);
    if (bytes != len) {
        free(buf);
        return NULL;
    }

    image = gslt_image_decode(mem, buf, len);
    free(buf);

    return image;
}

gslt_image_t *
decode_image_filename(gs_memory_t *mem, const char *filename)
{
    gslt_image_t *image;
    FILE *in;

    in = fopen(filename, "rb");
    if (in == NULL) return NULL;

    image = decode_image_file(mem, in);
    fclose(in);

    return image;
}

/* write out the image as a pnm file for verification */
int
write_image_file(gslt_image_t *image, FILE *out)
{
    byte *row;
    int j, bytes;

    if (image == NULL || image->samples == NULL) {
        fprintf(stderr, "ignoring empty image object\n");
        return  -1;
    }

    if (image->components == 1 && image->bits == 1) {
        /* PBM file */
        int i;
        int rowbytes = (image->width+7)>>3;
        byte *local = malloc(rowbytes);

        fprintf(out, "P4\n%d %d\n", image->width, image->height);
        row = image->samples;
        for (j = 0; j < image->height; j++) {
            /* PBM images are inverted relative to our XPS/PS convention */
            for (i = 0; i < rowbytes; i++)
                local[i] = row[i] ^ 0xFF;
            bytes = fwrite(local, 1, rowbytes, out);
            row += image->stride;
        }
        free(local);
    } else if (image->components == 1 && image->bits == 8) {
        /* PGM file */
        fprintf(out, "P5\n%d %d\n255\n", image->width, image->height);
        row = image->samples;
        for (j = 0; j < image->height; j++) {
            bytes = fwrite(row, 1, image->width, out);
            row += image->stride;
        }
    } else {
        /* PPM file */
        fprintf(out, "P6\n%d %d\n255\n", image->width, image->height);
        row = image->samples;
        for (j = 0; j < image->height; j++) {
            bytes = fwrite(row, image->components, image->width, out);
            row += image->stride;
        }
    }

    return 0;
}

int
write_image_filename(gslt_image_t *image, const char *filename)
{
    FILE *out;
    int error;

    out = fopen(filename, "wb");
    if (out == NULL) {
        fprintf(stderr, "could not open '%s' for writing\n", filename);
        return -1;
    }

    error = write_image_file(image, out);
    fclose(out);

    return error;
}

/* global input filename. */
char *filename;

void *
print_image(void *threadid)
{
    gs_memory_t *mem;
    gx_device *dev;
    gs_state *gs;
    gslt_image_t *image;
    char *s;
    int code = 0;

    /* get the device from the environment, or default */
    s = getenv("DEVICE");
    if (!s)
        s = "nullpage";

    /* initialize the graphicis library and set up a drawing state */
    mem = gslt_init_library();
    dev = gslt_init_device(mem, s);
    gs = gslt_init_state(mem, dev);

    gslt_get_device_param(mem, dev, "Name");
    gslt_set_device_param(mem, dev, "OutputFile", "gslt.out");

    /* prepare page for drawing (unused) */
    gs_erasepage(gs);
    gs_moveto(gs, 72.0, 72.0);

    /* load and decode the image */
    image = decode_image_filename(mem, filename);
    if (image == NULL) {
        fprintf(stderr, "reading image failed.\n");
        code = -1;
    }
    /* save an uncompressed copy for verification */
    {
        char outname[50];
        sprintf(outname, "out_%d.pnm", (int)threadid);
        fprintf(stderr, "writing %s\n", outname);
        write_image_filename(image, outname);
    }

    /* image could be drawn to the page here */

    /* release the image */
    gslt_image_free(mem, image);

    /* output the page (unused) */
    gs_output_page(gs, 1, 1);

    /* clean up the library */
    gslt_free_state(mem, gs);
    gslt_free_device(mem, dev);
    gslt_free_library(mem);

    pthread_exit(NULL);
}

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

    pthread_t thread1, thread2;
    int ret;
    int t1 = 1;
    int t2 = 2;

    filename = argv[argc-1];
    fprintf(stderr, "loading '%s'\n", filename);

    if ( ((ret=pthread_create(&thread1, NULL, print_image, (void *)t1)) != 0) ||
         ((ret=pthread_create(&thread2, NULL, print_image, (void *)t2)) != 0) ) {
        fprintf(stderr, "Error creating thread code=%d", ret);
        exit(-1);
    }
    pthread_exit(NULL);
}