From 123979a9db0cec406d710e3b89f351f2e98cb686 Mon Sep 17 00:00:00 2001 From: Michael R Sweet Date: Thu, 18 Jan 2018 17:20:18 -0500 Subject: Move libcupsimage to the "cups" subdirectory, along with its unit tests and benchmark. --- filter/Dependencies | 40 +- filter/Makefile | 171 +--- filter/api-raster.header | 35 - filter/api-raster.shtml | 158 ---- filter/error.c | 266 ------ filter/interpret.c | 1719 ---------------------------------- filter/libcupsimage2.def | 14 - filter/libcupsimage_s.exp | 16 - filter/raster.c | 2227 --------------------------------------------- filter/rasterbench.c | 336 ------- filter/testclient.c | 954 ------------------- filter/testraster.c | 1138 ----------------------- 12 files changed, 23 insertions(+), 7051 deletions(-) delete mode 100644 filter/api-raster.header delete mode 100644 filter/api-raster.shtml delete mode 100644 filter/error.c delete mode 100644 filter/interpret.c delete mode 100644 filter/libcupsimage2.def delete mode 100644 filter/libcupsimage_s.exp delete mode 100644 filter/raster.c delete mode 100644 filter/rasterbench.c delete mode 100644 filter/testclient.c delete mode 100644 filter/testraster.c (limited to 'filter') diff --git a/filter/Dependencies b/filter/Dependencies index 80a49b606..3c023d868 100644 --- a/filter/Dependencies +++ b/filter/Dependencies @@ -1,31 +1,17 @@ -error.o: error.c ../cups/raster-private.h ../cups/raster.h ../cups/cups.h \ - ../cups/file.h ../cups/versioning.h ../cups/ipp.h ../cups/http.h \ - ../cups/array.h ../cups/language.h ../cups/pwg.h \ - ../cups/debug-private.h ../cups/string-private.h ../config.h -interpret.o: interpret.c ../cups/raster-private.h ../cups/raster.h \ - ../cups/cups.h ../cups/file.h ../cups/versioning.h ../cups/ipp.h \ - ../cups/http.h ../cups/array.h ../cups/language.h ../cups/pwg.h \ - ../cups/debug-private.h ../cups/string-private.h ../config.h \ - ../cups/ppd.h -raster.o: raster.c ../cups/raster-private.h ../cups/raster.h \ - ../cups/cups.h ../cups/file.h ../cups/versioning.h ../cups/ipp.h \ - ../cups/http.h ../cups/array.h ../cups/language.h ../cups/pwg.h \ - ../cups/debug-private.h ../cups/string-private.h ../config.h commandtops.o: commandtops.c ../cups/cups-private.h \ ../cups/string-private.h ../config.h ../cups/debug-private.h \ ../cups/versioning.h ../cups/array-private.h ../cups/array.h \ ../cups/ipp-private.h ../cups/ipp.h ../cups/http.h \ - ../cups/http-private.h ../cups/language.h ../cups/md5-private.h \ - ../cups/language-private.h ../cups/transcode.h ../cups/pwg-private.h \ - ../cups/cups.h ../cups/file.h ../cups/pwg.h ../cups/thread-private.h \ - ../cups/ppd.h ../cups/raster.h ../cups/sidechannel.h + ../cups/http-private.h ../cups/language.h ../cups/language-private.h \ + ../cups/transcode.h ../cups/pwg-private.h ../cups/cups.h \ + ../cups/file.h ../cups/pwg.h ../cups/thread-private.h ../cups/ppd.h \ + ../cups/raster.h ../cups/sidechannel.h gziptoany.o: gziptoany.c ../cups/cups-private.h ../cups/string-private.h \ ../config.h ../cups/debug-private.h ../cups/versioning.h \ ../cups/array-private.h ../cups/array.h ../cups/ipp-private.h \ ../cups/ipp.h ../cups/http.h ../cups/http-private.h ../cups/language.h \ - ../cups/md5-private.h ../cups/language-private.h ../cups/transcode.h \ - ../cups/pwg-private.h ../cups/cups.h ../cups/file.h ../cups/pwg.h \ - ../cups/thread-private.h + ../cups/language-private.h ../cups/transcode.h ../cups/pwg-private.h \ + ../cups/cups.h ../cups/file.h ../cups/pwg.h ../cups/thread-private.h common.o: common.c common.h ../cups/string-private.h ../config.h \ ../cups/cups.h ../cups/file.h ../cups/versioning.h ../cups/ipp.h \ ../cups/http.h ../cups/array.h ../cups/language.h ../cups/pwg.h \ @@ -35,9 +21,6 @@ pstops.o: pstops.c common.h ../cups/string-private.h ../config.h \ ../cups/http.h ../cups/array.h ../cups/language.h ../cups/pwg.h \ ../cups/ppd.h ../cups/raster.h ../cups/language-private.h \ ../cups/transcode.h -rasterbench.o: rasterbench.c ../config.h ../cups/raster.h ../cups/cups.h \ - ../cups/file.h ../cups/versioning.h ../cups/ipp.h ../cups/http.h \ - ../cups/array.h ../cups/language.h ../cups/pwg.h rastertoepson.o: rastertoepson.c ../cups/cups.h ../cups/file.h \ ../cups/versioning.h ../cups/ipp.h ../cups/http.h ../cups/array.h \ ../cups/language.h ../cups/pwg.h ../cups/ppd.h ../cups/raster.h \ @@ -57,12 +40,7 @@ rastertopwg.o: rastertopwg.c ../cups/cups-private.h \ ../cups/string-private.h ../config.h ../cups/debug-private.h \ ../cups/versioning.h ../cups/array-private.h ../cups/array.h \ ../cups/ipp-private.h ../cups/ipp.h ../cups/http.h \ - ../cups/http-private.h ../cups/language.h ../cups/md5-private.h \ - ../cups/language-private.h ../cups/transcode.h ../cups/pwg-private.h \ - ../cups/cups.h ../cups/file.h ../cups/pwg.h ../cups/thread-private.h \ + ../cups/http-private.h ../cups/language.h ../cups/language-private.h \ + ../cups/transcode.h ../cups/pwg-private.h ../cups/cups.h \ + ../cups/file.h ../cups/pwg.h ../cups/thread-private.h \ ../cups/ppd-private.h ../cups/ppd.h ../cups/raster.h -testraster.o: testraster.c ../cups/raster-private.h ../cups/raster.h \ - ../cups/cups.h ../cups/file.h ../cups/versioning.h ../cups/ipp.h \ - ../cups/http.h ../cups/array.h ../cups/language.h ../cups/pwg.h \ - ../cups/debug-private.h ../cups/string-private.h ../config.h \ - ../cups/ppd.h diff --git a/filter/Makefile b/filter/Makefile index 9b1680995..4f67e20e9 100644 --- a/filter/Makefile +++ b/filter/Makefile @@ -1,16 +1,17 @@ # # Filter makefile for CUPS. # -# Copyright 2007-2017 by Apple Inc. +# Copyright 2007-2018 by Apple Inc. # Copyright 1997-2006 by Easy Software Products. # -# Licensed under Apache License v2.0. See the file "LICENSE" for more information. +# Licensed under Apache License v2.0. See the file "LICENSE" for more +# information. # include ../Makedefs -FILTERS = \ +TARGETS = \ commandtops \ gziptoany \ pstops \ @@ -18,22 +19,10 @@ FILTERS = \ rastertohp \ rastertolabel \ rastertopwg -LIBTARGETS = \ - $(LIBCUPSIMAGE) \ - libcupsimage.a -UNITTARGETS = \ - rasterbench \ - testclient \ - testraster -TARGETS = \ - $(LIBTARGETS) \ - $(FILTERS) -IMAGEOBJS = error.o interpret.o raster.o -OBJS = $(IMAGEOBJS) \ - commandtops.o gziptoany.o common.o pstops.o \ - rasterbench.o rastertoepson.o rastertohp.o rastertolabel.o \ - rastertopwg.o testclient.o testraster.o +OBJS = commandtops.o gziptoany.o common.o pstops.o \ + rastertoepson.o rastertohp.o rastertolabel.o \ + rastertopwg.o # @@ -119,28 +108,7 @@ install-headers: # Install libraries... # -install-libs: $(INSTALLSTATIC) - $(INSTALL_DIR) -m 755 $(LIBDIR) - $(INSTALL_LIB) $(LIBCUPSIMAGE) $(LIBDIR) - -if test $(LIBCUPSIMAGE) = "libcupsimage.so.2" -o $(LIBCUPSIMAGE) = "libcupsimage.sl.2"; then \ - $(RM) $(LIBDIR)/`basename $(LIBCUPSIMAGE) .2`; \ - $(LN) $(LIBCUPSIMAGE) $(LIBDIR)/`basename $(LIBCUPSIMAGE) .2`; \ - fi - -if test $(LIBCUPSIMAGE) = "libcupsimage.2.dylib"; then \ - $(RM) $(LIBDIR)/libcupsimage.dylib; \ - $(LN) $(LIBCUPSIMAGE) $(LIBDIR)/libcupsimage.dylib; \ - fi - if test "x$(SYMROOT)" != "x"; then \ - $(INSTALL_DIR) $(SYMROOT); \ - cp $(LIBCUPSIMAGE) $(SYMROOT); \ - dsymutil $(SYMROOT)/$(LIBCUPSIMAGE); \ - fi - -installstatic: - $(INSTALL_DIR) -m 755 $(LIBDIR) - $(INSTALL_LIB) -m 755 libcupsimage.a $(LIBDIR) - $(RANLIB) $(LIBDIR)/libcupsimage.a - $(CHMOD) 555 $(LIBDIR)/libcupsimage.a +install-libs: # @@ -154,15 +122,6 @@ uninstall: $(RM) $(SERVERBIN)/filter/rastertodymo -$(RMDIR) $(SERVERBIN)/filter -$(RMDIR) $(SERVERBIN) - $(RM) $(LIBDIR)/libcupsimage.2.dylib - $(RM) $(LIBDIR)/libcupsimage.a - $(RM) $(LIBDIR)/libcupsimage.dylib - $(RM) $(LIBDIR)/libcupsimage_s.a - $(RM) $(LIBDIR)/libcupsimage.sl - $(RM) $(LIBDIR)/libcupsimage.sl.2 - $(RM) $(LIBDIR)/libcupsimage.so - $(RM) $(LIBDIR)/libcupsimage.so.2 - -$(RMDIR) $(LIBDIR) # @@ -171,14 +130,6 @@ uninstall: apihelp: echo Generating CUPS API help files... - mxmldoc --section "Programming" --title "Raster API" \ - --css ../doc/cups-printable.css \ - --header api-raster.header --intro api-raster.shtml \ - api-raster.xml \ - ../cups/raster.h interpret.c raster.c \ - >../doc/help/api-raster.html - mxmldoc --tokens help/api-raster.html api-raster.xml >../doc/help/api-raster.tokens - $(RM) api-raster.xml mxmldoc --section "Programming" \ --title "Developing PostScript Printer Drivers" \ --css ../doc/cups-printable.css \ @@ -223,67 +174,6 @@ gziptoany: gziptoany.o ../Makedefs ../cups/$(LIBCUPS) $(LD_CC) $(LDFLAGS) -o $@ gziptoany.o $(LIBZ) $(LIBS) -# -# libcupsimage.so.2, libcupsimage.sl.2 -# - -libcupsimage.so.2 libcupsimage.sl.2: $(IMAGEOBJS) - echo Linking $@... - $(DSO) $(ARCHFLAGS) $(DSOFLAGS) -o $@ $(IMAGEOBJS) $(DSOLIBS) \ - -L../cups $(LINKCUPS) - $(RM) `basename $@ .2` - $(LN) $@ `basename $@ .2` - - -# -# libcupsimage.2.dylib -# - -libcupsimage.2.dylib: $(IMAGEOBJS) $(LIBCUPSIMAGEORDER) - echo Linking $@... - $(DSO) $(ARCHFLAGS) $(DSOFLAGS) -o $@ \ - -install_name $(libdir)/$@ \ - -current_version 2.3.0 \ - -compatibility_version 2.0.0 \ - $(IMAGEOBJS) $(DSOLIBS) -L../cups $(LINKCUPS) - $(RM) libcupsimage.dylib - $(LN) $@ libcupsimage.dylib - - -# -# libcupsimage_s.a -# - -libcupsimage_s.a: $(IMAGEOBJS) libcupsimage_s.exp - echo Linking $@... - $(DSO) $(DSOFLAGS) -Wl,-berok,-bexport:libcupsimage_s.exp \ - -o libcupsimage_s.o $(IMAGEOBJS) $(DSOLIBS) - $(RM) $@ - $(AR) $(ARFLAGS) $@ libcupsimage_s.o - - -# -# libcupsimage.la -# - -libcupsimage.la: $(IMAGEOBJS) - echo Linking $@... - $(DSO) $(ARCHFLAGS) $(DSOFLAGS) -o $@ $(IMAGEOBJS:.o=.lo) $(DSOLIBS) \ - -L../cups $(LINKCUPS) \ - -rpath $(LIBDIR) -version-info 2:3 - - -# -# libcupsimage.a -# - -libcupsimage.a: $(IMAGEOBJS) - echo Archiving $@... - $(RM) $@ - $(AR) $(ARFLAGS) $@ $(IMAGEOBJS) - $(RANLIB) $@ - - # # pstops # @@ -297,7 +187,7 @@ pstops: pstops.o common.o ../cups/$(LIBCUPS) # rastertoepson # -rastertoepson: rastertoepson.o ../cups/$(LIBCUPS) $(LIBCUPSIMAGE) +rastertoepson: rastertoepson.o ../cups/$(LIBCUPS) ../cups/$(LIBCUPSIMAGE) echo Linking $@... $(LD_CC) $(LDFLAGS) -o $@ rastertoepson.o $(LINKCUPSIMAGE) $(IMGLIBS) $(LIBS) @@ -306,7 +196,7 @@ rastertoepson: rastertoepson.o ../cups/$(LIBCUPS) $(LIBCUPSIMAGE) # rastertohp # -rastertohp: rastertohp.o ../cups/$(LIBCUPS) $(LIBCUPSIMAGE) +rastertohp: rastertohp.o ../cups/$(LIBCUPS) ../cups/$(LIBCUPSIMAGE) echo Linking $@... $(LD_CC) $(LDFLAGS) -o $@ rastertohp.o $(LINKCUPSIMAGE) $(IMGLIBS) $(LIBS) @@ -315,7 +205,7 @@ rastertohp: rastertohp.o ../cups/$(LIBCUPS) $(LIBCUPSIMAGE) # rastertolabel # -rastertolabel: rastertolabel.o ../cups/$(LIBCUPS) $(LIBCUPSIMAGE) +rastertolabel: rastertolabel.o ../cups/$(LIBCUPS) ../cups/$(LIBCUPSIMAGE) echo Linking $@... $(LD_CC) $(LDFLAGS) -o $@ rastertolabel.o $(LINKCUPSIMAGE) $(IMGLIBS) $(LIBS) @@ -324,50 +214,17 @@ rastertolabel: rastertolabel.o ../cups/$(LIBCUPS) $(LIBCUPSIMAGE) # rastertopwg # -rastertopwg: rastertopwg.o ../cups/$(LIBCUPS) $(LIBCUPSIMAGE) +rastertopwg: rastertopwg.o ../cups/$(LIBCUPS) ../cups/$(LIBCUPSIMAGE) echo Linking $@... $(LD_CC) $(LDFLAGS) -o $@ rastertopwg.o $(LINKCUPSIMAGE) $(IMGLIBS) $(LIBS) -rastertopwg-static: rastertopwg.o ../cups/$(LIBCUPSSTATIC) libcupsimage.a +rastertopwg-static: rastertopwg.o ../cups/$(LIBCUPSSTATIC) ../cups/libcupsimage.a echo Linking $@... - $(LD_CC) $(LDFLAGS) -o $@ rastertopwg.o libcupsimage.a \ + $(LD_CC) $(LDFLAGS) -o $@ rastertopwg.o ../cups/libcupsimage.a \ ../cups/$(LIBCUPSSTATIC) $(IMGLIBS) $(DSOLIBS) $(COMMONLIBS) \ $(SSLLIBS) $(DNSSDLIBS) $(LIBGSSAPI) -# -# testclient (dependency on static libraries is intentional) -# - -testclient: testclient.o ../cups/$(LIBCUPSSTATIC) libcupsimage.a - echo Linking $@... - $(LD_CC) $(LDFLAGS) -o $@ testclient.o \ - libcupsimage.a ../cups/$(LIBCUPSSTATIC) \ - $(LIBGSSAPI) $(SSLLIBS) $(DNSSDLIBS) $(COMMONLIBS) $(LIBZ) - - -# -# testraster -# - -testraster: testraster.o ../cups/$(LIBCUPSSTATIC) libcupsimage.a - echo Linking $@... - $(LD_CC) $(ARCHFLAGS) $(LDFLAGS) -o $@ testraster.o libcupsimage.a \ - ../cups/$(LIBCUPSSTATIC) $(IMGLIBS) $(DSOLIBS) $(COMMONLIBS) \ - $(SSLLIBS) $(DNSSDLIBS) $(LIBGSSAPI) - echo Running raster API tests... - ./testraster - - -# -# rasterbench -# - -rasterbench: rasterbench.o libcupsimage.a - echo Linking $@... - $(LD_CC) $(LDFLAGS) -o $@ rasterbench.o libcupsimage.a $(LIBS) - - # # Dependencies... # diff --git a/filter/api-raster.header b/filter/api-raster.header deleted file mode 100644 index 31b87b156..000000000 --- a/filter/api-raster.header +++ /dev/null @@ -1,35 +0,0 @@ - - -

Raster API

- -
- - - - - - - - - - - - - - - - -
Headercups/raster.h
Library-lcupsimage
See AlsoProgramming: Introduction to CUPS Programming
- Programming: CUPS API
- Programming: PPD API
- References: CUPS PPD Specification
diff --git a/filter/api-raster.shtml b/filter/api-raster.shtml deleted file mode 100644 index 6d458a2ed..000000000 --- a/filter/api-raster.shtml +++ /dev/null @@ -1,158 +0,0 @@ - - -

Overview

- -

The CUPS raster API provides a standard interface for reading and writing -CUPS raster streams which are used for printing to raster printers. Because the -raster format is updated from time to time, it is important to use this API to -avoid incompatibilities with newer versions of CUPS.

- -

Two kinds of CUPS filters use the CUPS raster API - raster image processor -(RIP) filters such as pstoraster and cgpdftoraster -(macOS) that produce CUPS raster files and printer driver filters that -convert CUPS raster files into a format usable by the printer. Printer -driver filters are by far the most common.

- -

CUPS raster files (application/vnd.cups-raster) consists of -a stream of raster page descriptions produced by one of the RIP filters such as -pstoraster, imagetoraster, or -cgpdftoraster. CUPS raster files are referred to using the -cups_raster_t type and are -opened using the cupsRasterOpen -function. For example, to read raster data from the standard input, open -file descriptor 0:

- -
-#include <cups/raster.h>
-
-cups_raster_t *ras = cupsRasterOpen(0, CUPS_RASTER_READ);
-
- -

Each page of data begins with a page dictionary structure called -cups_page_header2_t. This -structure contains the colorspace, bits per color, media size, media type, -hardware resolution, and so forth used for the page.

- -
Note: - -

Do not confuse the colorspace in the page header with the PPD - ColorModel keyword. ColorModel refers to the general type of - color used for a device (Gray, RGB, CMYK, DeviceN) and is often used to - select a particular colorspace for the page header along with the associate - color profile. The page header colorspace (cupsColorSpace) describes - both the type and organization of the color data, for example KCMY (black - first) instead of CMYK and RGBA (RGB + alpha) instead of RGB.

- -
- -

You read the page header using the -cupsRasterReadHeader2 -function:

- -
-#include <cups/raster.h>
-
-cups_raster_t *ras = cupsRasterOpen(0, CUPS_RASTER_READ);
-cups_page_header2_t header;
-
-while (cupsRasterReadHeader2(ras, &header))
-{
-  /* setup this page */
-
-  /* read raster data */
-
-  /* finish this page */
-}
-
- -

After the page dictionary comes the page data which is a full-resolution, -possibly compressed bitmap representing the page in the printer's output -colorspace. You read uncompressed raster data using the -cupsRasterReadPixels -function. A for loop is normally used to read the page one line -at a time:

- -
-#include <cups/raster.h>
-
-cups_raster_t *ras = cupsRasterOpen(0, CUPS_RASTER_READ);
-cups_page_header2_t header;
-int page = 0;
-int y;
-char *buffer;
-
-while (cupsRasterReadHeader2(ras, &header))
-{
-  /* setup this page */
-  page ++;
-  fprintf(stderr, "PAGE: %d %d\n", page, header.NumCopies);
-
-  /* allocate memory for 1 line */
-  buffer = malloc(header.cupsBytesPerLine);
-
-  /* read raster data */
-  for (y = 0; y < header.cupsHeight; y ++)
-  {
-    if (cupsRasterReadPixels(ras, buffer, header.cupsBytesPerLine) == 0)
-      break;
-
-    /* write raster data to printer on stdout */
-  }
-
-  /* finish this page */
-}
-
- -

When you are done reading the raster data, call the -cupsRasterClose function to free -the memory used to read the raster file:

- -
-cups_raster_t *ras;
-
-cupsRasterClose(ras);
-
- - -

Functions by Task

- -

Opening and Closing Raster Streams

- - - -

Reading Raster Streams

- - - -

Writing Raster Streams

- - diff --git a/filter/error.c b/filter/error.c deleted file mode 100644 index 0916a15fe..000000000 --- a/filter/error.c +++ /dev/null @@ -1,266 +0,0 @@ -/* - * Raster error handling for CUPS. - * - * Copyright 2007-2015 by Apple Inc. - * Copyright 2007 by Easy Software Products. - * - * Licensed under Apache License v2.0. See the file "LICENSE" for more information. - */ - -/* - * Include necessary headers... - */ - -#include - - -/* - * Local structures... - */ - -typedef struct _cups_raster_error_s /**** Error buffer structure ****/ -{ - char *start, /* Start of buffer */ - *current, /* Current position in buffer */ - *end; /* End of buffer */ -} _cups_raster_error_t; - - -/* - * Local functions... - */ - -static _cups_raster_error_t *get_error_buffer(void); - - -/* - * '_cupsRasterAddError()' - Add an error message to the error buffer. - */ - -void -_cupsRasterAddError(const char *f, /* I - Printf-style error message */ - ...) /* I - Additional arguments as needed */ -{ - _cups_raster_error_t *buf = get_error_buffer(); - /* Error buffer */ - va_list ap; /* Pointer to additional arguments */ - char s[2048]; /* Message string */ - ssize_t bytes; /* Bytes in message string */ - - - DEBUG_printf(("_cupsRasterAddError(f=\"%s\", ...)", f)); - - va_start(ap, f); - bytes = vsnprintf(s, sizeof(s), f, ap); - va_end(ap); - - if (bytes <= 0) - return; - - DEBUG_printf(("1_cupsRasterAddError: %s", s)); - - bytes ++; - - if ((size_t)bytes >= sizeof(s)) - return; - - if (bytes > (ssize_t)(buf->end - buf->current)) - { - /* - * Allocate more memory... - */ - - char *temp; /* New buffer */ - size_t size; /* Size of buffer */ - - - size = (size_t)(buf->end - buf->start + 2 * bytes + 1024); - - if (buf->start) - temp = realloc(buf->start, size); - else - temp = malloc(size); - - if (!temp) - return; - - /* - * Update pointers... - */ - - buf->end = temp + size; - buf->current = temp + (buf->current - buf->start); - buf->start = temp; - } - - /* - * Append the message to the end of the current string... - */ - - memcpy(buf->current, s, (size_t)bytes); - buf->current += bytes - 1; -} - - -/* - * '_cupsRasterClearError()' - Clear the error buffer. - */ - -void -_cupsRasterClearError(void) -{ - _cups_raster_error_t *buf = get_error_buffer(); - /* Error buffer */ - - - buf->current = buf->start; - - if (buf->start) - *(buf->start) = '\0'; -} - - -/* - * 'cupsRasterErrorString()' - Return the last error from a raster function. - * - * If there are no recent errors, NULL is returned. - * - * @since CUPS 1.3/macOS 10.5@ - */ - -const char * /* O - Last error */ -cupsRasterErrorString(void) -{ - _cups_raster_error_t *buf = get_error_buffer(); - /* Error buffer */ - - - if (buf->current == buf->start) - return (NULL); - else - return (buf->start); -} - - -#ifdef HAVE_PTHREAD_H -/* - * Implement per-thread globals... - */ - -# include - - -/* - * Local globals... - */ - -static pthread_key_t raster_key = 0; /* Thread local storage key */ -static pthread_once_t raster_key_once = PTHREAD_ONCE_INIT; - /* One-time initialization object */ - - -/* - * Local functions... - */ - -static void raster_init(void); -static void raster_destructor(void *value); - - -/* - * 'get_error_buffer()' - Return a pointer to thread local storage. - */ - -_cups_raster_error_t * /* O - Pointer to error buffer */ -get_error_buffer(void) -{ - _cups_raster_error_t *buf; /* Pointer to error buffer */ - - - /* - * Initialize the global data exactly once... - */ - - DEBUG_puts("3get_error_buffer()"); - - pthread_once(&raster_key_once, raster_init); - - /* - * See if we have allocated the data yet... - */ - - if ((buf = (_cups_raster_error_t *)pthread_getspecific(raster_key)) - == NULL) - { - DEBUG_puts("4get_error_buffer: allocating memory for thread."); - - /* - * No, allocate memory as set the pointer for the key... - */ - - buf = calloc(1, sizeof(_cups_raster_error_t)); - pthread_setspecific(raster_key, buf); - - DEBUG_printf(("4get_error_buffer: buf=%p", (void *)buf)); - } - - /* - * Return the pointer to the data... - */ - - return (buf); -} - - -/* - * 'raster_init()' - Initialize error buffer once. - */ - -static void -raster_init(void) -{ - pthread_key_create(&raster_key, raster_destructor); - - DEBUG_printf(("3raster_init(): raster_key=%x(%u)", (unsigned)raster_key, (unsigned)raster_key)); -} - - -/* - * 'raster_destructor()' - Free memory allocated by get_error_buffer(). - */ - -static void -raster_destructor(void *value) /* I - Data to free */ -{ - _cups_raster_error_t *buf = (_cups_raster_error_t *)value; - /* Error buffer */ - - - DEBUG_printf(("3raster_destructor(value=%p)", value)); - - if (buf->start) - free(buf->start); - - free(value); -} - - -#else -/* - * Implement static globals... - */ - -/* - * 'get_error_buffer()' - Return a pointer to thread local storage. - */ - -_cups_raster_error_t * /* O - Pointer to error buffer */ -get_error_buffer(void) -{ - static _cups_raster_error_t buf = { 0, 0, 0 }; - /* Error buffer */ - - - return (&buf); -} -#endif /* HAVE_PTHREAD_H */ diff --git a/filter/interpret.c b/filter/interpret.c deleted file mode 100644 index bb3de3004..000000000 --- a/filter/interpret.c +++ /dev/null @@ -1,1719 +0,0 @@ -/* - * PPD command interpreter for CUPS. - * - * Copyright 2007-2015 by Apple Inc. - * Copyright 1993-2007 by Easy Software Products. - * - * Licensed under Apache License v2.0. See the file "LICENSE" for more information. - */ - -/* - * Include necessary headers... - */ - -#include -#include - - -/* - * Stack values for the PostScript mini-interpreter... - */ - -typedef enum -{ - CUPS_PS_NAME, - CUPS_PS_NUMBER, - CUPS_PS_STRING, - CUPS_PS_BOOLEAN, - CUPS_PS_NULL, - CUPS_PS_START_ARRAY, - CUPS_PS_END_ARRAY, - CUPS_PS_START_DICT, - CUPS_PS_END_DICT, - CUPS_PS_START_PROC, - CUPS_PS_END_PROC, - CUPS_PS_CLEARTOMARK, - CUPS_PS_COPY, - CUPS_PS_DUP, - CUPS_PS_INDEX, - CUPS_PS_POP, - CUPS_PS_ROLL, - CUPS_PS_SETPAGEDEVICE, - CUPS_PS_STOPPED, - CUPS_PS_OTHER -} _cups_ps_type_t; - -typedef struct -{ - _cups_ps_type_t type; /* Object type */ - union - { - int boolean; /* Boolean value */ - char name[64]; /* Name value */ - double number; /* Number value */ - char other[64]; /* Other operator */ - char string[64]; /* Sring value */ - } value; /* Value */ -} _cups_ps_obj_t; - -typedef struct -{ - int num_objs, /* Number of objects on stack */ - alloc_objs; /* Number of allocated objects */ - _cups_ps_obj_t *objs; /* Objects in stack */ -} _cups_ps_stack_t; - - -/* - * Local functions... - */ - -static int cleartomark_stack(_cups_ps_stack_t *st); -static int copy_stack(_cups_ps_stack_t *st, int count); -static void delete_stack(_cups_ps_stack_t *st); -static void error_object(_cups_ps_obj_t *obj); -static void error_stack(_cups_ps_stack_t *st, const char *title); -static _cups_ps_obj_t *index_stack(_cups_ps_stack_t *st, int n); -static _cups_ps_stack_t *new_stack(void); -static _cups_ps_obj_t *pop_stack(_cups_ps_stack_t *st); -static _cups_ps_obj_t *push_stack(_cups_ps_stack_t *st, - _cups_ps_obj_t *obj); -static int roll_stack(_cups_ps_stack_t *st, int c, int s); -static _cups_ps_obj_t *scan_ps(_cups_ps_stack_t *st, char **ptr); -static int setpagedevice(_cups_ps_stack_t *st, - cups_page_header2_t *h, - int *preferred_bits); -#ifdef DEBUG -static void DEBUG_object(const char *prefix, _cups_ps_obj_t *obj); -static void DEBUG_stack(const char *prefix, _cups_ps_stack_t *st); -#endif /* DEBUG */ - - -/* - * 'cupsRasterInterpretPPD()' - Interpret PPD commands to create a page header. - * - * This function is used by raster image processing (RIP) filters like - * cgpdftoraster and imagetoraster when writing CUPS raster data for a page. - * It is not used by raster printer driver filters which only read CUPS - * raster data. - * - * - * @code cupsRasterInterpretPPD@ does not mark the options in the PPD using - * the "num_options" and "options" arguments. Instead, mark the options with - * @code cupsMarkOptions@ and @code ppdMarkOption@ prior to calling it - - * this allows for per-page options without manipulating the options array. - * - * The "func" argument specifies an optional callback function that is - * called prior to the computation of the final raster data. The function - * can make changes to the @link cups_page_header2_t@ data as needed to use a - * supported raster format and then returns 0 on success and -1 if the - * requested attributes cannot be supported. - * - * - * @code cupsRasterInterpretPPD@ supports a subset of the PostScript language. - * Currently only the @code [@, @code ]@, @code <<@, @code >>@, @code {@, - * @code }@, @code cleartomark@, @code copy@, @code dup@, @code index@, - * @code pop@, @code roll@, @code setpagedevice@, and @code stopped@ operators - * are supported. - * - * @since CUPS 1.2/macOS 10.5@ - */ - -int /* O - 0 on success, -1 on failure */ -cupsRasterInterpretPPD( - cups_page_header2_t *h, /* O - Page header to create */ - ppd_file_t *ppd, /* I - PPD file */ - int num_options, /* I - Number of options */ - cups_option_t *options, /* I - Options */ - cups_interpret_cb_t func) /* I - Optional page header callback (@code NULL@ for none) */ -{ - int status; /* Cummulative status */ - char *code; /* Code to run */ - const char *val; /* Option value */ - ppd_size_t *size; /* Current size */ - float left, /* Left position */ - bottom, /* Bottom position */ - right, /* Right position */ - top, /* Top position */ - temp1, temp2; /* Temporary variables for swapping */ - int preferred_bits; /* Preferred bits per color */ - - - /* - * Range check input... - */ - - _cupsRasterClearError(); - - if (!h) - { - _cupsRasterAddError("Page header cannot be NULL!\n"); - return (-1); - } - - /* - * Reset the page header to the defaults... - */ - - memset(h, 0, sizeof(cups_page_header2_t)); - - h->NumCopies = 1; - h->PageSize[0] = 612; - h->PageSize[1] = 792; - h->HWResolution[0] = 100; - h->HWResolution[1] = 100; - h->cupsBitsPerColor = 1; - h->cupsColorOrder = CUPS_ORDER_CHUNKED; - h->cupsColorSpace = CUPS_CSPACE_K; - h->cupsBorderlessScalingFactor = 1.0f; - h->cupsPageSize[0] = 612.0f; - h->cupsPageSize[1] = 792.0f; - h->cupsImagingBBox[0] = 0.0f; - h->cupsImagingBBox[1] = 0.0f; - h->cupsImagingBBox[2] = 612.0f; - h->cupsImagingBBox[3] = 792.0f; - - strlcpy(h->cupsPageSizeName, "Letter", sizeof(h->cupsPageSizeName)); - -#ifdef __APPLE__ - /* - * cupsInteger0 is also used for the total page count on macOS; set an - * uncommon default value so we can tell if the driver is using cupsInteger0. - */ - - h->cupsInteger[0] = 0x80000000; -#endif /* __APPLE__ */ - - /* - * Apply patches and options to the page header... - */ - - status = 0; - preferred_bits = 0; - - if (ppd) - { - /* - * Apply any patch code (used to override the defaults...) - */ - - if (ppd->patches) - status |= _cupsRasterExecPS(h, &preferred_bits, ppd->patches); - - /* - * Then apply printer options in the proper order... - */ - - if ((code = ppdEmitString(ppd, PPD_ORDER_DOCUMENT, 0.0)) != NULL) - { - status |= _cupsRasterExecPS(h, &preferred_bits, code); - free(code); - } - - if ((code = ppdEmitString(ppd, PPD_ORDER_ANY, 0.0)) != NULL) - { - status |= _cupsRasterExecPS(h, &preferred_bits, code); - free(code); - } - - if ((code = ppdEmitString(ppd, PPD_ORDER_PROLOG, 0.0)) != NULL) - { - status |= _cupsRasterExecPS(h, &preferred_bits, code); - free(code); - } - - if ((code = ppdEmitString(ppd, PPD_ORDER_PAGE, 0.0)) != NULL) - { - status |= _cupsRasterExecPS(h, &preferred_bits, code); - free(code); - } - } - - /* - * Allow option override for page scaling... - */ - - if ((val = cupsGetOption("cupsBorderlessScalingFactor", num_options, - options)) != NULL) - { - double sc = atof(val); /* Scale factor */ - - if (sc >= 0.1 && sc <= 2.0) - h->cupsBorderlessScalingFactor = (float)sc; - } - - /* - * Get the margins for the current size... - */ - - if ((size = ppdPageSize(ppd, NULL)) != NULL) - { - /* - * Use the margins from the PPD file... - */ - - left = size->left; - bottom = size->bottom; - right = size->right; - top = size->top; - - strlcpy(h->cupsPageSizeName, size->name, sizeof(h->cupsPageSizeName)); - - h->cupsPageSize[0] = size->width; - h->cupsPageSize[1] = size->length; - } - else - { - /* - * Use the default margins... - */ - - left = 0.0f; - bottom = 0.0f; - right = 612.0f; - top = 792.0f; - } - - /* - * Handle orientation... - */ - - switch (h->Orientation) - { - case CUPS_ORIENT_0 : - default : - /* Do nothing */ - break; - - case CUPS_ORIENT_90 : - temp1 = h->cupsPageSize[0]; - h->cupsPageSize[0] = h->cupsPageSize[1]; - h->cupsPageSize[1] = temp1; - - temp1 = left; - temp2 = right; - left = h->cupsPageSize[0] - top; - right = h->cupsPageSize[0] - bottom; - bottom = h->cupsPageSize[1] - temp1; - top = h->cupsPageSize[1] - temp2; - break; - - case CUPS_ORIENT_180 : - temp1 = left; - temp2 = bottom; - left = h->cupsPageSize[0] - right; - right = h->cupsPageSize[0] - temp1; - bottom = h->cupsPageSize[1] - top; - top = h->cupsPageSize[1] - temp2; - break; - - case CUPS_ORIENT_270 : - temp1 = h->cupsPageSize[0]; - h->cupsPageSize[0] = h->cupsPageSize[1]; - h->cupsPageSize[1] = temp1; - - temp1 = left; - temp2 = right; - left = bottom; - right = top; - bottom = h->cupsPageSize[1] - temp2; - top = h->cupsPageSize[1] - temp1; - break; - } - - if (left > right) - { - temp1 = left; - left = right; - right = temp1; - } - - if (bottom > top) - { - temp1 = bottom; - bottom = top; - top = temp1; - } - - h->PageSize[0] = (unsigned)(h->cupsPageSize[0] * - h->cupsBorderlessScalingFactor); - h->PageSize[1] = (unsigned)(h->cupsPageSize[1] * - h->cupsBorderlessScalingFactor); - h->Margins[0] = (unsigned)(left * - h->cupsBorderlessScalingFactor); - h->Margins[1] = (unsigned)(bottom * - h->cupsBorderlessScalingFactor); - h->ImagingBoundingBox[0] = (unsigned)(left * - h->cupsBorderlessScalingFactor); - h->ImagingBoundingBox[1] = (unsigned)(bottom * - h->cupsBorderlessScalingFactor); - h->ImagingBoundingBox[2] = (unsigned)(right * - h->cupsBorderlessScalingFactor); - h->ImagingBoundingBox[3] = (unsigned)(top * - h->cupsBorderlessScalingFactor); - h->cupsImagingBBox[0] = (float)left; - h->cupsImagingBBox[1] = (float)bottom; - h->cupsImagingBBox[2] = (float)right; - h->cupsImagingBBox[3] = (float)top; - - /* - * Use the callback to validate the page header... - */ - - if (func && (*func)(h, preferred_bits)) - { - _cupsRasterAddError("Page header callback returned error.\n"); - return (-1); - } - - /* - * Check parameters... - */ - - if (!h->HWResolution[0] || !h->HWResolution[1] || - !h->PageSize[0] || !h->PageSize[1] || - (h->cupsBitsPerColor != 1 && h->cupsBitsPerColor != 2 && - h->cupsBitsPerColor != 4 && h->cupsBitsPerColor != 8 && - h->cupsBitsPerColor != 16) || - h->cupsBorderlessScalingFactor < 0.1 || - h->cupsBorderlessScalingFactor > 2.0) - { - _cupsRasterAddError("Page header uses unsupported values.\n"); - return (-1); - } - - /* - * Compute the bitmap parameters... - */ - - h->cupsWidth = (unsigned)((right - left) * h->cupsBorderlessScalingFactor * - h->HWResolution[0] / 72.0f + 0.5f); - h->cupsHeight = (unsigned)((top - bottom) * h->cupsBorderlessScalingFactor * - h->HWResolution[1] / 72.0f + 0.5f); - - switch (h->cupsColorSpace) - { - case CUPS_CSPACE_W : - case CUPS_CSPACE_K : - case CUPS_CSPACE_WHITE : - case CUPS_CSPACE_GOLD : - case CUPS_CSPACE_SILVER : - case CUPS_CSPACE_SW : - h->cupsNumColors = 1; - h->cupsBitsPerPixel = h->cupsBitsPerColor; - break; - - default : - /* - * Ensure that colorimetric colorspaces use at least 8 bits per - * component... - */ - - if (h->cupsColorSpace >= CUPS_CSPACE_CIEXYZ && - h->cupsBitsPerColor < 8) - h->cupsBitsPerColor = 8; - - /* - * Figure out the number of bits per pixel... - */ - - if (h->cupsColorOrder == CUPS_ORDER_CHUNKED) - { - if (h->cupsBitsPerColor >= 8) - h->cupsBitsPerPixel = h->cupsBitsPerColor * 3; - else - h->cupsBitsPerPixel = h->cupsBitsPerColor * 4; - } - else - h->cupsBitsPerPixel = h->cupsBitsPerColor; - - h->cupsNumColors = 3; - break; - - case CUPS_CSPACE_KCMYcm : - if (h->cupsBitsPerColor == 1) - { - if (h->cupsColorOrder == CUPS_ORDER_CHUNKED) - h->cupsBitsPerPixel = 8; - else - h->cupsBitsPerPixel = 1; - - h->cupsNumColors = 6; - break; - } - - /* - * Fall through to CMYK code... - */ - - case CUPS_CSPACE_RGBA : - case CUPS_CSPACE_RGBW : - case CUPS_CSPACE_CMYK : - case CUPS_CSPACE_YMCK : - case CUPS_CSPACE_KCMY : - case CUPS_CSPACE_GMCK : - case CUPS_CSPACE_GMCS : - if (h->cupsColorOrder == CUPS_ORDER_CHUNKED) - h->cupsBitsPerPixel = h->cupsBitsPerColor * 4; - else - h->cupsBitsPerPixel = h->cupsBitsPerColor; - - h->cupsNumColors = 4; - break; - - case CUPS_CSPACE_DEVICE1 : - case CUPS_CSPACE_DEVICE2 : - case CUPS_CSPACE_DEVICE3 : - case CUPS_CSPACE_DEVICE4 : - case CUPS_CSPACE_DEVICE5 : - case CUPS_CSPACE_DEVICE6 : - case CUPS_CSPACE_DEVICE7 : - case CUPS_CSPACE_DEVICE8 : - case CUPS_CSPACE_DEVICE9 : - case CUPS_CSPACE_DEVICEA : - case CUPS_CSPACE_DEVICEB : - case CUPS_CSPACE_DEVICEC : - case CUPS_CSPACE_DEVICED : - case CUPS_CSPACE_DEVICEE : - case CUPS_CSPACE_DEVICEF : - h->cupsNumColors = h->cupsColorSpace - CUPS_CSPACE_DEVICE1 + 1; - - if (h->cupsColorOrder == CUPS_ORDER_CHUNKED) - h->cupsBitsPerPixel = h->cupsBitsPerColor * h->cupsNumColors; - else - h->cupsBitsPerPixel = h->cupsBitsPerColor; - break; - } - - h->cupsBytesPerLine = (h->cupsBitsPerPixel * h->cupsWidth + 7) / 8; - - if (h->cupsColorOrder == CUPS_ORDER_BANDED) - h->cupsBytesPerLine *= h->cupsNumColors; - - return (status); -} - - -/* - * '_cupsRasterExecPS()' - Execute PostScript code to initialize a page header. - */ - -int /* O - 0 on success, -1 on error */ -_cupsRasterExecPS( - cups_page_header2_t *h, /* O - Page header */ - int *preferred_bits,/* O - Preferred bits per color */ - const char *code) /* I - PS code to execute */ -{ - int error = 0; /* Error condition? */ - _cups_ps_stack_t *st; /* PostScript value stack */ - _cups_ps_obj_t *obj; /* Object from top of stack */ - char *codecopy, /* Copy of code */ - *codeptr; /* Pointer into copy of code */ - - - DEBUG_printf(("_cupsRasterExecPS(h=%p, preferred_bits=%p, code=\"%s\")\n", - h, preferred_bits, code)); - - /* - * Copy the PostScript code and create a stack... - */ - - if ((codecopy = strdup(code)) == NULL) - { - _cupsRasterAddError("Unable to duplicate code string.\n"); - return (-1); - } - - if ((st = new_stack()) == NULL) - { - _cupsRasterAddError("Unable to create stack.\n"); - free(codecopy); - return (-1); - } - - /* - * Parse the PS string until we run out of data... - */ - - codeptr = codecopy; - - while ((obj = scan_ps(st, &codeptr)) != NULL) - { -#ifdef DEBUG - DEBUG_printf(("_cupsRasterExecPS: Stack (%d objects)", st->num_objs)); - DEBUG_object("_cupsRasterExecPS", obj); -#endif /* DEBUG */ - - switch (obj->type) - { - default : - /* Do nothing for regular values */ - break; - - case CUPS_PS_CLEARTOMARK : - pop_stack(st); - - if (cleartomark_stack(st)) - _cupsRasterAddError("cleartomark: Stack underflow.\n"); - -#ifdef DEBUG - DEBUG_puts("1_cupsRasterExecPS: dup"); - DEBUG_stack("_cupsRasterExecPS", st); -#endif /* DEBUG */ - break; - - case CUPS_PS_COPY : - pop_stack(st); - if ((obj = pop_stack(st)) != NULL) - { - copy_stack(st, (int)obj->value.number); - -#ifdef DEBUG - DEBUG_puts("_cupsRasterExecPS: copy"); - DEBUG_stack("_cupsRasterExecPS", st); -#endif /* DEBUG */ - } - break; - - case CUPS_PS_DUP : - pop_stack(st); - copy_stack(st, 1); - -#ifdef DEBUG - DEBUG_puts("_cupsRasterExecPS: dup"); - DEBUG_stack("_cupsRasterExecPS", st); -#endif /* DEBUG */ - break; - - case CUPS_PS_INDEX : - pop_stack(st); - if ((obj = pop_stack(st)) != NULL) - { - index_stack(st, (int)obj->value.number); - -#ifdef DEBUG - DEBUG_puts("_cupsRasterExecPS: index"); - DEBUG_stack("_cupsRasterExecPS", st); -#endif /* DEBUG */ - } - break; - - case CUPS_PS_POP : - pop_stack(st); - pop_stack(st); - -#ifdef DEBUG - DEBUG_puts("_cupsRasterExecPS: pop"); - DEBUG_stack("_cupsRasterExecPS", st); -#endif /* DEBUG */ - break; - - case CUPS_PS_ROLL : - pop_stack(st); - if ((obj = pop_stack(st)) != NULL) - { - int c; /* Count */ - - - c = (int)obj->value.number; - - if ((obj = pop_stack(st)) != NULL) - { - roll_stack(st, (int)obj->value.number, c); - -#ifdef DEBUG - DEBUG_puts("_cupsRasterExecPS: roll"); - DEBUG_stack("_cupsRasterExecPS", st); -#endif /* DEBUG */ - } - } - break; - - case CUPS_PS_SETPAGEDEVICE : - pop_stack(st); - setpagedevice(st, h, preferred_bits); - -#ifdef DEBUG - DEBUG_puts("_cupsRasterExecPS: setpagedevice"); - DEBUG_stack("_cupsRasterExecPS", st); -#endif /* DEBUG */ - break; - - case CUPS_PS_START_PROC : - case CUPS_PS_END_PROC : - case CUPS_PS_STOPPED : - pop_stack(st); - break; - - case CUPS_PS_OTHER : - _cupsRasterAddError("Unknown operator \"%s\".\n", obj->value.other); - error = 1; - DEBUG_printf(("_cupsRasterExecPS: Unknown operator \"%s\".", obj->value.other)); - break; - } - - if (error) - break; - } - - /* - * Cleanup... - */ - - free(codecopy); - - if (st->num_objs > 0) - { - error_stack(st, "Stack not empty:"); - -#ifdef DEBUG - DEBUG_puts("_cupsRasterExecPS: Stack not empty"); - DEBUG_stack("_cupsRasterExecPS", st); -#endif /* DEBUG */ - - delete_stack(st); - - return (-1); - } - - delete_stack(st); - - /* - * Return success... - */ - - return (0); -} - - -/* - * 'cleartomark_stack()' - Clear to the last mark ([) on the stack. - */ - -static int /* O - 0 on success, -1 on error */ -cleartomark_stack(_cups_ps_stack_t *st) /* I - Stack */ -{ - _cups_ps_obj_t *obj; /* Current object on stack */ - - - while ((obj = pop_stack(st)) != NULL) - if (obj->type == CUPS_PS_START_ARRAY) - break; - - return (obj ? 0 : -1); -} - - -/* - * 'copy_stack()' - Copy the top N stack objects. - */ - -static int /* O - 0 on success, -1 on error */ -copy_stack(_cups_ps_stack_t *st, /* I - Stack */ - int c) /* I - Number of objects to copy */ -{ - int n; /* Index */ - - - if (c < 0) - return (-1); - else if (c == 0) - return (0); - - if ((n = st->num_objs - c) < 0) - return (-1); - - while (c > 0) - { - if (!push_stack(st, st->objs + n)) - return (-1); - - n ++; - c --; - } - - return (0); -} - - -/* - * 'delete_stack()' - Free memory used by a stack. - */ - -static void -delete_stack(_cups_ps_stack_t *st) /* I - Stack */ -{ - free(st->objs); - free(st); -} - - -/* - * 'error_object()' - Add an object's value to the current error message. - */ - -static void -error_object(_cups_ps_obj_t *obj) /* I - Object to add */ -{ - switch (obj->type) - { - case CUPS_PS_NAME : - _cupsRasterAddError(" /%s", obj->value.name); - break; - - case CUPS_PS_NUMBER : - _cupsRasterAddError(" %g", obj->value.number); - break; - - case CUPS_PS_STRING : - _cupsRasterAddError(" (%s)", obj->value.string); - break; - - case CUPS_PS_BOOLEAN : - if (obj->value.boolean) - _cupsRasterAddError(" true"); - else - _cupsRasterAddError(" false"); - break; - - case CUPS_PS_NULL : - _cupsRasterAddError(" null"); - break; - - case CUPS_PS_START_ARRAY : - _cupsRasterAddError(" ["); - break; - - case CUPS_PS_END_ARRAY : - _cupsRasterAddError(" ]"); - break; - - case CUPS_PS_START_DICT : - _cupsRasterAddError(" <<"); - break; - - case CUPS_PS_END_DICT : - _cupsRasterAddError(" >>"); - break; - - case CUPS_PS_START_PROC : - _cupsRasterAddError(" {"); - break; - - case CUPS_PS_END_PROC : - _cupsRasterAddError(" }"); - break; - - case CUPS_PS_COPY : - _cupsRasterAddError(" --copy--"); - break; - - case CUPS_PS_CLEARTOMARK : - _cupsRasterAddError(" --cleartomark--"); - break; - - case CUPS_PS_DUP : - _cupsRasterAddError(" --dup--"); - break; - - case CUPS_PS_INDEX : - _cupsRasterAddError(" --index--"); - break; - - case CUPS_PS_POP : - _cupsRasterAddError(" --pop--"); - break; - - case CUPS_PS_ROLL : - _cupsRasterAddError(" --roll--"); - break; - - case CUPS_PS_SETPAGEDEVICE : - _cupsRasterAddError(" --setpagedevice--"); - break; - - case CUPS_PS_STOPPED : - _cupsRasterAddError(" --stopped--"); - break; - - case CUPS_PS_OTHER : - _cupsRasterAddError(" --%s--", obj->value.other); - break; - } -} - - -/* - * 'error_stack()' - Add a stack to the current error message... - */ - -static void -error_stack(_cups_ps_stack_t *st, /* I - Stack */ - const char *title) /* I - Title string */ -{ - int c; /* Looping var */ - _cups_ps_obj_t *obj; /* Current object on stack */ - - - _cupsRasterAddError("%s", title); - - for (obj = st->objs, c = st->num_objs; c > 0; c --, obj ++) - error_object(obj); - - _cupsRasterAddError("\n"); -} - - -/* - * 'index_stack()' - Copy the Nth value on the stack. - */ - -static _cups_ps_obj_t * /* O - New object */ -index_stack(_cups_ps_stack_t *st, /* I - Stack */ - int n) /* I - Object index */ -{ - if (n < 0 || (n = st->num_objs - n - 1) < 0) - return (NULL); - - return (push_stack(st, st->objs + n)); -} - - -/* - * 'new_stack()' - Create a new stack. - */ - -static _cups_ps_stack_t * /* O - New stack */ -new_stack(void) -{ - _cups_ps_stack_t *st; /* New stack */ - - - if ((st = calloc(1, sizeof(_cups_ps_stack_t))) == NULL) - return (NULL); - - st->alloc_objs = 32; - - if ((st->objs = calloc(32, sizeof(_cups_ps_obj_t))) == NULL) - { - free(st); - return (NULL); - } - else - return (st); -} - - -/* - * 'pop_stock()' - Pop the top object off the stack. - */ - -static _cups_ps_obj_t * /* O - Object */ -pop_stack(_cups_ps_stack_t *st) /* I - Stack */ -{ - if (st->num_objs > 0) - { - st->num_objs --; - - return (st->objs + st->num_objs); - } - else - return (NULL); -} - - -/* - * 'push_stack()' - Push an object on the stack. - */ - -static _cups_ps_obj_t * /* O - New object */ -push_stack(_cups_ps_stack_t *st, /* I - Stack */ - _cups_ps_obj_t *obj) /* I - Object */ -{ - _cups_ps_obj_t *temp; /* New object */ - - - if (st->num_objs >= st->alloc_objs) - { - - - st->alloc_objs += 32; - - if ((temp = realloc(st->objs, (size_t)st->alloc_objs * - sizeof(_cups_ps_obj_t))) == NULL) - return (NULL); - - st->objs = temp; - memset(temp + st->num_objs, 0, 32 * sizeof(_cups_ps_obj_t)); - } - - temp = st->objs + st->num_objs; - st->num_objs ++; - - memcpy(temp, obj, sizeof(_cups_ps_obj_t)); - - return (temp); -} - - -/* - * 'roll_stack()' - Rotate stack objects. - */ - -static int /* O - 0 on success, -1 on error */ -roll_stack(_cups_ps_stack_t *st, /* I - Stack */ - int c, /* I - Number of objects */ - int s) /* I - Amount to shift */ -{ - _cups_ps_obj_t *temp; /* Temporary array of objects */ - int n; /* Index into array */ - - - DEBUG_printf(("3roll_stack(st=%p, s=%d, c=%d)", st, s, c)); - - /* - * Range check input... - */ - - if (c < 0) - return (-1); - else if (c == 0) - return (0); - - if ((n = st->num_objs - c) < 0) - return (-1); - - s %= c; - - if (s == 0) - return (0); - - /* - * Copy N objects and move things around... - */ - - if (s < 0) - { - /* - * Shift down... - */ - - s = -s; - - if ((temp = calloc((size_t)s, sizeof(_cups_ps_obj_t))) == NULL) - return (-1); - - memcpy(temp, st->objs + n, (size_t)s * sizeof(_cups_ps_obj_t)); - memmove(st->objs + n, st->objs + n + s, (size_t)(c - s) * sizeof(_cups_ps_obj_t)); - memcpy(st->objs + n + c - s, temp, (size_t)s * sizeof(_cups_ps_obj_t)); - } - else - { - /* - * Shift up... - */ - - if ((temp = calloc((size_t)s, sizeof(_cups_ps_obj_t))) == NULL) - return (-1); - - memcpy(temp, st->objs + n + c - s, (size_t)s * sizeof(_cups_ps_obj_t)); - memmove(st->objs + n + s, st->objs + n, (size_t)(c - s) * sizeof(_cups_ps_obj_t)); - memcpy(st->objs + n, temp, (size_t)s * sizeof(_cups_ps_obj_t)); - } - - free(temp); - - return (0); -} - - -/* - * 'scan_ps()' - Scan a string for the next PS object. - */ - -static _cups_ps_obj_t * /* O - New object or NULL on EOF */ -scan_ps(_cups_ps_stack_t *st, /* I - Stack */ - char **ptr) /* IO - String pointer */ -{ - _cups_ps_obj_t obj; /* Current object */ - char *start, /* Start of object */ - *cur, /* Current position */ - *valptr, /* Pointer into value string */ - *valend; /* End of value string */ - int parens; /* Parenthesis nesting level */ - - - /* - * Skip leading whitespace... - */ - - for (cur = *ptr; *cur; cur ++) - { - if (*cur == '%') - { - /* - * Comment, skip to end of line... - */ - - for (cur ++; *cur && *cur != '\n' && *cur != '\r'; cur ++); - - if (!*cur) - cur --; - } - else if (!isspace(*cur & 255)) - break; - } - - if (!*cur) - { - *ptr = NULL; - - return (NULL); - } - - /* - * See what we have... - */ - - memset(&obj, 0, sizeof(obj)); - - switch (*cur) - { - case '(' : /* (string) */ - obj.type = CUPS_PS_STRING; - start = cur; - - for (cur ++, parens = 1, valptr = obj.value.string, - valend = obj.value.string + sizeof(obj.value.string) - 1; - *cur; - cur ++) - { - if (*cur == ')' && parens == 1) - break; - - if (*cur == '(') - parens ++; - else if (*cur == ')') - parens --; - - if (valptr >= valend) - { - *ptr = start; - - return (NULL); - } - - if (*cur == '\\') - { - /* - * Decode escaped character... - */ - - cur ++; - - if (*cur == 'b') - *valptr++ = '\b'; - else if (*cur == 'f') - *valptr++ = '\f'; - else if (*cur == 'n') - *valptr++ = '\n'; - else if (*cur == 'r') - *valptr++ = '\r'; - else if (*cur == 't') - *valptr++ = '\t'; - else if (*cur >= '0' && *cur <= '7') - { - int ch = *cur - '0'; - - if (cur[1] >= '0' && cur[1] <= '7') - { - cur ++; - ch = (ch << 3) + *cur - '0'; - } - - if (cur[1] >= '0' && cur[1] <= '7') - { - cur ++; - ch = (ch << 3) + *cur - '0'; - } - - *valptr++ = (char)ch; - } - else if (*cur == '\r') - { - if (cur[1] == '\n') - cur ++; - } - else if (*cur != '\n') - *valptr++ = *cur; - } - else - *valptr++ = *cur; - } - - if (*cur != ')') - { - *ptr = start; - - return (NULL); - } - - cur ++; - break; - - case '[' : /* Start array */ - obj.type = CUPS_PS_START_ARRAY; - cur ++; - break; - - case ']' : /* End array */ - obj.type = CUPS_PS_END_ARRAY; - cur ++; - break; - - case '<' : /* Start dictionary or hex string */ - if (cur[1] == '<') - { - obj.type = CUPS_PS_START_DICT; - cur += 2; - } - else - { - obj.type = CUPS_PS_STRING; - start = cur; - - for (cur ++, valptr = obj.value.string, - valend = obj.value.string + sizeof(obj.value.string) - 1; - *cur; - cur ++) - { - int ch; /* Current character */ - - - - if (*cur == '>') - break; - else if (valptr >= valend || !isxdigit(*cur & 255)) - { - *ptr = start; - return (NULL); - } - - if (*cur >= '0' && *cur <= '9') - ch = (*cur - '0') << 4; - else - ch = (tolower(*cur) - 'a' + 10) << 4; - - if (isxdigit(cur[1] & 255)) - { - cur ++; - - if (*cur >= '0' && *cur <= '9') - ch |= *cur - '0'; - else - ch |= tolower(*cur) - 'a' + 10; - } - - *valptr++ = (char)ch; - } - - if (*cur != '>') - { - *ptr = start; - return (NULL); - } - - cur ++; - } - break; - - case '>' : /* End dictionary? */ - if (cur[1] == '>') - { - obj.type = CUPS_PS_END_DICT; - cur += 2; - } - else - { - obj.type = CUPS_PS_OTHER; - obj.value.other[0] = *cur; - - cur ++; - } - break; - - case '{' : /* Start procedure */ - obj.type = CUPS_PS_START_PROC; - cur ++; - break; - - case '}' : /* End procedure */ - obj.type = CUPS_PS_END_PROC; - cur ++; - break; - - case '-' : /* Possible number */ - case '+' : - if (!isdigit(cur[1] & 255) && cur[1] != '.') - { - obj.type = CUPS_PS_OTHER; - obj.value.other[0] = *cur; - - cur ++; - break; - } - - case '0' : /* Number */ - case '1' : - case '2' : - case '3' : - case '4' : - case '5' : - case '6' : - case '7' : - case '8' : - case '9' : - case '.' : - obj.type = CUPS_PS_NUMBER; - - start = cur; - for (cur ++; *cur; cur ++) - if (!isdigit(*cur & 255)) - break; - - if (*cur == '#') - { - /* - * Integer with radix... - */ - - obj.value.number = strtol(cur + 1, &cur, atoi(start)); - break; - } - else if (strchr(".Ee()<>[]{}/%", *cur) || isspace(*cur & 255)) - { - /* - * Integer or real number... - */ - - obj.value.number = _cupsStrScand(start, &cur, localeconv()); - break; - } - else - cur = start; - - default : /* Operator/variable name */ - start = cur; - - if (*cur == '/') - { - obj.type = CUPS_PS_NAME; - valptr = obj.value.name; - valend = obj.value.name + sizeof(obj.value.name) - 1; - cur ++; - } - else - { - obj.type = CUPS_PS_OTHER; - valptr = obj.value.other; - valend = obj.value.other + sizeof(obj.value.other) - 1; - } - - while (*cur) - { - if (strchr("()<>[]{}/%", *cur) || isspace(*cur & 255)) - break; - else if (valptr < valend) - *valptr++ = *cur++; - else - { - *ptr = start; - return (NULL); - } - } - - if (obj.type == CUPS_PS_OTHER) - { - if (!strcmp(obj.value.other, "true")) - { - obj.type = CUPS_PS_BOOLEAN; - obj.value.boolean = 1; - } - else if (!strcmp(obj.value.other, "false")) - { - obj.type = CUPS_PS_BOOLEAN; - obj.value.boolean = 0; - } - else if (!strcmp(obj.value.other, "null")) - obj.type = CUPS_PS_NULL; - else if (!strcmp(obj.value.other, "cleartomark")) - obj.type = CUPS_PS_CLEARTOMARK; - else if (!strcmp(obj.value.other, "copy")) - obj.type = CUPS_PS_COPY; - else if (!strcmp(obj.value.other, "dup")) - obj.type = CUPS_PS_DUP; - else if (!strcmp(obj.value.other, "index")) - obj.type = CUPS_PS_INDEX; - else if (!strcmp(obj.value.other, "pop")) - obj.type = CUPS_PS_POP; - else if (!strcmp(obj.value.other, "roll")) - obj.type = CUPS_PS_ROLL; - else if (!strcmp(obj.value.other, "setpagedevice")) - obj.type = CUPS_PS_SETPAGEDEVICE; - else if (!strcmp(obj.value.other, "stopped")) - obj.type = CUPS_PS_STOPPED; - } - break; - } - - /* - * Save the current position in the string and return the new object... - */ - - *ptr = cur; - - return (push_stack(st, &obj)); -} - - -/* - * 'setpagedevice()' - Simulate the PostScript setpagedevice operator. - */ - -static int /* O - 0 on success, -1 on error */ -setpagedevice( - _cups_ps_stack_t *st, /* I - Stack */ - cups_page_header2_t *h, /* O - Page header */ - int *preferred_bits)/* O - Preferred bits per color */ -{ - int i; /* Index into array */ - _cups_ps_obj_t *obj, /* Current object */ - *end; /* End of dictionary */ - const char *name; /* Attribute name */ - - - /* - * Make sure we have a dictionary on the stack... - */ - - if (st->num_objs == 0) - return (-1); - - obj = end = st->objs + st->num_objs - 1; - - if (obj->type != CUPS_PS_END_DICT) - return (-1); - - obj --; - - while (obj > st->objs) - { - if (obj->type == CUPS_PS_START_DICT) - break; - - obj --; - } - - if (obj < st->objs) - return (-1); - - /* - * Found the start of the dictionary, empty the stack to this point... - */ - - st->num_objs = (int)(obj - st->objs); - - /* - * Now pull /name and value pairs from the dictionary... - */ - - DEBUG_puts("3setpagedevice: Dictionary:"); - - for (obj ++; obj < end; obj ++) - { - /* - * Grab the name... - */ - - if (obj->type != CUPS_PS_NAME) - return (-1); - - name = obj->value.name; - obj ++; - -#ifdef DEBUG - DEBUG_printf(("4setpagedevice: /%s ", name)); - DEBUG_object("setpagedevice", obj); -#endif /* DEBUG */ - - /* - * Then grab the value... - */ - - if (!strcmp(name, "MediaClass") && obj->type == CUPS_PS_STRING) - strlcpy(h->MediaClass, obj->value.string, sizeof(h->MediaClass)); - else if (!strcmp(name, "MediaColor") && obj->type == CUPS_PS_STRING) - strlcpy(h->MediaColor, obj->value.string, sizeof(h->MediaColor)); - else if (!strcmp(name, "MediaType") && obj->type == CUPS_PS_STRING) - strlcpy(h->MediaType, obj->value.string, sizeof(h->MediaType)); - else if (!strcmp(name, "OutputType") && obj->type == CUPS_PS_STRING) - strlcpy(h->OutputType, obj->value.string, sizeof(h->OutputType)); - else if (!strcmp(name, "AdvanceDistance") && obj->type == CUPS_PS_NUMBER) - h->AdvanceDistance = (unsigned)obj->value.number; - else if (!strcmp(name, "AdvanceMedia") && obj->type == CUPS_PS_NUMBER) - h->AdvanceMedia = (unsigned)obj->value.number; - else if (!strcmp(name, "Collate") && obj->type == CUPS_PS_BOOLEAN) - h->Collate = (unsigned)obj->value.boolean; - else if (!strcmp(name, "CutMedia") && obj->type == CUPS_PS_NUMBER) - h->CutMedia = (cups_cut_t)(unsigned)obj->value.number; - else if (!strcmp(name, "Duplex") && obj->type == CUPS_PS_BOOLEAN) - h->Duplex = (unsigned)obj->value.boolean; - else if (!strcmp(name, "HWResolution") && obj->type == CUPS_PS_START_ARRAY) - { - if (obj[1].type == CUPS_PS_NUMBER && obj[2].type == CUPS_PS_NUMBER && - obj[3].type == CUPS_PS_END_ARRAY) - { - h->HWResolution[0] = (unsigned)obj[1].value.number; - h->HWResolution[1] = (unsigned)obj[2].value.number; - obj += 3; - } - else - return (-1); - } - else if (!strcmp(name, "InsertSheet") && obj->type == CUPS_PS_BOOLEAN) - h->InsertSheet = (unsigned)obj->value.boolean; - else if (!strcmp(name, "Jog") && obj->type == CUPS_PS_NUMBER) - h->Jog = (unsigned)obj->value.number; - else if (!strcmp(name, "LeadingEdge") && obj->type == CUPS_PS_NUMBER) - h->LeadingEdge = (unsigned)obj->value.number; - else if (!strcmp(name, "ManualFeed") && obj->type == CUPS_PS_BOOLEAN) - h->ManualFeed = (unsigned)obj->value.boolean; - else if ((!strcmp(name, "cupsMediaPosition") || - !strcmp(name, "MediaPosition")) && obj->type == CUPS_PS_NUMBER) - { - /* - * cupsMediaPosition is supported for backwards compatibility only. - * We added it back in the Ghostscript 5.50 days to work around a - * bug in Ghostscript WRT handling of MediaPosition and setpagedevice. - * - * All new development should set MediaPosition... - */ - - h->MediaPosition = (unsigned)obj->value.number; - } - else if (!strcmp(name, "MediaWeight") && obj->type == CUPS_PS_NUMBER) - h->MediaWeight = (unsigned)obj->value.number; - else if (!strcmp(name, "MirrorPrint") && obj->type == CUPS_PS_BOOLEAN) - h->MirrorPrint = (unsigned)obj->value.boolean; - else if (!strcmp(name, "NegativePrint") && obj->type == CUPS_PS_BOOLEAN) - h->NegativePrint = (unsigned)obj->value.boolean; - else if (!strcmp(name, "NumCopies") && obj->type == CUPS_PS_NUMBER) - h->NumCopies = (unsigned)obj->value.number; - else if (!strcmp(name, "Orientation") && obj->type == CUPS_PS_NUMBER) - h->Orientation = (unsigned)obj->value.number; - else if (!strcmp(name, "OutputFaceUp") && obj->type == CUPS_PS_BOOLEAN) - h->OutputFaceUp = (unsigned)obj->value.boolean; - else if (!strcmp(name, "PageSize") && obj->type == CUPS_PS_START_ARRAY) - { - if (obj[1].type == CUPS_PS_NUMBER && obj[2].type == CUPS_PS_NUMBER && - obj[3].type == CUPS_PS_END_ARRAY) - { - h->cupsPageSize[0] = (float)obj[1].value.number; - h->cupsPageSize[1] = (float)obj[2].value.number; - - h->PageSize[0] = (unsigned)obj[1].value.number; - h->PageSize[1] = (unsigned)obj[2].value.number; - - obj += 3; - } - else - return (-1); - } - else if (!strcmp(name, "Separations") && obj->type == CUPS_PS_BOOLEAN) - h->Separations = (unsigned)obj->value.boolean; - else if (!strcmp(name, "TraySwitch") && obj->type == CUPS_PS_BOOLEAN) - h->TraySwitch = (unsigned)obj->value.boolean; - else if (!strcmp(name, "Tumble") && obj->type == CUPS_PS_BOOLEAN) - h->Tumble = (unsigned)obj->value.boolean; - else if (!strcmp(name, "cupsMediaType") && obj->type == CUPS_PS_NUMBER) - h->cupsMediaType = (unsigned)obj->value.number; - else if (!strcmp(name, "cupsBitsPerColor") && obj->type == CUPS_PS_NUMBER) - h->cupsBitsPerColor = (unsigned)obj->value.number; - else if (!strcmp(name, "cupsPreferredBitsPerColor") && - obj->type == CUPS_PS_NUMBER) - *preferred_bits = (int)obj->value.number; - else if (!strcmp(name, "cupsColorOrder") && obj->type == CUPS_PS_NUMBER) - h->cupsColorOrder = (cups_order_t)(unsigned)obj->value.number; - else if (!strcmp(name, "cupsColorSpace") && obj->type == CUPS_PS_NUMBER) - h->cupsColorSpace = (cups_cspace_t)(unsigned)obj->value.number; - else if (!strcmp(name, "cupsCompression") && obj->type == CUPS_PS_NUMBER) - h->cupsCompression = (unsigned)obj->value.number; - else if (!strcmp(name, "cupsRowCount") && obj->type == CUPS_PS_NUMBER) - h->cupsRowCount = (unsigned)obj->value.number; - else if (!strcmp(name, "cupsRowFeed") && obj->type == CUPS_PS_NUMBER) - h->cupsRowFeed = (unsigned)obj->value.number; - else if (!strcmp(name, "cupsRowStep") && obj->type == CUPS_PS_NUMBER) - h->cupsRowStep = (unsigned)obj->value.number; - else if (!strcmp(name, "cupsBorderlessScalingFactor") && - obj->type == CUPS_PS_NUMBER) - h->cupsBorderlessScalingFactor = (float)obj->value.number; - else if (!strncmp(name, "cupsInteger", 11) && obj->type == CUPS_PS_NUMBER) - { - if ((i = atoi(name + 11)) < 0 || i > 15) - return (-1); - - h->cupsInteger[i] = (unsigned)obj->value.number; - } - else if (!strncmp(name, "cupsReal", 8) && obj->type == CUPS_PS_NUMBER) - { - if ((i = atoi(name + 8)) < 0 || i > 15) - return (-1); - - h->cupsReal[i] = (float)obj->value.number; - } - else if (!strncmp(name, "cupsString", 10) && obj->type == CUPS_PS_STRING) - { - if ((i = atoi(name + 10)) < 0 || i > 15) - return (-1); - - strlcpy(h->cupsString[i], obj->value.string, sizeof(h->cupsString[i])); - } - else if (!strcmp(name, "cupsMarkerType") && obj->type == CUPS_PS_STRING) - strlcpy(h->cupsMarkerType, obj->value.string, sizeof(h->cupsMarkerType)); - else if (!strcmp(name, "cupsPageSizeName") && obj->type == CUPS_PS_STRING) - strlcpy(h->cupsPageSizeName, obj->value.string, - sizeof(h->cupsPageSizeName)); - else if (!strcmp(name, "cupsRenderingIntent") && - obj->type == CUPS_PS_STRING) - strlcpy(h->cupsRenderingIntent, obj->value.string, - sizeof(h->cupsRenderingIntent)); - else - { - /* - * Ignore unknown name+value... - */ - - DEBUG_printf(("4setpagedevice: Unknown name (\"%s\") or value...\n", name)); - - while (obj[1].type != CUPS_PS_NAME && obj < end) - obj ++; - } - } - - return (0); -} - - -#ifdef DEBUG -/* - * 'DEBUG_object()' - Print an object's value... - */ - -static void -DEBUG_object(const char *prefix, /* I - Prefix string */ - _cups_ps_obj_t *obj) /* I - Object to print */ -{ - switch (obj->type) - { - case CUPS_PS_NAME : - DEBUG_printf(("4%s: /%s\n", prefix, obj->value.name)); - break; - - case CUPS_PS_NUMBER : - DEBUG_printf(("4%s: %g\n", prefix, obj->value.number)); - break; - - case CUPS_PS_STRING : - DEBUG_printf(("4%s: (%s)\n", prefix, obj->value.string)); - break; - - case CUPS_PS_BOOLEAN : - if (obj->value.boolean) - DEBUG_printf(("4%s: true", prefix)); - else - DEBUG_printf(("4%s: false", prefix)); - break; - - case CUPS_PS_NULL : - DEBUG_printf(("4%s: null", prefix)); - break; - - case CUPS_PS_START_ARRAY : - DEBUG_printf(("4%s: [", prefix)); - break; - - case CUPS_PS_END_ARRAY : - DEBUG_printf(("4%s: ]", prefix)); - break; - - case CUPS_PS_START_DICT : - DEBUG_printf(("4%s: <<", prefix)); - break; - - case CUPS_PS_END_DICT : - DEBUG_printf(("4%s: >>", prefix)); - break; - - case CUPS_PS_START_PROC : - DEBUG_printf(("4%s: {", prefix)); - break; - - case CUPS_PS_END_PROC : - DEBUG_printf(("4%s: }", prefix)); - break; - - case CUPS_PS_CLEARTOMARK : - DEBUG_printf(("4%s: --cleartomark--", prefix)); - break; - - case CUPS_PS_COPY : - DEBUG_printf(("4%s: --copy--", prefix)); - break; - - case CUPS_PS_DUP : - DEBUG_printf(("4%s: --dup--", prefix)); - break; - - case CUPS_PS_INDEX : - DEBUG_printf(("4%s: --index--", prefix)); - break; - - case CUPS_PS_POP : - DEBUG_printf(("4%s: --pop--", prefix)); - break; - - case CUPS_PS_ROLL : - DEBUG_printf(("4%s: --roll--", prefix)); - break; - - case CUPS_PS_SETPAGEDEVICE : - DEBUG_printf(("4%s: --setpagedevice--", prefix)); - break; - - case CUPS_PS_STOPPED : - DEBUG_printf(("4%s: --stopped--", prefix)); - break; - - case CUPS_PS_OTHER : - DEBUG_printf(("4%s: --%s--", prefix, obj->value.other)); - break; - } -} - - -/* - * 'DEBUG_stack()' - Print a stack... - */ - -static void -DEBUG_stack(const char *prefix, /* I - Prefix string */ - _cups_ps_stack_t *st) /* I - Stack */ -{ - int c; /* Looping var */ - _cups_ps_obj_t *obj; /* Current object on stack */ - - - for (obj = st->objs, c = st->num_objs; c > 0; c --, obj ++) - DEBUG_object(prefix, obj); -} -#endif /* DEBUG */ diff --git a/filter/libcupsimage2.def b/filter/libcupsimage2.def deleted file mode 100644 index 3c20284f5..000000000 --- a/filter/libcupsimage2.def +++ /dev/null @@ -1,14 +0,0 @@ -LIBRARY libcupsimage2 -VERSION 2.3 -EXPORTS -cupsRasterClose -cupsRasterErrorString -cupsRasterInterpretPPD -cupsRasterOpen -cupsRasterOpenIO -cupsRasterReadHeader -cupsRasterReadHeader2 -cupsRasterReadPixels -cupsRasterWriteHeader -cupsRasterWriteHeader2 -cupsRasterWritePixels diff --git a/filter/libcupsimage_s.exp b/filter/libcupsimage_s.exp deleted file mode 100644 index 57f4259f9..000000000 --- a/filter/libcupsimage_s.exp +++ /dev/null @@ -1,16 +0,0 @@ -_cupsImagePutCol -_cupsImagePutRow -_cupsImageReadBMP -_cupsImageReadGIF -_cupsImageReadJPEG -_cupsImageReadPIX -_cupsImageReadPNG -_cupsImageReadPNM -_cupsImageReadPhotoCD -_cupsImageReadSGI -_cupsImageReadSunRaster -_cupsImageReadTIFF -_cupsImageZoomDelete -_cupsImageZoomFill -_cupsImageZoomNew -_cupsRasterExecPS diff --git a/filter/raster.c b/filter/raster.c deleted file mode 100644 index 4efdc64ab..000000000 --- a/filter/raster.c +++ /dev/null @@ -1,2227 +0,0 @@ -/* - * Raster file routines for CUPS. - * - * Copyright 2007-2018 by Apple Inc. - * Copyright 1997-2006 by Easy Software Products. - * - * This file is part of the CUPS Imaging library. - * - * Licensed under Apache License v2.0. See the file "LICENSE" for more - * information. - */ - -/* - * Include necessary headers... - */ - -#include -#ifdef HAVE_STDINT_H -# include -#endif /* HAVE_STDINT_H */ - - -/* - * Private structures... - */ - -struct _cups_raster_s /**** Raster stream data ****/ -{ - unsigned sync; /* Sync word from start of stream */ - void *ctx; /* File descriptor */ - cups_raster_iocb_t iocb; /* IO callback */ - cups_mode_t mode; /* Read/write mode */ - cups_page_header2_t header; /* Raster header for current page */ - unsigned rowheight, /* Row height in lines */ - count, /* Current row run-length count */ - remaining, /* Remaining rows in page image */ - bpp; /* Bytes per pixel/color */ - unsigned char *pixels, /* Pixels for current row */ - *pend, /* End of pixel buffer */ - *pcurrent; /* Current byte in pixel buffer */ - int compressed, /* Non-zero if data is compressed */ - swapped; /* Non-zero if data is byte-swapped */ - unsigned char *buffer, /* Read/write buffer */ - *bufptr, /* Current (read) position in buffer */ - *bufend; /* End of current (read) buffer */ - size_t bufsize; /* Buffer size */ -#ifdef DEBUG - size_t iostart, /* Start of read/write buffer */ - iocount; /* Number of bytes read/written */ -#endif /* DEBUG */ - unsigned apple_page_count;/* Apple raster page count */ -}; - -typedef void (*_cups_copyfunc_t)(void *dst, const void *src, size_t bytes); - - -/* - * Local globals... - */ - -#ifdef DEBUG - static const char * const cups_color_spaces[] = - { /* Color spaces */ - "CUPS_CSPACE_W", - "CUPS_CSPACE_RGB", - "CUPS_CSPACE_RGBA", - "CUPS_CSPACE_K", - "CUPS_CSPACE_CMY", - "CUPS_CSPACE_YMC", - "CUPS_CSPACE_CMYK", - "CUPS_CSPACE_YMCK", - "CUPS_CSPACE_KCMY", - "CUPS_CSPACE_KCMYcm", - "CUPS_CSPACE_GMCK", - "CUPS_CSPACE_GMCS", - "CUPS_CSPACE_WHITE", - "CUPS_CSPACE_GOLD", - "CUPS_CSPACE_SILVER", - "CUPS_CSPACE_CIEXYZ", - "CUPS_CSPACE_CIELab", - "CUPS_CSPACE_RGBW", - "CUPS_CSPACE_SW", - "CUPS_CSPACE_SRGB", - "CUPS_CSPACE_ADOBERGB", - "21", - "22", - "23", - "24", - "25", - "26", - "27", - "28", - "29", - "30", - "31", - "CUPS_CSPACE_ICC1", - "CUPS_CSPACE_ICC2", - "CUPS_CSPACE_ICC3", - "CUPS_CSPACE_ICC4", - "CUPS_CSPACE_ICC5", - "CUPS_CSPACE_ICC6", - "CUPS_CSPACE_ICC7", - "CUPS_CSPACE_ICC8", - "CUPS_CSPACE_ICC9", - "CUPS_CSPACE_ICCA", - "CUPS_CSPACE_ICCB", - "CUPS_CSPACE_ICCC", - "CUPS_CSPACE_ICCD", - "CUPS_CSPACE_ICCE", - "CUPS_CSPACE_ICCF", - "47", - "CUPS_CSPACE_DEVICE1", - "CUPS_CSPACE_DEVICE2", - "CUPS_CSPACE_DEVICE3", - "CUPS_CSPACE_DEVICE4", - "CUPS_CSPACE_DEVICE5", - "CUPS_CSPACE_DEVICE6", - "CUPS_CSPACE_DEVICE7", - "CUPS_CSPACE_DEVICE8", - "CUPS_CSPACE_DEVICE9", - "CUPS_CSPACE_DEVICEA", - "CUPS_CSPACE_DEVICEB", - "CUPS_CSPACE_DEVICEC", - "CUPS_CSPACE_DEVICED", - "CUPS_CSPACE_DEVICEE", - "CUPS_CSPACE_DEVICEF" - }; - static const char * const cups_modes[] = - { /* Open modes */ - "CUPS_RASTER_READ", - "CUPS_RASTER_WRITE", - "CUPS_RASTER_WRITE_COMPRESSED", - "CUPS_RASTER_WRITE_PWG", - "CUPS_RASTER_WRITE_APPLE" - }; -#endif /* DEBUG */ - - -/* - * Local functions... - */ - -static ssize_t cups_raster_io(cups_raster_t *r, unsigned char *buf, size_t bytes); -static unsigned cups_raster_read_header(cups_raster_t *r); -static ssize_t cups_raster_read(cups_raster_t *r, unsigned char *buf, size_t bytes); -static int cups_raster_update(cups_raster_t *r); -static ssize_t cups_raster_write(cups_raster_t *r, const unsigned char *pixels); -static ssize_t cups_read_fd(void *ctx, unsigned char *buf, size_t bytes); -static void cups_swap(unsigned char *buf, size_t bytes); -static void cups_swap_copy(unsigned char *dst, const unsigned char *src, size_t bytes); -static ssize_t cups_write_fd(void *ctx, unsigned char *buf, size_t bytes); - - -/* - * 'cupsRasterClose()' - Close a raster stream. - * - * The file descriptor associated with the raster stream must be closed - * separately as needed. - */ - -void -cupsRasterClose(cups_raster_t *r) /* I - Stream to close */ -{ - if (r != NULL) - { - if (r->buffer) - free(r->buffer); - - if (r->pixels) - free(r->pixels); - - free(r); - } -} - - -/* - * 'cupsRasterInitPWGHeader()' - Initialize a page header for PWG Raster output. - * - * The "media" argument specifies the media to use. - * - * The "type" argument specifies a "pwg-raster-document-type-supported" value - * that controls the color space and bit depth of the raster data. - * - * The "xres" and "yres" arguments specify the raster resolution in dots per - * inch. - * - * The "sheet_back" argument specifies a "pwg-raster-document-sheet-back" value - * to apply for the back side of a page. Pass @code NULL@ for the front side. - * - * @since CUPS 2.2/macOS 10.12@ - */ - -int /* O - 1 on success, 0 on failure */ -cupsRasterInitPWGHeader( - cups_page_header2_t *h, /* I - Page header */ - pwg_media_t *media, /* I - PWG media information */ - const char *type, /* I - PWG raster type string */ - int xdpi, /* I - Cross-feed direction (horizontal) resolution */ - int ydpi, /* I - Feed direction (vertical) resolution */ - const char *sides, /* I - IPP "sides" option value */ - const char *sheet_back) /* I - Transform for back side or @code NULL@ for none */ -{ - if (!h || !media || !type || xdpi <= 0 || ydpi <= 0) - { - _cupsRasterAddError("%s", strerror(EINVAL)); - return (0); - } - - /* - * Initialize the page header... - */ - - memset(h, 0, sizeof(cups_page_header2_t)); - - strlcpy(h->cupsPageSizeName, media->pwg, sizeof(h->cupsPageSizeName)); - - h->PageSize[0] = (unsigned)(72 * media->width / 2540); - h->PageSize[1] = (unsigned)(72 * media->length / 2540); - - /* This never gets written but is needed for some applications */ - h->cupsPageSize[0] = 72.0f * media->width / 2540.0f; - h->cupsPageSize[1] = 72.0f * media->length / 2540.0f; - - h->ImagingBoundingBox[2] = h->PageSize[0]; - h->ImagingBoundingBox[3] = h->PageSize[1]; - - h->HWResolution[0] = (unsigned)xdpi; - h->HWResolution[1] = (unsigned)ydpi; - - h->cupsWidth = (unsigned)(media->width * xdpi / 2540); - h->cupsHeight = (unsigned)(media->length * ydpi / 2540); - - if (h->cupsWidth > 0x00ffffff || h->cupsHeight > 0x00ffffff) - { - _cupsRasterAddError("Raster dimensions too large."); - return (0); - } - - h->cupsInteger[CUPS_RASTER_PWG_ImageBoxRight] = h->cupsWidth; - h->cupsInteger[CUPS_RASTER_PWG_ImageBoxBottom] = h->cupsHeight; - - /* - * Colorspace and bytes per line... - */ - - if (!strcmp(type, "adobe-rgb_8")) - { - h->cupsBitsPerColor = 8; - h->cupsBitsPerPixel = 24; - h->cupsColorSpace = CUPS_CSPACE_ADOBERGB; - } - else if (!strcmp(type, "adobe-rgb_16")) - { - h->cupsBitsPerColor = 16; - h->cupsBitsPerPixel = 48; - h->cupsColorSpace = CUPS_CSPACE_ADOBERGB; - } - else if (!strcmp(type, "black_1")) - { - h->cupsBitsPerColor = 1; - h->cupsBitsPerPixel = 1; - h->cupsColorSpace = CUPS_CSPACE_K; - } - else if (!strcmp(type, "black_8")) - { - h->cupsBitsPerColor = 8; - h->cupsBitsPerPixel = 8; - h->cupsColorSpace = CUPS_CSPACE_K; - } - else if (!strcmp(type, "black_16")) - { - h->cupsBitsPerColor = 16; - h->cupsBitsPerPixel = 16; - h->cupsColorSpace = CUPS_CSPACE_K; - } - else if (!strcmp(type, "cmyk_8")) - { - h->cupsBitsPerColor = 8; - h->cupsBitsPerPixel = 32; - h->cupsColorSpace = CUPS_CSPACE_CMYK; - } - else if (!strcmp(type, "cmyk_16")) - { - h->cupsBitsPerColor = 16; - h->cupsBitsPerPixel = 64; - h->cupsColorSpace = CUPS_CSPACE_CMYK; - } - else if (!strncmp(type, "device", 6) && type[6] >= '1' && type[6] <= '9') - { - int ncolors, bits; /* Number of colors and bits */ - - - if (sscanf(type, "device%d_%d", &ncolors, &bits) != 2 || ncolors > 15 || (bits != 8 && bits != 16)) - { - _cupsRasterAddError("Unsupported raster type \'%s\'.", type); - return (0); - } - - h->cupsBitsPerColor = (unsigned)bits; - h->cupsBitsPerPixel = (unsigned)(ncolors * bits); - h->cupsColorSpace = (cups_cspace_t)(CUPS_CSPACE_DEVICE1 + ncolors - 1); - } - else if (!strcmp(type, "rgb_8")) - { - h->cupsBitsPerColor = 8; - h->cupsBitsPerPixel = 24; - h->cupsColorSpace = CUPS_CSPACE_RGB; - } - else if (!strcmp(type, "rgb_16")) - { - h->cupsBitsPerColor = 16; - h->cupsBitsPerPixel = 48; - h->cupsColorSpace = CUPS_CSPACE_RGB; - } - else if (!strcmp(type, "sgray_1")) - { - h->cupsBitsPerColor = 1; - h->cupsBitsPerPixel = 1; - h->cupsColorSpace = CUPS_CSPACE_SW; - } - else if (!strcmp(type, "sgray_8")) - { - h->cupsBitsPerColor = 8; - h->cupsBitsPerPixel = 8; - h->cupsColorSpace = CUPS_CSPACE_SW; - } - else if (!strcmp(type, "sgray_16")) - { - h->cupsBitsPerColor = 16; - h->cupsBitsPerPixel = 16; - h->cupsColorSpace = CUPS_CSPACE_SW; - } - else if (!strcmp(type, "srgb_8")) - { - h->cupsBitsPerColor = 8; - h->cupsBitsPerPixel = 24; - h->cupsColorSpace = CUPS_CSPACE_SRGB; - } - else if (!strcmp(type, "srgb_16")) - { - h->cupsBitsPerColor = 16; - h->cupsBitsPerPixel = 48; - h->cupsColorSpace = CUPS_CSPACE_SRGB; - } - else - { - _cupsRasterAddError("Unsupported raster type \'%s\'.", type); - return (0); - } - - h->cupsColorOrder = CUPS_ORDER_CHUNKED; - h->cupsNumColors = h->cupsBitsPerPixel / h->cupsBitsPerColor; - h->cupsBytesPerLine = (h->cupsWidth * h->cupsBitsPerPixel + 7) / 8; - - /* - * Duplex support... - */ - - h->cupsInteger[CUPS_RASTER_PWG_CrossFeedTransform] = 1; - h->cupsInteger[CUPS_RASTER_PWG_FeedTransform] = 1; - - if (sides) - { - if (!strcmp(sides, "two-sided-long-edge")) - { - h->Duplex = 1; - } - else if (!strcmp(sides, "two-sided-short-edge")) - { - h->Duplex = 1; - h->Tumble = 1; - } - else if (strcmp(sides, "one-sided")) - { - _cupsRasterAddError("Unsupported sides value \'%s\'.", sides); - return (0); - } - - if (sheet_back) - { - if (!strcmp(sheet_back, "flipped")) - { - if (h->Tumble) - h->cupsInteger[CUPS_RASTER_PWG_CrossFeedTransform] = 0xffffffffU; - else - h->cupsInteger[CUPS_RASTER_PWG_FeedTransform] = 0xffffffffU; - } - else if (!strcmp(sheet_back, "manual-tumble")) - { - if (h->Tumble) - { - h->cupsInteger[CUPS_RASTER_PWG_CrossFeedTransform] = 0xffffffffU; - h->cupsInteger[CUPS_RASTER_PWG_FeedTransform] = 0xffffffffU; - } - } - else if (!strcmp(sheet_back, "rotated")) - { - if (!h->Tumble) - { - h->cupsInteger[CUPS_RASTER_PWG_CrossFeedTransform] = 0xffffffffU; - h->cupsInteger[CUPS_RASTER_PWG_FeedTransform] = 0xffffffffU; - } - } - else if (strcmp(sheet_back, "normal")) - { - _cupsRasterAddError("Unsupported sheet_back value \'%s\'.", sheet_back); - return (0); - } - } - } - - return (1); -} - - -/* - * 'cupsRasterOpen()' - Open a raster stream using a file descriptor. - * - * This function associates a raster stream with the given file descriptor. - * For most printer driver filters, "fd" will be 0 (stdin). For most raster - * image processor (RIP) filters that generate raster data, "fd" will be 1 - * (stdout). - * - * When writing raster data, the @code CUPS_RASTER_WRITE@, - * @code CUPS_RASTER_WRITE_COMPRESS@, or @code CUPS_RASTER_WRITE_PWG@ mode can - * be used - compressed and PWG output is generally 25-50% smaller but adds a - * 100-300% execution time overhead. - */ - -cups_raster_t * /* O - New stream */ -cupsRasterOpen(int fd, /* I - File descriptor */ - cups_mode_t mode) /* I - Mode - @code CUPS_RASTER_READ@, - @code CUPS_RASTER_WRITE@, - @code CUPS_RASTER_WRITE_COMPRESSED@, - or @code CUPS_RASTER_WRITE_PWG@ */ -{ - DEBUG_printf(("cupsRasterOpen(fd=%d, mode=%s)", fd, cups_modes[mode])); - - if (mode == CUPS_RASTER_READ) - return (cupsRasterOpenIO(cups_read_fd, (void *)((intptr_t)fd), mode)); - else - return (cupsRasterOpenIO(cups_write_fd, (void *)((intptr_t)fd), mode)); -} - - -/* - * 'cupsRasterOpenIO()' - Open a raster stream using a callback function. - * - * This function associates a raster stream with the given callback function and - * context pointer. - * - * When writing raster data, the @code CUPS_RASTER_WRITE@, - * @code CUPS_RASTER_WRITE_COMPRESS@, or @code CUPS_RASTER_WRITE_PWG@ mode can - * be used - compressed and PWG output is generally 25-50% smaller but adds a - * 100-300% execution time overhead. - */ - -cups_raster_t * /* O - New stream */ -cupsRasterOpenIO( - cups_raster_iocb_t iocb, /* I - Read/write callback */ - void *ctx, /* I - Context pointer for callback */ - cups_mode_t mode) /* I - Mode - @code CUPS_RASTER_READ@, - @code CUPS_RASTER_WRITE@, - @code CUPS_RASTER_WRITE_COMPRESSED@, - or @code CUPS_RASTER_WRITE_PWG@ */ -{ - cups_raster_t *r; /* New stream */ - - - DEBUG_printf(("cupsRasterOpenIO(iocb=%p, ctx=%p, mode=%s)", (void *)iocb, ctx, cups_modes[mode])); - - _cupsRasterClearError(); - - if ((r = calloc(sizeof(cups_raster_t), 1)) == NULL) - { - _cupsRasterAddError("Unable to allocate memory for raster stream: %s\n", - strerror(errno)); - DEBUG_puts("1cupsRasterOpenIO: Returning NULL."); - return (NULL); - } - - r->ctx = ctx; - r->iocb = iocb; - r->mode = mode; - - if (mode == CUPS_RASTER_READ) - { - /* - * Open for read - get sync word... - */ - - if (cups_raster_io(r, (unsigned char *)&(r->sync), sizeof(r->sync)) != - sizeof(r->sync)) - { - _cupsRasterAddError("Unable to read header from raster stream: %s\n", - strerror(errno)); - free(r); - DEBUG_puts("1cupsRasterOpenIO: Unable to read header, returning NULL."); - return (NULL); - } - - if (r->sync != CUPS_RASTER_SYNC && - r->sync != CUPS_RASTER_REVSYNC && - r->sync != CUPS_RASTER_SYNCv1 && - r->sync != CUPS_RASTER_REVSYNCv1 && - r->sync != CUPS_RASTER_SYNCv2 && - r->sync != CUPS_RASTER_REVSYNCv2 && - r->sync != CUPS_RASTER_SYNCapple && - r->sync != CUPS_RASTER_REVSYNCapple) - { - _cupsRasterAddError("Unknown raster format %08x!\n", r->sync); - free(r); - DEBUG_puts("1cupsRasterOpenIO: Unknown format, returning NULL."); - return (NULL); - } - - if (r->sync == CUPS_RASTER_SYNCv2 || - r->sync == CUPS_RASTER_REVSYNCv2 || - r->sync == CUPS_RASTER_SYNCapple || - r->sync == CUPS_RASTER_REVSYNCapple) - r->compressed = 1; - - DEBUG_printf(("1cupsRasterOpenIO: sync=%08x", r->sync)); - - if (r->sync == CUPS_RASTER_REVSYNC || - r->sync == CUPS_RASTER_REVSYNCv1 || - r->sync == CUPS_RASTER_REVSYNCv2 || - r->sync == CUPS_RASTER_REVSYNCapple) - r->swapped = 1; - - if (r->sync == CUPS_RASTER_SYNCapple || - r->sync == CUPS_RASTER_REVSYNCapple) - { - unsigned char header[8]; /* File header */ - - if (cups_raster_io(r, (unsigned char *)header, sizeof(header)) != - sizeof(header)) - { - _cupsRasterAddError("Unable to read header from raster stream: %s\n", - strerror(errno)); - free(r); - DEBUG_puts("1cupsRasterOpenIO: Unable to read header, returning NULL."); - return (NULL); - } - } - -#ifdef DEBUG - r->iostart = r->iocount; -#endif /* DEBUG */ - } - else - { - /* - * Open for write - put sync word... - */ - - switch (mode) - { - default : - case CUPS_RASTER_WRITE : - r->sync = CUPS_RASTER_SYNC; - break; - - case CUPS_RASTER_WRITE_COMPRESSED : - r->compressed = 1; - r->sync = CUPS_RASTER_SYNCv2; - break; - - case CUPS_RASTER_WRITE_PWG : - r->compressed = 1; - r->sync = htonl(CUPS_RASTER_SYNC_PWG); - r->swapped = r->sync != CUPS_RASTER_SYNC_PWG; - break; - - case CUPS_RASTER_WRITE_APPLE : - r->compressed = 1; - r->sync = htonl(CUPS_RASTER_SYNCapple); - r->swapped = r->sync != CUPS_RASTER_SYNCapple; - r->apple_page_count = 0xffffffffU; - break; - } - - if (cups_raster_io(r, (unsigned char *)&(r->sync), sizeof(r->sync)) < (ssize_t)sizeof(r->sync)) - { - _cupsRasterAddError("Unable to write raster stream header: %s\n", - strerror(errno)); - free(r); - DEBUG_puts("1cupsRasterOpenIO: Unable to write header, returning NULL."); - return (NULL); - } - } - - DEBUG_printf(("1cupsRasterOpenIO: compressed=%d, swapped=%d, returning %p", r->compressed, r->swapped, (void *)r)); - - return (r); -} - - -/* - * 'cupsRasterReadHeader()' - Read a raster page header and store it in a - * version 1 page header structure. - * - * This function is deprecated. Use @link cupsRasterReadHeader2@ instead. - * - * Version 1 page headers were used in CUPS 1.0 and 1.1 and contain a subset - * of the version 2 page header data. This function handles reading version 2 - * page headers and copying only the version 1 data into the provided buffer. - * - * @deprecated@ - */ - -unsigned /* O - 1 on success, 0 on failure/end-of-file */ -cupsRasterReadHeader( - cups_raster_t *r, /* I - Raster stream */ - cups_page_header_t *h) /* I - Pointer to header data */ -{ - DEBUG_printf(("cupsRasterReadHeader(r=%p, h=%p)", (void *)r, (void *)h)); - - /* - * Get the raster header... - */ - - if (!cups_raster_read_header(r)) - { - memset(h, 0, sizeof(cups_page_header_t)); - DEBUG_puts("1cupsRasterReadHeader: Unable to read page header, returning 0."); - return (0); - } - - /* - * Copy the header to the user-supplied buffer... - */ - - memcpy(h, &(r->header), sizeof(cups_page_header_t)); - - DEBUG_printf(("1cupsRasterReadHeader: cupsColorSpace=%s", cups_color_spaces[h->cupsColorSpace])); - DEBUG_printf(("1cupsRasterReadHeader: cupsBitsPerColor=%u", h->cupsBitsPerColor)); - DEBUG_printf(("1cupsRasterReadHeader: cupsBitsPerPixel=%u", h->cupsBitsPerPixel)); - DEBUG_printf(("1cupsRasterReadHeader: cupsBytesPerLine=%u", h->cupsBytesPerLine)); - DEBUG_printf(("1cupsRasterReadHeader: cupsWidth=%u", h->cupsWidth)); - DEBUG_printf(("1cupsRasterReadHeader: cupsHeight=%u", h->cupsHeight)); - - DEBUG_puts("1cupsRasterReadHeader: Returning 1."); - return (1); -} - - -/* - * 'cupsRasterReadHeader2()' - Read a raster page header and store it in a - * version 2 page header structure. - * - * @since CUPS 1.2/macOS 10.5@ - */ - -unsigned /* O - 1 on success, 0 on failure/end-of-file */ -cupsRasterReadHeader2( - cups_raster_t *r, /* I - Raster stream */ - cups_page_header2_t *h) /* I - Pointer to header data */ -{ - /* - * Get the raster header... - */ - - DEBUG_printf(("cupsRasterReadHeader2(r=%p, h=%p)", (void *)r, (void *)h)); - - if (!cups_raster_read_header(r)) - { - memset(h, 0, sizeof(cups_page_header2_t)); - DEBUG_puts("1cupsRasterReadHeader2: Unable to read header, returning 0."); - return (0); - } - - /* - * Copy the header to the user-supplied buffer... - */ - - memcpy(h, &(r->header), sizeof(cups_page_header2_t)); - - DEBUG_printf(("1cupsRasterReadHeader2: cupsColorSpace=%s", cups_color_spaces[h->cupsColorSpace])); - DEBUG_printf(("1cupsRasterReadHeader2: cupsBitsPerColor=%u", h->cupsBitsPerColor)); - DEBUG_printf(("1cupsRasterReadHeader2: cupsBitsPerPixel=%u", h->cupsBitsPerPixel)); - DEBUG_printf(("1cupsRasterReadHeader2: cupsBytesPerLine=%u", h->cupsBytesPerLine)); - DEBUG_printf(("1cupsRasterReadHeader2: cupsWidth=%u", h->cupsWidth)); - DEBUG_printf(("1cupsRasterReadHeader2: cupsHeight=%u", h->cupsHeight)); - - DEBUG_puts("1cupsRasterReadHeader2: Returning 1."); - return (1); -} - - -/* - * 'cupsRasterReadPixels()' - Read raster pixels. - * - * For best performance, filters should read one or more whole lines. - * The "cupsBytesPerLine" value from the page header can be used to allocate - * the line buffer and as the number of bytes to read. - */ - -unsigned /* O - Number of bytes read */ -cupsRasterReadPixels(cups_raster_t *r, /* I - Raster stream */ - unsigned char *p, /* I - Pointer to pixel buffer */ - unsigned len) /* I - Number of bytes to read */ -{ - ssize_t bytes; /* Bytes read */ - unsigned cupsBytesPerLine; /* cupsBytesPerLine value */ - unsigned remaining; /* Bytes remaining */ - unsigned char *ptr, /* Pointer to read buffer */ - byte, /* Byte from file */ - *temp; /* Pointer into buffer */ - unsigned count; /* Repetition count */ - - - DEBUG_printf(("cupsRasterReadPixels(r=%p, p=%p, len=%u)", (void *)r, (void *)p, len)); - - if (r == NULL || r->mode != CUPS_RASTER_READ || r->remaining == 0 || - r->header.cupsBytesPerLine == 0) - { - DEBUG_puts("1cupsRasterReadPixels: Returning 0."); - return (0); - } - - DEBUG_printf(("1cupsRasterReadPixels: compressed=%d, remaining=%u", r->compressed, r->remaining)); - - if (!r->compressed) - { - /* - * Read without compression... - */ - - r->remaining -= len / r->header.cupsBytesPerLine; - - if (cups_raster_io(r, p, len) < (ssize_t)len) - { - DEBUG_puts("1cupsRasterReadPixels: Read error, returning 0."); - return (0); - } - - /* - * Swap bytes as needed... - */ - - if (r->swapped && - (r->header.cupsBitsPerColor == 16 || - r->header.cupsBitsPerPixel == 12 || - r->header.cupsBitsPerPixel == 16)) - cups_swap(p, len); - - /* - * Return... - */ - - DEBUG_printf(("1cupsRasterReadPixels: Returning %u", len)); - - return (len); - } - - /* - * Read compressed data... - */ - - remaining = len; - cupsBytesPerLine = r->header.cupsBytesPerLine; - - while (remaining > 0 && r->remaining > 0) - { - if (r->count == 0) - { - /* - * Need to read a new row... - */ - - if (remaining == cupsBytesPerLine) - ptr = p; - else - ptr = r->pixels; - - /* - * Read using a modified PackBits compression... - */ - - if (!cups_raster_read(r, &byte, 1)) - { - DEBUG_puts("1cupsRasterReadPixels: Read error, returning 0."); - return (0); - } - - r->count = (unsigned)byte + 1; - - if (r->count > 1) - ptr = r->pixels; - - temp = ptr; - bytes = (ssize_t)cupsBytesPerLine; - - while (bytes > 0) - { - /* - * Get a new repeat count... - */ - - if (!cups_raster_read(r, &byte, 1)) - { - DEBUG_puts("1cupsRasterReadPixels: Read error, returning 0."); - return (0); - } - - if (byte == 128) - { - /* - * Clear to end of line... - */ - - switch (r->header.cupsColorSpace) - { - case CUPS_CSPACE_W : - case CUPS_CSPACE_RGB : - case CUPS_CSPACE_SW : - case CUPS_CSPACE_SRGB : - case CUPS_CSPACE_RGBW : - case CUPS_CSPACE_ADOBERGB : - memset(temp, 0xff, (size_t)bytes); - break; - default : - memset(temp, 0x00, (size_t)bytes); - break; - } - - temp += bytes; - bytes = 0; - } - else if (byte & 128) - { - /* - * Copy N literal pixels... - */ - - count = (unsigned)(257 - byte) * r->bpp; - - if (count > (unsigned)bytes) - count = (unsigned)bytes; - - if (!cups_raster_read(r, temp, count)) - { - DEBUG_puts("1cupsRasterReadPixels: Read error, returning 0."); - return (0); - } - - temp += count; - bytes -= (ssize_t)count; - } - else - { - /* - * Repeat the next N bytes... - */ - - count = ((unsigned)byte + 1) * r->bpp; - if (count > (unsigned)bytes) - count = (unsigned)bytes; - - if (count < r->bpp) - break; - - bytes -= (ssize_t)count; - - if (!cups_raster_read(r, temp, r->bpp)) - { - DEBUG_puts("1cupsRasterReadPixels: Read error, returning 0."); - return (0); - } - - temp += r->bpp; - count -= r->bpp; - - while (count > 0) - { - memcpy(temp, temp - r->bpp, r->bpp); - temp += r->bpp; - count -= r->bpp; - } - } - } - - /* - * Swap bytes as needed... - */ - - if ((r->header.cupsBitsPerColor == 16 || - r->header.cupsBitsPerPixel == 12 || - r->header.cupsBitsPerPixel == 16) && - r->swapped) - { - DEBUG_puts("1cupsRasterReadPixels: Swapping bytes."); - cups_swap(ptr, (size_t)cupsBytesPerLine); - } - - /* - * Update pointers... - */ - - if (remaining >= cupsBytesPerLine) - { - bytes = (ssize_t)cupsBytesPerLine; - r->pcurrent = r->pixels; - r->count --; - r->remaining --; - } - else - { - bytes = (ssize_t)remaining; - r->pcurrent = r->pixels + bytes; - } - - /* - * Copy data as needed... - */ - - if (ptr != p) - memcpy(p, ptr, (size_t)bytes); - } - else - { - /* - * Copy fragment from buffer... - */ - - if ((unsigned)(bytes = (int)(r->pend - r->pcurrent)) > remaining) - bytes = (ssize_t)remaining; - - memcpy(p, r->pcurrent, (size_t)bytes); - r->pcurrent += bytes; - - if (r->pcurrent >= r->pend) - { - r->pcurrent = r->pixels; - r->count --; - r->remaining --; - } - } - - remaining -= (unsigned)bytes; - p += bytes; - } - - DEBUG_printf(("1cupsRasterReadPixels: Returning %u", len)); - - return (len); -} - - -/* - * 'cupsRasterWriteHeader()' - Write a raster page header from a version 1 page - * header structure. - * - * This function is deprecated. Use @link cupsRasterWriteHeader2@ instead. - * - * @deprecated@ - */ - -unsigned /* O - 1 on success, 0 on failure */ -cupsRasterWriteHeader( - cups_raster_t *r, /* I - Raster stream */ - cups_page_header_t *h) /* I - Raster page header */ -{ - DEBUG_printf(("cupsRasterWriteHeader(r=%p, h=%p)", (void *)r, (void *)h)); - - if (r == NULL || r->mode == CUPS_RASTER_READ) - { - DEBUG_puts("1cupsRasterWriteHeader: Returning 0."); - return (0); - } - - DEBUG_printf(("1cupsRasterWriteHeader: cupsColorSpace=%s", cups_color_spaces[h->cupsColorSpace])); - DEBUG_printf(("1cupsRasterWriteHeader: cupsBitsPerColor=%u", h->cupsBitsPerColor)); - DEBUG_printf(("1cupsRasterWriteHeader: cupsBitsPerPixel=%u", h->cupsBitsPerPixel)); - DEBUG_printf(("1cupsRasterWriteHeader: cupsBytesPerLine=%u", h->cupsBytesPerLine)); - DEBUG_printf(("1cupsRasterWriteHeader: cupsWidth=%u", h->cupsWidth)); - DEBUG_printf(("1cupsRasterWriteHeader: cupsHeight=%u", h->cupsHeight)); - - /* - * Make a copy of the header, and compute the number of raster - * lines in the page image... - */ - - memset(&(r->header), 0, sizeof(r->header)); - memcpy(&(r->header), h, sizeof(cups_page_header_t)); - - if (!cups_raster_update(r)) - { - DEBUG_puts("1cupsRasterWriteHeader: Unable to update parameters, returning 0."); - return (0); - } - - if (r->mode == CUPS_RASTER_WRITE_APPLE) - { - r->rowheight = h->HWResolution[0] / h->HWResolution[1]; - - if (h->HWResolution[0] != (r->rowheight * h->HWResolution[1])) - return (0); - } - else - r->rowheight = 1; - - /* - * Write the raster header... - */ - - if (r->mode == CUPS_RASTER_WRITE_PWG) - { - /* - * PWG raster data is always network byte order with much of the page header - * zeroed. - */ - - cups_page_header2_t fh; /* File page header */ - - memset(&fh, 0, sizeof(fh)); - - strlcpy(fh.MediaClass, "PwgRaster", sizeof(fh.MediaClass)); - /* PwgRaster */ - strlcpy(fh.MediaColor, r->header.MediaColor, sizeof(fh.MediaColor)); - strlcpy(fh.MediaType, r->header.MediaType, sizeof(fh.MediaType)); - strlcpy(fh.OutputType, r->header.OutputType, sizeof(fh.OutputType)); - /* PrintContentType */ - - fh.CutMedia = htonl(r->header.CutMedia); - fh.Duplex = htonl(r->header.Duplex); - fh.HWResolution[0] = htonl(r->header.HWResolution[0]); - fh.HWResolution[1] = htonl(r->header.HWResolution[1]); - fh.ImagingBoundingBox[0] = htonl(r->header.ImagingBoundingBox[0]); - fh.ImagingBoundingBox[1] = htonl(r->header.ImagingBoundingBox[1]); - fh.ImagingBoundingBox[2] = htonl(r->header.ImagingBoundingBox[2]); - fh.ImagingBoundingBox[3] = htonl(r->header.ImagingBoundingBox[3]); - fh.InsertSheet = htonl(r->header.InsertSheet); - fh.Jog = htonl(r->header.Jog); - fh.LeadingEdge = htonl(r->header.LeadingEdge); - fh.ManualFeed = htonl(r->header.ManualFeed); - fh.MediaPosition = htonl(r->header.MediaPosition); - fh.MediaWeight = htonl(r->header.MediaWeight); - fh.NumCopies = htonl(r->header.NumCopies); - fh.Orientation = htonl(r->header.Orientation); - fh.PageSize[0] = htonl(r->header.PageSize[0]); - fh.PageSize[1] = htonl(r->header.PageSize[1]); - fh.Tumble = htonl(r->header.Tumble); - fh.cupsWidth = htonl(r->header.cupsWidth); - fh.cupsHeight = htonl(r->header.cupsHeight); - fh.cupsBitsPerColor = htonl(r->header.cupsBitsPerColor); - fh.cupsBitsPerPixel = htonl(r->header.cupsBitsPerPixel); - fh.cupsBytesPerLine = htonl(r->header.cupsBytesPerLine); - fh.cupsColorOrder = htonl(r->header.cupsColorOrder); - fh.cupsColorSpace = htonl(r->header.cupsColorSpace); - fh.cupsNumColors = htonl(r->header.cupsNumColors); - fh.cupsInteger[0] = htonl(r->header.cupsInteger[0]); - /* TotalPageCount */ - fh.cupsInteger[1] = htonl(r->header.cupsInteger[1]); - /* CrossFeedTransform */ - fh.cupsInteger[2] = htonl(r->header.cupsInteger[2]); - /* FeedTransform */ - fh.cupsInteger[3] = htonl(r->header.cupsInteger[3]); - /* ImageBoxLeft */ - fh.cupsInteger[4] = htonl(r->header.cupsInteger[4]); - /* ImageBoxTop */ - fh.cupsInteger[5] = htonl(r->header.cupsInteger[5]); - /* ImageBoxRight */ - fh.cupsInteger[6] = htonl(r->header.cupsInteger[6]); - /* ImageBoxBottom */ - fh.cupsInteger[7] = htonl(r->header.cupsInteger[7]); - /* BlackPrimary */ - fh.cupsInteger[8] = htonl(r->header.cupsInteger[8]); - /* PrintQuality */ - fh.cupsInteger[14] = htonl(r->header.cupsInteger[14]); - /* VendorIdentifier */ - fh.cupsInteger[15] = htonl(r->header.cupsInteger[15]); - /* VendorLength */ - - void *dst = fh.cupsReal; /* Bypass bogus compiler warning */ - void *src = r->header.cupsReal; - memcpy(dst, src, sizeof(fh.cupsReal) + sizeof(fh.cupsString)); - /* VendorData */ - - strlcpy(fh.cupsRenderingIntent, r->header.cupsRenderingIntent, - sizeof(fh.cupsRenderingIntent)); - strlcpy(fh.cupsPageSizeName, r->header.cupsPageSizeName, - sizeof(fh.cupsPageSizeName)); - - return (cups_raster_io(r, (unsigned char *)&fh, sizeof(fh)) == sizeof(fh)); - } - else if (r->mode == CUPS_RASTER_WRITE_APPLE) - { - /* - * Raw raster data is always network byte order with most of the page header - * zeroed. - */ - - unsigned char appleheader[32]; /* Raw page header */ - - if (r->apple_page_count == 0xffffffffU) - { - /* - * Write raw page count from raster page header... - */ - - r->apple_page_count = r->header.cupsInteger[0]; - - appleheader[0] = 'A'; - appleheader[1] = 'S'; - appleheader[2] = 'T'; - appleheader[3] = 0; - appleheader[4] = (unsigned char)(r->apple_page_count >> 24); - appleheader[5] = (unsigned char)(r->apple_page_count >> 16); - appleheader[6] = (unsigned char)(r->apple_page_count >> 8); - appleheader[7] = (unsigned char)(r->apple_page_count); - - if (cups_raster_io(r, appleheader, 8) != 8) - return (0); - } - - memset(appleheader, 0, sizeof(appleheader)); - - appleheader[0] = (unsigned char)r->header.cupsBitsPerPixel; - appleheader[1] = r->header.cupsColorSpace == CUPS_CSPACE_SRGB ? 1 : - r->header.cupsColorSpace == CUPS_CSPACE_RGBW ? 2 : - r->header.cupsColorSpace == CUPS_CSPACE_ADOBERGB ? 3 : - r->header.cupsColorSpace == CUPS_CSPACE_W ? 4 : - r->header.cupsColorSpace == CUPS_CSPACE_RGB ? 5 : - r->header.cupsColorSpace == CUPS_CSPACE_CMYK ? 6 : 0; - appleheader[12] = (unsigned char)(r->header.cupsWidth >> 24); - appleheader[13] = (unsigned char)(r->header.cupsWidth >> 16); - appleheader[14] = (unsigned char)(r->header.cupsWidth >> 8); - appleheader[15] = (unsigned char)(r->header.cupsWidth); - appleheader[16] = (unsigned char)(r->header.cupsHeight >> 24); - appleheader[17] = (unsigned char)(r->header.cupsHeight >> 16); - appleheader[18] = (unsigned char)(r->header.cupsHeight >> 8); - appleheader[19] = (unsigned char)(r->header.cupsHeight); - appleheader[20] = (unsigned char)(r->header.HWResolution[0] >> 24); - appleheader[21] = (unsigned char)(r->header.HWResolution[0] >> 16); - appleheader[22] = (unsigned char)(r->header.HWResolution[0] >> 8); - appleheader[23] = (unsigned char)(r->header.HWResolution[0]); - - return (cups_raster_io(r, appleheader, sizeof(appleheader)) == sizeof(appleheader)); - } - else - return (cups_raster_io(r, (unsigned char *)&(r->header), sizeof(r->header)) - == sizeof(r->header)); -} - - -/* - * 'cupsRasterWriteHeader2()' - Write a raster page header from a version 2 - * page header structure. - * - * The page header can be initialized using @link cupsRasterInitPWGHeader@. - * - * @since CUPS 1.2/macOS 10.5@ - */ - -unsigned /* O - 1 on success, 0 on failure */ -cupsRasterWriteHeader2( - cups_raster_t *r, /* I - Raster stream */ - cups_page_header2_t *h) /* I - Raster page header */ -{ - DEBUG_printf(("cupsRasterWriteHeader(r=%p, h=%p)", (void *)r, (void *)h)); - - if (r == NULL || r->mode == CUPS_RASTER_READ) - { - DEBUG_puts("1cupsRasterWriteHeader2: Returning 0."); - return (0); - } - - DEBUG_printf(("1cupsRasterWriteHeader2: cupsColorSpace=%s", cups_color_spaces[h->cupsColorSpace])); - DEBUG_printf(("1cupsRasterWriteHeader2: cupsBitsPerColor=%u", h->cupsBitsPerColor)); - DEBUG_printf(("1cupsRasterWriteHeader2: cupsBitsPerPixel=%u", h->cupsBitsPerPixel)); - DEBUG_printf(("1cupsRasterWriteHeader2: cupsBytesPerLine=%u", h->cupsBytesPerLine)); - DEBUG_printf(("1cupsRasterWriteHeader2: cupsWidth=%u", h->cupsWidth)); - DEBUG_printf(("1cupsRasterWriteHeader2: cupsHeight=%u", h->cupsHeight)); - - /* - * Make a copy of the header, and compute the number of raster - * lines in the page image... - */ - - memcpy(&(r->header), h, sizeof(cups_page_header2_t)); - - if (!cups_raster_update(r)) - { - DEBUG_puts("1cupsRasterWriteHeader: Unable to update parameters, returning 0."); - return (0); - } - - if (r->mode == CUPS_RASTER_WRITE_APPLE) - { - r->rowheight = h->HWResolution[0] / h->HWResolution[1]; - - if (h->HWResolution[0] != (r->rowheight * h->HWResolution[1])) - return (0); - } - else - r->rowheight = 1; - - /* - * Write the raster header... - */ - - if (r->mode == CUPS_RASTER_WRITE_PWG) - { - /* - * PWG raster data is always network byte order with most of the page header - * zeroed. - */ - - cups_page_header2_t fh; /* File page header */ - - memset(&fh, 0, sizeof(fh)); - strlcpy(fh.MediaClass, "PwgRaster", sizeof(fh.MediaClass)); - strlcpy(fh.MediaColor, r->header.MediaColor, sizeof(fh.MediaColor)); - strlcpy(fh.MediaType, r->header.MediaType, sizeof(fh.MediaType)); - strlcpy(fh.OutputType, r->header.OutputType, sizeof(fh.OutputType)); - strlcpy(fh.cupsRenderingIntent, r->header.cupsRenderingIntent, - sizeof(fh.cupsRenderingIntent)); - strlcpy(fh.cupsPageSizeName, r->header.cupsPageSizeName, - sizeof(fh.cupsPageSizeName)); - - fh.CutMedia = htonl(r->header.CutMedia); - fh.Duplex = htonl(r->header.Duplex); - fh.HWResolution[0] = htonl(r->header.HWResolution[0]); - fh.HWResolution[1] = htonl(r->header.HWResolution[1]); - fh.ImagingBoundingBox[0] = htonl(r->header.ImagingBoundingBox[0]); - fh.ImagingBoundingBox[1] = htonl(r->header.ImagingBoundingBox[1]); - fh.ImagingBoundingBox[2] = htonl(r->header.ImagingBoundingBox[2]); - fh.ImagingBoundingBox[3] = htonl(r->header.ImagingBoundingBox[3]); - fh.InsertSheet = htonl(r->header.InsertSheet); - fh.Jog = htonl(r->header.Jog); - fh.LeadingEdge = htonl(r->header.LeadingEdge); - fh.ManualFeed = htonl(r->header.ManualFeed); - fh.MediaPosition = htonl(r->header.MediaPosition); - fh.MediaWeight = htonl(r->header.MediaWeight); - fh.NumCopies = htonl(r->header.NumCopies); - fh.Orientation = htonl(r->header.Orientation); - fh.PageSize[0] = htonl(r->header.PageSize[0]); - fh.PageSize[1] = htonl(r->header.PageSize[1]); - fh.Tumble = htonl(r->header.Tumble); - fh.cupsWidth = htonl(r->header.cupsWidth); - fh.cupsHeight = htonl(r->header.cupsHeight); - fh.cupsBitsPerColor = htonl(r->header.cupsBitsPerColor); - fh.cupsBitsPerPixel = htonl(r->header.cupsBitsPerPixel); - fh.cupsBytesPerLine = htonl(r->header.cupsBytesPerLine); - fh.cupsColorOrder = htonl(r->header.cupsColorOrder); - fh.cupsColorSpace = htonl(r->header.cupsColorSpace); - fh.cupsNumColors = htonl(r->header.cupsNumColors); - fh.cupsInteger[0] = htonl(r->header.cupsInteger[0]); - fh.cupsInteger[1] = htonl(r->header.cupsInteger[1]); - fh.cupsInteger[2] = htonl(r->header.cupsInteger[2]); - fh.cupsInteger[3] = htonl((unsigned)(r->header.cupsImagingBBox[0] * r->header.HWResolution[0] / 72.0)); - fh.cupsInteger[4] = htonl((unsigned)(r->header.cupsImagingBBox[1] * r->header.HWResolution[1] / 72.0)); - fh.cupsInteger[5] = htonl((unsigned)(r->header.cupsImagingBBox[2] * r->header.HWResolution[0] / 72.0)); - fh.cupsInteger[6] = htonl((unsigned)(r->header.cupsImagingBBox[3] * r->header.HWResolution[1] / 72.0)); - fh.cupsInteger[7] = htonl(0xffffff); - - return (cups_raster_io(r, (unsigned char *)&fh, sizeof(fh)) == sizeof(fh)); - } - else if (r->mode == CUPS_RASTER_WRITE_APPLE) - { - /* - * Raw raster data is always network byte order with most of the page header - * zeroed. - */ - - unsigned char appleheader[32]; /* Raw page header */ - unsigned height = r->header.cupsHeight * r->rowheight; - /* Computed page height */ - - if (r->apple_page_count == 0xffffffffU) - { - /* - * Write raw page count from raster page header... - */ - - r->apple_page_count = r->header.cupsInteger[0]; - - appleheader[0] = 'A'; - appleheader[1] = 'S'; - appleheader[2] = 'T'; - appleheader[3] = 0; - appleheader[4] = (unsigned char)(r->apple_page_count >> 24); - appleheader[5] = (unsigned char)(r->apple_page_count >> 16); - appleheader[6] = (unsigned char)(r->apple_page_count >> 8); - appleheader[7] = (unsigned char)(r->apple_page_count); - - if (cups_raster_io(r, appleheader, 8) != 8) - return (0); - } - - memset(appleheader, 0, sizeof(appleheader)); - - appleheader[0] = (unsigned char)r->header.cupsBitsPerPixel; - appleheader[1] = r->header.cupsColorSpace == CUPS_CSPACE_SRGB ? 1 : - r->header.cupsColorSpace == CUPS_CSPACE_RGBW ? 2 : - r->header.cupsColorSpace == CUPS_CSPACE_ADOBERGB ? 3 : - r->header.cupsColorSpace == CUPS_CSPACE_W ? 4 : - r->header.cupsColorSpace == CUPS_CSPACE_RGB ? 5 : - r->header.cupsColorSpace == CUPS_CSPACE_CMYK ? 6 : 0; - appleheader[12] = (unsigned char)(r->header.cupsWidth >> 24); - appleheader[13] = (unsigned char)(r->header.cupsWidth >> 16); - appleheader[14] = (unsigned char)(r->header.cupsWidth >> 8); - appleheader[15] = (unsigned char)(r->header.cupsWidth); - appleheader[16] = (unsigned char)(height >> 24); - appleheader[17] = (unsigned char)(height >> 16); - appleheader[18] = (unsigned char)(height >> 8); - appleheader[19] = (unsigned char)(height); - appleheader[20] = (unsigned char)(r->header.HWResolution[0] >> 24); - appleheader[21] = (unsigned char)(r->header.HWResolution[0] >> 16); - appleheader[22] = (unsigned char)(r->header.HWResolution[0] >> 8); - appleheader[23] = (unsigned char)(r->header.HWResolution[0]); - - return (cups_raster_io(r, appleheader, sizeof(appleheader)) == sizeof(appleheader)); - } - else - return (cups_raster_io(r, (unsigned char *)&(r->header), sizeof(r->header)) - == sizeof(r->header)); -} - - -/* - * 'cupsRasterWritePixels()' - Write raster pixels. - * - * For best performance, filters should write one or more whole lines. - * The "cupsBytesPerLine" value from the page header can be used to allocate - * the line buffer and as the number of bytes to write. - */ - -unsigned /* O - Number of bytes written */ -cupsRasterWritePixels(cups_raster_t *r, /* I - Raster stream */ - unsigned char *p, /* I - Bytes to write */ - unsigned len)/* I - Number of bytes to write */ -{ - ssize_t bytes; /* Bytes read */ - unsigned remaining; /* Bytes remaining */ - - - DEBUG_printf(("cupsRasterWritePixels(r=%p, p=%p, len=%u), remaining=%u", (void *)r, (void *)p, len, r->remaining)); - - if (r == NULL || r->mode == CUPS_RASTER_READ || r->remaining == 0) - return (0); - - if (!r->compressed) - { - /* - * Without compression, just write the raster data raw unless the data needs - * to be swapped... - */ - - r->remaining -= len / r->header.cupsBytesPerLine; - - if (r->swapped && - (r->header.cupsBitsPerColor == 16 || - r->header.cupsBitsPerPixel == 12 || - r->header.cupsBitsPerPixel == 16)) - { - unsigned char *bufptr; /* Pointer into write buffer */ - - /* - * Allocate a write buffer as needed... - */ - - if ((size_t)len > r->bufsize) - { - if (r->buffer) - bufptr = realloc(r->buffer, len); - else - bufptr = malloc(len); - - if (!bufptr) - return (0); - - r->buffer = bufptr; - r->bufsize = len; - } - - /* - * Byte swap the pixels and write them... - */ - - cups_swap_copy(r->buffer, p, len); - - bytes = cups_raster_io(r, r->buffer, len); - } - else - bytes = cups_raster_io(r, p, len); - - if (bytes < len) - return (0); - else - return (len); - } - - /* - * Otherwise, compress each line... - */ - - for (remaining = len; remaining > 0; remaining -= (unsigned)bytes, p += bytes) - { - /* - * Figure out the number of remaining bytes on the current line... - */ - - if ((bytes = (ssize_t)remaining) > (ssize_t)(r->pend - r->pcurrent)) - bytes = (ssize_t)(r->pend - r->pcurrent); - - if (r->count > 0) - { - /* - * Check to see if this line is the same as the previous line... - */ - - if (memcmp(p, r->pcurrent, (size_t)bytes)) - { - if (cups_raster_write(r, r->pixels) <= 0) - return (0); - - r->count = 0; - } - else - { - /* - * Mark more bytes as the same... - */ - - r->pcurrent += bytes; - - if (r->pcurrent >= r->pend) - { - /* - * Increase the repeat count... - */ - - r->count += r->rowheight; - r->pcurrent = r->pixels; - - /* - * Flush out this line if it is the last one... - */ - - r->remaining --; - - if (r->remaining == 0) - { - if (cups_raster_write(r, r->pixels) <= 0) - return (0); - else - return (len); - } - else if (r->count > (256 - r->rowheight)) - { - if (cups_raster_write(r, r->pixels) <= 0) - return (0); - - r->count = 0; - } - } - - continue; - } - } - - if (r->count == 0) - { - /* - * Copy the raster data to the buffer... - */ - - memcpy(r->pcurrent, p, (size_t)bytes); - - r->pcurrent += bytes; - - if (r->pcurrent >= r->pend) - { - /* - * Increase the repeat count... - */ - - r->count += r->rowheight; - r->pcurrent = r->pixels; - - /* - * Flush out this line if it is the last one... - */ - - r->remaining --; - - if (r->remaining == 0) - { - if (cups_raster_write(r, r->pixels) <= 0) - return (0); - } - } - } - } - - return (len); -} - - -/* - * 'cups_raster_read_header()' - Read a raster page header. - */ - -static unsigned /* O - 1 on success, 0 on fail */ -cups_raster_read_header( - cups_raster_t *r) /* I - Raster stream */ -{ - size_t len; /* Length for read/swap */ - - - DEBUG_printf(("3cups_raster_read_header(r=%p), r->mode=%s", (void *)r, r ? cups_modes[r->mode] : "")); - - if (r == NULL || r->mode != CUPS_RASTER_READ) - return (0); - - DEBUG_printf(("4cups_raster_read_header: r->iocount=" CUPS_LLFMT, CUPS_LLCAST r->iocount)); - - memset(&(r->header), 0, sizeof(r->header)); - - /* - * Read the header... - */ - - switch (r->sync) - { - default : - /* - * Get the length of the raster header... - */ - - if (r->sync == CUPS_RASTER_SYNCv1 || r->sync == CUPS_RASTER_REVSYNCv1) - len = sizeof(cups_page_header_t); - else - len = sizeof(cups_page_header2_t); - - DEBUG_printf(("4cups_raster_read_header: len=%d", (int)len)); - - /* - * Read it... - */ - - if (cups_raster_read(r, (unsigned char *)&(r->header), len) < (ssize_t)len) - { - DEBUG_printf(("4cups_raster_read_header: EOF, r->iocount=" CUPS_LLFMT, CUPS_LLCAST r->iocount)); - return (0); - } - - /* - * Swap bytes as needed... - */ - - if (r->swapped) - { - unsigned *s, /* Current word */ - temp; /* Temporary copy */ - - - DEBUG_puts("4cups_raster_read_header: Swapping header bytes."); - - for (len = 81, s = &(r->header.AdvanceDistance); - len > 0; - len --, s ++) - { - temp = *s; - *s = ((temp & 0xff) << 24) | - ((temp & 0xff00) << 8) | - ((temp & 0xff0000) >> 8) | - ((temp & 0xff000000) >> 24); - - DEBUG_printf(("4cups_raster_read_header: %08x => %08x", temp, *s)); - } - } - break; - - case CUPS_RASTER_SYNCapple : - case CUPS_RASTER_REVSYNCapple : - { - unsigned char appleheader[32]; /* Raw header */ - static const unsigned rawcspace[] = - { - CUPS_CSPACE_SW, - CUPS_CSPACE_SRGB, - CUPS_CSPACE_RGBW, - CUPS_CSPACE_ADOBERGB, - CUPS_CSPACE_W, - CUPS_CSPACE_RGB, - CUPS_CSPACE_CMYK - }; - static const unsigned rawnumcolors[] = - { - 1, - 3, - 4, - 3, - 1, - 3, - 4 - }; - - if (cups_raster_read(r, appleheader, sizeof(appleheader)) < (ssize_t)sizeof(appleheader)) - { - DEBUG_printf(("4cups_raster_read_header: EOF, r->iocount=" CUPS_LLFMT, CUPS_LLCAST r->iocount)); - return (0); - } - - strlcpy(r->header.MediaClass, "PwgRaster", sizeof(r->header.MediaClass)); - /* PwgRaster */ - r->header.cupsBitsPerPixel = appleheader[0]; - r->header.cupsColorSpace = appleheader[1] >= (sizeof(rawcspace) / sizeof(rawcspace[0])) ? CUPS_CSPACE_DEVICE1 : rawcspace[appleheader[1]]; - r->header.cupsNumColors = appleheader[1] >= (sizeof(rawnumcolors) / sizeof(rawnumcolors[0])) ? 1 : rawnumcolors[appleheader[1]]; - r->header.cupsBitsPerColor = r->header.cupsBitsPerPixel / r->header.cupsNumColors; - r->header.cupsWidth = ((((((unsigned)appleheader[12] << 8) | (unsigned)appleheader[13]) << 8) | (unsigned)appleheader[14]) << 8) | (unsigned)appleheader[15]; - r->header.cupsHeight = ((((((unsigned)appleheader[16] << 8) | (unsigned)appleheader[17]) << 8) | (unsigned)appleheader[18]) << 8) | (unsigned)appleheader[19]; - r->header.cupsBytesPerLine = r->header.cupsWidth * r->header.cupsBitsPerPixel / 8; - r->header.cupsColorOrder = CUPS_ORDER_CHUNKED; - r->header.HWResolution[0] = r->header.HWResolution[1] = ((((((unsigned)appleheader[20] << 8) | (unsigned)appleheader[21]) << 8) | (unsigned)appleheader[22]) << 8) | (unsigned)appleheader[23]; - - if (r->header.HWResolution[0] > 0) - { - r->header.PageSize[0] = (unsigned)(r->header.cupsWidth * 72 / r->header.HWResolution[0]); - r->header.PageSize[1] = (unsigned)(r->header.cupsHeight * 72 / r->header.HWResolution[1]); - r->header.cupsPageSize[0] = (float)(r->header.cupsWidth * 72.0 / r->header.HWResolution[0]); - r->header.cupsPageSize[1] = (float)(r->header.cupsHeight * 72.0 / r->header.HWResolution[1]); - } - - r->header.cupsInteger[0] = r->apple_page_count; - r->header.cupsInteger[7] = 0xffffff; - } - break; - } - - /* - * Update the header and row count... - */ - - if (!cups_raster_update(r)) - return (0); - - DEBUG_printf(("4cups_raster_read_header: cupsBitsPerPixel=%u, cupsBitsPerColor=%u, cupsBytesPerLine=%u, cupsWidth=%u, cupsHeight=%u, r->bpp=%d", r->header.cupsBitsPerPixel, r->header.cupsBitsPerColor, r->header.cupsBytesPerLine, r->header.cupsWidth, r->header.cupsHeight, r->bpp)); - - return (r->header.cupsBitsPerPixel > 0 && r->header.cupsBitsPerPixel <= 240 && r->header.cupsBitsPerColor > 0 && r->header.cupsBitsPerColor <= 16 && r->header.cupsBytesPerLine > 0 && r->header.cupsBytesPerLine <= 0x7fffffff && r->header.cupsHeight != 0 && (r->header.cupsBytesPerLine % r->bpp) == 0); -} - - -/* - * 'cups_raster_io()' - Read/write bytes from a context, handling interruptions. - */ - -static ssize_t /* O - Bytes read/write or -1 */ -cups_raster_io(cups_raster_t *r, /* I - Raster stream */ - unsigned char *buf, /* I - Buffer for read/write */ - size_t bytes) /* I - Number of bytes to read/write */ -{ - ssize_t count, /* Number of bytes read/written */ - total; /* Total bytes read/written */ - - - DEBUG_printf(("5cups_raster_io(r=%p, buf=%p, bytes=" CUPS_LLFMT ")", (void *)r, (void *)buf, CUPS_LLCAST bytes)); - - for (total = 0; total < (ssize_t)bytes; total += count, buf += count) - { - count = (*r->iocb)(r->ctx, buf, bytes - (size_t)total); - - DEBUG_printf(("6cups_raster_io: count=%d, total=%d", (int)count, (int)total)); - if (count == 0) - break; -// { -// DEBUG_puts("6cups_raster_io: Returning 0."); -// return (0); -// } - else if (count < 0) - { - DEBUG_puts("6cups_raster_io: Returning -1 on error."); - return (-1); - } - -#ifdef DEBUG - r->iocount += (size_t)count; -#endif /* DEBUG */ - } - - DEBUG_printf(("6cups_raster_io: iocount=" CUPS_LLFMT, CUPS_LLCAST r->iocount)); - DEBUG_printf(("6cups_raster_io: Returning " CUPS_LLFMT ".", CUPS_LLCAST total)); - - return (total); -} - - -/* - * 'cups_raster_read()' - Read through the raster buffer. - */ - -static ssize_t /* O - Number of bytes read */ -cups_raster_read(cups_raster_t *r, /* I - Raster stream */ - unsigned char *buf, /* I - Buffer */ - size_t bytes) /* I - Number of bytes to read */ -{ - ssize_t count, /* Number of bytes read */ - remaining, /* Remaining bytes in buffer */ - total; /* Total bytes read */ - - - DEBUG_printf(("4cups_raster_read(r=%p, buf=%p, bytes=" CUPS_LLFMT "), offset=" CUPS_LLFMT, (void *)r, (void *)buf, CUPS_LLCAST bytes, CUPS_LLCAST (r->iostart + r->bufptr - r->buffer))); - - if (!r->compressed) - return (cups_raster_io(r, buf, bytes)); - - /* - * Allocate a read buffer as needed... - */ - - count = (ssize_t)(2 * r->header.cupsBytesPerLine); - if (count < 65536) - count = 65536; - - if ((size_t)count > r->bufsize) - { - ssize_t offset = r->bufptr - r->buffer; - /* Offset to current start of buffer */ - ssize_t end = r->bufend - r->buffer;/* Offset to current end of buffer */ - unsigned char *rptr; /* Pointer in read buffer */ - - if (r->buffer) - rptr = realloc(r->buffer, (size_t)count); - else - rptr = malloc((size_t)count); - - if (!rptr) - return (0); - - r->buffer = rptr; - r->bufptr = rptr + offset; - r->bufend = rptr + end; - r->bufsize = (size_t)count; - } - - /* - * Loop until we have read everything... - */ - - for (total = 0, remaining = (int)(r->bufend - r->bufptr); - total < (ssize_t)bytes; - total += count, buf += count) - { - count = (ssize_t)bytes - total; - - DEBUG_printf(("5cups_raster_read: count=" CUPS_LLFMT ", remaining=" CUPS_LLFMT ", buf=%p, bufptr=%p, bufend=%p", CUPS_LLCAST count, CUPS_LLCAST remaining, (void *)buf, (void *)r->bufptr, (void *)r->bufend)); - - if (remaining == 0) - { - if (count < 16) - { - /* - * Read into the raster buffer and then copy... - */ - -#ifdef DEBUG - r->iostart += (size_t)(r->bufend - r->buffer); -#endif /* DEBUG */ - - remaining = (*r->iocb)(r->ctx, r->buffer, r->bufsize); - if (remaining <= 0) - return (0); - - r->bufptr = r->buffer; - r->bufend = r->buffer + remaining; - -#ifdef DEBUG - r->iocount += (size_t)remaining; -#endif /* DEBUG */ - } - else - { - /* - * Read directly into "buf"... - */ - - count = (*r->iocb)(r->ctx, buf, (size_t)count); - - if (count <= 0) - return (0); - -#ifdef DEBUG - r->iostart += (size_t)count; - r->iocount += (size_t)count; -#endif /* DEBUG */ - - continue; - } - } - - /* - * Copy bytes from raster buffer to "buf"... - */ - - if (count > remaining) - count = remaining; - - if (count == 1) - { - /* - * Copy 1 byte... - */ - - *buf = *(r->bufptr)++; - remaining --; - } - else if (count < 128) - { - /* - * Copy up to 127 bytes without using memcpy(); this is - * faster because it avoids an extra function call and is - * often further optimized by the compiler... - */ - - unsigned char *bufptr; /* Temporary buffer pointer */ - - remaining -= count; - - for (bufptr = r->bufptr; count > 0; count --, total ++) - *buf++ = *bufptr++; - - r->bufptr = bufptr; - } - else - { - /* - * Use memcpy() for a large read... - */ - - memcpy(buf, r->bufptr, (size_t)count); - r->bufptr += count; - remaining -= count; - } - } - - DEBUG_printf(("5cups_raster_read: Returning %ld", (long)total)); - - return (total); -} - - -/* - * 'cups_raster_update()' - Update the raster header and row count for the - * current page. - */ - -static int /* O - 1 on success, 0 on failure */ -cups_raster_update(cups_raster_t *r) /* I - Raster stream */ -{ - if (r->sync == CUPS_RASTER_SYNCv1 || r->sync == CUPS_RASTER_REVSYNCv1 || - r->header.cupsNumColors == 0) - { - /* - * Set the "cupsNumColors" field according to the colorspace... - */ - - switch (r->header.cupsColorSpace) - { - case CUPS_CSPACE_W : - case CUPS_CSPACE_K : - case CUPS_CSPACE_WHITE : - case CUPS_CSPACE_GOLD : - case CUPS_CSPACE_SILVER : - case CUPS_CSPACE_SW : - r->header.cupsNumColors = 1; - break; - - case CUPS_CSPACE_RGB : - case CUPS_CSPACE_CMY : - case CUPS_CSPACE_YMC : - case CUPS_CSPACE_CIEXYZ : - case CUPS_CSPACE_CIELab : - case CUPS_CSPACE_SRGB : - case CUPS_CSPACE_ADOBERGB : - case CUPS_CSPACE_ICC1 : - case CUPS_CSPACE_ICC2 : - case CUPS_CSPACE_ICC3 : - case CUPS_CSPACE_ICC4 : - case CUPS_CSPACE_ICC5 : - case CUPS_CSPACE_ICC6 : - case CUPS_CSPACE_ICC7 : - case CUPS_CSPACE_ICC8 : - case CUPS_CSPACE_ICC9 : - case CUPS_CSPACE_ICCA : - case CUPS_CSPACE_ICCB : - case CUPS_CSPACE_ICCC : - case CUPS_CSPACE_ICCD : - case CUPS_CSPACE_ICCE : - case CUPS_CSPACE_ICCF : - r->header.cupsNumColors = 3; - break; - - case CUPS_CSPACE_RGBA : - case CUPS_CSPACE_RGBW : - case CUPS_CSPACE_CMYK : - case CUPS_CSPACE_YMCK : - case CUPS_CSPACE_KCMY : - case CUPS_CSPACE_GMCK : - case CUPS_CSPACE_GMCS : - r->header.cupsNumColors = 4; - break; - - case CUPS_CSPACE_KCMYcm : - if (r->header.cupsBitsPerPixel < 8) - r->header.cupsNumColors = 6; - else - r->header.cupsNumColors = 4; - break; - - case CUPS_CSPACE_DEVICE1 : - case CUPS_CSPACE_DEVICE2 : - case CUPS_CSPACE_DEVICE3 : - case CUPS_CSPACE_DEVICE4 : - case CUPS_CSPACE_DEVICE5 : - case CUPS_CSPACE_DEVICE6 : - case CUPS_CSPACE_DEVICE7 : - case CUPS_CSPACE_DEVICE8 : - case CUPS_CSPACE_DEVICE9 : - case CUPS_CSPACE_DEVICEA : - case CUPS_CSPACE_DEVICEB : - case CUPS_CSPACE_DEVICEC : - case CUPS_CSPACE_DEVICED : - case CUPS_CSPACE_DEVICEE : - case CUPS_CSPACE_DEVICEF : - r->header.cupsNumColors = r->header.cupsColorSpace - - CUPS_CSPACE_DEVICE1 + 1; - break; - - default : - /* Unknown color space */ - return (0); - } - } - - /* - * Set the number of bytes per pixel/color... - */ - - if (r->header.cupsColorOrder == CUPS_ORDER_CHUNKED) - r->bpp = (r->header.cupsBitsPerPixel + 7) / 8; - else - r->bpp = (r->header.cupsBitsPerColor + 7) / 8; - - if (r->bpp == 0) - r->bpp = 1; - - /* - * Set the number of remaining rows... - */ - - if (r->header.cupsColorOrder == CUPS_ORDER_PLANAR) - r->remaining = r->header.cupsHeight * r->header.cupsNumColors; - else - r->remaining = r->header.cupsHeight; - - /* - * Allocate the compression buffer... - */ - - if (r->compressed) - { - if (r->pixels != NULL) - free(r->pixels); - - if ((r->pixels = calloc(r->header.cupsBytesPerLine, 1)) == NULL) - { - r->pcurrent = NULL; - r->pend = NULL; - r->count = 0; - - return (0); - } - - r->pcurrent = r->pixels; - r->pend = r->pixels + r->header.cupsBytesPerLine; - r->count = 0; - } - - return (1); -} - - -/* - * 'cups_raster_write()' - Write a row of compressed raster data... - */ - -static ssize_t /* O - Number of bytes written */ -cups_raster_write( - cups_raster_t *r, /* I - Raster stream */ - const unsigned char *pixels) /* I - Pixel data to write */ -{ - const unsigned char *start, /* Start of sequence */ - *ptr, /* Current pointer in sequence */ - *pend, /* End of raster buffer */ - *plast; /* Pointer to last pixel */ - unsigned char *wptr; /* Pointer into write buffer */ - unsigned bpp, /* Bytes per pixel */ - count; /* Count */ - _cups_copyfunc_t cf; /* Copy function */ - - - DEBUG_printf(("3cups_raster_write(r=%p, pixels=%p)", (void *)r, (void *)pixels)); - - /* - * Determine whether we need to swap bytes... - */ - - if (r->swapped && (r->header.cupsBitsPerColor == 16 || r->header.cupsBitsPerPixel == 12 || r->header.cupsBitsPerPixel == 16)) - { - DEBUG_puts("4cups_raster_write: Swapping bytes when writing."); - cf = (_cups_copyfunc_t)cups_swap_copy; - } - else - cf = (_cups_copyfunc_t)memcpy; - - /* - * Allocate a write buffer as needed... - */ - - count = r->header.cupsBytesPerLine * 2; - if (count < 65536) - count = 65536; - - if ((size_t)count > r->bufsize) - { - if (r->buffer) - wptr = realloc(r->buffer, count); - else - wptr = malloc(count); - - if (!wptr) - { - DEBUG_printf(("4cups_raster_write: Unable to allocate " CUPS_LLFMT " bytes for raster buffer: %s", CUPS_LLCAST count, strerror(errno))); - return (-1); - } - - r->buffer = wptr; - r->bufsize = count; - } - - /* - * Write the row repeat count... - */ - - bpp = r->bpp; - pend = pixels + r->header.cupsBytesPerLine; - plast = pend - bpp; - wptr = r->buffer; - *wptr++ = (unsigned char)(r->count - 1); - - /* - * Write using a modified PackBits compression... - */ - - for (ptr = pixels; ptr < pend;) - { - start = ptr; - ptr += bpp; - - if (ptr == pend) - { - /* - * Encode a single pixel at the end... - */ - - *wptr++ = 0; - (*cf)(wptr, start, bpp); - wptr += bpp; - } - else if (!memcmp(start, ptr, bpp)) - { - /* - * Encode a sequence of repeating pixels... - */ - - for (count = 2; count < 128 && ptr < plast; count ++, ptr += bpp) - if (memcmp(ptr, ptr + bpp, bpp)) - break; - - *wptr++ = (unsigned char)(count - 1); - (*cf)(wptr, ptr, bpp); - wptr += bpp; - ptr += bpp; - } - else - { - /* - * Encode a sequence of non-repeating pixels... - */ - - for (count = 1; count < 128 && ptr < plast; count ++, ptr += bpp) - if (!memcmp(ptr, ptr + bpp, bpp)) - break; - - if (ptr >= plast && count < 128) - { - count ++; - ptr += bpp; - } - - *wptr++ = (unsigned char)(257 - count); - - count *= bpp; - (*cf)(wptr, start, count); - wptr += count; - } - } - - DEBUG_printf(("4cups_raster_write: Writing " CUPS_LLFMT " bytes.", CUPS_LLCAST (wptr - r->buffer))); - - return (cups_raster_io(r, r->buffer, (size_t)(wptr - r->buffer))); -} - - -/* - * 'cups_read_fd()' - Read bytes from a file. - */ - -static ssize_t /* O - Bytes read or -1 */ -cups_read_fd(void *ctx, /* I - File descriptor as pointer */ - unsigned char *buf, /* I - Buffer for read */ - size_t bytes) /* I - Maximum number of bytes to read */ -{ - int fd = (int)((intptr_t)ctx); - /* File descriptor */ - ssize_t count; /* Number of bytes read */ - - -#ifdef WIN32 /* Sigh */ - while ((count = read(fd, buf, (unsigned)bytes)) < 0) -#else - while ((count = read(fd, buf, bytes)) < 0) -#endif /* WIN32 */ - if (errno != EINTR && errno != EAGAIN) - { - DEBUG_printf(("8cups_read_fd: %s", strerror(errno))); - return (-1); - } - - DEBUG_printf(("8cups_read_fd: Returning %d bytes.", (int)count)); - - return (count); -} - - -/* - * 'cups_swap()' - Swap bytes in raster data... - */ - -static void -cups_swap(unsigned char *buf, /* I - Buffer to swap */ - size_t bytes) /* I - Number of bytes to swap */ -{ - unsigned char even, odd; /* Temporary variables */ - - - bytes /= 2; - - while (bytes > 0) - { - even = buf[0]; - odd = buf[1]; - buf[0] = odd; - buf[1] = even; - - buf += 2; - bytes --; - } -} - - -/* - * 'cups_swap_copy()' - Copy and swap bytes in raster data... - */ - -static void -cups_swap_copy( - unsigned char *dst, /* I - Destination */ - const unsigned char *src, /* I - Source */ - size_t bytes) /* I - Number of bytes to swap */ -{ - bytes /= 2; - - while (bytes > 0) - { - dst[0] = src[1]; - dst[1] = src[0]; - - dst += 2; - src += 2; - bytes --; - } -} - - -/* - * 'cups_write_fd()' - Write bytes to a file. - */ - -static ssize_t /* O - Bytes written or -1 */ -cups_write_fd(void *ctx, /* I - File descriptor pointer */ - unsigned char *buf, /* I - Bytes to write */ - size_t bytes) /* I - Number of bytes to write */ -{ - int fd = (int)((intptr_t)ctx); - /* File descriptor */ - ssize_t count; /* Number of bytes written */ - - -#ifdef WIN32 /* Sigh */ - while ((count = write(fd, buf, (unsigned)bytes)) < 0) -#else - while ((count = write(fd, buf, bytes)) < 0) -#endif /* WIN32 */ - if (errno != EINTR && errno != EAGAIN) - { - DEBUG_printf(("8cups_write_fd: %s", strerror(errno))); - return (-1); - } - - return (count); -} diff --git a/filter/rasterbench.c b/filter/rasterbench.c deleted file mode 100644 index f7d8c56c5..000000000 --- a/filter/rasterbench.c +++ /dev/null @@ -1,336 +0,0 @@ -/* - * Raster benchmark program for CUPS. - * - * Copyright 2007-2016 by Apple Inc. - * Copyright 1997-2006 by Easy Software Products. - * - * Licensed under Apache License v2.0. See the file "LICENSE" for more information. - */ - -/* - * Include necessary headers... - */ - -#include -#include -#include -#include -#include -#include -#include -#include - - -/* - * Constants... - */ - -#define TEST_WIDTH 1024 -#define TEST_HEIGHT 1024 -#define TEST_PAGES 16 -#define TEST_PASSES 20 - - -/* - * Local functions... - */ - -static double compute_median(double *secs); -static double get_time(void); -static void read_test(int fd); -static int run_read_test(void); -static void write_test(int fd, cups_mode_t mode); - - -/* - * 'main()' - Benchmark the raster read/write functions. - */ - -int /* O - Exit status */ -main(int argc, /* I - Number of command-line args */ - char *argv[]) /* I - Command-line arguments */ -{ - int i; /* Looping var */ - int ras_fd, /* File descriptor for read process */ - status; /* Exit status of read process */ - double start_secs, /* Start time */ - write_secs, /* Write time */ - read_secs, /* Read time */ - pass_secs[TEST_PASSES]; /* Total test times */ - cups_mode_t mode; /* Write mode */ - - - /* - * See if we have anything on the command-line... - */ - - if (argc > 2 || (argc == 2 && strcmp(argv[1], "-z"))) - { - puts("Usage: rasterbench [-z]"); - return (1); - } - - mode = argc > 1 ? CUPS_RASTER_WRITE_COMPRESSED : CUPS_RASTER_WRITE; - - /* - * Ignore SIGPIPE... - */ - - signal(SIGPIPE, SIG_IGN); - - /* - * Run the tests several times to get a good average... - */ - - printf("Test read/write speed of %d pages, %dx%d pixels...\n\n", - TEST_PAGES, TEST_WIDTH, TEST_HEIGHT); - for (i = 0; i < TEST_PASSES; i ++) - { - printf("PASS %2d: ", i + 1); - fflush(stdout); - - ras_fd = run_read_test(); - start_secs = get_time(); - - write_test(ras_fd, mode); - - write_secs = get_time(); - printf(" %.3f write,", write_secs - start_secs); - fflush(stdout); - - close(ras_fd); - wait(&status); - - read_secs = get_time(); - pass_secs[i] = read_secs - start_secs; - printf(" %.3f read, %.3f total\n", read_secs - write_secs, pass_secs[i]); - } - - printf("\nMedian Total Time: %.3f seconds per document\n", - compute_median(pass_secs)); - - return (0); -} - - -/* - * 'compute_median()' - Compute the median time for a test. - */ - -static double /* O - Median time in seconds */ -compute_median(double *secs) /* I - Array of time samples */ -{ - int i, j; /* Looping vars */ - double temp; /* Swap variable */ - - - /* - * Sort the array into ascending order using a quicky bubble sort... - */ - - for (i = 0; i < (TEST_PASSES - 1); i ++) - for (j = i + 1; j < TEST_PASSES; j ++) - if (secs[i] > secs[j]) - { - temp = secs[i]; - secs[i] = secs[j]; - secs[j] = temp; - } - - /* - * Return the average of the middle two samples... - */ - - return (0.5 * (secs[TEST_PASSES / 2 - 1] + secs[TEST_PASSES / 2])); -} - - -/* - * 'get_time()' - Get the current time in seconds. - */ - -static double /* O - Time in seconds */ -get_time(void) -{ - struct timeval curtime; /* Current time */ - - - gettimeofday(&curtime, NULL); - return (curtime.tv_sec + 0.000001 * curtime.tv_usec); -} - - -/* - * 'read_test()' - Benchmark the raster read functions. - */ - -static void -read_test(int fd) /* I - File descriptor to read from */ -{ - unsigned y; /* Looping var */ - cups_raster_t *r; /* Raster stream */ - cups_page_header2_t header; /* Page header */ - unsigned char buffer[8 * TEST_WIDTH]; - /* Read buffer */ - - - /* - * Test read speed... - */ - - if ((r = cupsRasterOpen(fd, CUPS_RASTER_READ)) == NULL) - { - perror("Unable to create raster input stream"); - return; - } - - while (cupsRasterReadHeader2(r, &header)) - { - for (y = 0; y < header.cupsHeight; y ++) - cupsRasterReadPixels(r, buffer, header.cupsBytesPerLine); - } - - cupsRasterClose(r); -} - - -/* - * 'run_read_test()' - Run the read test as a child process via pipes. - */ - -static int /* O - Standard input of child */ -run_read_test(void) -{ - int ras_pipes[2]; /* Raster data pipes */ - int pid; /* Child process ID */ - - - if (pipe(ras_pipes)) - return (-1); - - if ((pid = fork()) < 0) - { - /* - * Fork error - return -1 on error... - */ - - close(ras_pipes[0]); - close(ras_pipes[1]); - - return (-1); - } - else if (pid == 0) - { - /* - * Child comes here - read data from the input pipe... - */ - - close(ras_pipes[1]); - read_test(ras_pipes[0]); - exit(0); - } - else - { - /* - * Parent comes here - return the output pipe... - */ - - close(ras_pipes[0]); - return (ras_pipes[1]); - } -} - - -/* - * 'write_test()' - Benchmark the raster write functions. - */ - -static void -write_test(int fd, /* I - File descriptor to write to */ - cups_mode_t mode) /* I - Write mode */ -{ - unsigned page, x, y; /* Looping vars */ - unsigned count; /* Number of bytes to set */ - cups_raster_t *r; /* Raster stream */ - cups_page_header2_t header; /* Page header */ - unsigned char data[32][8 * TEST_WIDTH]; - /* Raster data to write */ - - - /* - * Create a combination of random data and repeated data to simulate - * text with some whitespace. - */ - - CUPS_SRAND(time(NULL)); - - memset(data, 0, sizeof(data)); - - for (y = 0; y < 28; y ++) - { - for (x = CUPS_RAND() & 127, count = (CUPS_RAND() & 15) + 1; - x < sizeof(data[0]); - x ++, count --) - { - if (count <= 0) - { - x += (CUPS_RAND() & 15) + 1; - count = (CUPS_RAND() & 15) + 1; - - if (x >= sizeof(data[0])) - break; - } - - data[y][x] = (unsigned char)CUPS_RAND(); - } - } - - /* - * Test write speed... - */ - - if ((r = cupsRasterOpen(fd, mode)) == NULL) - { - perror("Unable to create raster output stream"); - return; - } - - for (page = 0; page < TEST_PAGES; page ++) - { - memset(&header, 0, sizeof(header)); - header.cupsWidth = TEST_WIDTH; - header.cupsHeight = TEST_HEIGHT; - header.cupsBytesPerLine = TEST_WIDTH; - - if (page & 1) - { - header.cupsBytesPerLine *= 4; - 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); - - for (y = 0; y < TEST_HEIGHT; y ++) - cupsRasterWritePixels(r, data[y & 31], header.cupsBytesPerLine); - } - - cupsRasterClose(r); -} diff --git a/filter/testclient.c b/filter/testclient.c deleted file mode 100644 index aa3e3e039..000000000 --- a/filter/testclient.c +++ /dev/null @@ -1,954 +0,0 @@ -/* - * Simulated client test program for CUPS. - * - * Copyright 2017 by Apple Inc. - * - * Licensed under Apache License v2.0. See the file "LICENSE" for more information. - */ - -/* - * Include necessary headers... - */ - -#include -#include -#include -#include -#include -#include - - -/* - * Local types... - */ - -typedef struct _client_monitor_s -{ - const char *uri, /* Printer URI */ - *hostname, /* Hostname */ - *user, /* Username */ - *resource; /* Resource path */ - int port; /* Port number */ - http_encryption_t encryption; /* Use encryption? */ - ipp_pstate_t printer_state; /* Current printer state */ - char printer_state_reasons[1024]; - /* Current printer-state-reasons */ - int job_id; /* Job ID for submitted job */ - ipp_jstate_t job_state; /* Current job state */ - char job_state_reasons[1024]; - /* Current job-state-reasons */ -} _client_monitor_t; - - -/* - * Local functions... - */ - -static const char *make_raster_file(ipp_t *response, int grayscale, char *tempname, size_t tempsize, const char **format); -static void *monitor_printer(_client_monitor_t *monitor); -static void show_attributes(const char *title, int request, ipp_t *ipp); -static void show_capabilities(ipp_t *response); -static void usage(void); - - -/* - * 'main()' - Main entry. - */ - -int /* O - Exit status */ -main(int argc, /* I - Number of command-line arguments */ - char *argv[]) /* I - Command-line arguments */ -{ - int i; /* Looping var */ - const char *opt, /* Current option */ - *uri = NULL, /* Printer URI */ - *printfile = NULL, - /* Print file */ - *printformat = NULL; - /* Print format */ - int keepfile = 0, /* Keep temp file? */ - grayscale = 0, /* Force grayscale? */ - verbosity = 0; /* Verbosity */ - char tempfile[1024] = "", - /* Temporary file (if any) */ - scheme[32], /* URI scheme */ - userpass[256], /* Username:password */ - hostname[256], /* Hostname */ - resource[256]; /* Resource path */ - int port; /* Port number */ - http_encryption_t encryption; /* Encryption mode */ - _client_monitor_t monitor; /* Monitoring data */ - http_t *http; /* HTTP connection */ - ipp_t *request, /* IPP request */ - *response; /* IPP response */ - ipp_attribute_t *attr; /* IPP attribute */ - static const char * const pattrs[] = /* Printer attributes we are interested in */ - { - "job-template", - "printer-defaults", - "printer-description", - "media-col-database", - "media-col-ready" - }; - - - /* - * Parse command-line options... - */ - - for (i = 1; i < argc; i ++) - { - if (argv[i][0] == '-') - { - for (opt = argv[i] + 1; *opt; opt ++) - { - switch (*opt) - { - case 'd' : /* -d document-format */ - if (printformat) - { - puts("Document format can only be specified once."); - usage(); - return (1); - } - - i ++; - if (i >= argc) - { - puts("Expected document format after '-d'."); - usage(); - return (1); - } - - printformat = argv[i]; - break; - - case 'f' : /* -f print-file */ - if (printfile) - { - puts("Print file can only be specified once."); - usage(); - return (1); - } - - i ++; - if (i >= argc) - { - puts("Expected print file after '-f'."); - usage(); - return (1); - } - - printfile = argv[i]; - break; - - case 'g' : - grayscale = 1; - break; - - case 'k' : - keepfile = 1; - break; - - case 'v' : - verbosity ++; - break; - - default : - printf("Unknown option '-%c'.\n", *opt); - usage(); - return (1); - } - } - } - else if (uri || (strncmp(argv[i], "ipp://", 6) && strncmp(argv[i], "ipps://", 7))) - { - printf("Unknown command-line argument '%s'.\n", argv[i]); - usage(); - return (1); - } - else - uri = argv[i]; - } - - /* - * Make sure we have everything we need. - */ - - if (!uri) - { - puts("Expected printer URI."); - usage(); - return (1); - } - - /* - * Connect to the printer... - */ - - if (httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, sizeof(scheme), userpass, sizeof(userpass), hostname, sizeof(hostname), &port, resource, sizeof(resource)) < HTTP_URI_STATUS_OK) - { - printf("Bad printer URI '%s'.\n", uri); - return (1); - } - - if (!port) - port = IPP_PORT; - - if (!strcmp(scheme, "https") || !strcmp(scheme, "ipps")) - encryption = HTTP_ENCRYPTION_ALWAYS; - else - encryption = HTTP_ENCRYPTION_IF_REQUESTED; - - if ((http = httpConnect2(hostname, port, NULL, AF_UNSPEC, encryption, 1, 0, NULL)) == NULL) - { - printf("Unable to connect to '%s' on port %d: %s\n", hostname, port, cupsLastErrorString()); - return (1); - } - - /* - * Query printer status and capabilities... - */ - - request = ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES); - ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri); - ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", NULL, cupsUser()); - ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "requested-attributes", (int)(sizeof(pattrs) / sizeof(pattrs[0])), NULL, pattrs); - - response = cupsDoRequest(http, request, resource); - - if (verbosity) - show_capabilities(response); - - /* - * Now figure out what we will be printing... - */ - - if (printfile) - { - /* - * User specified a print file, figure out the format... - */ - - if ((opt = strrchr(printfile, '.')) != NULL) - { - /* - * Guess the format from the extension... - */ - - if (!strcmp(opt, ".jpg")) - printformat = "image/jpeg"; - else if (!strcmp(opt, ".pdf")) - printformat = "application/pdf"; - else if (!strcmp(opt, ".ps")) - printformat = "application/postscript"; - else if (!strcmp(opt, ".pwg")) - printformat = "image/pwg-raster"; - else if (!strcmp(opt, ".urf")) - printformat = "image/urf"; - else - printformat = "application/octet-stream"; - } - else - { - /* - * Tell the printer to auto-detect... - */ - - printformat = "application/octet-stream"; - } - } - else - { - /* - * No file specified, make something to test with... - */ - - if ((printfile = make_raster_file(response, grayscale, tempfile, sizeof(tempfile), &printformat)) == NULL) - return (1); - } - - ippDelete(response); - - /* - * Start monitoring the printer in the background... - */ - - memset(&monitor, 0, sizeof(monitor)); - - monitor.uri = uri; - monitor.hostname = hostname; - monitor.resource = resource; - monitor.port = port; - monitor.encryption = encryption; - - _cupsThreadCreate((_cups_thread_func_t)monitor_printer, &monitor); - - /* - * Create the job and wait for completion... - */ - - request = ippNewRequest(IPP_OP_CREATE_JOB); - ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri); - ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", NULL, cupsUser()); - - if ((opt = strrchr(printfile, '/')) != NULL) - opt ++; - else - opt = printfile; - - ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL, opt); - - if (verbosity) - show_attributes("Create-Job request", 1, request); - - response = cupsDoRequest(http, request, resource); - - if (verbosity) - show_attributes("Create-Job response", 0, response); - - if (cupsLastError() >= IPP_STATUS_REDIRECTION_OTHER_SITE) - { - printf("Unable to create print job: %s\n", cupsLastErrorString()); - - monitor.job_state = IPP_JSTATE_ABORTED; - - goto cleanup; - } - - if ((attr = ippFindAttribute(response, "job-id", IPP_TAG_INTEGER)) == NULL) - { - puts("No job-id returned in Create-Job request."); - - monitor.job_state = IPP_JSTATE_ABORTED; - - goto cleanup; - } - - monitor.job_id = ippGetInteger(attr, 0); - - printf("CREATED JOB %d, sending %s of type %s\n", monitor.job_id, printfile, printformat); - - ippDelete(response); - - request = ippNewRequest(IPP_OP_SEND_DOCUMENT); - ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri); - ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", monitor.job_id); - ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", NULL, cupsUser()); - ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE, "document-format", NULL, printformat); - ippAddBoolean(request, IPP_TAG_OPERATION, "last-document", 1); - - if (verbosity) - show_attributes("Send-Document request", 1, request); - - response = cupsDoFileRequest(http, request, resource, printfile); - - if (verbosity) - show_attributes("Send-Document response", 0, response); - - if (cupsLastError() >= IPP_STATUS_REDIRECTION_OTHER_SITE) - { - printf("Unable to print file: %s\n", cupsLastErrorString()); - - monitor.job_state = IPP_JSTATE_ABORTED; - - goto cleanup; - } - - puts("WAITING FOR JOB TO COMPLETE"); - - while (monitor.job_state < IPP_JSTATE_CANCELED) - sleep(1); - - /* - * Cleanup after ourselves... - */ - - cleanup: - - httpClose(http); - - if (tempfile[0] && !keepfile) - unlink(tempfile); - - return (monitor.job_state == IPP_JSTATE_COMPLETED); -} - - -/* - * 'make_raster_file()' - Create a temporary raster file. - */ - -static const char * /* O - Print filename */ -make_raster_file(ipp_t *response, /* I - Printer attributes */ - int grayscale, /* I - Force grayscale? */ - char *tempname, /* I - Temporary filename buffer */ - size_t tempsize, /* I - Size of temp file buffer */ - const char **format) /* O - Print format */ -{ - int i, /* Looping var */ - count; /* Number of values */ - ipp_attribute_t *attr; /* Printer attribute */ - const char *type = NULL; /* Raster type (colorspace + bits) */ - pwg_media_t *media = NULL; /* Media size */ - int xdpi = 0, /* Horizontal resolution */ - ydpi = 0; /* Vertical resolution */ - int fd; /* Temporary file */ - cups_mode_t mode; /* Raster mode */ - cups_raster_t *ras; /* Raster stream */ - cups_page_header2_t header; /* Page header */ - unsigned char *line, /* Line of raster data */ - *lineptr; /* Pointer into line */ - unsigned y, /* Current position on page */ - xcount, ycount, /* Current count for X and Y */ - xrep, yrep, /* Repeat count for X and Y */ - xoff, yoff, /* Offsets for X and Y */ - yend; /* End Y value */ - int temprow, /* Row in template */ - tempcolor; /* Template color */ - const char *template; /* Pointer into template */ - const unsigned char *color; /* Current color */ - static const unsigned char colors[][3] = - { /* Colors for test */ - { 191, 191, 191 }, - { 127, 127, 127 }, - { 63, 63, 63 }, - { 0, 0, 0 }, - { 255, 0, 0 }, - { 255, 127, 0 }, - { 255, 255, 0 }, - { 127, 255, 0 }, - { 0, 255, 0 }, - { 0, 255, 127 }, - { 0, 255, 255 }, - { 0, 127, 255 }, - { 0, 0, 255 }, - { 127, 0, 255 }, - { 255, 0, 255 } - }; - static const char * const templates[] = - { /* Raster template */ - " CCC U U PPPP SSS TTTTT EEEEE SSS TTTTT 000 1 222 333 4 55555 66 77777 888 999 ", - "C C U U P P S S T E S S T 0 0 11 2 2 3 3 4 4 5 6 7 8 8 9 9 ", - "C U U P P S T E S T 0 0 1 2 3 4 4 5 6 7 8 8 9 9 ", - "C U U PPPP SSS ----- T EEEE SSS T 0 0 0 1 22 333 44444 555 6666 7 888 9999 ", - "C U U P S T E S T 0 0 1 2 3 4 5 6 6 7 8 8 9 ", - "C C U U P S S T E S S T 0 0 1 2 3 3 4 5 5 6 6 7 8 8 9 ", - " CCC UUU P SSS T EEEEE SSS T 000 111 22222 333 4 555 666 7 888 99 ", - " " - }; - - - /* - * Figure out the output format... - */ - - if ((attr = ippFindAttribute(response, "document-format-supported", IPP_TAG_MIMETYPE)) == NULL) - { - puts("No supported document formats, aborting."); - return (NULL); - } - - if (*format) - { - if (!ippContainsString(attr, *format)) - { - printf("Printer does not support document-format '%s'.\n", *format); - return (NULL); - } - - if (!strcmp(*format, "image/urf")) - mode = CUPS_RASTER_WRITE_APPLE; - else if (!strcmp(*format, "image/pwg-raster")) - mode = CUPS_RASTER_WRITE_PWG; - else - { - printf("Unable to generate document-format '%s'.\n", *format); - return (NULL); - } - } - else if (ippContainsString(attr, "image/urf")) - { - /* - * Apple Raster format... - */ - - *format = "image/urf"; - mode = CUPS_RASTER_WRITE_APPLE; - } - else if (ippContainsString(attr, "image/pwg-raster")) - { - /* - * PWG Raster format... - */ - - *format = "image/pwg-raster"; - mode = CUPS_RASTER_WRITE_PWG; - } - else - { - /* - * No supported raster format... - */ - - puts("Printer does not support Apple or PWG raster files, aborting."); - return (NULL); - } - - /* - * Figure out the the media, resolution, and color mode... - */ - - if ((attr = ippFindAttribute(response, "media-default", IPP_TAG_KEYWORD)) != NULL) - { - /* - * Use default media... - */ - - media = pwgMediaForPWG(ippGetString(attr, 0, NULL)); - } - else if ((attr = ippFindAttribute(response, "media-ready", IPP_TAG_KEYWORD)) != NULL) - { - /* - * Use ready media... - */ - - if (ippContainsString(attr, "na_letter_8.5x11in")) - media = pwgMediaForPWG("na_letter_8.5x11in"); - else if (ippContainsString(attr, "iso_a4_210x297mm")) - media = pwgMediaForPWG("iso_a4_210x297mm"); - else - media = pwgMediaForPWG(ippGetString(attr, 0, NULL)); - } - else - { - puts("No default or ready media reported by printer, aborting."); - return (NULL); - } - - if (mode == CUPS_RASTER_WRITE_APPLE && (attr = ippFindAttribute(response, "urf-supported", IPP_TAG_KEYWORD)) != NULL) - { - for (i = 0, count = ippGetCount(attr); i < count; i ++) - { - const char *val = ippGetString(attr, i, NULL); - - if (!strncmp(val, "RS", 2)) - xdpi = ydpi = atoi(val + 2); - else if (!strncmp(val, "W8", 2) && !type) - type = "sgray_8"; - else if (!strncmp(val, "SRGB24", 6) && !grayscale) - type = "srgb_8"; - } - } - else if (mode == CUPS_RASTER_WRITE_PWG && (attr = ippFindAttribute(response, "pwg-raster-document-resolution-supported", IPP_TAG_RESOLUTION)) != NULL) - { - for (i = 0, count = ippGetCount(attr); i < count; i ++) - { - int tempxdpi, tempydpi; - ipp_res_t tempunits; - - tempxdpi = ippGetResolution(attr, 0, &tempydpi, &tempunits); - - if (i == 0 || tempxdpi < xdpi || tempydpi < ydpi) - { - xdpi = tempxdpi; - ydpi = tempydpi; - } - } - - if ((attr = ippFindAttribute(response, "pwg-raster-document-type-supported", IPP_TAG_KEYWORD)) != NULL) - { - if (!grayscale && ippContainsString(attr, "srgb_8")) - type = "srgb_8"; - else if (ippContainsString(attr, "sgray_8")) - type = "sgray_8"; - } - } - - if (xdpi < 72 || ydpi < 72) - { - puts("No supported raster resolutions, aborting."); - return (NULL); - } - - if (!type) - { - puts("No supported color spaces or bit depths, aborting."); - return (NULL); - } - - /* - * Make the raster context and details... - */ - - if (!cupsRasterInitPWGHeader(&header, media, type, xdpi, ydpi, "one-sided", NULL)) - { - printf("Unable to initialize raster context: %s\n", cupsRasterErrorString()); - return (NULL); - } - - header.cupsInteger[CUPS_RASTER_PWG_TotalPageCount] = 1; - - if (header.cupsWidth > (4 * header.HWResolution[0])) - { - xoff = header.HWResolution[0] / 2; - yoff = header.HWResolution[1] / 2; - } - else - { - xoff = 0; - yoff = 0; - } - - xrep = (header.cupsWidth - 2 * xoff) / 140; - yrep = xrep * header.HWResolution[1] / header.HWResolution[0]; - yend = header.cupsHeight - yoff; - - /* - * Prepare the raster file... - */ - - if ((line = malloc(header.cupsBytesPerLine)) == NULL) - { - printf("Unable to allocate %u bytes for raster output: %s\n", header.cupsBytesPerLine, strerror(errno)); - return (NULL); - } - - if ((fd = cupsTempFd(tempname, (int)tempsize)) < 0) - { - printf("Unable to create temporary print file: %s\n", strerror(errno)); - return (NULL); - } - - if ((ras = cupsRasterOpen(fd, mode)) == NULL) - { - printf("Unable to open raster stream: %s\n", cupsRasterErrorString()); - close(fd); - return (NULL); - } - - /* - * Write a single page consisting of the template dots repeated over the page. - */ - - cupsRasterWriteHeader2(ras, &header); - - memset(line, 0xff, header.cupsBytesPerLine); - - for (y = 0; y < yoff; y ++) - cupsRasterWritePixels(ras, line, header.cupsBytesPerLine); - - for (temprow = 0, tempcolor = 0; y < yend;) - { - template = templates[temprow]; - color = colors[tempcolor]; - - temprow ++; - if (temprow >= (int)(sizeof(templates) / sizeof(templates[0]))) - { - temprow = 0; - tempcolor ++; - if (tempcolor >= (int)(sizeof(colors) / sizeof(colors[0]))) - tempcolor = 0; - else if (tempcolor > 3 && header.cupsColorSpace == CUPS_CSPACE_SW) - tempcolor = 0; - } - - memset(line, 0xff, header.cupsBytesPerLine); - - if (header.cupsColorSpace == CUPS_CSPACE_SW) - { - /* - * Do grayscale output... - */ - - for (lineptr = line + xoff; *template; template ++) - { - if (*template != ' ') - { - for (xcount = xrep; xcount > 0; xcount --) - *lineptr++ = *color; - } - else - { - lineptr += xrep; - } - } - } - else - { - /* - * Do color output... - */ - - for (lineptr = line + 3 * xoff; *template; template ++) - { - if (*template != ' ') - { - for (xcount = xrep; xcount > 0; xcount --, lineptr += 3) - memcpy(lineptr, color, 3); - } - else - { - lineptr += 3 * xrep; - } - } - } - - for (ycount = yrep; ycount > 0 && y < yend; ycount --, y ++) - cupsRasterWritePixels(ras, line, header.cupsBytesPerLine); - } - - memset(line, 0xff, header.cupsBytesPerLine); - - for (y = 0; y < header.cupsHeight; y ++) - cupsRasterWritePixels(ras, line, header.cupsBytesPerLine); - - cupsRasterClose(ras); - - close(fd); - - printf("PRINT FILE: %s\n", tempname); - - return (tempname); -} - - -/* - * 'monitor_printer()' - Monitor the job and printer states. - */ - -static void * /* O - Thread exit code */ -monitor_printer( - _client_monitor_t *monitor) /* I - Monitoring data */ -{ - http_t *http; /* Connection to printer */ - ipp_t *request, /* IPP request */ - *response; /* IPP response */ - ipp_attribute_t *attr; /* Attribute in response */ - ipp_pstate_t printer_state; /* Printer state */ - char printer_state_reasons[1024]; - /* Printer state reasons */ - ipp_jstate_t job_state; /* Job state */ - char job_state_reasons[1024];/* Printer state reasons */ - static const char * const jattrs[] = /* Job attributes we want */ - { - "job-state", - "job-state-reasons" - }; - static const char * const pattrs[] = /* Printer attributes we want */ - { - "printer-state", - "printer-state-reasons" - }; - - - /* - * Open a connection to the printer... - */ - - http = httpConnect2(monitor->hostname, monitor->port, NULL, AF_UNSPEC, monitor->encryption, 1, 0, NULL); - - /* - * Loop until the job is canceled, aborted, or completed. - */ - - printer_state = (ipp_pstate_t)0; - printer_state_reasons[0] = '\0'; - - job_state = (ipp_jstate_t)0; - job_state_reasons[0] = '\0'; - - while (monitor->job_state < IPP_JSTATE_CANCELED) - { - /* - * Reconnect to the printer as needed... - */ - - if (httpGetFd(http) < 0) - httpReconnect2(http, 30000, NULL); - - if (httpGetFd(http) >= 0) - { - /* - * Connected, so check on the printer state... - */ - - request = ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES); - ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, monitor->uri); - ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", NULL, cupsUser()); - ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "requested-attributes", (int)(sizeof(pattrs) / sizeof(pattrs[0])), NULL, pattrs); - - response = cupsDoRequest(http, request, monitor->resource); - - if ((attr = ippFindAttribute(response, "printer-state", IPP_TAG_ENUM)) != NULL) - printer_state = (ipp_pstate_t)ippGetInteger(attr, 0); - - if ((attr = ippFindAttribute(response, "printer-state-reasons", IPP_TAG_KEYWORD)) != NULL) - ippAttributeString(attr, printer_state_reasons, sizeof(printer_state_reasons)); - - if (printer_state != monitor->printer_state || strcmp(printer_state_reasons, monitor->printer_state_reasons)) - { - printf("PRINTER: %s (%s)\n", ippEnumString("printer-state", printer_state), printer_state_reasons); - - monitor->printer_state = printer_state; - strlcpy(monitor->printer_state_reasons, printer_state_reasons, sizeof(monitor->printer_state_reasons)); - } - - ippDelete(response); - - if (monitor->job_id > 0) - { - /* - * Check the status of the job itself... - */ - - request = ippNewRequest(IPP_OP_GET_JOB_ATTRIBUTES); - ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, monitor->uri); - ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", monitor->job_id); - ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", NULL, cupsUser()); - ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "requested-attributes", (int)(sizeof(jattrs) / sizeof(jattrs[0])), NULL, jattrs); - - response = cupsDoRequest(http, request, monitor->resource); - - if ((attr = ippFindAttribute(response, "job-state", IPP_TAG_ENUM)) != NULL) - job_state = (ipp_jstate_t)ippGetInteger(attr, 0); - - if ((attr = ippFindAttribute(response, "job-state-reasons", IPP_TAG_KEYWORD)) != NULL) - ippAttributeString(attr, job_state_reasons, sizeof(job_state_reasons)); - - if (job_state != monitor->job_state || strcmp(job_state_reasons, monitor->job_state_reasons)) - { - printf("JOB %d: %s (%s)\n", monitor->job_id, ippEnumString("job-state", job_state), job_state_reasons); - - monitor->job_state = job_state; - strlcpy(monitor->job_state_reasons, job_state_reasons, sizeof(monitor->job_state_reasons)); - } - - ippDelete(response); - } - } - - if (monitor->job_state < IPP_JSTATE_CANCELED) - { - /* - * Sleep for 5 seconds... - */ - - sleep(5); - } - } - - /* - * Cleanup and return... - */ - - httpClose(http); - - return (NULL); -} - - -/* - * 'show_attributes()' - Show attributes in a request or response. - */ - -static void -show_attributes(const char *title, /* I - Title */ - int request, /* I - 1 for request, 0 for response */ - ipp_t *ipp) /* I - IPP request/response */ -{ - int minor, major = ippGetVersion(ipp, &minor); - /* IPP version number */ - ipp_tag_t group = IPP_TAG_ZERO; - /* Current group tag */ - ipp_attribute_t *attr; /* Current attribute */ - const char *name; /* Attribute name */ - char buffer[1024]; /* Value */ - - - printf("%s:\n", title); - printf(" version=%d.%d\n", major, minor); - printf(" request-id=%d\n", ippGetRequestId(ipp)); - if (!request) - printf(" status-code=%s\n", ippErrorString(ippGetStatusCode(ipp))); - - for (attr = ippFirstAttribute(ipp); attr; attr = ippNextAttribute(ipp)) - { - if (group != ippGetGroupTag(attr)) - { - group = ippGetGroupTag(attr); - if (group) - printf(" %s:\n", ippTagString(group)); - } - - if ((name = ippGetName(attr)) != NULL) - { - ippAttributeString(attr, buffer, sizeof(buffer)); - printf(" %s(%s%s)=%s\n", name, ippGetCount(attr) > 1 ? "1setOf " : "", ippTagString(ippGetValueTag(attr)), buffer); - } - } -} - - -/* - * 'show_capabilities()' - Show printer capabilities. - */ - -static void -show_capabilities(ipp_t *response) /* I - Printer attributes */ -{ - int i; /* Looping var */ - ipp_attribute_t *attr; /* Attribute */ - char buffer[1024]; /* Attribute value buffer */ - static const char * const pattrs[] = /* Attributes we want to show */ - { - "copies-default", - "copies-supported", - "finishings-default", - "finishings-ready", - "finishings-supported", - "media-default", - "media-ready", - "media-supported", - "output-bin-default", - "output-bin-supported", - "print-color-mode-default", - "print-color-mode-supported", - "sides-default", - "sides-supported", - "document-format-default", - "document-format-supported", - "pwg-raster-document-resolution-supported", - "pwg-raster-document-type-supported", - "urf-supported" - }; - - - puts("CAPABILITIES:"); - for (i = 0; i < (int)(sizeof(pattrs) / sizeof(pattrs[0])); i ++) - { - if ((attr = ippFindAttribute(response, pattrs[i], IPP_TAG_ZERO)) != NULL) - { - ippAttributeString(attr, buffer, sizeof(buffer)); - printf(" %s=%s\n", pattrs[i], buffer); - } - } -} - - -/* - * 'usage()' - Show program usage... - */ - -static void -usage(void) -{ - puts("Usage: ./testclient printer-uri [options]"); - puts("Options:"); - puts(" -d document-format Generate the specified format"); - puts(" -f print-file Print the named file"); - puts(" -g Force grayscale printing"); - puts(" -k Keep temporary files"); - puts(" -v Be more verbose"); -} diff --git a/filter/testraster.c b/filter/testraster.c deleted file mode 100644 index faf2dd2f9..000000000 --- a/filter/testraster.c +++ /dev/null @@ -1,1138 +0,0 @@ -/* - * Raster test program routines for CUPS. - * - * Copyright 2007-2018 by Apple Inc. - * Copyright 1997-2007 by Easy Software Products. - * - * Licensed under Apache License v2.0. See the file "LICENSE" for more - * information. - */ - -/* - * Include necessary headers... - */ - -#include -#include -#include - - -/* - * Test PS commands and header... - */ - -static const char *dsc_code = -"[{\n" -"%%BeginFeature: *PageSize Tabloid\n" -"<>setpagedevice\n" -"%%EndFeature\n" -"} stopped cleartomark\n"; -static const char *setpagedevice_code = -"<<" -"/MediaClass(Media Class)" -"/MediaColor((Media Color))" -"/MediaType(Media\\\\Type)" -"/OutputType<416263>" -"/AdvanceDistance 1000" -"/AdvanceMedia 1" -"/Collate false" -"/CutMedia 2" -"/Duplex true" -"/HWResolution[100 200]" -"/InsertSheet true" -"/Jog 3" -"/LeadingEdge 1" -"/ManualFeed true" -"/MediaPosition 8#777" -"/MediaWeight 16#fe01" -"/MirrorPrint true" -"/NegativePrint true" -"/NumCopies 1" -"/Orientation 1" -"/OutputFaceUp true" -"/PageSize[612 792.1]" -"/Separations true" -"/TraySwitch true" -"/Tumble true" -"/cupsMediaType 2" -"/cupsColorOrder 1" -"/cupsColorSpace 1" -"/cupsCompression 1" -"/cupsRowCount 1" -"/cupsRowFeed 1" -"/cupsRowStep 1" -"/cupsBorderlessScalingFactor 1.001" -"/cupsInteger0 1" -"/cupsInteger1 2" -"/cupsInteger2 3" -"/cupsInteger3 4" -"/cupsInteger4 5" -"/cupsInteger5 6" -"/cupsInteger6 7" -"/cupsInteger7 8" -"/cupsInteger8 9" -"/cupsInteger9 10" -"/cupsInteger10 11" -"/cupsInteger11 12" -"/cupsInteger12 13" -"/cupsInteger13 14" -"/cupsInteger14 15" -"/cupsInteger15 16" -"/cupsReal0 1.1" -"/cupsReal1 2.1" -"/cupsReal2 3.1" -"/cupsReal3 4.1" -"/cupsReal4 5.1" -"/cupsReal5 6.1" -"/cupsReal6 7.1" -"/cupsReal7 8.1" -"/cupsReal8 9.1" -"/cupsReal9 10.1" -"/cupsReal10 11.1" -"/cupsReal11 12.1" -"/cupsReal12 13.1" -"/cupsReal13 14.1" -"/cupsReal14 15.1" -"/cupsReal15 16.1" -"/cupsString0(1)" -"/cupsString1(2)" -"/cupsString2(3)" -"/cupsString3(4)" -"/cupsString4(5)" -"/cupsString5(6)" -"/cupsString6(7)" -"/cupsString7(8)" -"/cupsString8(9)" -"/cupsString9(10)" -"/cupsString10(11)" -"/cupsString11(12)" -"/cupsString12(13)" -"/cupsString13(14)" -"/cupsString14(15)" -"/cupsString15(16)" -"/cupsMarkerType(Marker Type)" -"/cupsRenderingIntent(Rendering Intent)" -"/cupsPageSizeName(Letter)" -"/cupsPreferredBitsPerColor 17" -">> setpagedevice"; - -static cups_page_header2_t setpagedevice_header = -{ - "Media Class", /* MediaClass */ - "(Media Color)", /* MediaColor */ - "Media\\Type", /* MediaType */ - "Abc", /* OutputType */ - 1000, /* AdvanceDistance */ - CUPS_ADVANCE_FILE, /* AdvanceMedia */ - CUPS_FALSE, /* Collate */ - CUPS_CUT_JOB, /* CutMedia */ - CUPS_TRUE, /* Duplex */ - { 100, 200 }, /* HWResolution */ - { 0, 0, 0, 0 }, /* ImagingBoundingBox */ - CUPS_TRUE, /* InsertSheet */ - CUPS_JOG_SET, /* Jog */ - CUPS_EDGE_RIGHT, /* LeadingEdge */ - { 0, 0 }, /* Margins */ - CUPS_TRUE, /* ManualFeed */ - 0777, /* MediaPosition */ - 0xfe01, /* MediaWeight */ - CUPS_TRUE, /* MirrorPrint */ - CUPS_TRUE, /* NegativePrint */ - 1, /* NumCopies */ - CUPS_ORIENT_90, /* Orientation */ - CUPS_TRUE, /* OutputFaceUp */ - { 612, 792 }, /* PageSize */ - CUPS_TRUE, /* Separations */ - CUPS_TRUE, /* TraySwitch */ - CUPS_TRUE, /* Tumble */ - 0, /* cupsWidth */ - 0, /* cupsHeight */ - 2, /* cupsMediaType */ - 0, /* cupsBitsPerColor */ - 0, /* cupsBitsPerPixel */ - 0, /* cupsBytesPerLine */ - CUPS_ORDER_BANDED, /* cupsColorOrder */ - CUPS_CSPACE_RGB, /* cupsColorSpace */ - 1, /* cupsCompression */ - 1, /* cupsRowCount */ - 1, /* cupsRowFeed */ - 1, /* cupsRowStep */ - 0, /* cupsNumColors */ - 1.001f, /* cupsBorderlessScalingFactor */ - { 612.0f, 792.1f }, /* cupsPageSize */ - { 0.0f, 0.0f, 0.0f, 0.0f }, /* cupsImagingBBox */ - { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }, - /* cupsInteger[16] */ - { 1.1f, 2.1f, 3.1f, 4.1f, 5.1f, 6.1f, 7.1f, 8.1f, 9.1f, 10.1f, 11.1f, 12.1f, 13.1f, 14.1f, 15.1f, 16.1f }, /* cupsReal[16] */ - { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", - "14", "15", "16" }, /* cupsString[16] */ - "Marker Type", /* cupsMarkerType */ - "Rendering Intent", /* cupsRenderingIntent */ - "Letter" /* cupsPageSizeName */ -}; - - -/* - * Local functions... - */ - -static int do_ppd_tests(const char *filename, int num_options, - cups_option_t *options); -static int do_ps_tests(void); -static int do_ras_file(const char *filename); -static int do_raster_tests(cups_mode_t mode); -static void print_changes(cups_page_header2_t *header, - cups_page_header2_t *expected); - - -/* - * 'main()' - Test the raster functions. - */ - -int /* O - Exit status */ -main(int argc, /* I - Number of command-line args */ - char *argv[]) /* I - Command-line arguments */ -{ - int errors; /* Number of errors */ - const char *ext; /* Filename extension */ - - - if (argc == 1) - { - errors = do_ps_tests(); - errors += do_raster_tests(CUPS_RASTER_WRITE); - errors += do_raster_tests(CUPS_RASTER_WRITE_COMPRESSED); - errors += do_raster_tests(CUPS_RASTER_WRITE_PWG); - errors += do_raster_tests(CUPS_RASTER_WRITE_APPLE); - } - else - { - int i; /* Looping var */ - int num_options; /* Number of options */ - cups_option_t *options; /* Options */ - - - for (errors = 0, num_options = 0, options = NULL, i = 1; i < argc; i ++) - { - if (argv[i][0] == '-') - { - if (argv[i][1] == 'o') - { - if (argv[i][2]) - num_options = cupsParseOptions(argv[i] + 2, num_options, &options); - else - { - i ++; - if (i < argc) - num_options = cupsParseOptions(argv[i], num_options, &options); - else - { - puts("Usage: testraster [-o name=value ...] [filename.ppd ...]"); - puts(" testraster [filename.ras ...]"); - return (1); - } - } - } - else - { - puts("Usage: testraster [-o name=value ...] [filename.ppd ...]"); - puts(" testraster [filename.ras ...]"); - return (1); - } - } - else if ((ext = strrchr(argv[i], '.')) != NULL) - { - if (!strcmp(ext, ".ppd")) - errors += do_ppd_tests(argv[i], num_options, options); - else - errors += do_ras_file(argv[i]); - } - else - { - puts("Usage: testraster [-o name=value ...] [filename.ppd ...]"); - puts(" testraster [filename.ras ...]"); - return (1); - } - } - - cupsFreeOptions(num_options, options); - } - - return (errors); -} - - -/* - * 'do_ppd_tests()' - Test the default option commands in a PPD file. - */ - -static int /* O - Number of errors */ -do_ppd_tests(const char *filename, /* I - PPD file */ - int num_options, /* I - Number of options */ - cups_option_t *options) /* I - Options */ -{ - ppd_file_t *ppd; /* PPD file data */ - cups_page_header2_t header; /* Page header */ - - - printf("\"%s\": ", filename); - fflush(stdout); - - if ((ppd = ppdOpenFile(filename)) == NULL) - { - ppd_status_t status; /* Status from PPD loader */ - int line; /* Line number containing error */ - - - status = ppdLastError(&line); - - puts("FAIL (bad PPD file)"); - printf(" %s on line %d\n", ppdErrorString(status), line); - - return (1); - } - - ppdMarkDefaults(ppd); - cupsMarkOptions(ppd, num_options, options); - - if (cupsRasterInterpretPPD(&header, ppd, 0, NULL, NULL)) - { - puts("FAIL (error from function)"); - puts(cupsRasterErrorString()); - - return (1); - } - else - { - puts("PASS"); - - return (0); - } -} - - -/* - * 'do_ps_tests()' - Test standard PostScript commands. - */ - -static int -do_ps_tests(void) -{ - cups_page_header2_t header; /* Page header */ - int preferred_bits; /* Preferred bits */ - int errors = 0; /* Number of errors */ - - - /* - * Test PS exec code... - */ - - fputs("_cupsRasterExecPS(\"setpagedevice\"): ", stdout); - fflush(stdout); - - memset(&header, 0, sizeof(header)); - header.Collate = CUPS_TRUE; - preferred_bits = 0; - - if (_cupsRasterExecPS(&header, &preferred_bits, setpagedevice_code)) - { - puts("FAIL (error from function)"); - puts(cupsRasterErrorString()); - errors ++; - } - else if (preferred_bits != 17 || - memcmp(&header, &setpagedevice_header, sizeof(header))) - { - puts("FAIL (bad header)"); - - if (preferred_bits != 17) - printf(" cupsPreferredBitsPerColor %d, expected 17\n", - preferred_bits); - - print_changes(&setpagedevice_header, &header); - errors ++; - } - else - puts("PASS"); - - fputs("_cupsRasterExecPS(\"roll\"): ", stdout); - fflush(stdout); - - if (_cupsRasterExecPS(&header, &preferred_bits, - "792 612 0 0 0\n" - "pop pop pop\n" - "<>" - "setpagedevice\n")) - { - puts("FAIL (error from function)"); - puts(cupsRasterErrorString()); - errors ++; - } - else if (header.PageSize[0] != 792 || header.PageSize[1] != 612) - { - printf("FAIL (PageSize [%d %d], expected [792 612])\n", header.PageSize[0], - header.PageSize[1]); - errors ++; - } - else - puts("PASS"); - - fputs("_cupsRasterExecPS(\"dup index\"): ", stdout); - fflush(stdout); - - if (_cupsRasterExecPS(&header, &preferred_bits, - "true false dup\n" - "<>setpagedevice\n" - "pop pop pop")) - { - puts("FAIL (error from function)"); - puts(cupsRasterErrorString()); - errors ++; - } - else - { - if (!header.Collate) - { - printf("FAIL (Collate false, expected true)\n"); - errors ++; - } - - if (header.Duplex) - { - printf("FAIL (Duplex true, expected false)\n"); - errors ++; - } - - if (header.Tumble) - { - printf("FAIL (Tumble true, expected false)\n"); - errors ++; - } - - if(header.Collate && !header.Duplex && !header.Tumble) - puts("PASS"); - } - - fputs("_cupsRasterExecPS(\"%%Begin/EndFeature code\"): ", stdout); - fflush(stdout); - - if (_cupsRasterExecPS(&header, &preferred_bits, dsc_code)) - { - puts("FAIL (error from function)"); - puts(cupsRasterErrorString()); - errors ++; - } - else if (header.PageSize[0] != 792 || header.PageSize[1] != 1224) - { - printf("FAIL (bad PageSize [%d %d], expected [792 1224])\n", - header.PageSize[0], header.PageSize[1]); - errors ++; - } - else - puts("PASS"); - - return (errors); -} - - -/* - * 'do_ras_file()' - Test reading of a raster file. - */ - -static int /* O - Number of errors */ -do_ras_file(const char *filename) /* I - Filename */ -{ - unsigned y; /* Looping vars */ - int fd; /* File descriptor */ - cups_raster_t *ras; /* Raster stream */ - cups_page_header2_t header; /* Page header */ - unsigned char *data; /* Raster data */ - int errors = 0; /* Number of errors */ - unsigned pages = 0; /* Number of pages */ - - - if ((fd = open(filename, O_RDONLY)) < 0) - { - printf("%s: %s\n", filename, strerror(errno)); - return (1); - } - - if ((ras = cupsRasterOpen(fd, CUPS_RASTER_READ)) == NULL) - { - printf("%s: cupsRasterOpen failed.\n", filename); - close(fd); - return (1); - } - - printf("%s:\n", filename); - - while (cupsRasterReadHeader2(ras, &header)) - { - pages ++; - data = malloc(header.cupsBytesPerLine); - - printf(" Page %u: %ux%ux%u@%ux%udpi", pages, - header.cupsWidth, header.cupsHeight, header.cupsBitsPerPixel, - header.HWResolution[0], header.HWResolution[1]); - fflush(stdout); - - for (y = 0; y < header.cupsHeight; y ++) - if (cupsRasterReadPixels(ras, data, header.cupsBytesPerLine) < - header.cupsBytesPerLine) - break; - - if (y < header.cupsHeight) - printf(" ERROR AT LINE %d\n", y); - else - putchar('\n'); - - free(data); - } - - printf("EOF at %ld\n", (long)lseek(fd, SEEK_CUR, 0)); - - cupsRasterClose(ras); - close(fd); - - return (errors); -} - - -/* - * 'do_raster_tests()' - Test reading and writing of raster data. - */ - -static int /* O - Number of errors */ -do_raster_tests(cups_mode_t mode) /* O - Write mode */ -{ - unsigned page, x, y, count;/* Looping vars */ - FILE *fp; /* Raster file */ - cups_raster_t *r; /* Raster stream */ - cups_page_header2_t header, /* Page header */ - expected; /* Expected page header */ - unsigned char data[2048]; /* Raster data */ - int errors = 0; /* Number of errors */ - - - /* - * Test writing... - */ - - printf("cupsRasterOpen(%s): ", - mode == CUPS_RASTER_WRITE ? "CUPS_RASTER_WRITE" : - mode == CUPS_RASTER_WRITE_COMPRESSED ? "CUPS_RASTER_WRITE_COMPRESSED" : - mode == CUPS_RASTER_WRITE_PWG ? "CUPS_RASTER_WRITE_PWG" : - "CUPS_RASTER_WRITE_APPLE"); - fflush(stdout); - - if ((fp = fopen("test.raster", "wb")) == NULL) - { - printf("FAIL (%s)\n", strerror(errno)); - return (1); - } - - if ((r = cupsRasterOpen(fileno(fp), mode)) == NULL) - { - printf("FAIL (%s)\n", strerror(errno)); - fclose(fp); - return (1); - } - - puts("PASS"); - - for (page = 0; page < 4; page ++) - { - memset(&header, 0, sizeof(header)); - header.cupsWidth = 256; - header.cupsHeight = 256; - header.cupsBytesPerLine = 256; - header.HWResolution[0] = 64; - header.HWResolution[1] = 64; - header.PageSize[0] = 288; - header.PageSize[1] = 288; - header.cupsPageSize[0] = 288.0f; - header.cupsPageSize[1] = 288.0f; - - if (page & 1) - { - header.cupsBytesPerLine *= 4; - header.cupsColorSpace = CUPS_CSPACE_CMYK; - header.cupsColorOrder = CUPS_ORDER_CHUNKED; - header.cupsNumColors = 4; - } - else - { - header.cupsColorSpace = CUPS_CSPACE_W; - header.cupsColorOrder = CUPS_ORDER_CHUNKED; - header.cupsNumColors = 1; - } - - 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; - } - - printf("cupsRasterWriteHeader2(page %d): ", page + 1); - - if (cupsRasterWriteHeader2(r, &header)) - { - puts("PASS"); - } - else - { - puts("FAIL"); - errors ++; - } - - fputs("cupsRasterWritePixels: ", stdout); - fflush(stdout); - - memset(data, 0, header.cupsBytesPerLine); - for (y = 0; y < 64; y ++) - if (!cupsRasterWritePixels(r, data, header.cupsBytesPerLine)) - break; - - if (y < 64) - { - puts("FAIL"); - errors ++; - } - else - { - for (x = 0; x < header.cupsBytesPerLine; x ++) - data[x] = (unsigned char)x; - - for (y = 0; y < 64; y ++) - if (!cupsRasterWritePixels(r, data, header.cupsBytesPerLine)) - break; - - if (y < 64) - { - puts("FAIL"); - errors ++; - } - else - { - memset(data, 255, header.cupsBytesPerLine); - for (y = 0; y < 64; y ++) - if (!cupsRasterWritePixels(r, data, header.cupsBytesPerLine)) - break; - - if (y < 64) - { - puts("FAIL"); - errors ++; - } - else - { - for (x = 0; x < header.cupsBytesPerLine; x ++) - data[x] = (unsigned char)(x / 4); - - for (y = 0; y < 64; y ++) - if (!cupsRasterWritePixels(r, data, header.cupsBytesPerLine)) - break; - - if (y < 64) - { - puts("FAIL"); - errors ++; - } - else - puts("PASS"); - } - } - } - } - - cupsRasterClose(r); - fclose(fp); - - /* - * Test reading... - */ - - fputs("cupsRasterOpen(CUPS_RASTER_READ): ", stdout); - fflush(stdout); - - if ((fp = fopen("test.raster", "rb")) == NULL) - { - printf("FAIL (%s)\n", strerror(errno)); - return (1); - } - - if ((r = cupsRasterOpen(fileno(fp), CUPS_RASTER_READ)) == NULL) - { - printf("FAIL (%s)\n", strerror(errno)); - fclose(fp); - return (1); - } - - puts("PASS"); - - for (page = 0; page < 4; page ++) - { - memset(&expected, 0, sizeof(expected)); - expected.cupsWidth = 256; - expected.cupsHeight = 256; - expected.cupsBytesPerLine = 256; - expected.HWResolution[0] = 64; - expected.HWResolution[1] = 64; - expected.PageSize[0] = 288; - expected.PageSize[1] = 288; - - if (mode != CUPS_RASTER_WRITE_PWG) - { - expected.cupsPageSize[0] = 288.0f; - expected.cupsPageSize[1] = 288.0f; - } - - if (mode >= CUPS_RASTER_WRITE_PWG) - { - strlcpy(expected.MediaClass, "PwgRaster", sizeof(expected.MediaClass)); - expected.cupsInteger[7] = 0xffffff; - } - - if (page & 1) - { - expected.cupsBytesPerLine *= 4; - expected.cupsColorSpace = CUPS_CSPACE_CMYK; - expected.cupsColorOrder = CUPS_ORDER_CHUNKED; - expected.cupsNumColors = 4; - } - else - { - expected.cupsColorSpace = CUPS_CSPACE_W; - expected.cupsColorOrder = CUPS_ORDER_CHUNKED; - expected.cupsNumColors = 1; - } - - if (page & 2) - { - expected.cupsBytesPerLine *= 2; - expected.cupsBitsPerColor = 16; - expected.cupsBitsPerPixel = (page & 1) ? 64 : 16; - } - else - { - expected.cupsBitsPerColor = 8; - expected.cupsBitsPerPixel = (page & 1) ? 32 : 8; - } - - printf("cupsRasterReadHeader2(page %d): ", page + 1); - fflush(stdout); - - if (!cupsRasterReadHeader2(r, &header)) - { - puts("FAIL (read error)"); - errors ++; - break; - } - else if (memcmp(&header, &expected, sizeof(header))) - { - puts("FAIL (bad page header)"); - errors ++; - print_changes(&header, &expected); - } - else - puts("PASS"); - - fputs("cupsRasterReadPixels: ", stdout); - fflush(stdout); - - for (y = 0; y < 64; y ++) - { - if (!cupsRasterReadPixels(r, data, header.cupsBytesPerLine)) - { - puts("FAIL (read error)"); - errors ++; - break; - } - - if (data[0] != 0 || memcmp(data, data + 1, header.cupsBytesPerLine - 1)) - { - printf("FAIL (raster line %d corrupt)\n", y); - - for (x = 0, count = 0; x < header.cupsBytesPerLine && count < 10; x ++) - { - if (data[x]) - { - count ++; - - if (count == 10) - puts(" ..."); - else - printf(" %4u %02X (expected %02X)\n", x, data[x], 0); - } - } - - errors ++; - break; - } - } - - if (y == 64) - { - for (y = 0; y < 64; y ++) - { - if (!cupsRasterReadPixels(r, data, header.cupsBytesPerLine)) - { - puts("FAIL (read error)"); - errors ++; - break; - } - - for (x = 0; x < header.cupsBytesPerLine; x ++) - if (data[x] != (x & 255)) - break; - - if (x < header.cupsBytesPerLine) - { - printf("FAIL (raster line %d corrupt)\n", y + 64); - - for (x = 0, count = 0; x < header.cupsBytesPerLine && count < 10; x ++) - { - if (data[x] != (x & 255)) - { - count ++; - - if (count == 10) - puts(" ..."); - else - printf(" %4u %02X (expected %02X)\n", x, data[x], x & 255); - } - } - - errors ++; - break; - } - } - - if (y == 64) - { - for (y = 0; y < 64; y ++) - { - if (!cupsRasterReadPixels(r, data, header.cupsBytesPerLine)) - { - puts("FAIL (read error)"); - errors ++; - break; - } - - if (data[0] != 255 || memcmp(data, data + 1, header.cupsBytesPerLine - 1)) - { - printf("fail (raster line %d corrupt)\n", y + 128); - - for (x = 0, count = 0; x < header.cupsBytesPerLine && count < 10; x ++) - { - if (data[x] != 255) - { - count ++; - - if (count == 10) - puts(" ..."); - else - printf(" %4u %02X (expected %02X)\n", x, data[x], 255); - } - } - - errors ++; - break; - } - } - - if (y == 64) - { - for (y = 0; y < 64; y ++) - { - if (!cupsRasterReadPixels(r, data, header.cupsBytesPerLine)) - { - puts("FAIL (read error)"); - errors ++; - break; - } - - for (x = 0; x < header.cupsBytesPerLine; x ++) - if (data[x] != ((x / 4) & 255)) - break; - - if (x < header.cupsBytesPerLine) - { - printf("FAIL (raster line %d corrupt)\n", y + 192); - - for (x = 0, count = 0; x < header.cupsBytesPerLine && count < 10; x ++) - { - if (data[x] != ((x / 4) & 255)) - { - count ++; - - if (count == 10) - puts(" ..."); - else - printf(" %4u %02X (expected %02X)\n", x, data[x], (x / 4) & 255); - } - } - - errors ++; - break; - } - } - - if (y == 64) - puts("PASS"); - } - } - } - } - - cupsRasterClose(r); - fclose(fp); - - return (errors); -} - - -/* - * 'print_changes()' - Print differences in the page header. - */ - -static void -print_changes( - cups_page_header2_t *header, /* I - Actual page header */ - cups_page_header2_t *expected) /* I - Expected page header */ -{ - int i; /* Looping var */ - - - if (strcmp(header->MediaClass, expected->MediaClass)) - printf(" MediaClass (%s), expected (%s)\n", header->MediaClass, - expected->MediaClass); - - if (strcmp(header->MediaColor, expected->MediaColor)) - printf(" MediaColor (%s), expected (%s)\n", header->MediaColor, - expected->MediaColor); - - if (strcmp(header->MediaType, expected->MediaType)) - printf(" MediaType (%s), expected (%s)\n", header->MediaType, - expected->MediaType); - - if (strcmp(header->OutputType, expected->OutputType)) - printf(" OutputType (%s), expected (%s)\n", header->OutputType, - expected->OutputType); - - if (header->AdvanceDistance != expected->AdvanceDistance) - printf(" AdvanceDistance %d, expected %d\n", header->AdvanceDistance, - expected->AdvanceDistance); - - if (header->AdvanceMedia != expected->AdvanceMedia) - printf(" AdvanceMedia %d, expected %d\n", header->AdvanceMedia, - expected->AdvanceMedia); - - if (header->Collate != expected->Collate) - printf(" Collate %d, expected %d\n", header->Collate, - expected->Collate); - - if (header->CutMedia != expected->CutMedia) - printf(" CutMedia %d, expected %d\n", header->CutMedia, - expected->CutMedia); - - if (header->Duplex != expected->Duplex) - printf(" Duplex %d, expected %d\n", header->Duplex, - expected->Duplex); - - if (header->HWResolution[0] != expected->HWResolution[0] || - header->HWResolution[1] != expected->HWResolution[1]) - printf(" HWResolution [%d %d], expected [%d %d]\n", - header->HWResolution[0], header->HWResolution[1], - expected->HWResolution[0], expected->HWResolution[1]); - - if (memcmp(header->ImagingBoundingBox, expected->ImagingBoundingBox, - sizeof(header->ImagingBoundingBox))) - printf(" ImagingBoundingBox [%d %d %d %d], expected [%d %d %d %d]\n", - header->ImagingBoundingBox[0], - header->ImagingBoundingBox[1], - header->ImagingBoundingBox[2], - header->ImagingBoundingBox[3], - expected->ImagingBoundingBox[0], - expected->ImagingBoundingBox[1], - expected->ImagingBoundingBox[2], - expected->ImagingBoundingBox[3]); - - if (header->InsertSheet != expected->InsertSheet) - printf(" InsertSheet %d, expected %d\n", header->InsertSheet, - expected->InsertSheet); - - if (header->Jog != expected->Jog) - printf(" Jog %d, expected %d\n", header->Jog, - expected->Jog); - - if (header->LeadingEdge != expected->LeadingEdge) - printf(" LeadingEdge %d, expected %d\n", header->LeadingEdge, - expected->LeadingEdge); - - if (header->Margins[0] != expected->Margins[0] || - header->Margins[1] != expected->Margins[1]) - printf(" Margins [%d %d], expected [%d %d]\n", - header->Margins[0], header->Margins[1], - expected->Margins[0], expected->Margins[1]); - - if (header->ManualFeed != expected->ManualFeed) - printf(" ManualFeed %d, expected %d\n", header->ManualFeed, - expected->ManualFeed); - - if (header->MediaPosition != expected->MediaPosition) - printf(" MediaPosition %d, expected %d\n", header->MediaPosition, - expected->MediaPosition); - - if (header->MediaWeight != expected->MediaWeight) - printf(" MediaWeight %d, expected %d\n", header->MediaWeight, - expected->MediaWeight); - - if (header->MirrorPrint != expected->MirrorPrint) - printf(" MirrorPrint %d, expected %d\n", header->MirrorPrint, - expected->MirrorPrint); - - if (header->NegativePrint != expected->NegativePrint) - printf(" NegativePrint %d, expected %d\n", header->NegativePrint, - expected->NegativePrint); - - if (header->NumCopies != expected->NumCopies) - printf(" NumCopies %d, expected %d\n", header->NumCopies, - expected->NumCopies); - - if (header->Orientation != expected->Orientation) - printf(" Orientation %d, expected %d\n", header->Orientation, - expected->Orientation); - - if (header->OutputFaceUp != expected->OutputFaceUp) - printf(" OutputFaceUp %d, expected %d\n", header->OutputFaceUp, - expected->OutputFaceUp); - - if (header->PageSize[0] != expected->PageSize[0] || - header->PageSize[1] != expected->PageSize[1]) - printf(" PageSize [%d %d], expected [%d %d]\n", - header->PageSize[0], header->PageSize[1], - expected->PageSize[0], expected->PageSize[1]); - - if (header->Separations != expected->Separations) - printf(" Separations %d, expected %d\n", header->Separations, - expected->Separations); - - if (header->TraySwitch != expected->TraySwitch) - printf(" TraySwitch %d, expected %d\n", header->TraySwitch, - expected->TraySwitch); - - if (header->Tumble != expected->Tumble) - printf(" Tumble %d, expected %d\n", header->Tumble, - expected->Tumble); - - if (header->cupsWidth != expected->cupsWidth) - printf(" cupsWidth %d, expected %d\n", header->cupsWidth, - expected->cupsWidth); - - if (header->cupsHeight != expected->cupsHeight) - printf(" cupsHeight %d, expected %d\n", header->cupsHeight, - expected->cupsHeight); - - if (header->cupsMediaType != expected->cupsMediaType) - printf(" cupsMediaType %d, expected %d\n", header->cupsMediaType, - expected->cupsMediaType); - - if (header->cupsBitsPerColor != expected->cupsBitsPerColor) - printf(" cupsBitsPerColor %d, expected %d\n", header->cupsBitsPerColor, - expected->cupsBitsPerColor); - - if (header->cupsBitsPerPixel != expected->cupsBitsPerPixel) - printf(" cupsBitsPerPixel %d, expected %d\n", header->cupsBitsPerPixel, - expected->cupsBitsPerPixel); - - if (header->cupsBytesPerLine != expected->cupsBytesPerLine) - printf(" cupsBytesPerLine %d, expected %d\n", header->cupsBytesPerLine, - expected->cupsBytesPerLine); - - if (header->cupsColorOrder != expected->cupsColorOrder) - printf(" cupsColorOrder %d, expected %d\n", header->cupsColorOrder, - expected->cupsColorOrder); - - if (header->cupsColorSpace != expected->cupsColorSpace) - printf(" cupsColorSpace %d, expected %d\n", header->cupsColorSpace, - expected->cupsColorSpace); - - if (header->cupsCompression != expected->cupsCompression) - printf(" cupsCompression %d, expected %d\n", header->cupsCompression, - expected->cupsCompression); - - if (header->cupsRowCount != expected->cupsRowCount) - printf(" cupsRowCount %d, expected %d\n", header->cupsRowCount, - expected->cupsRowCount); - - if (header->cupsRowFeed != expected->cupsRowFeed) - printf(" cupsRowFeed %d, expected %d\n", header->cupsRowFeed, - expected->cupsRowFeed); - - if (header->cupsRowStep != expected->cupsRowStep) - printf(" cupsRowStep %d, expected %d\n", header->cupsRowStep, - expected->cupsRowStep); - - if (header->cupsNumColors != expected->cupsNumColors) - printf(" cupsNumColors %d, expected %d\n", header->cupsNumColors, - expected->cupsNumColors); - - if (fabs(header->cupsBorderlessScalingFactor - expected->cupsBorderlessScalingFactor) > 0.001) - printf(" cupsBorderlessScalingFactor %g, expected %g\n", - header->cupsBorderlessScalingFactor, - expected->cupsBorderlessScalingFactor); - - if (fabs(header->cupsPageSize[0] - expected->cupsPageSize[0]) > 0.001 || - fabs(header->cupsPageSize[1] - expected->cupsPageSize[1]) > 0.001) - printf(" cupsPageSize [%g %g], expected [%g %g]\n", - header->cupsPageSize[0], header->cupsPageSize[1], - expected->cupsPageSize[0], expected->cupsPageSize[1]); - - if (fabs(header->cupsImagingBBox[0] - expected->cupsImagingBBox[0]) > 0.001 || - fabs(header->cupsImagingBBox[1] - expected->cupsImagingBBox[1]) > 0.001 || - fabs(header->cupsImagingBBox[2] - expected->cupsImagingBBox[2]) > 0.001 || - fabs(header->cupsImagingBBox[3] - expected->cupsImagingBBox[3]) > 0.001) - printf(" cupsImagingBBox [%g %g %g %g], expected [%g %g %g %g]\n", - header->cupsImagingBBox[0], header->cupsImagingBBox[1], - header->cupsImagingBBox[2], header->cupsImagingBBox[3], - expected->cupsImagingBBox[0], expected->cupsImagingBBox[1], - expected->cupsImagingBBox[2], expected->cupsImagingBBox[3]); - - for (i = 0; i < 16; i ++) - if (header->cupsInteger[i] != expected->cupsInteger[i]) - printf(" cupsInteger%d %d, expected %d\n", i, header->cupsInteger[i], - expected->cupsInteger[i]); - - for (i = 0; i < 16; i ++) - if (fabs(header->cupsReal[i] - expected->cupsReal[i]) > 0.001) - printf(" cupsReal%d %g, expected %g\n", i, header->cupsReal[i], - expected->cupsReal[i]); - - for (i = 0; i < 16; i ++) - if (strcmp(header->cupsString[i], expected->cupsString[i])) - printf(" cupsString%d (%s), expected (%s)\n", i, - header->cupsString[i], expected->cupsString[i]); - - if (strcmp(header->cupsMarkerType, expected->cupsMarkerType)) - printf(" cupsMarkerType (%s), expected (%s)\n", header->cupsMarkerType, - expected->cupsMarkerType); - - if (strcmp(header->cupsRenderingIntent, expected->cupsRenderingIntent)) - printf(" cupsRenderingIntent (%s), expected (%s)\n", - header->cupsRenderingIntent, - expected->cupsRenderingIntent); - - if (strcmp(header->cupsPageSizeName, expected->cupsPageSizeName)) - printf(" cupsPageSizeName (%s), expected (%s)\n", - header->cupsPageSizeName, - expected->cupsPageSizeName); -} -- cgit v1.2.1