summaryrefslogtreecommitdiff
path: root/filter/testraster.c
blob: bc35bb01a41ab61776a27b8ec211fbcc5ee9d8d8 (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
/*
 * "$Id: testraster.c 4545 2005-06-21 19:26:28Z mike $"
 *
 *   Raster test program routines for the Common UNIX Printing System (CUPS).
 *
 *   Copyright 1997-2005 by Easy Software Products.
 *
 *   These coded instructions, statements, and computer programs are the
 *   property of Easy Software Products and are protected by Federal
 *   copyright law.  Distribution and use rights for the CUPS Raster source
 *   files are outlined in the GNU Library General Public License, located
 *   in the "pstoraster" directory.  If this file is missing or damaged
 *   please contact Easy Software Products at:
 *
 *       Attn: CUPS Licensing Information
 *       Easy Software Products
 *       44141 Airport View Drive, Suite 204
 *       Hollywood, Maryland 20636 USA
 *
 *       Voice: (301) 373-9600
 *       EMail: cups-info@cups.org
 *         WWW: http://www.cups.org
 *
 *   This code and any derivative of it may be used and distributed
 *   freely under the terms of the GNU General Public License when
 *   used with GNU Ghostscript or its derivatives.  Use of the code
 *   (or any derivative of it) with software other than GNU
 *   GhostScript (or its derivatives) is governed by the CUPS license
 *   agreement.
 *
 *   This file is subject to the Apple OS-Developed Software exception.
 *
 * Contents:
 *
 */

/*
 * Include necessary headers...
 */

#include "raster.h"
#include <stdlib.h>
#include <cups/string.h>


/*
 * 'main()' - Test the raster read/write functions.
 */

int					/* O - Exit status */
main(void)
{
  int			page, x, y;	/* Looping vars */
  FILE			*fp;		/* Raster file */
  cups_raster_t		*r;		/* Raster stream */
  cups_page_header2_t	header;		/* Page header */
  unsigned char		data[2048];	/* Raster data */


  if ((fp = fopen("test.raster", "wb")) == NULL)
  {
    perror("Unable to create test.raster");
    return (1);
  }

  if ((r = cupsRasterOpen(fileno(fp), CUPS_RASTER_WRITE)) == NULL)
  {
    perror("Unable to create raster output stream");
    fclose(fp);
    return (1);
  }

  for (page = 0; page < 4; page ++)
  {
    memset(&header, 0, sizeof(header));
    header.cupsWidth        = 256;
    header.cupsHeight       = 256;
    header.cupsBytesPerLine = 256;

    if (page & 1)
    {
      header.cupsBytesPerLine *= 2;
      header.cupsColorSpace = CUPS_CSPACE_CMYK;
      header.cupsColorOrder = CUPS_ORDER_CHUNKED;
    }
    else
    {
      header.cupsColorSpace = CUPS_CSPACE_K;
      header.cupsColorOrder = CUPS_ORDER_BANDED;
    }

    if (page & 2)
    {
      header.cupsBytesPerLine *= 2;
      header.cupsBitsPerColor = 16;
      header.cupsBitsPerPixel = (page & 1) ? 64 : 16;
    }
    else
    {
      header.cupsBitsPerColor = 8;
      header.cupsBitsPerPixel = (page & 1) ? 32 : 8;
    }

    cupsRasterWriteHeader2(r, &header);

    memset(data, 0, header.cupsBytesPerLine);
    for (y = 0; y < 64; y ++)
      cupsRasterWritePixels(r, data, header.cupsBytesPerLine);

    for (x = 0; x < header.cupsBytesPerLine; x ++)
      data[x] = x;

    for (y = 0; y < 64; y ++)
      cupsRasterWritePixels(r, data, header.cupsBytesPerLine);

    memset(data, 255, header.cupsBytesPerLine);
    for (y = 0; y < 64; y ++)
      cupsRasterWritePixels(r, data, header.cupsBytesPerLine);

    for (x = 0; x < header.cupsBytesPerLine; x ++)
      data[x] = x / 4;

    for (y = 0; y < 64; y ++)
      cupsRasterWritePixels(r, data, header.cupsBytesPerLine);
  }

  cupsRasterClose(r);
  fclose(fp);

  if ((fp = fopen("test.raster", "rb")) == NULL)
  {
    perror("Unable to open test.raster");
    return (1);
  }

  if ((r = cupsRasterOpen(fileno(fp), CUPS_RASTER_READ)) == NULL)
  {
    perror("Unable to create raster input stream");
    fclose(fp);
    return (1);
  }

  for (page = 0; page < 4; page ++)
  {
    cupsRasterReadHeader2(r, &header);

    printf("Page %d:\n", page + 1);
    printf("    cupsWidth        = %d\n", header.cupsWidth);
    printf("    cupsHeight       = %d\n", header.cupsHeight);
    printf("    cupsBitsPerColor = %d\n", header.cupsBitsPerColor);
    printf("    cupsBitsPerPixel = %d\n", header.cupsBitsPerPixel);
    printf("    cupsColorSpace   = %d\n", header.cupsColorSpace);
    printf("    cupsColorOrder   = %d\n", header.cupsColorOrder);
    printf("    cupsBytesPerLine = %d\n", header.cupsBytesPerLine);

    for (y = 0; y < 64; y ++)
    {
      cupsRasterReadPixels(r, data, header.cupsBytesPerLine);

      if (data[0] != 0 || memcmp(data, data + 1, header.cupsBytesPerLine - 1))
        printf("    RASTER LINE %d CORRUPT AT %d (%02X instead of 00!)\n",
	       y, x, data[x]);
    }

    for (y = 0; y < 64; y ++)
    {
      cupsRasterReadPixels(r, data, header.cupsBytesPerLine);

      for (x = 0; x < header.cupsBytesPerLine; x ++)
        if (data[x] != (x & 255))
	  break;

      if (x < header.cupsBytesPerLine)
        printf("    RASTER LINE %d CORRUPT AT %d (%02X instead of %02X!)\n",
	       y + 64, x, data[x], x & 255);
    }

    for (y = 0; y < 64; y ++)
    {
      cupsRasterReadPixels(r, data, header.cupsBytesPerLine);

      if (data[0] != 255 || memcmp(data, data + 1, header.cupsBytesPerLine - 1))
        printf("    RASTER LINE %d CORRUPT AT %d (%02X instead of FF!)\n",
	       y + 128, x, data[x]);
    }

    for (y = 0; y < 64; y ++)
    {
      cupsRasterReadPixels(r, data, header.cupsBytesPerLine);

      for (x = 0; x < header.cupsBytesPerLine; x ++)
        if (data[x] != ((x / 4) & 255))
	  break;

      if (x < header.cupsBytesPerLine)
        printf("    RASTER LINE %d CORRUPT AT %d (%02X instead of %02X!)\n",
	       y + 192, x, data[x], (x / 4) & 255);
    }
  }

  cupsRasterClose(r);
  fclose(fp);

  return (0);
}


/*
 * End of "$Id: testraster.c 4545 2005-06-21 19:26:28Z mike $".
 */