home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / GDEVTFAX.C < prev    next >
C/C++ Source or Header  |  1994-08-01  |  14KB  |  429 lines

  1. /* Copyright (C) 1994 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* gdevtfax.c */
  20. /* Plain-bits or TIFF/F fax device. */
  21. #include "gdevprn.h"
  22. /*#include "gdevtifs.h"*/            /* see TIFF section below */
  23. #include "strimpl.h"
  24. #include "scfx.h"
  25.  
  26. /* Define the device parameters. */
  27. #define X_DPI 204
  28. #define Y_DPI 196
  29. #define LINE_SIZE ((X_DPI * 101 / 10 + 7) / 8)    /* max bytes per line */
  30.  
  31. /* The device descriptors */
  32.  
  33. private dev_proc_print_page(faxg3_print_page);
  34. private dev_proc_print_page(faxg32d_print_page);
  35. private dev_proc_print_page(faxg4_print_page);
  36. private dev_proc_open_device(tifff_prn_open);
  37. private dev_proc_print_page(tiffg3_print_page);
  38. private dev_proc_print_page(tiffg32d_print_page);
  39. private dev_proc_print_page(tiffg4_print_page);
  40.  
  41. struct gx_device_tfax_s {
  42.     gx_device_common;
  43.     gx_prn_device_common;
  44.     long prev_dir;        /* file offset of previous directory offset */
  45.     long dir_off;        /* file offset of next write */
  46.     uint iwidth;        /* width of image data in pixels */
  47. };
  48. typedef struct gx_device_tfax_s gx_device_tfax;
  49.  
  50. gx_device_tfax far_data gs_faxg3_device =
  51. {   prn_device_std_body(gx_device_tfax, prn_std_procs, "faxg3",
  52.     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  53.     X_DPI, Y_DPI,
  54.     0,0,0,0,            /* margins */
  55.     1, faxg3_print_page)
  56. };
  57.  
  58. gx_device_tfax far_data gs_faxg32d_device =
  59. {   prn_device_std_body(gx_device_tfax, prn_std_procs, "faxg32d",
  60.     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  61.     X_DPI, Y_DPI,
  62.     0,0,0,0,            /* margins */
  63.     1, faxg32d_print_page)
  64. };
  65.  
  66. gx_device_tfax far_data gs_faxg4_device =
  67. {   prn_device_std_body(gx_device_tfax, prn_std_procs, "faxg4",
  68.     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  69.     X_DPI, Y_DPI,
  70.     0,0,0,0,            /* margins */
  71.     1, faxg4_print_page)
  72. };
  73.  
  74. private gx_device_procs tifff_procs =
  75.   prn_procs(tifff_prn_open, gdev_prn_output_page, gdev_prn_close);
  76.  
  77. gx_device_tfax far_data gs_tiffg3_device =
  78. {   prn_device_std_body(gx_device_tfax, tifff_procs, "tiffg3",
  79.     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  80.     X_DPI, Y_DPI,
  81.     0,0,0,0,            /* margins */
  82.     1, tiffg3_print_page)
  83. };
  84.  
  85. gx_device_tfax far_data gs_tiffg32d_device =
  86. {   prn_device_std_body(gx_device_tfax, tifff_procs, "tiffg32d",
  87.     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  88.     X_DPI, Y_DPI,
  89.     0,0,0,0,            /* margins */
  90.     1, tiffg32d_print_page)
  91. };
  92.  
  93. gx_device_tfax far_data gs_tiffg4_device =
  94. {   prn_device_std_body(gx_device_tfax, tifff_procs, "tiffg4",
  95.     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  96.     X_DPI, Y_DPI,
  97.     0,0,0,0,            /* margins */
  98.     1, tiffg4_print_page)
  99. };
  100.  
  101. /* Send the page to the printer. */
  102. int
  103. gdev_fax_print_page(gx_device_printer *pdev, FILE *prn_stream,
  104.   stream_CFE_state *ss)
  105. {    gs_memory_t *mem = &gs_memory_default;
  106.     const stream_template _ds *temp = &s_CFE_template;
  107.     int code;
  108.     stream_cursor_read r;
  109.     stream_cursor_write w;
  110.     int in_size = gdev_mem_bytes_per_scan_line((gx_device *)pdev);
  111.     int lnum;
  112.     byte *in;
  113.     byte *out;
  114.     /* If the file is 'nul', don't even do the writes. */
  115.     int nul = !strcmp(pdev->fname, "nul");
  116.  
  117.     /* Initialize the common part of the encoder state. */
  118.     ss->template = temp;
  119.     ss->memory = mem;
  120.     /* Initialize the rest of the encoder state. */
  121.     ss->Uncompressed = false;
  122.     ss->EndOfLine = true;
  123.     ss->Columns = pdev->width;
  124.     ss->Rows = pdev->height;
  125.     ss->BlackIs1 = true;
  126.     ss->FirstBitLowOrder = false;
  127.     /* Now initialize the encoder. */
  128.     code = (*temp->init)((stream_state *)ss);
  129.     if ( code < 0 )
  130.         return code;
  131.  
  132.     /* Allocate the buffers. */
  133.     in = gs_alloc_bytes(mem, temp->min_in_size + in_size + 1, "gdev_fax_print_page(in)");
  134. #define out_size 1000
  135.     out = gs_alloc_bytes(mem, out_size, "gdev_fax_print_page(out)");
  136.     if ( in == 0 || out == 0 )
  137.     {    code = gs_note_error(gs_error_VMerror);
  138.         goto done;
  139.     }
  140.  
  141.     /* Set up the processing loop. */
  142.     lnum = 0;
  143.     r.ptr = r.limit = in - 1;
  144.     w.ptr = out - 1;
  145.     w.limit = w.ptr + out_size;
  146.  
  147.     /* Process the image. */
  148.     for ( ; ; )
  149.     {    int status;
  150.         if_debug7('w', "[w]bitcfe: lnum=%d r=0x%lx,0x%lx,0x%lx w=0x%lx,0x%lx,0x%lx\n", lnum,
  151.             (ulong)in, (ulong)r.ptr, (ulong)r.limit,
  152.             (ulong)out, (ulong)w.ptr, (ulong)w.limit);
  153.         status = (*temp->process)((stream_state *)ss,
  154.                       &r, &w, lnum == pdev->height);
  155.         if_debug7('w', "...%d, r=0x%lx,0x%lx,0x%lx w=0x%lx,0x%lx,0x%lx\n", status,
  156.             (ulong)in, (ulong)r.ptr, (ulong)r.limit,
  157.             (ulong)out, (ulong)w.ptr, (ulong)w.limit);
  158.         switch ( status )
  159.         {
  160.         case 0:            /* need more input data */
  161.         {    uint left;
  162.             if ( lnum == pdev->height )
  163.                 goto ok;
  164.             left = r.limit - r.ptr;
  165.             memcpy(in, r.ptr + 1, left);
  166.             gdev_prn_copy_scan_lines(pdev, lnum++, in + left, in_size);
  167.             r.limit = in + left + in_size - 1;
  168.             r.ptr = in - 1;
  169.         }    break;
  170.         case 1:            /* need to write output */
  171.             if ( !nul )
  172.                 fwrite(out, 1, w.ptr + 1 - out, prn_stream);
  173.             w.ptr = out - 1;
  174.             break;
  175.         }
  176.     }
  177.  
  178. ok:
  179.     /* Write out any remaining output. */
  180.     if ( !nul )
  181.         fwrite(out, 1, w.ptr + 1 - out, prn_stream);
  182.  
  183. done:
  184.     gs_free_object(mem, out, "gdev_fax_print_page(out)");
  185.     gs_free_object(mem, in, "gdev_fax_print_page(in)");
  186.     return code;
  187. }
  188.  
  189. /* Print a 1-D Group 3 page. */
  190. private int
  191. faxg3_print_page(gx_device_printer *pdev, FILE *prn_stream)
  192. {    stream_CFE_state state;
  193.     state.K = 0;
  194.     state.EncodedByteAlign = false;
  195.     state.EndOfBlock = false;
  196.     return gdev_fax_print_page(pdev, prn_stream, &state);
  197. }
  198.  
  199. /* Print a 2-D Group 3 page. */
  200. private int
  201. faxg32d_print_page(gx_device_printer *pdev, FILE *prn_stream)
  202. {    stream_CFE_state state;
  203.     state.K = 5;
  204.     state.EncodedByteAlign = false;
  205.     state.EndOfBlock = false;
  206.     return gdev_fax_print_page(pdev, prn_stream, &state);
  207. }
  208.  
  209. /* Print a Group 4 page. */
  210. private int
  211. faxg4_print_page(gx_device_printer *pdev, FILE *prn_stream)
  212. {    stream_CFE_state state;
  213.     state.K = -1;
  214.     state.EncodedByteAlign = false;
  215.     state.EndOfBlock = false;
  216.     return gdev_fax_print_page(pdev, prn_stream, &state);
  217. }
  218.  
  219. /* ---------------- TIFF/F output ---------------- */
  220.  
  221. #include "gdevtifs.h"
  222.  
  223. /* Define the TIFF directory we use. */
  224. /* NB: this array is sorted by tag number (assumed below) */
  225. typedef struct TIFF_directory_s {
  226.     TIFF_dir_entry    SubFileType;
  227.     TIFF_dir_entry    ImageWidth;
  228.     TIFF_dir_entry    ImageLength;
  229.     TIFF_dir_entry    BitsPerSample;
  230.     TIFF_dir_entry    Compression;
  231.     TIFF_dir_entry    Photometric;
  232.     TIFF_dir_entry    FillOrder;
  233.     TIFF_dir_entry    StripOffsets;
  234.     TIFF_dir_entry    Orientation;
  235.     TIFF_dir_entry    SamplesPerPixel;
  236.     TIFF_dir_entry    RowsPerStrip;
  237.     TIFF_dir_entry    StripByteCounts;
  238.     TIFF_dir_entry    XResolution;
  239.     TIFF_dir_entry    YResolution;
  240.     TIFF_dir_entry    PlanarConfig;
  241.     TIFF_dir_entry    T4T6Options;
  242.     TIFF_dir_entry    ResolutionUnit;
  243.     TIFF_dir_entry    CleanFaxData;
  244.     TIFF_ulong    diroff;            /* offset to next directory */
  245.     TIFF_ulong    xresValue[2];        /* xresolution indirect value */
  246.     TIFF_ulong    yresValue[2];        /* yresolution indirect value */
  247. } TIFF_directory;
  248. private const TIFF_directory dirTemplate = {
  249.     { TIFFTAG_SubFileType,    TIFF_LONG,  1, SubFileType_page },
  250.     { TIFFTAG_ImageWidth,    TIFF_LONG,  1 },
  251.     { TIFFTAG_ImageLength,    TIFF_LONG,  1 },
  252.     { TIFFTAG_BitsPerSample,    TIFF_SHORT, 1, 1 },
  253.     { TIFFTAG_Compression,    TIFF_SHORT, 1, Compression_CCITT_T4 },
  254.     { TIFFTAG_Photometric,    TIFF_SHORT, 1, Photometric_min_is_white },
  255.     { TIFFTAG_FillOrder,    TIFF_SHORT, 1, FillOrder_MSB2LSB },
  256.     { TIFFTAG_StripOffsets,    TIFF_LONG,  1 },
  257.     { TIFFTAG_Orientation,    TIFF_SHORT, 1, Orientation_top_left },
  258.     { TIFFTAG_SamplesPerPixel,    TIFF_SHORT, 1, 1 },
  259.     { TIFFTAG_RowsPerStrip,    TIFF_LONG,  1, -1L },
  260.     { TIFFTAG_StripByteCounts,    TIFF_LONG,  1, 1 },
  261.     { TIFFTAG_XResolution,    TIFF_RATIONAL, 1 },
  262.     { TIFFTAG_YResolution,    TIFF_RATIONAL, 1 },
  263.     { TIFFTAG_PlanarConfig,    TIFF_SHORT, 1, PlanarConfig_contig },
  264.     { TIFFTAG_T4Options,    TIFF_LONG,  1, 0 },
  265.     { TIFFTAG_ResolutionUnit,    TIFF_SHORT, 1, ResolutionUnit_inch },
  266.     { TIFFTAG_CleanFaxData,    TIFF_SHORT, 1, CleanFaxData_clean },
  267.     0, { 0, 1 }, { 0, 1 }
  268. };
  269. #define    NTAGS (offset_of(TIFF_directory, diroff) / sizeof(TIFF_dir_entry))
  270.  
  271. /* Forward references */
  272. private int tiff_begin_page(P3(gx_device_tfax *, FILE *, TIFF_directory *));
  273. private int tiff_end_page(P2(gx_device_tfax *, FILE *));
  274.  
  275. private const gx_device_paper_info
  276.   paper_info_letter = { 1728.0 / X_DPI, 11.0, 1728 },
  277.   paper_info_a4     = { 1728.0 / X_DPI, 11.7, 1728 },
  278.   paper_info_b4     = { 2048.0 / X_DPI, 14.3, 2048 };
  279.  
  280. const gx_device_paper_info *
  281. gdev_fax_paper_size(gx_device *dev)
  282. {    float height_inches = dev->height / dev->y_pixels_per_inch;
  283.     return (height_inches >= 11.8 ? &paper_info_b4 :
  284.         height_inches >= 11.1 ? &paper_info_a4 :
  285.         &paper_info_letter);
  286. }
  287.  
  288. #define tfdev ((gx_device_tfax *)dev)
  289.  
  290. /* Open the device, adjusting the paper size. */
  291. private int
  292. tifff_prn_open(gx_device *dev)
  293. {    const gx_device_paper_info *pi = gdev_fax_paper_size(dev);
  294.     dev->width = (int)(pi->width_inches * dev->x_pixels_per_inch);
  295.     dev->height = (int)(pi->height_inches * dev->y_pixels_per_inch);
  296.     tfdev->iwidth = pi->width;
  297.     return gdev_prn_open(dev);
  298. }
  299.  
  300. /* Print the page. */
  301. private int
  302. tifff_print_page(gx_device_printer *dev, FILE *prn_stream,
  303.   stream_CFE_state *pstate, TIFF_directory *pdir)
  304. {    int code;
  305.     tiff_begin_page(tfdev, prn_stream, pdir);
  306.     code = gdev_fax_print_page(dev, prn_stream, pstate);
  307.     tiff_end_page(tfdev, prn_stream);
  308.     return code;
  309. }
  310. private int
  311. tiffg3_print_page(gx_device_printer *dev, FILE *prn_stream)
  312. {    stream_CFE_state state;
  313.     TIFF_directory dir;
  314.     state.K = 0;
  315.     state.EncodedByteAlign = true;
  316.     state.EndOfBlock = true;
  317.     dir = dirTemplate;
  318.     dir.Compression.value = Compression_CCITT_T4;
  319.     dir.T4T6Options.tag = TIFFTAG_T4Options;
  320.     dir.T4T6Options.value = T4Options_fill_bits;
  321.     return tifff_print_page(dev, prn_stream, &state, &dir);
  322. }
  323. private int
  324. tiffg32d_print_page(gx_device_printer *dev, FILE *prn_stream)
  325. {    stream_CFE_state state;
  326.     TIFF_directory dir;
  327.     state.K = (dev->y_pixels_per_inch < 100 ? 3 : 5);
  328.     state.EncodedByteAlign = true;
  329.     state.EndOfBlock = true;
  330.     dir = dirTemplate;
  331.     dir.Compression.value = Compression_CCITT_T4;
  332.     dir.T4T6Options.tag = TIFFTAG_T4Options;
  333.     dir.T4T6Options.value = T4Options_2D_encoding | T4Options_fill_bits;
  334.     return tifff_print_page(dev, prn_stream, &state, &dir);
  335. }
  336. private int
  337. tiffg4_print_page(gx_device_printer *dev, FILE *prn_stream)
  338. {    stream_CFE_state state;
  339.     TIFF_directory dir;
  340.     state.K = -1;
  341.     state.EncodedByteAlign = false;  /* no fill_bits option for T6 */
  342.     state.EndOfBlock = true;
  343.     dir = dirTemplate;
  344.     dir.Compression.value = Compression_CCITT_T6;
  345.     dir.T4T6Options.tag = TIFFTAG_T6Options;
  346.     return tifff_print_page(dev, prn_stream, &state, &dir);
  347. }
  348.  
  349. #undef tfdev
  350.  
  351. /* Fix up tag values on big-endian machines if necessary. */
  352. private void
  353. tiff_fixuptags(TIFF_dir_entry *dp, int n)
  354. {
  355. #if arch_is_big_endian
  356.     for ( ; n-- > 0; dp++ )
  357.       switch ( dp->type )
  358.         {
  359.         case TIFF_SHORT: case TIFF_SSHORT:
  360.           dp->value <<= 16; break;
  361.         case TIFF_BYTE: case TIFF_SBYTE:
  362.           dp->value <<= 24; break;
  363.         }
  364. #endif
  365. }
  366.  
  367. /* Begin a TIFF/F page. */
  368. private int
  369. tiff_begin_page(gx_device_tfax *tfdev, FILE *fp, TIFF_directory *pdir)
  370. {    if (gdev_prn_file_is_new((gx_device_printer *)tfdev))
  371.       {    /* This is a new file; write the TIFF header. */
  372.         static const TIFF_header hdr =
  373.           {
  374. #if arch_is_big_endian
  375.             TIFF_magic_big_endian,
  376. #else
  377.             TIFF_magic_little_endian,
  378. #endif
  379.             TIFF_version_value,
  380.             sizeof(TIFF_header)
  381.           };
  382.         fwrite((char *)&hdr, sizeof(hdr), 1, fp);
  383.         tfdev->prev_dir = 0;
  384.       }
  385.     else
  386.       {    /* Patch pointer to this directory from previous. */
  387.         fseek(fp, tfdev->prev_dir, SEEK_SET);
  388.         fwrite((char *)&tfdev->dir_off, sizeof(tfdev->dir_off), 1, fp);
  389.         fseek(fp, tfdev->dir_off, SEEK_SET);
  390.       }
  391.  
  392.     /* Write count of tags in directory. */
  393.     {    short dircount = NTAGS;
  394.         fwrite((char *)&dircount, sizeof(dircount), 1, fp);
  395.     }
  396.     tfdev->dir_off = ftell(fp);
  397.  
  398.     /* Fill in directory tags and write the directory. */
  399.     pdir->ImageWidth.value = gdev_fax_paper_size((gx_device *)tfdev)->width;
  400.     pdir->ImageLength.value = tfdev->height;
  401.     pdir->StripOffsets.value = tfdev->dir_off + sizeof(TIFF_directory);
  402.     pdir->XResolution.value =
  403.       tfdev->dir_off + offset_of(TIFF_directory, xresValue[0]);
  404.     pdir->YResolution.value =
  405.       tfdev->dir_off + offset_of(TIFF_directory, yresValue[0]);
  406.     pdir->xresValue[0] = tfdev->x_pixels_per_inch;
  407.     pdir->yresValue[0] = tfdev->y_pixels_per_inch;
  408.     tiff_fixuptags(&pdir->SubFileType, NTAGS);
  409.     fwrite((char *)pdir, sizeof(*pdir), 1, fp);
  410.  
  411.     /*puteol(tfdev);*/    /****** USES EndOfBlock ****** WRONG ******/
  412.     return 0;
  413. }
  414.  
  415. /* End a TIFF/F page. */
  416. private int
  417. tiff_end_page(gx_device_tfax *tfdev, FILE *fp)
  418. {    long dir_off, cc;
  419.  
  420.     dir_off = tfdev->dir_off;
  421.     tfdev->prev_dir = tfdev->dir_off + offset_of(TIFF_directory, diroff);
  422.     tfdev->dir_off = ftell(fp);
  423.     /* Patch strip byte counts value. */
  424.     cc = tfdev->dir_off - (dir_off + sizeof(TIFF_directory));
  425.     fseek(fp, dir_off + offset_of(TIFF_directory, StripByteCounts.value), SEEK_SET);
  426.     fwrite(&cc, sizeof(cc), 1, fp);
  427.     return 0;
  428. }
  429.