[gs-commits] ghostpdl branch, master, updated. jbig2dec-0.14-1859-g98a8b29

[email protected] (Robin Watts) Wed, 13 Nov 2019 18:02:01 +0000 (UTC)
Newsgroups gmane.comp.printing.ghostscript.cvs
Message-ID <[email protected]>
The ghostpdl branch, master has been updated
       via  98a8b291b135d49ffca23d9a289786971cf33a12 (commit)
       via  91b104936ec55b3f1f22673d7e286ff67e4e2475 (commit)
      from  af558a708b40a78eef9c0bc5b2ae229d771a3537 (commit)

----------------------------------------------------------------------
commit 98a8b291b135d49ffca23d9a289786971cf33a12
Author: Robin Watts <[email protected]>
Date:   Thu Nov 7 15:36:55 2019 +0000

    img language implementation
    
    JPEGs only at this point.

diff --git a/Makefile.in b/Makefile.in
index 83d1735..add2f36 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -63,6 +63,10 @@ URFSRCDIR=@srcdir@/urf
 URFGENDIR=./$(BUILDDIRPREFIX)@OBJDIR_BSDMAKE_WORKAROUND@
 URFOBJDIR=./$(BUILDDIRPREFIX)@OBJDIR_BSDMAKE_WORKAROUND@
 
+IMGSRCDIR=@srcdir@/gpdl/image
+IMGGENDIR=./$(BUILDDIRPREFIX)@OBJDIR_BSDMAKE_WORKAROUND@
+IMGOBJDIR=./$(BUILDDIRPREFIX)@OBJDIR_BSDMAKE_WORKAROUND@
+
 CONTRIBDIR=@srcdir@/contrib
 
 # Do not edit the next group of lines.
diff --git a/gpdl/gpdl.mak b/gpdl/gpdl.mak
index bd66b0e..b2f39aa 100644
--- a/gpdl/gpdl.mak
+++ b/gpdl/gpdl.mak
@@ -18,6 +18,7 @@ GPDL_MAK=$(GPDLSRCDIR)$(D)gpdl.mak
 GPDLSRC=$(GPDLSRCDIR)$(D)
 GPDLPSISRC=$(GPDLSRCDIR)$(D)psi$(D)
 GPDLURFSRC=$(URFSRCDIR)$(D)
+GPDLIMGSRC=$(GPDLSRCDIR)$(D)image$(D)
 
 GPDLOBJ=$(GPDLOBJDIR)$(D)
 GPDLGEN=$(GPDLGENDIR)$(D)
@@ -29,9 +30,13 @@ GPDL_PSI_TOP_OBJ=$(GPDLOBJ)/$(GPDL_PSI_TOP_OBJ_FILE)
 
 GPDL_URF_TOP_OBJ_FILE=urftop.$(OBJ)
 
-GPDL_PSI_TOP_OBJS=$(GPDL_URF_TOP_OBJ) $(GPDL_PSI_TOP_OBJ) $(GPDLOBJ)gpdlimpl.$(OBJ)
+GPDL_IMG_TOP_OBJ_FILE=imagetop.$(OBJ)
+GPDL_IMG_TOP_OBJ=$(GPDLOBJ)/$(GPDL_IMG_TOP_OBJ_FILE)
+
+GPDL_PSI_TOP_OBJS=$(GPDL_IMG_TOP_OBJ) $(GPDL_URF_TOP_OBJ) $(GPDL_PSI_TOP_OBJ) $(GPDLOBJ)gpdlimpl.$(OBJ)
+
+LANG_CFLAGS=$(D_)PCL_INCLUDED$(_D) $(D_)PSI_INCLUDED$(_D) $(D_)XPS_INCLUDED$(_D) $(ENABLE_URF) $(D_)IMG_INCLUDED$(_D)
 
-LANG_CFLAGS=$(D_)PCL_INCLUDED$(_D) $(D_)PSI_INCLUDED$(_D) $(D_)XPS_INCLUDED$(_D) $(ENABLE_URF)
 
 GPDLCC=$(CC_) $(LANG_CFLAGS) $(I_)$(PSSRCDIR)$(_I) $(I_)$(PLSRCDIR)$(_I) $(I_)$(GLSRCDIR)$(_I) $(I_)$(DEVSRCDIR)$(_I) $(I_)$(GLGENDIR)$(_I) $(C_)
 
@@ -61,3 +66,8 @@ $(GPDLOBJ)/$(GPDL_URF_TOP_OBJ_FILE): $(GPDLURFSRC)urftop.c $(AK)\
  $(gxdevice_h) $(gserrors_h) $(gsstate_h) $(surfx_h) $(strimpl_h)\
  $(gscoord_h) $(pltop_h)
 	$(GPDLCC) $(GPDLURFSRC)urftop.c $(GPDLO_)$(GPDL_URF_TOP_OBJ_FILE)
+
+$(GPDL_IMG_TOP_OBJ): $(GPDLIMGSRC)imagetop.c $(AK)\
+ $(gxdevice_h) $(gserrors_h) $(gsstate_h) $(strimpl_h) $(gscoord_h)\
+ $(gsstate_h) $(jpeglib_h) $(setjmp__h) $(sjpeg_h)
+	$(GPDLCC) $(II)$(JI_)$(_I) $(GPDLIMGSRC)imagetop.c $(GPDLO_)$(GPDL_IMG_TOP_OBJ_FILE)
diff --git a/gpdl/image/imagetop.c b/gpdl/image/imagetop.c
new file mode 100644
index 0000000..b3232dc
--- /dev/null
+++ b/gpdl/image/imagetop.c
@@ -0,0 +1,855 @@
+/* Copyright (C) 2019 Artifex Software, Inc.
+   All Rights Reserved.
+
+   This software is provided AS-IS with no warranty, either express or
+   implied.
+
+   This software is distributed under license and may not be copied,
+   modified or distributed except as expressly authorized under the terms
+   of the license contained in the file LICENSE in this distribution.
+
+   Refer to licensing information at http://www.artifex.com or contact
+   Artifex Software, Inc.,  1305 Grant Avenue - Suite 200, Novato,
+   CA 94945, U.S.A., +1(415)492-9861, for further information.
+*/
+
+/* imagetop.c */
+/* Top-level API implementation of "Image" Language Interface */
+
+#include "pltop.h"
+#include "gserrors.h"
+#include "gxdevice.h"
+#include "gsstate.h"
+#include "strimpl.h"
+#include "gscoord.h"
+#include "jpeglib.h"
+#include "setjmp_.h"
+#include "sjpeg.h"
+
+/* Forward decls */
+
+/************************************************************/
+/******** Language wrapper implementation (see pltop.h) *****/
+/************************************************************/
+
+typedef enum
+{
+    ii_state_identifying = 0,
+    ii_state_jpeg,
+    ii_state_jpeg_header,
+    ii_state_jpeg_start,
+    ii_state_jpeg_rows,
+    ii_state_jpeg_finish,
+    ii_state_flush
+} ii_state;
+
+typedef struct {
+    struct jpeg_error_mgr pub;
+    jmp_buf setjmp_buffer;
+} img_error_mgr;
+
+/*
+ * Image interpreter instance
+ */
+typedef struct img_interp_instance_s {
+    gs_memory_t       *memory;
+    gs_memory_t       *cmemory;
+    gx_device         *dev;
+    gx_device         *nulldev;
+
+    gs_color_space    *gray;
+    gs_color_space    *rgb;
+    gs_color_space    *cmyk;
+
+    /* Image parser state machine */
+    ii_state           state;
+
+    int                pages;
+
+    uint8_t            bpp;
+    uint8_t            cs;
+    uint32_t           width;
+    uint32_t           height;
+    uint32_t           xresolution;
+    uint32_t           yresolution;
+
+    uint32_t           num_comps;
+    uint32_t           byte_width;
+    uint32_t           y;
+
+    uint32_t           bytes_available_on_entry;
+
+    gs_image_t         image;
+    gs_image_enum     *penum;
+    gs_gstate         *pgs;
+
+    struct jpeg_decompress_struct cinfo;
+    struct jpeg_source_mgr        jsrc;
+    size_t             bytes_to_skip;
+    img_error_mgr      jerr;
+
+    byte              *samples;
+
+} img_interp_instance_t;
+
+static int
+img_detect_language(const char *s, int len)
+{
+    const byte *hdr = (const byte *)s;
+    if (len >= 11) {
+        if (hdr[0] == 0xFF && hdr[1] == 0xd8 && hdr[2] == 0xff && hdr[3] == 0xe0 &&
+            strncmp("JFIF", s+6, 5) == 0)
+            return 100;
+    }
+    /* FIXME: Other formats go here */
+
+    return 0;
+}
+
+static const pl_interp_characteristics_t img_characteristics = {
+    "IMAGE",
+    img_detect_language,
+    "Artifex",
+    "0.01",
+    "11 Nov 2019",
+    1 /* minimum input size */
+};
+
+/* GS's fakakta jpeg integration insists on putting a
+ * memory structure pointer in the decompress structs client data.
+ * This is no good to find our instance from. We therefore find
+ * it by offsetting back from the address of the cinfo. This is
+ * a nasty bit of casting, so wrap it in a macro. */
+#define IMG_FROM_CINFO(CINFO) \
+    (img_interp_instance_t *)(((char *)(CINFO))-offsetof(img_interp_instance_t,cinfo))
+
+
+/* Get implementation's characteristics */
+static const pl_interp_characteristics_t * /* always returns a descriptor */
+img_impl_characteristics(const pl_interp_implementation_t *impl)     /* implementation of interpreter to alloc */
+{
+  return &img_characteristics;
+}
+
+static void
+img_deallocate(img_interp_instance_t *img)
+{
+    if (img == NULL)
+        return;
+
+    rc_decrement_cs(img->gray, "img_deallocate");
+    rc_decrement_cs(img->rgb, "img_deallocate");
+    rc_decrement_cs(img->cmyk, "img_deallocate");
+
+    if (img->pgs != NULL)
+        gs_gstate_free_chain(img->pgs);
+    gs_free_object(img->memory, img, "img_impl_allocate_interp_instance");
+}
+
+/* Deallocate a interpreter instance */
+static int
+img_impl_deallocate_interp_instance(pl_interp_implementation_t *impl)
+{
+    img_interp_instance_t *img = (img_interp_instance_t *)impl->interp_client_data;
+
+    img_deallocate(img);
+    impl->interp_client_data = NULL;
+
+    return 0;
+}
+
+/* Do per-instance interpreter allocation/init. */
+static int
+img_impl_allocate_interp_instance(pl_interp_implementation_t *impl, gs_memory_t *mem)
+{
+    int code;
+    img_interp_instance_t *img
+        = (img_interp_instance_t *)gs_alloc_bytes(mem,
+                                                  sizeof(img_interp_instance_t),
+                                                  "img_impl_allocate_interp_instance");
+    if (!img)
+        return_error(gs_error_VMerror);
+    memset(img, 0, sizeof(*img));
+
+    img->memory = mem;
+    img->pgs = gs_gstate_alloc(mem);
+    if (img->pgs == NULL)
+        goto failVM;
+
+    /* Push one save level onto the stack to assuage the memory handling */
+    code = gs_gsave(img->pgs);
+    if (code < 0)
+        goto fail;
+
+    code = gsicc_init_iccmanager(img->pgs);
+    if (code < 0)
+        goto fail;
+
+    img->gray = gs_cspace_new_ICC(mem, img->pgs, 1);
+    img->rgb  = gs_cspace_new_ICC(mem, img->pgs, 3);
+    img->cmyk = gs_cspace_new_ICC(mem, img->pgs, 4);
+
+    impl->interp_client_data = img;
+
+    return 0;
+
+failVM:
+    code = gs_note_error(gs_error_VMerror);
+fail:
+    (void)img_deallocate(img);
+    return code;
+}
+
+/*
+ * Get the allocator with which to allocate a device
+ */
+static gs_memory_t *
+img_impl_get_device_memory(pl_interp_implementation_t *impl)
+{
+    img_interp_instance_t *img = (img_interp_instance_t *)impl->interp_client_data;
+
+    return img->dev ? img->dev->memory : NULL;
+}
+
+#if 0 /* UNUSED */
+static int
+img_impl_set_param(pl_interp_implementation_t *impl,
+                   pl_set_param_type           type,
+                   const char                 *param,
+                   const void                 *val)
+{
+    img_interp_instance_t *img = (img_interp_instance_t *)impl->interp_client_data;
+
+    /* No params set here */
+    return 0;
+}
+
+static int
+img_impl_add_path(pl_interp_implementation_t *impl,
+                  const char                 *path)
+{
+    img_interp_instance_t *img = (img_interp_instance_t *)impl->interp_client_data;
+
+    /* No paths to add */
+    return 0;
+}
+
+static int
+img_impl_post_args_init(pl_interp_implementation_t *impl)
+{
+    img_interp_instance_t *img = (img_interp_instance_t *)impl->interp_client_data;
+
+    /* No post args processing */
+    return 0;
+}
+#endif
+
+/* Prepare interp instance for the next "job" */
+static int
+img_impl_init_job(pl_interp_implementation_t *impl,
+                  gx_device                  *device)
+{
+    img_interp_instance_t *img = (img_interp_instance_t *)impl->interp_client_data;
+
+    img->dev = device;
+    img->state = ii_state_identifying;
+
+    return 0;
+}
+
+#if 0 /* UNUSED */
+static int
+img_impl_run_prefix_commands(pl_interp_implementation_t *impl,
+                             const char                 *prefix)
+{
+    img_interp_instance_t *img = (img_interp_instance_t *)impl->interp_client_data;
+
+    return 0;
+}
+
+static int
+img_impl_process_file(pl_interp_implementation_t *impl, const char *filename)
+{
+    img_interp_instance_t *img = (img_interp_instance_t *)impl->interp_client_data;
+
+    return 0;
+}
+#endif
+
+/* Do any setup for parser per-cursor */
+static int                      /* ret 0 or +ve if ok, else -ve error code */
+img_impl_process_begin(pl_interp_implementation_t * impl)
+{
+    img_interp_instance_t *img = (img_interp_instance_t *)impl->interp_client_data;
+
+    return 0;
+}
+
+/* Ensure we have 'required' bytes to read, and further ensure
+ * that we have no UEL's within those bytes. */
+static int
+ensure_bytes(img_interp_instance_t *img, stream_cursor_read *pr, int required)
+{
+    int n;
+    const uint8_t *p = pr->ptr+1;
+    const uint8_t *q;
+    int avail;
+
+    /* Find out how many bytes we need to check */
+    n = pr->limit - pr->ptr;
+    if (n > required)
+        n = required;
+
+    /* Make sure there are no UELs in that block */
+    q = p + n;
+    while (p != q) {
+        while (p != q && *p != '\033')
+            p++;
+        if (p == q)
+            break;
+        avail = pr->limit - pr->ptr;
+        if (memcmp(p, "\033%-12345X", min(avail, 9)) == 0) {
+            /* At least a partial match to a UEL */
+            return avail < 9 ? gs_error_NeedInput : gs_error_InterpreterExit;
+        }
+        p++;
+    }
+
+    /* If we have enough bytes, great, if not, get some more */
+    return (n < required) ? gs_error_NeedInput : 0;
+}
+
+static int
+flush_to_uel(stream_cursor_read *pr)
+{
+    const uint8_t *p = pr->ptr+1;
+    const uint8_t *q = pr->limit+1;
+    int avail;
+
+    while (p != q) {
+        while (p != q && *p != '\033')
+            p++;
+        if (p == q)
+            break;
+        avail = pr->limit - pr->ptr;
+        if (memcmp(p, "\033%-12345X", min(avail, 9)) == 0) {
+            /* At least a partial match to a UEL. Bin everything to
+             * the start of the match. */
+            pr->ptr = p-1;
+            if (avail == 9) /* Complete match. Exit! */
+                return gs_error_InterpreterExit;
+            /* Partial match. Get more data. */
+            return gs_error_NeedInput;
+        }
+        p++;
+    }
+
+    pr->ptr = pr->limit;
+
+    return 0;
+}
+
+static int
+bytes_until_uel(const stream_cursor_read *pr)
+{
+    const uint8_t *p = pr->ptr+1;
+    const uint8_t *q = pr->limit+1;
+    int avail;
+
+    while (p != q) {
+        while (p != q && *p != '\033')
+            p++;
+        if (p == q)
+            break;
+        avail = pr->limit - pr->ptr;
+        if (memcmp(p, "\033%-12345X", min(avail, 9)) == 0) {
+            /* At least a partial match to a UEL. Everything up to
+             * the start of the match is up for grabs. */
+            return p - (pr->ptr+1);
+        }
+        p++;
+    }
+
+    return pr->limit - pr->ptr;
+}
+
+static int
+get32be(stream_cursor_read *pr)
+{
+    int v = pr->ptr[1] << 24;
+    v |= pr->ptr[2] << 16;
+    v |= pr->ptr[3] << 8;
+    v |= pr->ptr[4];
+    pr->ptr += 4;
+
+    return v;
+}
+
+static int
+get8(stream_cursor_read *pr)
+{
+    return *++(pr->ptr);
+}
+
+static void
+img_jpeg_init_source(j_decompress_ptr cinfo)
+{
+    /* We've already inited the source */
+}
+
+static boolean
+img_fill_input_buffer(j_decompress_ptr cinfo)
+{
+    return FALSE; /* We've filled the buffer already */
+}
+
+static void
+img_skip_input_data(j_decompress_ptr cinfo, long num_bytes)
+{
+    img_interp_instance_t *img = IMG_FROM_CINFO(cinfo);
+    size_t n = cinfo->src->bytes_in_buffer;
+
+    if (n > num_bytes)
+        n = num_bytes;
+    img->jsrc.next_input_byte += num_bytes;
+    img->jsrc.bytes_in_buffer -= n;
+}
+
+static boolean
+img_resync_to_restart(j_decompress_ptr cinfo, int desired)
+{
+    return FALSE;
+}
+
+static void
+img_term_source(j_decompress_ptr cinfo)
+{
+}
+
+static void
+img_error_exit(j_common_ptr cinfo)
+{
+    img_error_mgr *jerr = (img_error_mgr *)cinfo->err;
+
+    longjmp(jerr->setjmp_buffer, 1);
+}
+
+static int
+fill_jpeg_source(img_interp_instance_t *img, stream_cursor_read * pr)
+{
+    size_t n = pr->limit - pr->ptr;
+    size_t skip = img->bytes_to_skip;
+
+    /* Skip any bytes we are supposed to be skipping. */
+    if (skip > 0) {
+        if (skip > n)
+            skip = n;
+        pr->ptr += skip;
+        n -= skip;
+        img->bytes_to_skip -= skip;
+        if (img->bytes_to_skip != 0)
+            return 1; /* Still more to skip */
+    }
+
+    /* Set up for the call into the jpeg lib */
+    img->jsrc.next_input_byte = pr->ptr+1;
+    img->jsrc.bytes_in_buffer = pr->limit - pr->ptr;
+    img->bytes_available_on_entry = img->jsrc.bytes_in_buffer;
+
+    return 0;
+}
+
+static int
+consume_jpeg_data(img_interp_instance_t *img, stream_cursor_read *pr)
+{
+    size_t bytes_read = img->jsrc.next_input_byte - (pr->ptr+1);
+    size_t n = pr->limit - pr->ptr;
+
+    if (n > bytes_read)
+        n = bytes_read;
+    pr->ptr += n;
+    bytes_read -= n;
+
+    /* We need to skip the next bytes_read bytes */
+    img->bytes_to_skip = bytes_read;
+
+    return (pr->limit - pr->ptr == img->bytes_available_on_entry);
+}
+
+static int
+img_impl_process(pl_interp_implementation_t * impl, stream_cursor_read * pr)
+{
+    img_interp_instance_t *img = (img_interp_instance_t *)impl->interp_client_data;
+    int code = 0;
+    int need_more_data;
+
+    do
+    {
+        need_more_data = 0;
+        switch(img->state)
+        {
+        case ii_state_identifying:
+        {
+            const byte *hdr;
+            /* Try and get us 11 bytes */
+            code = ensure_bytes(img, pr, 11);
+            if (code < 0)
+                return code;
+            hdr = pr->ptr+1;
+            if (hdr[0] == 0xFF && hdr[1] == 0xd8 && hdr[2] == 0xff && hdr[3] == 0xe0 &&
+                memcmp("JFIF", hdr+6, 5) == 0)
+            {
+                img->state = ii_state_jpeg;
+                break;
+            }
+            img->state = ii_state_flush;
+            break;
+        }
+        case ii_state_jpeg:
+            code = gs_jpeg_mem_init(img->memory, (j_common_ptr)&img->cinfo);
+            if (code < 0) {
+                img->state = ii_state_flush;
+                break;
+            }
+
+            img->cinfo.err = jpeg_std_error(&img->jerr.pub);
+            img->jerr.pub.error_exit = img_error_exit;
+            if (setjmp(img->jerr.setjmp_buffer)) {
+                img->state = ii_state_flush;
+                break;
+            }
+
+            jpeg_create_decompress(&img->cinfo);
+
+            img->cinfo.src = &img->jsrc;
+            img->jsrc.init_source = img_jpeg_init_source;
+            img->jsrc.fill_input_buffer = img_fill_input_buffer;
+            img->jsrc.skip_input_data = img_skip_input_data;
+            img->jsrc.resync_to_restart = img_resync_to_restart;
+            img->jsrc.term_source = img_term_source;
+
+            img->state = ii_state_jpeg_header;
+            break;
+        case ii_state_jpeg_header:
+        {
+            int ok;
+            gs_color_space *cs;
+            float scale, xext, yext, xoffset, yoffset;
+
+            if (fill_jpeg_source(img, pr)) {
+                need_more_data = 1;
+                break; /* No bytes left after skipping */
+            }
+            if (setjmp(img->jerr.setjmp_buffer)) {
+                jpeg_destroy_decompress(&img->cinfo);
+                img->state = ii_state_flush;
+                break;
+            }
+            ok = jpeg_read_header(&img->cinfo, TRUE);
+            need_more_data = consume_jpeg_data(img, pr);
+            if (ok == JPEG_SUSPENDED)
+                break;
+            need_more_data = 0;
+
+            img->width = img->cinfo.image_width;
+            img->height = img->cinfo.image_height;
+            img->num_comps = img->cinfo.num_components;
+            img->bpp = 8 * img->num_comps;
+            switch(img->num_comps) {
+            default:
+            case 1:
+                cs = img->gray;
+                img->cinfo.out_color_space = JCS_GRAYSCALE;
+                break;
+            case 3:
+                cs = img->rgb;
+                img->cinfo.out_color_space = JCS_RGB;
+                break;
+            case 4:
+                cs = img->cmyk;
+                img->cinfo.out_color_space = JCS_CMYK;
+                break;
+            }
+
+            /* Find us some X and Y resolutions */
+            img->xresolution = img->cinfo.X_density;
+            img->yresolution = img->cinfo.Y_density;
+            if (img->xresolution == 0)
+                img->xresolution = img->yresolution;
+            if (img->yresolution == 0)
+                img->yresolution = img->xresolution;
+            if (img->xresolution == 0)
+                img->xresolution = 72;
+            if (img->yresolution == 0)
+                img->yresolution = 72;
+
+            /* Scale to fit, if too large. */
+            scale = 1.0f;
+            if (img->width * 72 > img->dev->width * img->xresolution)
+                scale = ((float)img->dev->width * img->xresolution) / (img->width * 72);
+            if (scale * img->height * 72 > img->dev->height * img->yresolution)
+                scale = ((float)img->dev->height * img->yresolution) / (img->height * 72);
+
+            /* Centre */
+            xext = ((float)img->width * 72 * scale / img->xresolution);
+            xoffset = (img->dev->width - xext)/2;
+            yext = ((float)img->height * 72 * scale / img->yresolution);
+            yoffset = (img->dev->height - yext)/2;
+
+
+            /* Now we've got the data from the image header, we can
+             * make the gs image instance */
+            img->byte_width = (img->bpp>>3)*img->width;
+
+            img->nulldev = gs_currentdevice(img->pgs);
+            rc_increment(img->nulldev);
+            code = gs_setdevice_no_erase(img->pgs, img->dev);
+            if (code < 0) {
+                img->state = ii_state_flush;
+                break;
+            }
+            gs_initmatrix(img->pgs);
+
+            code = gs_translate(img->pgs, xoffset, img->dev->height-yoffset);
+            if (code >= 0)
+                code = gs_scale(img->pgs, scale, -scale);
+            if (code >= 0)
+                code = gs_erasepage(img->pgs);
+            if (code < 0) {
+                img->state = ii_state_flush;
+                break;
+            }
+
+            img->samples = gs_alloc_bytes(img->memory, img->byte_width, "img_impl_process(samples)");
+            if (img->samples == NULL) {
+                img->state = ii_state_flush;
+                break;
+            }
+
+            memset(&img->image, 0, sizeof(img->image));
+            gs_image_t_init(&img->image, cs);
+            img->image.BitsPerComponent = img->bpp/img->num_comps;
+            img->image.Width = img->width;
+            img->image.Height = img->height;
+
+            img->image.ImageMatrix.xx = img->xresolution / 72.0;
+            img->image.ImageMatrix.yy = img->yresolution / 72.0;
+
+            img->penum = gs_image_enum_alloc(img->memory, "img_impl_process(penum)");
+            if (img->penum == NULL) {
+                code = gs_note_error(gs_error_VMerror);
+                img->state = ii_state_flush;
+                return code;
+            }
+
+            code = gs_image_init(img->penum,
+                                 &img->image,
+                                 false,
+                                 false,
+                                 img->pgs);
+            if (code < 0) {
+                img->state = ii_state_flush;
+                return code;
+            }
+
+            img->state = ii_state_jpeg_start;
+            break;
+        }
+        case ii_state_jpeg_start:
+        {
+            int ok;
+            if (fill_jpeg_source(img, pr)) {
+                need_more_data = 1;
+                break; /* No bytes left after skipping */
+            }
+            if (setjmp(img->jerr.setjmp_buffer)) {
+                jpeg_destroy_decompress(&img->cinfo);
+                img->state = ii_state_flush;
+                break;
+            }
+            ok = jpeg_start_decompress(&img->cinfo);
+            (void)consume_jpeg_data(img, pr);
+            if (ok == FALSE)
+                break;
+            img->state = ii_state_jpeg_rows;
+            break;
+        }
+        case ii_state_jpeg_rows:
+        {
+            int rows_decoded;
+            unsigned int used;
+
+            if (fill_jpeg_source(img, pr)) {
+                need_more_data = 1;
+                break; /* No bytes left after skipping */
+            }
+            if (setjmp(img->jerr.setjmp_buffer)) {
+                jpeg_destroy_decompress(&img->cinfo);
+                img->state = ii_state_flush;
+                break;
+            }
+            rows_decoded = jpeg_read_scanlines(&img->cinfo, &img->samples, 1);
+            need_more_data = consume_jpeg_data(img, pr);
+            if (rows_decoded == 0)
+                break; /* Not enough data for a scanline yet */
+            need_more_data = 0;
+
+            code = gs_image_next(img->penum, img->samples, img->byte_width, &used);
+            if (code < 0) {
+                img->state = ii_state_flush;
+                break;
+            }
+            img->y++;
+            if (img->y == img->height) {
+                code = gs_image_cleanup_and_free_enum(img->penum, img->pgs);
+                img->penum = NULL;
+                if (code < 0) {
+                    img->state = ii_state_flush;
+                    break;
+                }
+                code = pl_finish_page(img->memory->gs_lib_ctx->top_of_system,
+                                      img->pgs, 1, true);
+                if (code < 0) {
+                    img->state = ii_state_flush;
+                    break;
+                }
+                img->state = ii_state_jpeg_finish;
+            }
+            break;
+        }
+        case ii_state_jpeg_finish:
+        {
+            int ok;
+
+            if (fill_jpeg_source(img, pr)) {
+                need_more_data = 1;
+                break; /* No bytes left after skipping */
+            }
+            if (setjmp(img->jerr.setjmp_buffer)) {
+                jpeg_destroy_decompress(&img->cinfo);
+                img->state = ii_state_flush;
+                break;
+            }
+            ok = jpeg_finish_decompress(&img->cinfo);
+            need_more_data = consume_jpeg_data(img, pr);
+            if (ok == FALSE)
+                break;
+            need_more_data = 0;
+            img->state = ii_state_flush;
+            break;
+        }
+        default:
+        case ii_state_flush:
+            if (setjmp(img->jerr.setjmp_buffer))
+                break;
+            jpeg_destroy_decompress(&img->cinfo);
+
+            gs_jpeg_mem_term((j_common_ptr)&img->cinfo);
+
+            if (img->penum) {
+                (void)gs_image_cleanup_and_free_enum(img->penum, img->pgs);
+                img->penum = NULL;
+            }
+
+            gs_free_object(img->memory, img->samples, "img_impl_process(samples)");
+            img->samples = NULL;
+            /* We want to bin any data we get up to, but not including
+             * a UEL. */
+            return flush_to_uel(pr);
+        }
+    } while (!need_more_data);
+
+    return code;
+}
+
+static int
+img_impl_process_end(pl_interp_implementation_t * impl)
+{
+    img_interp_instance_t *img = (img_interp_instance_t *)impl->interp_client_data;
+    int code = 0;
+
+    /* FIXME: */
+    if (code == gs_error_InterpreterExit || code == gs_error_NeedInput)
+        code = 0;
+
+    return code;
+}
+
+/* Not implemented */
+static int
+img_impl_flush_to_eoj(pl_interp_implementation_t *impl, stream_cursor_read *cursor)
+{
+    const byte *p = cursor->ptr;
+    const byte *rlimit = cursor->limit;
+
+    /* Skip to, but leave UEL in buffer for PJL to find later */
+    for (; p < rlimit; ++p)
+        if (p[1] == '\033') {
+            uint avail = rlimit - p;
+
+            if (memcmp(p + 1, "\033%-12345X", min(avail, 9)))
+                continue;
+            if (avail < 9)
+                break;
+            cursor->ptr = p;
+            return 1;           /* found eoj */
+        }
+    cursor->ptr = p;
+    return 0;                   /* need more */
+}
+
+/* Parser action for end-of-file */
+static int
+img_impl_process_eof(pl_interp_implementation_t *impl)
+{
+    return 0;
+}
+
+/* Report any errors after running a job */
+static int
+img_impl_report_errors(pl_interp_implementation_t *impl,          /* interp instance to wrap up job in */
+                       int                         code,          /* prev termination status */
+                       long                        file_position, /* file position of error, -1 if unknown */
+                       bool                        force_to_cout  /* force errors to cout */
+)
+{
+    return 0;
+}
+
+/* Wrap up interp instance after a "job" */
+static int
+img_impl_dnit_job(pl_interp_implementation_t *impl)
+{
+    img_interp_instance_t *img = (img_interp_instance_t *)impl->interp_client_data;
+
+    if (img->nulldev) {
+        int code = gs_setdevice(img->pgs, img->nulldev);
+        img->dev = NULL;
+        rc_decrement(img->nulldev, "img_impl_dnit_job(nulldevice)");
+        img->nulldev = NULL;
+        return code;
+    }
+    return 0;
+}
+
+/* Parser implementation descriptor */
+const pl_interp_implementation_t img_implementation = {
+  img_impl_characteristics,
+  img_impl_allocate_interp_instance,
+  img_impl_get_device_memory,
+  NULL, /* img_impl_set_param */
+  NULL, /* img_impl_add_path */
+  NULL, /* img_impl_post_args_init */
+  img_impl_init_job,
+  NULL, /* img_impl_run_prefix_commands */
+  NULL, /* img_impl_process_file */
+  img_impl_process_begin,
+  img_impl_process,
+  img_impl_process_end,
+  img_impl_flush_to_eoj,
+  img_impl_process_eof,
+  img_impl_report_errors,
+  img_impl_dnit_job,
+  img_impl_deallocate_interp_instance,
+  NULL
+};
diff --git a/pcl/pl/plimpl.c b/pcl/pl/plimpl.c
index 526ff19..ebfe3a4 100644
--- a/pcl/pl/plimpl.c
+++ b/pcl/pl/plimpl.c
@@ -37,6 +37,8 @@ extern pl_interp_implementation_t ps_implementation;
 
 extern pl_interp_implementation_t urf_implementation;
 
+extern pl_interp_implementation_t img_implementation;
+
 /* Zero-terminated list of pointers to implementations */
 pl_interp_implementation_t *pdl_implementations[] = {
     &pjl_implementation,
@@ -53,6 +55,9 @@ pl_interp_implementation_t *pdl_implementations[] = {
 #ifdef URF_INCLUDED
     &urf_implementation,
 #endif
+#ifdef IMG_INCLUDED
+    &img_implementation,
+#endif
     0
 };
 
@@ -66,4 +71,3 @@ const gs_ptr_procs_t ptr_string_procs = { NULL, NULL, NULL };
 const gs_ptr_procs_t ptr_const_string_procs = { NULL, NULL, NULL };
 const gs_ptr_procs_t ptr_name_index_procs = { NULL, NULL, NULL };
 #endif
-
diff --git a/psi/msvc.mak b/psi/msvc.mak
index c01e34d..f5e9258 100644
--- a/psi/msvc.mak
+++ b/psi/msvc.mak
@@ -242,6 +242,16 @@ URFGENDIR=$(GLGENDIR)
 URFOBJDIR=$(GLOBJDIR)
 !endif
 
+!ifndef IMGSRCDIR
+IMGSRCDIR=.\gpdl\img
+!endif
+!ifndef IMGGENDIR
+IMGGENDIR=$(GLGENDIR)
+!endif
+!ifndef IMGOBJDIR
+IMGOBJDIR=$(GLOBJDIR)
+!endif
+
 CONTRIBDIR=.\contrib
 
 # Can we build PCL and XPS
diff --git a/windows/ghostpdl.vcproj b/windows/ghostpdl.vcproj
index c6a385a..0d35c34 100644
--- a/windows/ghostpdl.vcproj
+++ b/windows/ghostpdl.vcproj
@@ -1,4 +1,4 @@
-<?xml version="1.0" encoding="UTF-8"?>
+<?xml version="1.0" encoding="UTF-8"?>
 <VisualStudioProject
 	ProjectType="Visual C++"
 	Version="8.00"
@@ -318,6 +318,14 @@
 				>
 			</File>
 		</Filter>
+		<Filter
+			Name="image"
+			>
+			<File
+				RelativePath="..\gpdl\image\imagetop.c"
+				>
+			</File>
+		</Filter>
 	</Files>
 	<Globals>
 	</Globals>

----------------------------------------------------------------------
commit 91b104936ec55b3f1f22673d7e286ff67e4e2475
Author: Robin Watts <[email protected]>
Date:   Thu Nov 7 15:36:55 2019 +0000

    Public build changes to accommodate private URF support.
    
    This includes build rubrik for devices to generate URF files
    (urfgray, urfrgb, urfcmyk), a decompression filter for the
    rle variant used in URF files and a urf "language" interpreter
    implementation for gpdl.
    
    Note, this is only the build framework for these things. The
    actual implementation code lives in the private 'urf'
    git module, and will be activated automatically as part of
    the build if it is in position at configure time.

diff --git a/Makefile.in b/Makefile.in
index 8e36018..83d1735 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -59,6 +59,10 @@ GPDLSRCDIR=@srcdir@/gpdl
 GPDLGENDIR=./$(BUILDDIRPREFIX)@OBJDIR_BSDMAKE_WORKAROUND@
 GPDLOBJDIR=./$(BUILDDIRPREFIX)@OBJDIR_BSDMAKE_WORKAROUND@
 
+URFSRCDIR=@srcdir@/urf
+URFGENDIR=./$(BUILDDIRPREFIX)@OBJDIR_BSDMAKE_WORKAROUND@
+URFOBJDIR=./$(BUILDDIRPREFIX)@OBJDIR_BSDMAKE_WORKAROUND@
+
 CONTRIBDIR=@srcdir@/contrib
 
 # Do not edit the next group of lines.
@@ -328,6 +332,12 @@ LCMS2_CFLAGS=-DSHARE_LCMS=$(SHARE_LCMS) @LCMS2_ENDIAN@ @SQRTF_SUBST@  @LCMS2_PTR
 # Options are currently lcms or lcms2
 WHICH_CMS=@WHICHLCMS@
 
+# Do we have URF support?
+ENABLE_URF=@ENABLEURF@
+GPDL_URF_TOP_OBJ=@GPDL_URF_TOP_OBJ@
+URF_INCLUDE=@URF_INCLUDE@
+URF_DEV=@URF_DEV@
+SURFX_H=@SURFX_H@
 
 EXPATSRCDIR=@EXPATDIR@
 EXPAT_CFLAGS=@EXPAT_CFLAGS@
@@ -558,7 +568,7 @@ FEATURE_DEVS=$(GLD)pipe.dev $(GLD)gsnogc.dev $(GLD)htxlib.dev $(GLD)psl3lib.dev
 	     $(GLD)seprlib.dev $(GLD)translib.dev $(GLD)cidlib.dev $(GLD)psf0lib.dev $(GLD)psf1lib.dev\
              $(GLD)psf2lib.dev $(GLD)lzwd.dev $(GLD)sicclib.dev \
              $(GLD)sjbig2.dev $(GLD)sjpx.dev $(GLD)ramfs.dev \
-             $(GLD)pwgd.dev $(GLD)siscale.dev
+             $(GLD)pwgd.dev $(GLD)siscale.dev $(URF_DEV)
 
 #FEATURE_DEVS=$(PSD)psl3.dev $(PSD)pdf.dev
 #FEATURE_DEVS=$(PSD)psl3.dev $(PSD)pdf.dev $(PSD)ttfont.dev $(GLD)pipe.dev
diff --git a/base/lib.mak b/base/lib.mak
index 5df3bf5..3292f51 100644
--- a/base/lib.mak
+++ b/base/lib.mak
@@ -595,6 +595,7 @@ spprint_h=$(GLSRC)spprint.h
 spsdf_h=$(GLSRC)spsdf.h
 srlx_h=$(GLSRC)srlx.h
 spwgx_h=$(GLSRC)spwgx.h
+surfx_h=$(SURFX_H)
 sstring_h=$(GLSRC)sstring.h
 strimpl_h=$(GLSRC)strimpl.h
 szlibx_h=$(GLSRC)szlibx.h
@@ -1942,6 +1943,16 @@ $(GLOBJ)spwgd.$(OBJ) : $(GLSRC)spwgd.c $(AK) $(stdio__h) $(memory__h)\
  $(spwgx_h) $(strimpl_h) $(LIB_MAK) $(MAKEDIRS)
 	$(GLCC) $(GLO_)spwgd.$(OBJ) $(C_) $(GLSRC)spwgd.c
 
+# ---------------- URF RunLength decode filter ---------------- #
+
+urfd_=$(GLOBJ)surfd.$(OBJ)
+$(GLD)urfd.dev : $(LIB_MAK) $(ECHOGS_XE) $(urfd_) $(LIB_MAK) $(MAKEDIRS)
+	$(SETMOD) $(GLD)urfd $(urfd_)
+
+$(GLOBJ)surfd.$(OBJ) : $(URFSRCDIR)$(D)surfd.c $(AK) $(stdio__h) $(memory__h)\
+ $(surfx_h) $(strimpl_h) $(LIB_MAK) $(MAKEDIRS)
+	$(GLCC) $(GLO_)surfd.$(OBJ) $(C_) $(URFSRCDIR)$(D)surfd.c
+
 # ---------------- String encoding/decoding filters ---------------- #
 # These are used by the PostScript and PDF writers, and also by the
 # PostScript interpreter.
diff --git a/base/unix-gcc.mak b/base/unix-gcc.mak
index a316038..99c2caa 100644
--- a/base/unix-gcc.mak
+++ b/base/unix-gcc.mak
@@ -521,7 +521,7 @@ FEATURE_DEVS=$(GLD)pipe.dev $(GLD)gsnogc.dev $(GLD)htxlib.dev $(GLD)psl3lib.dev
 	     $(GLD)seprlib.dev $(GLD)translib.dev $(GLD)cidlib.dev $(GLD)psf0lib.dev $(GLD)psf1lib.dev\
              $(GLD)psf2lib.dev $(GLD)lzwd.dev $(GLD)sicclib.dev \
              $(GLD)sjbig2.dev $(GLD)sjpx.dev $(GLD)ramfs.dev \
-             $(GLD)pwgd.dev
+             $(GLD)pwgd.dev $(GLD)urfd.dev
 
 
 
diff --git a/configure.ac b/configure.ac
index e80be41..71826fe 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1849,6 +1849,33 @@ AC_SUBST(JPXDIR)
 AC_SUBST(SHARE_JPX)
 AC_SUBST(JPXDEVS)
 
+ENABLEURF=
+URF_DEVS=''
+URF_INCLUDE=
+URF_DEV=
+SURFX_H=
+AC_ARG_WITH([urf], AC_HELP_STRING([--without-urf],
+    [do not try to include URF support]))
+
+if test x$with_urf != xno; then
+  AC_MSG_CHECKING([for URF support])
+  if test -d $srcdir/urf; then
+    AC_MSG_RESULT([yes])
+    ENABLEURF="$(D_)URF_INCLUDED$(_D)"
+    URF_DEVS='urfgray urfrgb urfcmyk'
+    GPDL_URF_TOP_OBJ=$(GPDLOBJ)/$(GPDL_URF_TOP_OBJ_FILE)
+    URF_INCLUDE=$(I_)$(URFSRCDIR)$(_I)
+    URF_DEV=$(GLD)urfd.dev
+    SURFX_H=SURFX_H=$(URFSRCDIR)$(D)surfx.h
+fi
+fi
+
+AC_SUBST(ENABLEURF)
+AC_SUBST(GPDL_URF_TOP_OBJ)
+AC_SUBST(URF_INCLUDE)
+AC_SUBST(URF_DEV)
+AC_SUBST(SURFX_H)
+
 AC_ARG_WITH([cal], AC_HELP_STRING([--without-cal],
     [do not try to use the CAL library for acceleration]))
 
@@ -2282,7 +2309,7 @@ while test -n "$drivers"; do
                 fi
                 ;;
         PRINTERS)
-                P_DEVS0="$P_DEVS0 $CANON_DEVS $EPSON_DEVS $HP_DEVS $LEXMARK_DEVS $BROTHER_DEVS $APPLE_DEVS $IBM_DEVS $OKI_DEVS $JAPAN_DEVS $MISC_PDEVS $ETS_HALFTONING_DEVS"
+                P_DEVS0="$P_DEVS0 $CANON_DEVS $EPSON_DEVS $HP_DEVS $LEXMARK_DEVS $BROTHER_DEVS $APPLE_DEVS $IBM_DEVS $OKI_DEVS $JAPAN_DEVS $MISC_PDEVS $ETS_HALFTONING_DEVS $URF_DEVS"
                 IJS_DEVS0="$IJSDEVS"
                 if test x$ac_cv_lib_dl_dlopen != xno -a x$found_iconv != xno; then
                         P_DEVS0="$P_DEVS0 $OPVP_DEVS"
diff --git a/devices/devs.mak b/devices/devs.mak
index 0183de7..64aaa90 100644
--- a/devices/devs.mak
+++ b/devices/devs.mak
@@ -1947,6 +1947,25 @@ $(DEVOBJ)gdevpdfimg.$(OBJ) : $(DEVSRC)gdevpdfimg.c $(AK) $(gdevkrnlsclass_h) \
   $(slzwx_h) $(szlibx_h) $(jpeglib__h) $(sdct_h) $(srlx_h) $(gsicc_cache_h) $(sjpeg_h)
 	$(DEVCC) $(DEVO_)gdevpdfimg.$(OBJ) $(C_) $(DEVSRC)gdevpdfimg.c
 
+### -------- URF device --------------------- ###
+urf=$(DEVOBJ)gdevurf.$(OBJ)
+$(DD)urfgray.dev : $(urf) $(GLD)page.dev $(GDEV) $(DEVS_MAK) $(MAKEDIRS)
+	$(SETPDEV2) $(DD)urfgray $(urf)
+	$(ADDMOD) $(DD)urfgray -include $(GLD)page
+
+$(DD)urfrgb.dev : $(urf) $(GLD)page.dev $(GDEV) $(DEVS_MAK) $(MAKEDIRS)
+	$(SETPDEV2) $(DD)urfrgb $(urf)
+	$(ADDMOD) $(DD)urfrgb -include $(GLD)page
+
+$(DD)urfcmyk.dev : $(urf) $(GLD)page.dev $(GDEV) $(DEVS_MAK) $(MAKEDIRS)
+	$(SETPDEV2) $(DD)urfcmyk $(urf)
+	$(ADDMOD) $(DD)urfcmyk -include $(GLD)page
+
+$(DEVOBJ)gdevurf.$(OBJ) : $(URFSRCDIR)$(D)gdevurf.c $(AK) $(PDEVH) \
+ $(gsparam_h) $(gdevdcrd_h) $(gscrd_h) $(gscrdp_h) $(gxlum_h) $(gxdcconv_h)\
+ $(gsutil_h) $(DEVS_MAK) $(MAKEDIRS)
+	$(DEVCC) $(DEVO_)gdevurf.$(OBJ) $(C_) $(URFSRCDIR)$(D)gdevurf.c
+
 # Dependencies:
 $(DEVSRC)gxfcopy.h:$(GLSRC)gsfont.h
 $(DEVSRC)gxfcopy.h:$(GLSRC)gsmatrix.h
diff --git a/gpdl/gpdl.mak b/gpdl/gpdl.mak
index 6a1bbc7..bd66b0e 100644
--- a/gpdl/gpdl.mak
+++ b/gpdl/gpdl.mak
@@ -17,6 +17,7 @@ GPDL_MAK=$(GPDLSRCDIR)$(D)gpdl.mak
 
 GPDLSRC=$(GPDLSRCDIR)$(D)
 GPDLPSISRC=$(GPDLSRCDIR)$(D)psi$(D)
+GPDLURFSRC=$(URFSRCDIR)$(D)
 
 GPDLOBJ=$(GPDLOBJDIR)$(D)
 GPDLGEN=$(GPDLGENDIR)$(D)
@@ -26,9 +27,11 @@ GLGEN=$(GLGENDIR)$(D)
 GPDL_PSI_TOP_OBJ_FILE=psitop.$(OBJ)
 GPDL_PSI_TOP_OBJ=$(GPDLOBJ)/$(GPDL_PSI_TOP_OBJ_FILE)
 
-GPDL_PSI_TOP_OBJS=$(GPDL_PSI_TOP_OBJ) $(GPDLOBJ)gpdlimpl.$(OBJ)
+GPDL_URF_TOP_OBJ_FILE=urftop.$(OBJ)
 
-LANG_CFLAGS=$(D_)PCL_INCLUDED$(_D) $(D_)PSI_INCLUDED$(_D) $(D_)XPS_INCLUDED$(_D)
+GPDL_PSI_TOP_OBJS=$(GPDL_URF_TOP_OBJ) $(GPDL_PSI_TOP_OBJ) $(GPDLOBJ)gpdlimpl.$(OBJ)
+
+LANG_CFLAGS=$(D_)PCL_INCLUDED$(_D) $(D_)PSI_INCLUDED$(_D) $(D_)XPS_INCLUDED$(_D) $(ENABLE_URF)
 
 GPDLCC=$(CC_) $(LANG_CFLAGS) $(I_)$(PSSRCDIR)$(_I) $(I_)$(PLSRCDIR)$(_I) $(I_)$(GLSRCDIR)$(_I) $(I_)$(DEVSRCDIR)$(_I) $(I_)$(GLGENDIR)$(_I) $(C_)
 
@@ -50,3 +53,11 @@ $(GPDL_PSI_TOP_OBJ): $(GPDLPSISRC)psitop.c $(AK) $(stdio__h)\
  $(gsnogc_h) $(pltop_h) $(psitop_h) $(plparse_h) $(gsicc_manage_h)\
  $(plfont_h) $(uconfig_h)
 	$(GPDLCC) $(GPDLPSISRC)psitop.c $(GPDLO_)$(GPDL_PSI_TOP_OBJ_FILE)
+
+# Note that we don't use $(GPDL_URF_TOP_OBJ) as the target of the
+# next make rule, as this expands to "" in builds that don't use
+# URF.
+$(GPDLOBJ)/$(GPDL_URF_TOP_OBJ_FILE): $(GPDLURFSRC)urftop.c $(AK)\
+ $(gxdevice_h) $(gserrors_h) $(gsstate_h) $(surfx_h) $(strimpl_h)\
+ $(gscoord_h) $(pltop_h)
+	$(GPDLCC) $(GPDLURFSRC)urftop.c $(GPDLO_)$(GPDL_URF_TOP_OBJ_FILE)
diff --git a/pcl/pl/plimpl.c b/pcl/pl/plimpl.c
index cc758cb..526ff19 100644
--- a/pcl/pl/plimpl.c
+++ b/pcl/pl/plimpl.c
@@ -35,6 +35,8 @@ extern pl_interp_implementation_t svg_implementation;
 
 extern pl_interp_implementation_t ps_implementation;
 
+extern pl_interp_implementation_t urf_implementation;
+
 /* Zero-terminated list of pointers to implementations */
 pl_interp_implementation_t *pdl_implementations[] = {
     &pjl_implementation,
@@ -48,6 +50,9 @@ pl_interp_implementation_t *pdl_implementations[] = {
 #ifdef PSI_INCLUDED
     &ps_implementation,
 #endif
+#ifdef URF_INCLUDED
+    &urf_implementation,
+#endif
     0
 };
 
diff --git a/psi/int.mak b/psi/int.mak
index e6cdbfd..4be5a56 100644
--- a/psi/int.mak
+++ b/psi/int.mak
@@ -358,7 +358,7 @@ $(PSOBJ)zfilter.$(OBJ) : $(PSSRC)zfilter.c $(OP) $(memory__h)\
  $(files_h) $(ialloc_h) $(idict_h) $(idparam_h) $(ifilter_h) $(ilevel_h)\
  $(sfilter_h) $(srlx_h) $(sstring_h) $(stream_h) $(strimpl_h)\
  $(INT_MAK) $(MAKEDIRS)
-	$(PSCC) $(PSO_)zfilter.$(OBJ) $(C_) $(PSSRC)zfilter.c
+	$(PSCC) $(ENABLE_URF) $(URF_INCLUDE) $(PSO_)zfilter.$(OBJ) $(C_) $(PSSRC)zfilter.c
 
 $(PSOBJ)zfproc.$(OBJ) : $(PSSRC)zfproc.c $(GH) $(memory__h) $(stat__h)\
  $(oper_h)\
diff --git a/psi/msvc.mak b/psi/msvc.mak
index 7b097f9..c01e34d 100644
--- a/psi/msvc.mak
+++ b/psi/msvc.mak
@@ -232,6 +232,16 @@ GPDLGENDIR=$(GLGENDIR)
 GPDLOBJDIR=$(GLOBJDIR)
 !endif
 
+!ifndef URFSRCDIR
+URFSRCDIR=.\urf
+!endif
+!ifndef URFGENDIR
+URFGENDIR=$(GLGENDIR)
+!endif
+!ifndef URFOBJDIR
+URFOBJDIR=$(GLOBJDIR)
+!endif
+
 CONTRIBDIR=.\contrib
 
 # Can we build PCL and XPS
@@ -547,6 +557,28 @@ WITH_CUPS=0
 WITH_CUPS=0
 !endif
 
+# Should we build URF...
+!ifdef WITH_URF
+!if "$(WITH_URF)"!="0"
+WITH_URF=1
+!else
+WITH_URF=0
+!endif
+!else
+!if exist ("$(URFSRCDIR)")
+WITH_URF=1
+!else
+WITH_URF=0
+!endif
+!endif
+!if "$(WITH_URF)"=="1"
+ENABLE_URF=$(D_)URF_INCLUDED$(_D)
+GPDL_URF_TOP_OBJ=$(GPDLOBJ)/$(GPDL_URF_TOP_OBJ_FILE)
+URF_INCLUDE=$(I_)$(URFSRCDIR)$(_I)
+URF_DEV=$(GLD)urfd.dev
+SURFX_H=$(URFSRCDIR)$(D)surfx.h
+!endif
+
 # Should we build using CAL....
 CALSRCDIR=cal
 !ifdef WITH_CAL
@@ -1424,7 +1456,7 @@ FEATURE_DEVS=$(GLD)pipe.dev $(GLD)gsnogc.dev $(GLD)htxlib.dev $(GLD)psl3lib.dev
 	     $(GLD)seprlib.dev $(GLD)translib.dev $(GLD)cidlib.dev $(GLD)psf0lib.dev $(GLD)psf1lib.dev\
              $(GLD)psf2lib.dev $(GLD)lzwd.dev $(GLD)sicclib.dev $(GLD)mshandle.dev $(GLD)mspoll.dev \
              $(GLD)ramfs.dev $(GLD)sjpx.dev $(GLD)sjbig2.dev \
-             $(GLD)pwgd.dev $(GLD)siscale.dev
+             $(GLD)pwgd.dev $(GLD)siscale.dev $(URF_DEV)
 
 
 !ifndef METRO
@@ -1487,6 +1519,9 @@ DEVICE_DEVS16=$(DD)bbox.dev $(DD)plib.dev $(DD)plibg.dev $(DD)plibm.dev $(DD)pli
 !if "$(WITH_CUPS)" == "1"
 DEVICE_DEVS16=$(DEVICE_DEVS16) $(DD)cups.dev
 !endif
+!if "$(WITH_URF)" == "1"
+DEVICE_DEVS16=$(DEVICE_DEVS16) $(DD)urfgray.dev $(DD)urfrgb.dev $(DD)urfcmyk.dev
+!endif
 # Overflow for DEVS3,4,5,6,9
 DEVICE_DEVS17=$(DD)ljet3.dev $(DD)ljet3d.dev $(DD)ljet4.dev $(DD)ljet4d.dev
 DEVICE_DEVS18=$(DD)pj.dev $(DD)pjxl.dev $(DD)pjxl300.dev $(DD)jetp3852.dev $(DD)r4081.dev
diff --git a/psi/zfilter.c b/psi/zfilter.c
index dd06c75..dfe3a1d 100644
--- a/psi/zfilter.c
+++ b/psi/zfilter.c
@@ -32,6 +32,10 @@
 #include "ifilter.h"
 #include "files.h"		/* for filter_open, file_d'_buffer_size */
 
+#ifdef URF_INCLUDED
+#include "urffilter.c"
+#endif
+
 /* <source> ASCIIHexEncode/filter <file> */
 /* <source> <dict> ASCIIHexEncode/filter <file> */
 static int
@@ -497,6 +501,9 @@ const op_def zfilter_op_defs[] = {
     {"2RunLengthEncode", zRLE},
     {"1RunLengthDecode", zRLD},
     {"1PWGDecode", zPWGD},
+#ifdef URF_INCLUDED
+    {"1URFDecode", zURFD},
+#endif
     {"3SubFileDecode", zSFD},
     {"1.EOFDecode", zEOFD},
     op_def_end(0)
diff --git a/windows/ghostpdl.vcproj b/windows/ghostpdl.vcproj
index bfddbd2..c6a385a 100644
--- a/windows/ghostpdl.vcproj
+++ b/windows/ghostpdl.vcproj
@@ -310,6 +310,14 @@
 				>
 			</File>
 		</Filter>
+		<Filter
+			Name="urf"
+			>
+			<File
+				RelativePath="..\gpdl\urf\urftop.c"
+				>
+			</File>
+		</Filter>
 	</Files>
 	<Globals>
 	</Globals>
diff --git a/windows/ghostscript.vcproj b/windows/ghostscript.vcproj
index 71ba467..95337bd 100644
--- a/windows/ghostscript.vcproj
+++ b/windows/ghostscript.vcproj
@@ -9628,6 +9628,26 @@
 				>
 			</File>
 		</Filter>
+		<Filter
+			Name="urf"
+			>
+			<File
+				RelativePath="..\urf\gdevurf.c"
+				>
+			</File>
+			<File
+				RelativePath="..\urf\surfd.c"
+				>
+			</File>
+			<File
+				RelativePath="..\urf\surfx.h"
+				>
+			</File>
+			<File
+				RelativePath="..\urf\urffilter.c"
+				>
+			</File>
+		</Filter>
 	</Files>
 	<Globals>
 	</Globals>


Summary of changes:
 Makefile.in                |  16 +-
 base/lib.mak               |  11 +
 base/unix-gcc.mak          |   2 +-
 configure.ac               |  29 +-
 devices/devs.mak           |  19 +
 gpdl/gpdl.mak              |  25 +-
 gpdl/image/imagetop.c      | 855 +++++++++++++++++++++++++++++++++++++++++++++
 pcl/pl/plimpl.c            |  11 +-
 psi/int.mak                |   2 +-
 psi/msvc.mak               |  47 ++-
 psi/zfilter.c              |   7 +
 windows/ghostpdl.vcproj    |  18 +-
 windows/ghostscript.vcproj |  20 ++
 13 files changed, 1053 insertions(+), 9 deletions(-)
 create mode 100644 gpdl/image/imagetop.c