| 1 | /*
|
|---|
| 2 |
|
|---|
| 3 | gbmpsg.c - PSEG support
|
|---|
| 4 |
|
|---|
| 5 | Reads and writes PSEG files.
|
|---|
| 6 | Input options: fixrec (attempt to handle corrupt record structure)
|
|---|
| 7 | Output options: scalex=#, scaley=# (default is 1000)
|
|---|
| 8 | Output options: double (will double scale factors in header).
|
|---|
| 9 | Output options: pelx=#,pely=# (default is 2400).
|
|---|
| 10 |
|
|---|
| 11 | This code was written using a subset of the full documentation.
|
|---|
| 12 | The missing bits were filled in by reverse engineering a selection of files.
|
|---|
| 13 | Guesses as to which records are important were made (BeginPageSegment etc.)
|
|---|
| 14 | and these seem to correspond with other conversion programs choices.
|
|---|
| 15 | Also image (and cell) widths and heights can apparently be any value, although
|
|---|
| 16 | all PSEGs I have seen always use a multiple of 8 (and 32). This code will allow
|
|---|
| 17 | any width that is a multiple of 8 (for easy coding), and any height at all.
|
|---|
| 18 |
|
|---|
| 19 | When a PSEG is uploaded to VM, it must have its VM FILE block record structure
|
|---|
| 20 | reestablished. Various VM EXECs exist to do this, such as FIXL3820, REFLOW, and
|
|---|
| 21 | REST38PP, although in my experience, FIXL3820 always works, and REST38PP always
|
|---|
| 22 | fails for large files.
|
|---|
| 23 |
|
|---|
| 24 | */
|
|---|
| 25 |
|
|---|
| 26 | /*...sincludes:0:*/
|
|---|
| 27 | #include <stdio.h>
|
|---|
| 28 | #include <ctype.h>
|
|---|
| 29 | #include <stddef.h>
|
|---|
| 30 | #include <stdlib.h>
|
|---|
| 31 | #include <string.h>
|
|---|
| 32 | #include "gbm.h"
|
|---|
| 33 | #include "gbmhelp.h"
|
|---|
| 34 |
|
|---|
| 35 | /*...vgbm\46\h:0:*/
|
|---|
| 36 | /*...vgbmhelp\46\h:0:*/
|
|---|
| 37 |
|
|---|
| 38 | #ifndef min
|
|---|
| 39 | #define min(a,b) (((a)<(b))?(a):(b))
|
|---|
| 40 | #endif
|
|---|
| 41 | /*...e*/
|
|---|
| 42 |
|
|---|
| 43 | /*...suseful:0:*/
|
|---|
| 44 | #define low_byte(w) ((byte) ((w)&0x00ff) )
|
|---|
| 45 | #define high_byte(w) ((byte) (((w)&0xff00)>>8))
|
|---|
| 46 | #define make_word(a,b) (((word)a) + (((word)b) << 8))
|
|---|
| 47 | /*...e*/
|
|---|
| 48 |
|
|---|
| 49 | static GBMFT psg_gbmft =
|
|---|
| 50 | {
|
|---|
| 51 | "PSEG",
|
|---|
| 52 | "IBM Printer Page Segment",
|
|---|
| 53 | "PSE PSEG PSEG38PP PSEG3820",
|
|---|
| 54 | GBM_FT_R1|
|
|---|
| 55 | GBM_FT_W1,
|
|---|
| 56 | };
|
|---|
| 57 |
|
|---|
| 58 | #define GBM_ERR_PSEG_NOT_5A ((GBM_ERR) 1600)
|
|---|
| 59 | #define GBM_ERR_PSEG_BAD_RECORD ((GBM_ERR) 1601)
|
|---|
| 60 | #define GBM_ERR_PSEG_SIZE ((GBM_ERR) 1602)
|
|---|
| 61 | #define GBM_ERR_PSEG_INV_WIDTH ((GBM_ERR) 1603)
|
|---|
| 62 | #define GBM_ERR_PSEG_INV_DEFCEL ((GBM_ERR) 1604)
|
|---|
| 63 | #define GBM_ERR_PSEG_INV_CEL ((GBM_ERR) 1605)
|
|---|
| 64 | #define GBM_ERR_PSEG_INV_CELPOS ((GBM_ERR) 1606)
|
|---|
| 65 | #define GBM_ERR_PSEG_UNEX_REC ((GBM_ERR) 1607)
|
|---|
| 66 |
|
|---|
| 67 | typedef struct
|
|---|
| 68 | {
|
|---|
| 69 | long pos;
|
|---|
| 70 | int xcellsizedef, ycellsizedef;
|
|---|
| 71 | BOOLEAN fix_badrec;
|
|---|
| 72 | } PSEG_PRIV;
|
|---|
| 73 |
|
|---|
| 74 | /*...smax:0:*/
|
|---|
| 75 | #ifndef max
|
|---|
| 76 | #define max(a,b) (((a)>(b))?(a):(b))
|
|---|
| 77 | #endif
|
|---|
| 78 | /*...e*/
|
|---|
| 79 | /*...sascii \47\ ebcdic:0:*/
|
|---|
| 80 | #ifdef NEVER
|
|---|
| 81 | /*...sebcdic_to_ascii:0:*/
|
|---|
| 82 | static byte ebcdic_to_ascii[0x100] =
|
|---|
| 83 | {
|
|---|
| 84 | 0x00,0x01,0x02,0x03,0xcf,0x09,0xd3,0x7f,
|
|---|
| 85 | 0xd4,0xd5,0xc3,0x0b,0x0c,0x0d,0x0e,0x0f,
|
|---|
| 86 | 0x10,0x11,0x12,0x13,0xc7,0xb4,0x08,0xc9,
|
|---|
| 87 | 0x18,0x19,0xcc,0xcd,0x83,0x1d,0xd2,0x1f,
|
|---|
| 88 | 0x81,0x82,0x1c,0x84,0x86,0x0a,0x17,0x1b,
|
|---|
| 89 | 0x89,0x91,0x92,0x95,0xa2,0x05,0x06,0x07,
|
|---|
| 90 | 0xe0,0xee,0x16,0xe5,0xd0,0x1e,0xea,0x04,
|
|---|
| 91 | 0x8a,0xf6,0xc6,0xc2,0x14,0x15,0xc1,0x1a,
|
|---|
| 92 | 0x20,0xa6,0xe1,0x80,0xeb,0x90,0x9f,0xe2,
|
|---|
| 93 | 0xab,0x8b,0x9b,0x2e,0x3c,0x28,0x2b,0x7c,
|
|---|
| 94 | 0x26,0xa9,0xaa,0x9c,0xdb,0xa5,0x99,0xe3,
|
|---|
| 95 | 0xa8,0x9e,0x21,0x24,0x2a,0x29,0x3b,0x5e,
|
|---|
| 96 | 0x2d,0x2f,0xdf,0xdc,0x9a,0xdd,0xde,0x98,
|
|---|
| 97 | 0x9d,0xac,0xba,0x2c,0x25,0x5f,0x3e,0x3f,
|
|---|
| 98 | 0xd7,0x88,0x94,0xb0,0xb1,0xb2,0xfc,0xd6,
|
|---|
| 99 | 0xfb,0x60,0x3a,0x23,0x40,0x27,0x3d,0x22,
|
|---|
| 100 | 0xf8,0x61,0x62,0x63,0x64,0x65,0x66,0x67,
|
|---|
| 101 | 0x68,0x69,0x96,0xa4,0xf3,0xaf,0xae,0xc5,
|
|---|
| 102 | 0x8c,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f,0x70,
|
|---|
| 103 | 0x71,0x72,0x97,0x87,0xce,0x93,0xf1,0xfe,
|
|---|
| 104 | 0xc8,0x7e,0x73,0x74,0x75,0x76,0x77,0x78,
|
|---|
| 105 | 0x79,0x7a,0xef,0xc0,0xda,0x5b,0xf2,0xf9,
|
|---|
| 106 | 0xb5,0xb6,0xfd,0xb7,0xb8,0xb9,0xe6,0xbb,
|
|---|
| 107 | 0xbc,0xbd,0x8d,0xd9,0xbf,0x5d,0xd8,0xc4,
|
|---|
| 108 | 0x7b,0x41,0x42,0x43,0x44,0x45,0x46,0x47,
|
|---|
| 109 | 0x48,0x49,0xcb,0xca,0xbe,0xe8,0xec,0xed,
|
|---|
| 110 | 0x7d,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,0x50,
|
|---|
| 111 | 0x51,0x52,0xa1,0xad,0xf5,0xf4,0xa3,0x8f,
|
|---|
| 112 | 0x5c,0xe7,0x53,0x54,0x55,0x56,0x57,0x58,
|
|---|
| 113 | 0x59,0x5a,0xa0,0x85,0x8e,0xe9,0xe4,0xd1,
|
|---|
| 114 | 0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,
|
|---|
| 115 | 0x38,0x39,0xb3,0xf7,0xf0,0xfa,0xa7,0xff,
|
|---|
| 116 | };
|
|---|
| 117 | /*...e*/
|
|---|
| 118 | #endif
|
|---|
| 119 | /*...sascii_to_ebcdic:0:*/
|
|---|
| 120 | static byte ascii_to_ebcdic[0x100] =
|
|---|
| 121 | {
|
|---|
| 122 | 0x00,0x01,0x02,0x03,0x37,0x2d,0x2e,0x2f,
|
|---|
| 123 | 0x16,0x05,0x25,0x0b,0x0c,0x0d,0x0e,0x0f,
|
|---|
| 124 | 0x10,0x11,0x12,0x13,0x3c,0x3d,0x32,0x26,
|
|---|
| 125 | 0x18,0x19,0x3f,0x27,0x22,0x1d,0x35,0x1f,
|
|---|
| 126 | 0x40,0x5a,0x7f,0x7b,0x5b,0x6c,0x50,0x7d,
|
|---|
| 127 | 0x4d,0x5d,0x5c,0x4e,0x6b,0x60,0x4b,0x61,
|
|---|
| 128 | 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
|
|---|
| 129 | 0xf8,0xf9,0x7a,0x5e,0x4c,0x7e,0x6e,0x6f,
|
|---|
| 130 | 0x7c,0xc1,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,
|
|---|
| 131 | 0xc8,0xc9,0xd1,0xd2,0xd3,0xd4,0xd5,0xd6,
|
|---|
| 132 | 0xd7,0xd8,0xd9,0xe2,0xe3,0xe4,0xe5,0xe6,
|
|---|
| 133 | 0xe7,0xe8,0xe9,0xad,0xe0,0xbd,0x5f,0x6d,
|
|---|
| 134 | 0x79,0x81,0x82,0x83,0x84,0x85,0x86,0x87,
|
|---|
| 135 | 0x88,0x89,0x91,0x92,0x93,0x94,0x95,0x96,
|
|---|
| 136 | 0x97,0x98,0x99,0xa2,0xa3,0xa4,0xa5,0xa6,
|
|---|
| 137 | 0xa7,0xa8,0xa9,0xc0,0x4f,0xd0,0xa1,0x07,
|
|---|
| 138 | 0x43,0x20,0x21,0x1c,0x23,0xeb,0x24,0x9b,
|
|---|
| 139 | 0x71,0x28,0x38,0x49,0x90,0xba,0xec,0xdf,
|
|---|
| 140 | 0x45,0x29,0x2a,0x9d,0x72,0x2b,0x8a,0x9a,
|
|---|
| 141 | 0x67,0x56,0x64,0x4a,0x53,0x68,0x59,0x46,
|
|---|
| 142 | 0xea,0xda,0x2c,0xde,0x8b,0x55,0x41,0xfe,
|
|---|
| 143 | 0x58,0x51,0x52,0x48,0x69,0xdb,0x8e,0x8d,
|
|---|
| 144 | 0x73,0x74,0x75,0xfa,0x15,0xb0,0xb1,0xb3,
|
|---|
| 145 | 0xb4,0xb5,0x6a,0xb7,0xb8,0xb9,0xcc,0xbc,
|
|---|
| 146 | 0xab,0x3e,0x3b,0x0a,0xbf,0x8f,0x3a,0x14,
|
|---|
| 147 | 0xa0,0x17,0xcb,0xca,0x1a,0x1b,0x9c,0x04,
|
|---|
| 148 | 0x34,0xef,0x1e,0x06,0x08,0x09,0x77,0x70,
|
|---|
| 149 | 0xbe,0xbb,0xac,0x54,0x63,0x65,0x66,0x62,
|
|---|
| 150 | 0x30,0x42,0x47,0x57,0xee,0x33,0xb6,0xe1,
|
|---|
| 151 | 0xcd,0xed,0x36,0x44,0xce,0xcf,0x31,0xaa,
|
|---|
| 152 | 0xfc,0x9e,0xae,0x8c,0xdd,0xdc,0x39,0xfb,
|
|---|
| 153 | 0x80,0xaf,0xfd,0x78,0x76,0xb2,0x9f,0xff,
|
|---|
| 154 | };
|
|---|
| 155 | /*...e*/
|
|---|
| 156 |
|
|---|
| 157 | static void ebcdic(char *dst, char *src)
|
|---|
| 158 | {
|
|---|
| 159 | while ( *src )
|
|---|
| 160 | *dst++ = ascii_to_ebcdic[*src++];
|
|---|
| 161 | }
|
|---|
| 162 | /*...e*/
|
|---|
| 163 | /*...sgetword:0:*/
|
|---|
| 164 | static int getword(byte *c)
|
|---|
| 165 | {
|
|---|
| 166 | return (int) ( ((unsigned int) c[0] << 8) + (unsigned int) c[1] );
|
|---|
| 167 | }
|
|---|
| 168 | /*...e*/
|
|---|
| 169 | /*...sputword:0:*/
|
|---|
| 170 | static void putword(byte *c, int d)
|
|---|
| 171 | {
|
|---|
| 172 | c[0] = (byte) ((unsigned)d>>8);
|
|---|
| 173 | c[1] = (byte) d ;
|
|---|
| 174 | }
|
|---|
| 175 | /*...e*/
|
|---|
| 176 |
|
|---|
| 177 | /*...spsg_qft:0:*/
|
|---|
| 178 | GBM_ERR psg_qft(GBMFT *gbmft)
|
|---|
| 179 | {
|
|---|
| 180 | *gbmft = psg_gbmft;
|
|---|
| 181 | return GBM_ERR_OK;
|
|---|
| 182 | }
|
|---|
| 183 | /*...e*/
|
|---|
| 184 | /*...spsg_rhdr:0:*/
|
|---|
| 185 | GBM_ERR psg_rhdr(const char *fn, int fd, GBM *gbm, const char *opt)
|
|---|
| 186 | {
|
|---|
| 187 | PSEG_PRIV *psg_priv = (PSEG_PRIV *) gbm->priv;
|
|---|
| 188 | byte buf[1+2+6];
|
|---|
| 189 |
|
|---|
| 190 | fn=fn; /* Suppress 'unref arg' compiler warnings */
|
|---|
| 191 |
|
|---|
| 192 | psg_priv->fix_badrec = ( gbm_find_word(opt, "fixrec") != NULL );
|
|---|
| 193 |
|
|---|
| 194 | gbm->bpp = 1;
|
|---|
| 195 | gbm->w = -1;
|
|---|
| 196 |
|
|---|
| 197 | while ( gbm_file_read(fd, buf, sizeof(buf)) == sizeof(buf) )
|
|---|
| 198 | {
|
|---|
| 199 | long seek_by; int f;
|
|---|
| 200 | if ( psg_priv->fix_badrec )
|
|---|
| 201 | for ( f = 0; buf[0] != 0x5a && f < 10; f++ )
|
|---|
| 202 | {
|
|---|
| 203 | long pos = gbm_file_lseek(fd, 0L, SEEK_CUR);
|
|---|
| 204 | if ( pos == 0L )
|
|---|
| 205 | break;
|
|---|
| 206 | gbm_file_lseek(fd, pos - (sizeof(buf)+1), SEEK_SET);
|
|---|
| 207 | gbm_file_read(fd, buf, sizeof(buf));
|
|---|
| 208 | }
|
|---|
| 209 | if ( buf[0] != 0x5a )
|
|---|
| 210 | return GBM_ERR_PSEG_NOT_5A;
|
|---|
| 211 | seek_by = (long) getword(buf+1)-(2+6);
|
|---|
| 212 | if ( !memcmp(buf+1+2, "\xd3\xee\xee", 3) )
|
|---|
| 213 | ; /* Comment record, ignore it */
|
|---|
| 214 | else if ( !memcmp(buf+1+2, "\xd3\xa8\x5f", 3) )
|
|---|
| 215 | ; /* BeginPageSegment, ignore it */
|
|---|
| 216 | else if ( !memcmp(buf+1+2, "\xd3\xa8\x7b", 3) )
|
|---|
| 217 | ; /* BeginImageBlock, ignore it */
|
|---|
| 218 | else if ( !memcmp(buf+1+2, "\xd3\xa7\x7b", 3) )
|
|---|
| 219 | ; /* ImageOutputControl, ignore it */
|
|---|
| 220 | else if ( !memcmp(buf+1+2, "\xd3\xa6\x7b", 3) )
|
|---|
| 221 | /* InputImageDescriptor, remember its info */
|
|---|
| 222 | {
|
|---|
| 223 | byte buf2[6+12+2+2+6+2+2+4];
|
|---|
| 224 | if ( gbm_file_read(fd, buf2, sizeof(buf2)) != sizeof(buf2) )
|
|---|
| 225 | return GBM_ERR_READ;
|
|---|
| 226 | seek_by -= sizeof(buf2);
|
|---|
| 227 | gbm->w = getword(buf2+6+12);
|
|---|
| 228 | gbm->h = getword(buf2+6+12+2);
|
|---|
| 229 | if ( gbm->w & 7 )
|
|---|
| 230 | return GBM_ERR_PSEG_INV_WIDTH;
|
|---|
| 231 | psg_priv->xcellsizedef = getword(buf2+6+12+2+2+6);
|
|---|
| 232 | psg_priv->ycellsizedef = getword(buf2+6+12+2+2+6+2);
|
|---|
| 233 | if ( psg_priv->xcellsizedef == 0 &&
|
|---|
| 234 | psg_priv->ycellsizedef == 0 )
|
|---|
| 235 | /* No cell structure, image is one lump */
|
|---|
| 236 | {
|
|---|
| 237 | psg_priv->xcellsizedef = gbm->w;
|
|---|
| 238 | psg_priv->ycellsizedef = gbm->h;
|
|---|
| 239 | }
|
|---|
| 240 | else if ( psg_priv->xcellsizedef & 7 )
|
|---|
| 241 | /* Insist upon a multiple of 8 */
|
|---|
| 242 | return GBM_ERR_PSEG_INV_DEFCEL;
|
|---|
| 243 | }
|
|---|
| 244 | else if ( !memcmp(buf+1+2, "\xd3\xee\x7b", 3) ||
|
|---|
| 245 | !memcmp(buf+1+2, "\xd3\xac\x7b", 3) )
|
|---|
| 246 | /* RasterData, or */
|
|---|
| 247 | /* ImageCellPosition, seek back and abort loop */
|
|---|
| 248 | {
|
|---|
| 249 | psg_priv->pos = gbm_file_lseek(fd, - (long) sizeof(buf), SEEK_CUR);
|
|---|
| 250 | return gbm->w != -1 ? GBM_ERR_OK : GBM_ERR_PSEG_SIZE;
|
|---|
| 251 | }
|
|---|
| 252 | else
|
|---|
| 253 | return GBM_ERR_PSEG_BAD_RECORD;
|
|---|
| 254 | gbm_file_lseek(fd, seek_by, SEEK_CUR);
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 | return GBM_ERR_READ;
|
|---|
| 258 | }
|
|---|
| 259 | /*...e*/
|
|---|
| 260 | /*...spsg_rpal:0:*/
|
|---|
| 261 | GBM_ERR psg_rpal(int fd, GBM *gbm, GBMRGB *gbmrgb)
|
|---|
| 262 | {
|
|---|
| 263 | fd=fd; gbm=gbm; /* Suppress compiler warnings */
|
|---|
| 264 | gbmrgb[0].r = gbmrgb[0].g = gbmrgb[0].b = 0xff;
|
|---|
| 265 | gbmrgb[1].r = gbmrgb[1].g = gbmrgb[1].b = 0x00;
|
|---|
| 266 | return GBM_ERR_OK;
|
|---|
| 267 | }
|
|---|
| 268 | /*...e*/
|
|---|
| 269 | /*...spsg_rdata:0:*/
|
|---|
| 270 | GBM_ERR psg_rdata(int fd, GBM *gbm, byte *data)
|
|---|
| 271 | {
|
|---|
| 272 | PSEG_PRIV *psg_priv = (PSEG_PRIV *) gbm->priv;
|
|---|
| 273 | byte buf[1+2+6];
|
|---|
| 274 | int stride = ( ( gbm->w*gbm->bpp + 31 ) / 32 ) * 4;
|
|---|
| 275 | int xcellpos = 0;
|
|---|
| 276 | int ycellpos = 0;
|
|---|
| 277 | int xcellsize = psg_priv->xcellsizedef;
|
|---|
| 278 | int ycellsize = psg_priv->ycellsizedef;
|
|---|
| 279 |
|
|---|
| 280 | gbm_file_lseek(fd, psg_priv->pos, SEEK_SET);
|
|---|
| 281 |
|
|---|
| 282 | while ( gbm_file_read(fd, buf, sizeof(buf)) == sizeof(buf) )
|
|---|
| 283 | {
|
|---|
| 284 | long seek_by; int f;
|
|---|
| 285 | if ( psg_priv->fix_badrec )
|
|---|
| 286 | for ( f = 0; buf[0] != 0x5a && f < 10; f++ )
|
|---|
| 287 | {
|
|---|
| 288 | long pos = gbm_file_lseek(fd, 0L, SEEK_CUR);
|
|---|
| 289 | if ( pos == 0L )
|
|---|
| 290 | break;
|
|---|
| 291 | gbm_file_lseek(fd, pos - (sizeof(buf)+1), SEEK_SET);
|
|---|
| 292 | gbm_file_read(fd, buf, sizeof(buf));
|
|---|
| 293 | }
|
|---|
| 294 | if ( buf[0] != 0x5a )
|
|---|
| 295 | return GBM_ERR_PSEG_NOT_5A;
|
|---|
| 296 | seek_by = (long) getword(buf+1)-(2+6);
|
|---|
| 297 | if ( !memcmp(buf+1+2, "\xd3\xee\xee", 3) )
|
|---|
| 298 | ; /* Comment record, ignore it */
|
|---|
| 299 | else if ( !memcmp(buf+1+2, "\xd3\xa8\x5f", 3) )
|
|---|
| 300 | return GBM_ERR_PSEG_UNEX_REC; /* BeginPageSegment */
|
|---|
| 301 | else if ( !memcmp(buf+1+2, "\xd3\xa8\x7b", 3) )
|
|---|
| 302 | return GBM_ERR_PSEG_UNEX_REC; /* BeginImageBlock */
|
|---|
| 303 | else if ( !memcmp(buf+1+2, "\xd3\xa7\x7b", 3) )
|
|---|
| 304 | return GBM_ERR_PSEG_UNEX_REC; /* ImageOutputControl */
|
|---|
| 305 | else if ( !memcmp(buf+1+2, "\xd3\xa6\x7b", 3) )
|
|---|
| 306 | return GBM_ERR_PSEG_UNEX_REC; /* InputImageDescriptor */
|
|---|
| 307 | else if ( !memcmp(buf+1+2, "\xd3\xac\x7b", 3) )
|
|---|
| 308 | /* ImageCellPosition, ignore it */
|
|---|
| 309 | {
|
|---|
| 310 | byte buf2[12], *p;
|
|---|
| 311 | int xfillsize, yfillsize, y;
|
|---|
| 312 | if ( gbm_file_read(fd, buf2, sizeof(buf2)) != sizeof(buf2) )
|
|---|
| 313 | return GBM_ERR_READ;
|
|---|
| 314 | xcellpos = getword(buf2);
|
|---|
| 315 | if ( xcellpos & 7 )
|
|---|
| 316 | return GBM_ERR_PSEG_INV_CELPOS;
|
|---|
| 317 | ycellpos = getword(buf2+2);
|
|---|
| 318 | xcellsize = getword(buf2+4);
|
|---|
| 319 | if ( xcellsize & 7 )
|
|---|
| 320 | return GBM_ERR_PSEG_INV_CEL;
|
|---|
| 321 | ycellsize = getword(buf2+6);
|
|---|
| 322 | xfillsize = getword(buf2+8);
|
|---|
| 323 | yfillsize = getword(buf2+10);
|
|---|
| 324 | if ( xfillsize != 0xffff && yfillsize != 0xffff )
|
|---|
| 325 | {
|
|---|
| 326 | p = data + ( (gbm->h-1-ycellpos) * stride + xcellpos/8 );
|
|---|
| 327 | for ( y = 0; y < yfillsize; y++, p -= stride )
|
|---|
| 328 | memset(p, 0x00, xfillsize/8);
|
|---|
| 329 | }
|
|---|
| 330 | seek_by -= sizeof(buf2);
|
|---|
| 331 | }
|
|---|
| 332 | else if ( !memcmp(buf+1+2, "\xd3\xee\x7b", 3) )
|
|---|
| 333 | /* RasterData, use the data */
|
|---|
| 334 | {
|
|---|
| 335 | byte *p = data + ( (gbm->h-1-ycellpos) * stride + xcellpos/8 );
|
|---|
| 336 | int xc = xcellsize/8;
|
|---|
| 337 |
|
|---|
| 338 | for ( ; seek_by >= xc; seek_by -= xc, p -= stride, ycellpos++, ycellsize-- )
|
|---|
| 339 | if ( gbm_file_read(fd, p, xc) != xc )
|
|---|
| 340 | return GBM_ERR_READ;
|
|---|
| 341 | }
|
|---|
| 342 | else if ( !memcmp(buf+1+2, "\xd3\xa9\x7b", 3) )
|
|---|
| 343 | /* EndImageBlock */
|
|---|
| 344 | return GBM_ERR_OK;
|
|---|
| 345 | else
|
|---|
| 346 | return GBM_ERR_PSEG_BAD_RECORD;
|
|---|
| 347 | gbm_file_lseek(fd, seek_by, SEEK_CUR);
|
|---|
| 348 | }
|
|---|
| 349 |
|
|---|
| 350 | return GBM_ERR_READ;
|
|---|
| 351 | }
|
|---|
| 352 | /*...e*/
|
|---|
| 353 | /*...spsg_w:0:*/
|
|---|
| 354 | /*...srecord:0:*/
|
|---|
| 355 | static BOOLEAN record(int fd, byte *rec, char *six, int len)
|
|---|
| 356 | {
|
|---|
| 357 | rec[0] = 0x5a;
|
|---|
| 358 | putword(rec+1, 2+6+len);
|
|---|
| 359 | memcpy(rec+1+2, six, 6);
|
|---|
| 360 | return gbm_file_write(fd, rec, 1+2+6+len) == 1+2+6+len;
|
|---|
| 361 | }
|
|---|
| 362 | /*...e*/
|
|---|
| 363 |
|
|---|
| 364 | GBM_ERR psg_w(const char *fn, int fd, const GBM *gbm, const GBMRGB *gbmrgb, const byte *data, const char *opt)
|
|---|
| 365 | {
|
|---|
| 366 | static char by_gbm[] = "Written by GBM", buf8[8+1];
|
|---|
| 367 | static byte *rec, *r;
|
|---|
| 368 | int stride = ( ( gbm->w + 31 ) / 32 ) * 4;
|
|---|
| 369 | int len = (gbm->w+7)/8;
|
|---|
| 370 | int i;
|
|---|
| 371 | byte lastbits = 0xff;
|
|---|
| 372 | const char *str;
|
|---|
| 373 | int scalex = 1000, scaley = 1000;
|
|---|
| 374 | int pelx = 2400, pely = 2400;
|
|---|
| 375 |
|
|---|
| 376 | gbmrgb=gbmrgb; /* Suppress compiler warning */
|
|---|
| 377 |
|
|---|
| 378 | if ( gbm->bpp != 1 )
|
|---|
| 379 | return GBM_ERR_NOT_SUPP;
|
|---|
| 380 |
|
|---|
| 381 | if ( gbm->w > 2400 || gbm->h > 2400 )
|
|---|
| 382 | return GBM_ERR_BAD_SIZE;
|
|---|
| 383 |
|
|---|
| 384 | if ( (str = gbm_find_word_prefix(opt, "scalex=")) != NULL )
|
|---|
| 385 | sscanf(str+7, "%i", &scalex);
|
|---|
| 386 | if ( (str = gbm_find_word_prefix(opt, "scaley=")) != NULL )
|
|---|
| 387 | sscanf(str+7, "%i", &scaley);
|
|---|
| 388 | if ( gbm_find_word(opt, "double") != NULL )
|
|---|
| 389 | { scalex *= 2; scaley *= 2; }
|
|---|
| 390 | if ( (str = gbm_find_word_prefix(opt, "pelx=")) != NULL )
|
|---|
| 391 | sscanf(str+5, "%i", &pelx);
|
|---|
| 392 | if ( (str = gbm_find_word_prefix(opt, "pely=")) != NULL )
|
|---|
| 393 | sscanf(str+5, "%i", &pely);
|
|---|
| 394 |
|
|---|
| 395 | if ( (rec = malloc((size_t) (1+2+6+max(100,stride)))) == NULL )
|
|---|
| 396 | return GBM_ERR_MEM;
|
|---|
| 397 | r = rec + 1+2+6;
|
|---|
| 398 |
|
|---|
| 399 | sprintf(buf8, "%-8.8s", fn);
|
|---|
| 400 | ebcdic(buf8, buf8);
|
|---|
| 401 |
|
|---|
| 402 | /* Comment */
|
|---|
| 403 | ebcdic((char *) r, by_gbm);
|
|---|
| 404 | if ( !record(fd, rec, "\xd3\xee\xee\x00\x00\x00", strlen(by_gbm)) )
|
|---|
| 405 | { free(rec); return GBM_ERR_WRITE; }
|
|---|
| 406 |
|
|---|
| 407 | /* BeginPageSegment (we will use filename) */
|
|---|
| 408 | memcpy(r, buf8, 8);
|
|---|
| 409 | if ( !record(fd, rec, "\xd3\xa8\x5f\x00\x00\x00", 8) )
|
|---|
| 410 | { free(rec); return GBM_ERR_WRITE; }
|
|---|
| 411 |
|
|---|
| 412 | /* BeginImageBlock (use filename again) */
|
|---|
| 413 | if ( !record(fd, rec, "\xd3\xa8\x7b\x00\x00\x00", 8) )
|
|---|
| 414 | { free(rec); return GBM_ERR_WRITE; }
|
|---|
| 415 |
|
|---|
| 416 | /* ImageOutputControl */
|
|---|
| 417 | memset(r, 0x00, 6+4+8); /* Image Block Origin, Orientation, Reserved */
|
|---|
| 418 | r[8] = 0x2d; /* 90 degrees */
|
|---|
| 419 | putword(r+6+4+8 , scalex);
|
|---|
| 420 | putword(r+6+4+8+2, scaley);
|
|---|
| 421 | memset(r+6+4+8+4, 0xff, 2);
|
|---|
| 422 | if ( !record(fd, rec, "\xd3\xa7\x7b\x00\x00\x00", 6+4+8+4+2) )
|
|---|
| 423 | { free(rec); return GBM_ERR_WRITE; }
|
|---|
| 424 |
|
|---|
| 425 | /* ImageInputDescriptor */
|
|---|
| 426 | memset(r, 0x00, 6+12+2+2+6);
|
|---|
| 427 | putword(r+2, pelx); /* Or 0? */
|
|---|
| 428 | putword(r+4, pely); /* Or 0? */
|
|---|
| 429 | putword(r+6+ 8, pelx);
|
|---|
| 430 | putword(r+6+10, pely);
|
|---|
| 431 | putword(r+6+12, (gbm->w+7)&~7);
|
|---|
| 432 | putword(r+6+12+2, gbm->h);
|
|---|
| 433 | r[6+12+2+2 + 4] = 0x2d;
|
|---|
| 434 | putword(r+6+12+2+2+6, 0); /* Not writing in cells */
|
|---|
| 435 | putword(r+6+12+2+2+6+2, 0); /* Not writing in cells */
|
|---|
| 436 | memcpy( r+6+12+2+2+6+2+2, "\x00\x01\xff\xff", 4);
|
|---|
| 437 | if ( !record(fd, rec, "\xd3\xa6\x7b\x00\x00\x00", 6+12+2+2+6+2+2+4) )
|
|---|
| 438 | { free(rec); return GBM_ERR_WRITE; }
|
|---|
| 439 |
|
|---|
| 440 | if ( (unsigned)gbm->w&7U )
|
|---|
| 441 | lastbits = (byte) ( 0xff00U >> ((unsigned)gbm->w&7U) );
|
|---|
| 442 | data += (gbm->h-1) * stride;
|
|---|
| 443 | for ( i = 0; i < gbm->h; i++, data -= stride )
|
|---|
| 444 | {
|
|---|
| 445 | memcpy(r, data, len); r[len-1] &= lastbits;
|
|---|
| 446 | if ( !record(fd, rec, "\xd3\xee\x7b\x00\x00\x00", len) )
|
|---|
| 447 | { free(rec); return GBM_ERR_WRITE; }
|
|---|
| 448 | }
|
|---|
| 449 |
|
|---|
| 450 | /* EndImageBlock, (use filename) */
|
|---|
| 451 | memcpy(r, buf8, 8);
|
|---|
| 452 | if ( !record(fd, rec, "\xd3\xa9\x7b\x00\x00\x00", 8) )
|
|---|
| 453 | { free(rec); return GBM_ERR_WRITE; }
|
|---|
| 454 |
|
|---|
| 455 | /* EndPageSegment (use filename from before) */
|
|---|
| 456 | if ( !record(fd, rec, "\xd3\xa9\x5f\x00\x00\x00", 8) )
|
|---|
| 457 | { free(rec); return GBM_ERR_WRITE; }
|
|---|
| 458 |
|
|---|
| 459 | free(rec);
|
|---|
| 460 |
|
|---|
| 461 | return GBM_ERR_OK;
|
|---|
| 462 | }
|
|---|
| 463 | /*...e*/
|
|---|
| 464 | /*...spsg_err:0:*/
|
|---|
| 465 | const char *psg_err(GBM_ERR rc)
|
|---|
| 466 | {
|
|---|
| 467 | switch ( (int) rc )
|
|---|
| 468 | {
|
|---|
| 469 | case GBM_ERR_PSEG_NOT_5A:
|
|---|
| 470 | return "record in the PSEG file does not start with 0x5a (try fixrec option)";
|
|---|
| 471 | case GBM_ERR_PSEG_BAD_RECORD:
|
|---|
| 472 | return "bad/unknown record in PSEG file";
|
|---|
| 473 | case GBM_ERR_PSEG_SIZE:
|
|---|
| 474 | return "size record not seen before raster data or image cell position records";
|
|---|
| 475 | case GBM_ERR_PSEG_INV_WIDTH:
|
|---|
| 476 | return "width must be a multiple of 8";
|
|---|
| 477 | case GBM_ERR_PSEG_INV_DEFCEL:
|
|---|
| 478 | return "default cell size must be a multiple of 8";
|
|---|
| 479 | case GBM_ERR_PSEG_INV_CEL:
|
|---|
| 480 | return "cell size must be a multiple of 8";
|
|---|
| 481 | case GBM_ERR_PSEG_INV_CELPOS:
|
|---|
| 482 | return "cell position must be a multiple of 8";
|
|---|
| 483 | case GBM_ERR_PSEG_UNEX_REC:
|
|---|
| 484 | return "record found in an unexpected place";
|
|---|
| 485 | }
|
|---|
| 486 | return NULL;
|
|---|
| 487 | }
|
|---|
| 488 | /*...e*/
|
|---|