%% options copyright owner = Dirk Krause copyright year = 2017-xxxx SPDX-License-Identifier: BSD-3-Clause %% module #include "dk4conf.h" #if DK4_HAVE_MATH_H #ifndef MATH_H_INCLUDED #if DK4_ON_WINDOWS #ifndef _USE_MATH_DEFINES #define _USE_MATH_DEFINES 1 #endif #endif #include #define MATH_H_INCLUDED 1 #endif #endif #ifndef DK4MEM_H_INCLUDED #include "dk4mem.h" #endif #ifndef DK4STRMCO_H_INCLUDED #include "dk4strmco.h" #endif #ifndef DK4GRA_H_INCLUDED #include "dk4gra.h" #endif #ifndef DK4MAO8D_H_INCLUDED #include "dk4mao8d.h" #endif #ifndef DK4MAO8DNS_H_INCLUDED #include "dk4mao8dns.h" #endif #ifndef DK4BIF_H_INCLUDED #include "dk4bif.h" #endif #ifndef DK4BIFA_H_INCLUDED #include "dk4bifa.h" #endif #ifndef DK4ISADM_H_INCLUDED #include "dk4isadm.h" #endif #ifndef DK4FOPT_H_INCLUDED #include "dk4fopt.h" #endif #ifndef DK4FOPD_H_INCLUDED #include "dk4fopd.h" #endif #ifndef DK4BITSH_H_INCLUDED #include "dk4bitsh.h" #endif #ifndef DK4EDSTM_H_INCLUDED #include "dk4edstm.h" #endif #ifndef GRA_H_INCLUDED #include "gra.h" #endif #ifndef BIF_H_INCLUDED #include "bif.h" #endif #if DK4_HAVE_ASSERT_H #ifndef ASSERT_H_INCLUDED #include #define ASSERT_H_INCLUDED 1 #endif #endif $!trace-include /* FOR DEBUGGING ONLY! The FORCE_... constants are for debugging purposes only. They must be disabled in release versions. */ #ifdef FORCE_COE #undef FORCE_COE #endif /** Set to 1 to force a specific compression/encoding combination. Note: You can not use DK4_GRA_COE_DCT_ASCII85 */ #define FORCE_COE 0 #ifndef FORCED_COE /** Compression/encoding combination to enforce. */ #define FORCED_COE DK4_GRA_COE_FLATE_ASCII85 #endif #ifdef FORCE_CS #undef FORCE_CS #endif /** Set to 1 to force a specific color space. Note: You can only use DK4_CS_GRAY, DK4_CS_RGB and DK4_CS_CMYK. */ #define FORCE_CS 0 #ifndef FORCED_CS /** Color space to enforce. */ #define FORCED_CS DK4_CS_CMYK #endif #ifdef FORCE_BPC #undef FORCE_BPC #endif /** Set to 1 to force a specific bits per component number. Note: The number must be one from 1, 2, 4, 8, 12. */ #define FORCE_BPC 0 #ifndef FORCED_BPC /** Bits per component number to enforce. */ #define FORCED_BPC 8 #endif /** Constant text fragments used in this module. */ static const char * const dk4gra_eps_c8_kw[] = { $!string-table # # 0 # \n # # 1 # {\n # # 2 # } exec\n # # 3 4 5 # gsave\n grestore\n showpage\n # # 6 space # # # 7 translate # translate\n # # 8 rotate # rotate\n # # 9 scale # scale\n # # 10 11 15 dict begin end # 15 dict begin\n end\n # # 12 13 14 color spaces # /DeviceRGB setcolorspace\n /DeviceGray setcolorspace\n /DeviceCMYK setcolorspace\n # # 15...19 decoding filters # /fa currentfile /ASCII85Decode filter def\n /fb fa /FlateDecode filter def\n /fb fa /LZWDecode filter def\n /fb fa /RunLengthDecode filter def\n /fb fa << >> /DCTDecode filter def\n # # 20 21 close files. # fb closefile\n fa flushfile fa closefile\n # # 22 23 dictionary borders # <<\n >>\n # # 24 # /ImageType 1\n # # 25 26 # /Width /Height # # 27 # /BitsPerComponent # # 28...31 # /ImageMatrix [ 0 0 - 0 ]\n # # 32 33 # /MultipleDataSources false\n /MultipleDataSources true\n # # 34...39 # /DataSource [\n { fb sr readstring pop }\n { fb sg readstring pop }\n { fb sb readstring pop }\n { fb sk readstring pop }\n ]\n # # 40...44 # /sr /sg /sb /sk string def\n # # 45 46 # /DataSource fb\n /DataSource fa\n # # 47...52 # /Decode [0 1]\n /Decode [1 0]\n /Decode [0 1 0 1 0 1]\n /Decode [1 0 1 0 1 0]\n /Decode [0 1 0 1 0 1 0 1]\n /Decode [1 0 1 0 1 0 1 0]\n # # 53 # image\n # # 54 # /Interpolate true\n # # # $!end }; /** Write keyword to stream, reset a result variable on error. @param os Output stream to write to. @param kwi Index of keyword in dk4gra_eps_c8_kw array. @param backptr Address of success variable to reset on error. @param erp Error report, may be NULL. */ static void dk4gra_eps_put_kw( dk4_stream_t *os, size_t kwi, int *backptr, dk4_er_t *erp ) { if (0 == dk4stream_write_char_string(os, dk4gra_eps_c8_kw[kwi], erp)) { if (NULL != backptr) { *backptr = 0; } } } /** Put double value to stream, no scientific notation. On error reset a success variable. @param os Output stream. @param val Value to write. @param backptr Address of success variable to reset on error. @param erp Error report, may be NULL. */ static void dk4gra_eps_put_dbl( dk4_stream_t *os, double val, int *backptr, dk4_er_t *erp ) { if ( 0 == dk4ma_write_c8_double_no_sci_to_stream( os, val, DK4_GRA_EPSILON_COORDINATES, 0, 1, erp ) ) { if (NULL != backptr) { *backptr = 0; } } } /** Write unsigned integer number to stream, reset a success variable on error. @param os Output stream to write to. @param val Value to write. @param backptr Address of success variable to reset on error. @param erp Error report, may be NULL. */ static void dk4gra_eps_put_uim( dk4_stream_t *os, dk4_um_t val, int *backptr, dk4_er_t *erp ) { char buf[16*sizeof(dk4_um_t)]; /* Buffer to construct string */ if (0 != dk4ma_write_c8_decimal_unsigned(buf, sizeof(buf), val, 0, erp)) { if (0 == dk4stream_write_char_string(os, buf, erp)) { if (NULL != backptr) { *backptr = 0; } } } else { if (NULL != backptr) { *backptr = 0; } } } /** Write procedure before writing data. @param gra Graphics output structure. @param os Output stream for current page. @param cotra Transformation (xshift, yshift, rotation, xscale, yscale). @param bif Bitmap image. @param cs Color space corrected to supported color space. @param bpc Bits per component. @param deci Flag: Decoding inverted. @param ipol Flag: Interpolation. @param spip Flag: Write showpage operator in procedure. @param coe Compression and encoding: - 0: DCT + ASCII85, - 1: Flate + ASCII85, - 2: LZW + ASCII85, - 3: Run-length + ASCII85, - 4: Pure ASCII85. @param erp Error report, may be NULL. @return 1 on success, 0 on error. */ static int dk4gra_eps_write_procedure( dk4_gra_t *gra, dk4_stream_t *os, double *cotra, dk4_bif_t const *bif, int cs, dk4_px_bit_depth_t bpc, int deci, int ipol, int spip, int coe, dk4_er_t *erp ) { size_t w = 0; /* Image width */ size_t h = 0; /* Image height */ dk4_um_t bn = 0; /* Byte number for run-length compression */ int back = 1; /* Function result */ w = dk4bif_get_width(bif); h = dk4bif_get_height(bif); bn = (w * bpc) / 8; if (0 != ((w * bpc) % 8)) { bn++; } /* { */ dk4gra_eps_put_kw(os, 1, &back, erp); /* gsave */ dk4gra_eps_put_kw(os, 3, &back, erp); /* ... ... translate */ if ( (DK4_GRA_EPSILON_COORDINATES < fabs(cotra[0])) || (DK4_GRA_EPSILON_COORDINATES < fabs(cotra[1])) ) { dk4gra_eps_put_dbl(os, cotra[0], &back, erp); dk4gra_eps_put_kw(os, 6, &back, erp); dk4gra_eps_put_dbl(os, cotra[1], &back, erp); dk4gra_eps_put_kw(os, 7, &back, erp); } /* ... rotate */ if (DK4_GRA_EPSILON_COORDINATES < fabs(cotra[2])) { dk4gra_eps_put_dbl(os, cotra[2], &back, erp); dk4gra_eps_put_kw(os, 8, &back, erp); } /* ... ... scale */ dk4gra_eps_put_dbl(os, cotra[3], &back, erp); dk4gra_eps_put_kw(os, 6, &back, erp); dk4gra_eps_put_dbl(os, cotra[4], &back, erp); dk4gra_eps_put_kw(os, 9, &back, erp); /* 13 dict begin */ dk4gra_eps_put_kw(os, 10, &back, erp); /* ... setcolorspace */ switch (cs) { case DK4_CS_GRAY : { dk4gra_eps_put_kw(os, 13, &back, erp); } break; case DK4_CS_CMYK : { dk4gra_eps_put_kw(os, 14, &back, erp); } break; default : { dk4gra_eps_put_kw(os, 12, &back, erp); } break; } /* ... filter def ... */ switch (coe) { case DK4_GRA_COE_ASCII85 : { /* ASCII85 */ dk4gra_eps_put_kw(os, 15, &back, erp); } break; case DK4_GRA_COE_RUNLENGTH_ASCII85 : { /* Run-length ASCII85 */ dk4gra_eps_put_kw(os, 15, &back, erp); dk4gra_eps_put_kw(os, 18, &back, erp); } break; case DK4_GRA_COE_LZW_ASCII85 : { /* LZW ASCII85 */ dk4gra_eps_put_kw(os, 15, &back, erp); dk4gra_eps_put_kw(os, 17, &back, erp); } break; case DK4_GRA_COE_FLATE_ASCII85 : { /* Flate ASCII85 */ dk4gra_eps_put_kw(os, 15, &back, erp); dk4gra_eps_put_kw(os, 16, &back, erp); } break; default: { /* DCT ASCII85 */ dk4gra_eps_put_kw(os, 15, &back, erp); dk4gra_eps_put_kw(os, 19, &back, erp); } break; } if (DK4_GRA_COE_RUNLENGTH_ASCII85 == coe) { if (DK4_CS_GRAY != cs) { switch (cs) { case DK4_CS_CMYK : { dk4gra_eps_put_kw(os, 40, &back, erp); dk4gra_eps_put_uim(os, bn, &back, erp); dk4gra_eps_put_kw(os, 44, &back, erp); dk4gra_eps_put_kw(os, 41, &back, erp); dk4gra_eps_put_uim(os, bn, &back, erp); dk4gra_eps_put_kw(os, 44, &back, erp); dk4gra_eps_put_kw(os, 42, &back, erp); dk4gra_eps_put_uim(os, bn, &back, erp); dk4gra_eps_put_kw(os, 44, &back, erp); dk4gra_eps_put_kw(os, 43, &back, erp); dk4gra_eps_put_uim(os, bn, &back, erp); dk4gra_eps_put_kw(os, 44, &back, erp); } break; default : { dk4gra_eps_put_kw(os, 40, &back, erp); dk4gra_eps_put_uim(os, bn, &back, erp); dk4gra_eps_put_kw(os, 44, &back, erp); dk4gra_eps_put_kw(os, 41, &back, erp); dk4gra_eps_put_uim(os, bn, &back, erp); dk4gra_eps_put_kw(os, 44, &back, erp); dk4gra_eps_put_kw(os, 42, &back, erp); dk4gra_eps_put_uim(os, bn, &back, erp); dk4gra_eps_put_kw(os, 44, &back, erp); } break; } } } /* << */ dk4gra_eps_put_kw(os, 22, &back, erp); /* /ImageType 1 */ dk4gra_eps_put_kw(os, 24, &back, erp); /* /Width ... */ dk4gra_eps_put_kw(os, 25, &back, erp); dk4gra_eps_put_uim(os, w, &back, erp); dk4gra_eps_put_kw(os, 0, &back, erp); /* /Height ... */ dk4gra_eps_put_kw(os, 26, &back, erp); dk4gra_eps_put_uim(os, h, &back, erp); dk4gra_eps_put_kw(os, 0, &back, erp); /* ImageMatrix */ dk4gra_eps_put_kw(os, 28, &back, erp); dk4gra_eps_put_uim(os, w, &back, erp); dk4gra_eps_put_kw(os, 29, &back, erp); dk4gra_eps_put_uim(os, h, &back, erp); dk4gra_eps_put_kw(os, 30, &back, erp); dk4gra_eps_put_uim(os, h, &back, erp); dk4gra_eps_put_kw(os, 31, &back, erp); /* BitsPerComponent */ dk4gra_eps_put_kw(os, 27, &back, erp); dk4gra_eps_put_uim(os, bpc, &back, erp); dk4gra_eps_put_kw(os, 0, &back, erp); /* MultipleDataSources */ if (DK4_GRA_COE_RUNLENGTH_ASCII85 == coe) { if (DK4_CS_GRAY == cs) { dk4gra_eps_put_kw(os, 32, &back, erp); } else { dk4gra_eps_put_kw(os, 33, &back, erp); } } else { dk4gra_eps_put_kw(os, 32, &back, erp); } /* DataSource */ if (DK4_GRA_COE_RUNLENGTH_ASCII85 == coe) { if (DK4_CS_GRAY == cs) { dk4gra_eps_put_kw(os, 45, &back, erp); } else { dk4gra_eps_put_kw(os, 34, &back, erp); dk4gra_eps_put_kw(os, 35, &back, erp); dk4gra_eps_put_kw(os, 36, &back, erp); dk4gra_eps_put_kw(os, 37, &back, erp); if (DK4_CS_CMYK == cs) { dk4gra_eps_put_kw(os, 38, &back, erp); } dk4gra_eps_put_kw(os, 39, &back, erp); } } else { if (DK4_GRA_COE_ASCII85 == coe) { dk4gra_eps_put_kw(os, 46, &back, erp); } else { dk4gra_eps_put_kw(os, 45, &back, erp); } } /* Decode */ switch (cs) { case DK4_CS_GRAY : { dk4gra_eps_put_kw(os, ((0 != deci) ? (48) : (47)), &back, erp); } break; case DK4_CS_CMYK : { dk4gra_eps_put_kw(os, ((0 != deci) ? (52) : (51)), &back, erp); } break; default : { dk4gra_eps_put_kw(os, ((0 != deci) ? (50) : (49)), &back, erp); } break; } /* Interpolate */ if (0 != ipol) { dk4gra_eps_put_kw(os, 54, &back, erp); } /* >> */ dk4gra_eps_put_kw(os, 23, &back, erp); /* image */ dk4gra_eps_put_kw(os, 53, &back, erp); /* ... closefile ... */ if (DK4_GRA_COE_ASCII85 != coe) { dk4gra_eps_put_kw(os, 20, &back, erp); } dk4gra_eps_put_kw(os, 21, &back, erp); /* end */ dk4gra_eps_put_kw(os, 11, &back, erp); /* grestore */ dk4gra_eps_put_kw(os, 4, &back, erp); /* showpage */ if ((DK4_GRA_DRIVER_EPS != gra->dr) && (0 != spip)) { dk4gra_eps_put_kw(os, 5, &back, erp); } /* } exec */ dk4gra_eps_put_kw(os, 2, &back, erp); return back; } /** Re-use DCT encoded data from JPEG file directly. @param gra Graphics output structure. @param bptr Current page. @param cotra Coordinates transformation values. @param bif Bitmap image. @param cs Color space correted to supported color space @param fn Bitmap image file name. @param ipol Flag: Interpolation. @param spip Flag: Use showpage in procedure. @param ijpg Flag: Interpolation for JPEGs too. @param erp Error report, may be NULL. */ static int dk4gra_eps_jpg_bif_fig_image( dk4_gra_t *gra, dk4_gra_ps_page_t *bptr, double *cotra, dk4_bif_t const *bif, int cs, const dkChar *fn, int ipol, int spip, int ijpg, dk4_er_t *erp ) { char b1[256]; /* Default buffer */ FILE *infile = NULL; /* Input file */ dk4_stream_t *ostrm = NULL; /* Output stream, ASCII85 encoding */ char *bufptr = b1; /* Buffer to use */ char *mybuf = NULL; /* Allocated buffer */ dk4_bif_jpeg_per_frame_t *pjpgf; size_t szbuf = sizeof(b1); /* Size of buffer to use */ size_t rdb = 0; /* Number of bytes read in pass */ int back = 0; /* Function result */ int deci = 0; /* Flag: Invert Decode values */ int fotest = DK4_FOPEN_SC_USER; /* Test for files */ dk4_px_bit_depth_t bpc = 8; /* Number of bits per component */ $? "+ dk4gra_eps_jpg_bif_fig_image" /* */ if (0 != dk4isadmin()) { fotest = DK4_FOPEN_SC_PRIVILEGED; } infile = dk4fopen(fn, dkT("rb"), fotest, erp); if (NULL == infile) { goto finished; } /* Allocate memory for buffer */ mybuf = (char *)malloc(4096); if (NULL != mybuf) { bufptr = mybuf; szbuf = 4096; } /* Open output encoding stream for ASCII85 */ ostrm = dk4strmco_open( bptr->memstrm, DK4_STRMCO_PP_NONE, DK4_STRMCO_CO_NONE, DK4_STRMCO_OE_ASCII85, (DK4_STRMCO_FL_EOD_OE | DK4_STRMCO_FL_NL_OE), erp ); if (NULL == ostrm) { goto finished; } pjpgf = (dk4_bif_jpeg_per_frame_t *)(bif->cf->tsdata); if (NULL != pjpgf) { if ((DK4_CS_CMYK == cs) && (0 != pjpgf->is_adobe)) { deci = 1; } } /* Ressources allocated successfully */ bpc = dk4bif_get_original_bit_depth(bif); back = dk4gra_eps_write_procedure( gra, bptr->memstrm, cotra, bif, cs, bpc, deci, (((0 != ipol) && (0 != ijpg)) ? (1) : (0)), spip, 0, erp ); /* Copy file contents */ do { rdb = fread(bufptr, 1, szbuf, infile); if (0 < rdb) { if (0 == dk4stream_write(ostrm, bufptr, rdb, erp)) { back = 0; } } } while (0 < rdb); /* Clean up and return */ finished: if (NULL != ostrm) { if (0 == dk4stream_close(ostrm, erp)) { back = 0; } } if (NULL != mybuf) { dk4mem_free(mybuf); } if (NULL != infile) { fclose(infile); } $? "- dk4gra_eps_jpg_bif_fig_image %d", back return back; } int dk4gra_eps_bif_fig_image( dk4_gra_t *gra, double *cotra, dk4_bif_t *bif, const dkChar *fn, int ifl, dk4_er_t *erp ) { dk4_px_t pix[8]; /* Pixel components */ dk4_bit_shift_t bs; /* Bit shifter */ dk4_gra_ps_page_t *bptr = NULL; /* Current page */ dk4_stream_t *costrm = NULL; /* Compressed+encoded output stream */ dk4_um_t nby = 0; /* Number of bytes per line */ size_t szpix = sizeof(pix) / sizeof(dk4_px_t); size_t h = 0; /* Image height */ size_t w = 0; /* Image width */ size_t x = 0; /* Current column position */ size_t y = 0; /* Current row position */ size_t nco = 3; /* Number of components */ size_t i = 0; /* Traverse pixel components */ int back = 0; /* Function result */ int cs = DK4_CS_RGB; /* Color space */ int coe = DK4_GRA_COE_FLATE_ASCII85; dk4_px_bit_depth_t bpc = 8; /* Number of bits per component */ $? "+ dk4gra_eps_bif_fig_image" #if DK4_USE_ASSERT assert(NULL != gra); assert(NULL != cotra); assert(NULL != bif); assert(NULL != fn); #endif /* Check arguments */ bptr = (dk4_gra_ps_page_t *)(gra->curpg); if (NULL == bptr) { dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); goto finished; } if ((NULL == bptr->memstrm) || (0 != bptr->spip)) { dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); goto finished; } if (0 != (DK4_GRA_IMG_FLAG_ONLY_PAGE_CONTENTS & ifl)) { if (0 != bptr->cont) { dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); goto finished; } bptr->spip = 1; } /* File name is required to re-use DCT encoded data directly */ if (NULL == fn) { ifl &= (~(DK4_GRA_IMG_FLAG_DCT)); } /* Retrieve color space */ if (0 == (DK4_GRA_IMG_FLAG_COLOR & ifl)) { if (DK4_CS_GRAY != dk4bif_get_color_space(bif)) { if (DK4_CS_GRAY_ALPHA != dk4bif_get_color_space(bif)) { ifl &= (~(DK4_GRA_IMG_FLAG_DCT)); } } } cs = dk4bif_get_color_space(bif); #if TRACE_DEBUG $? ". cs as received from bif" switch (cs) { case DK4_CS_CMYK : { $? ". cs = cmyk" } break; case DK4_CS_RGB_ALPHA : { $? ". cs = rgba" } break; case DK4_CS_RGB : { $? ". cs = rgb" } break; case DK4_CS_GRAY_ALPHA : { $? ". cs = ga" } break; case DK4_CS_GRAY : { $? ". cs = g" } break; } #endif /* Re-use DCT encoded data from JPEG directly, if possible */ $? ". ajpg = %d", (DK4_GRA_IMG_FLAG_DCT & ifl) if ((DK4_CS_GRAY == cs) || (DK4_CS_RGB == cs) || (DK4_CS_CMYK == cs)) { if ( (DK4_BIF_TYPE_JPEG == bif->imgtype) && (0 != (DK4_GRA_IMG_FLAG_DCT & ifl)) ) { back = dk4gra_eps_jpg_bif_fig_image( gra, bptr, cotra, bif, cs, fn, ((0 != (DK4_GRA_IMG_FLAG_INTERPOLATION & ifl)) ? (1) :(0)), ((0 != (DK4_GRA_IMG_FLAG_ONLY_PAGE_CONTENTS & ifl)) ? 1 : 0), ((0 != (DK4_GRA_IMG_FLAG_DCT_INTERPOLATION & ifl)) ? (1) : (0)), erp ); $? ". back = %d after writing dct data", back goto finished; } } $? ". cs correction 1" /* Correct color space to supported color space */ if (DK4_CS_GRAY_ALPHA == cs) { $? ". change cs from GA to G" cs = DK4_CS_GRAY; $? ". cs = g" } if (DK4_CS_RGB_ALPHA == cs) { $? ". change cs from RGBA to RGB" cs = DK4_CS_RGB; $? ". cs = rgb" } /* Skip processing for unsupported color spaces */ if (DK4_CS_GRAY != cs) { if (DK4_CS_RGB != cs) { if (DK4_CS_CMYK != cs) { dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); goto finished; } } } w = dk4bif_get_width(bif); h = dk4bif_get_height(bif); bpc = dk4bif_get_original_bit_depth(bif); nby = ((dk4_um_t)w * (dk4_um_t)bpc) / (dk4_um_t)8UL; $? ". cs=%d bpc=%d", cs, (int)bpc if ( (0 != (DK4_GRA_IMG_FLAG_ANALYZE_BPC & ifl)) || (0 != (DK4_GRA_IMG_FLAG_ANALYZE_COLOR & ifl)) ) { (void)dk4bif_analyze( &cs, &bpc, bif, ((0 != (DK4_GRA_IMG_FLAG_ANALYZE_BPC & ifl)) ? (1) : (0)), ((0 != (DK4_GRA_IMG_FLAG_ANALYZE_COLOR & ifl)) ? (1) : (0)), 0, erp ); } $? ". cs=%d bpc=%d", cs, (int)bpc $? ". cs correction 2" /* Correct color space to supported color space */ if (DK4_CS_GRAY_ALPHA == cs) { $? ". change cs from GA to G" cs = DK4_CS_GRAY; $? ". cs = g" } if (DK4_CS_RGB_ALPHA == cs) { $? ". change cs from RGBA to RGB" cs = DK4_CS_RGB; $? ". cs = rgb" } $? ". cs=%d bpc=%d", cs, (int)bpc $? ". cs correction 3" if (0 == (DK4_GRA_IMG_FLAG_COLOR & ifl)) { switch (cs) { case DK4_CS_RGB : case DK4_CS_CMYK : { cs = DK4_CS_GRAY; } break; } } $? ". cs=%d bpc=%d", cs, (int)bpc /* Skip processing for unsupported color spaces */ if (DK4_CS_GRAY != cs) { if (DK4_CS_RGB != cs) { if (DK4_CS_CMYK != cs) { dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); goto finished; } } } if (12 < bpc) { $? ". correct bits per component to 12" bpc = 12; } $? ". cs=%d bpc=%d", cs, (int)bpc if ((dk4_um_t)0UL != (((dk4_um_t)w * (dk4_um_t)bpc) % (dk4_um_t)8UL)) { nby++; } #if FORCE_CS $? "! warning: Use forced cs (debugging feature)" cs = FORCED_CS; #endif #if FORCE_BPC $? "! warning: Use forced bpc (debugging feature)" bpc = FORCED_BPC; #endif nco = 3; switch (cs) { case DK4_CS_GRAY : { nco = 1; }; break; case DK4_CS_CMYK : { nco = 4; }; break; } if (3 != gra->out.ps.llev) { if (0 != (DK4_GRA_IMG_FLAG_LZW & ifl)) { coe = DK4_GRA_COE_LZW_ASCII85; } else { coe = DK4_GRA_COE_ASCII85; /* or 3 for run-length */ if ((DK4_CS_GRAY == cs) || ((dk4_um_t)16384UL > nby)) { coe = DK4_GRA_COE_RUNLENGTH_ASCII85; } } } #if FORCE_COE $? "! warning: Use forced coe (debugging feature)" coe = FORCED_COE; #endif switch (coe) { case DK4_GRA_COE_DCT_ASCII85 : { /* DCT ASCII85 */ $? "! bug" dk4error_set_simple_error_code(erp, DK4_E_BUG); } break; case DK4_GRA_COE_LZW_ASCII85 : { /* LZW ASCII85 */ costrm = dk4strmco_open( bptr->memstrm, DK4_STRMCO_PP_NONE, DK4_STRMCO_CO_LZW, DK4_STRMCO_OE_ASCII85, (DK4_STRMCO_FL_EOD_OE | DK4_STRMCO_FL_NL_OE), erp ); } break; case DK4_GRA_COE_RUNLENGTH_ASCII85 : { /* Run-length ASCII85 */ costrm = dk4strmco_open( bptr->memstrm, DK4_STRMCO_PP_RUNLENGTH, DK4_STRMCO_CO_NONE, DK4_STRMCO_OE_ASCII85, ( DK4_STRMCO_FL_EOD_OE | DK4_STRMCO_FL_EOD_PP | DK4_STRMCO_FL_NL_OE ), erp ); } break; case DK4_GRA_COE_ASCII85 : { /* Pure ASCII85 */ costrm = dk4strmco_open( bptr->memstrm, DK4_STRMCO_PP_NONE, DK4_STRMCO_CO_NONE, DK4_STRMCO_OE_ASCII85, (DK4_STRMCO_FL_EOD_OE | DK4_STRMCO_FL_NL_OE), erp ); } break; default: { /* Flate ASCII85 */ costrm = dk4strmco_open( bptr->memstrm, DK4_STRMCO_PP_NONE, DK4_STRMCO_CO_FLATE, DK4_STRMCO_OE_ASCII85, (DK4_STRMCO_FL_EOD_OE | DK4_STRMCO_FL_NL_OE), erp ); } break; } if (NULL == costrm) { goto finished; } back = dk4gra_eps_write_procedure( gra, bptr->memstrm, cotra, bif, cs, bpc, 0, ((0 != (DK4_GRA_IMG_FLAG_INTERPOLATION & ifl)) ? (1) : (0)), ((0 != (DK4_GRA_IMG_FLAG_ONLY_PAGE_CONTENTS & ifl)) ? (1) : (0)), coe, erp ); $? ". back = %d after writing procedure", back dk4bif_set_output_bit_depth(bif, bpc); if (8 == bpc) { $? ". bpc == 8" if ((3 == coe) && (DK4_CS_GRAY != cs)) { for (y = 0; y < h; y++) { for (i = 0; i < nco; i++) { for (x = 0; x < w; x++) { if ( 0 == dk4bif_get_pixel( pix, szpix, bif, (dk4_bif_dim_t)y, (dk4_bif_dim_t)x, cs ) ) { back = 0; $? "! failed to get pixel" } $? ". pixel value = %lu", (unsigned long)(pix[i]) if (0 == dk4stream_write_byte(costrm, (char)(pix[i]), erp)) { back = 0; $? "! failed to write co stream" } } } } } else { for (y = 0; y < h; y++) { for (x = 0; x < w; x++) { if ( 0 == dk4bif_get_pixel( pix, szpix, bif, (dk4_bif_dim_t)y, (dk4_bif_dim_t)x, cs ) ) { back = 0; } for (i = 0; i < nco; i++) { #if 0 $? ". pixel value = %lu", (unsigned long)(pix[i]) #endif if (0 == dk4stream_write_byte(costrm, (char)(pix[i]), erp)) { back = 0; $? "! failed to write co stream" } } } } } } else { $? ". bpc != 8" if ((3 == coe) && (DK4_CS_GRAY != cs)) { for (y = 0; y < h; y++) { for (i = 0; i < nco; i++) { dk4bit_shift_init(&bs, NULL); for (x = 0; x < w; x++) { if ( 0 == dk4bif_get_pixel( pix, szpix, bif, (dk4_bif_dim_t)y, (dk4_bif_dim_t)x, cs ) ) { back = 0; $? "! failed to get pixel" } $? ". xxx back = %d", back dk4gratool_put_pixel_value( costrm, &bs, pix[i], bpc, &back, erp ); $? ". xxx back = %d", back } if (DK4_EDSTM_FINISHED == dk4bit_shift_finish(&bs, NULL)) { if (0 == dk4stream_write_byte( costrm, dk4bit_shift_output(&bs, NULL), erp ) ) { back = 0; $? "! failed to write to co stream" } } } } } else { for (y = 0; y < h; y++) { dk4bit_shift_init(&bs, NULL); for (x = 0; x < w; x++) { if ( 0 == dk4bif_get_pixel( pix, szpix, bif, (dk4_bif_dim_t)y, (dk4_bif_dim_t)x, cs ) ) { back = 0; $? "! failed to obtain pixel value" } for (i = 0; i < nco; i++) { $? ". xxx back = %d", back dk4gratool_put_pixel_value( costrm, &bs, pix[i], bpc, &back, erp ); $? ". xxx back = %d", back } } if (DK4_EDSTM_FINISHED == dk4bit_shift_finish(&bs, NULL)) { if (0 == dk4stream_write_byte( costrm, dk4bit_shift_output(&bs, NULL), erp ) ) { back = 0; $? "! failed to write to co stream" } } } } } /* Cleanup */ finished: if (NULL != costrm) { if (0 == dk4stream_close(costrm, erp)) { back = 0; $? "! failed to close compressing stream" } } $? "- dk4gra_eps_bif_fig_image %d", back return back; } /* vim: set ai sw=4 ts=4 : */