| 1 | /*
|
|---|
| 2 |
|
|---|
| 3 | gbm.c - Generalised Bitmap Module
|
|---|
| 4 |
|
|---|
| 5 | */
|
|---|
| 6 |
|
|---|
| 7 | /*...sincludes:0:*/
|
|---|
| 8 | #include <stdio.h>
|
|---|
| 9 | #include <ctype.h>
|
|---|
| 10 | #include <stddef.h>
|
|---|
| 11 | #include <stdlib.h>
|
|---|
| 12 | #include <string.h>
|
|---|
| 13 | #if defined(AIX) || defined(LINUX) || defined(MAC)
|
|---|
| 14 | #include <unistd.h>
|
|---|
| 15 | #else
|
|---|
| 16 | #include <io.h>
|
|---|
| 17 | #endif
|
|---|
| 18 | #include <fcntl.h>
|
|---|
| 19 | #ifdef MAC
|
|---|
| 20 | #include <types.h>
|
|---|
| 21 | #include <stat.h>
|
|---|
| 22 | #else
|
|---|
| 23 | #include <sys/types.h>
|
|---|
| 24 | #include <sys/stat.h>
|
|---|
| 25 | #endif
|
|---|
| 26 | #include "gbm.h"
|
|---|
| 27 | #include "gbmhelp.h"
|
|---|
| 28 |
|
|---|
| 29 | /*...vgbm\46\h:0:*/
|
|---|
| 30 | /*...vgbmhelp\46\h:0:*/
|
|---|
| 31 | /*...e*/
|
|---|
| 32 |
|
|---|
| 33 | /*...sentrypoints:0:*/
|
|---|
| 34 | #include "gbmpgm.h"
|
|---|
| 35 | #include "gbmppm.h"
|
|---|
| 36 | #include "gbmbmp.h"
|
|---|
| 37 | #include "gbmtga.h"
|
|---|
| 38 | #include "gbmkps.h"
|
|---|
| 39 | #include "gbmiax.h"
|
|---|
| 40 | #include "gbmpcx.h"
|
|---|
| 41 | #include "gbmtif.h"
|
|---|
| 42 | #include "gbmlbm.h"
|
|---|
| 43 | #include "gbmvid.h"
|
|---|
| 44 | #include "gbmgif.h"
|
|---|
| 45 | #include "gbmxbm.h"
|
|---|
| 46 | #include "gbmspr.h"
|
|---|
| 47 | #include "gbmpsg.h"
|
|---|
| 48 | #include "gbmgem.h"
|
|---|
| 49 | #include "gbmcvp.h"
|
|---|
| 50 | #include "gbmjpg.h"
|
|---|
| 51 |
|
|---|
| 52 | /*...vgbmpgm\46\h:0:*/
|
|---|
| 53 | /*...vgbmppm\46\h:0:*/
|
|---|
| 54 | /*...vgbmbmp\46\h:0:*/
|
|---|
| 55 | /*...vgbmtga\46\h:0:*/
|
|---|
| 56 | /*...vgbmkps\46\h:0:*/
|
|---|
| 57 | /*...vgbmiax\46\h:0:*/
|
|---|
| 58 | /*...vgbmpcx\46\h:0:*/
|
|---|
| 59 | /*...vgbmtif\46\h:0:*/
|
|---|
| 60 | /*...vgbmlbm\46\h:0:*/
|
|---|
| 61 | /*...vgbmvid\46\h:0:*/
|
|---|
| 62 | /*...vgbmgif\46\h:0:*/
|
|---|
| 63 | /*...vgbmxbm\46\h:0:*/
|
|---|
| 64 | /*...vgbmspr\46\h:0:*/
|
|---|
| 65 | /*...vgbmpsg\46\h:0:*/
|
|---|
| 66 | /*...vgbmgem\46\h:0:*/
|
|---|
| 67 | /*...vgbmcvp\46\h:0:*/
|
|---|
| 68 | /*...vgbmjpg\46\h:0:*/
|
|---|
| 69 |
|
|---|
| 70 | typedef struct
|
|---|
| 71 | {
|
|---|
| 72 | GBM_ERR (*query_filetype)(GBMFT *gbmft);
|
|---|
| 73 | GBM_ERR (*read_header )(const char *fn, int fd, GBM *gbm, const char *opt);
|
|---|
| 74 | GBM_ERR (*read_palette)(int fd, GBM *gbm, GBMRGB *gbmrgb);
|
|---|
| 75 | GBM_ERR (*read_data )(int fd, GBM *gbm, byte *data);
|
|---|
| 76 | GBM_ERR (*write )(const char *fn, int fd, const GBM *gbm, const GBMRGB *gbmrgb, const byte *data, const char *opt);
|
|---|
| 77 | const char * (*err )(GBM_ERR rc);
|
|---|
| 78 | } FT;
|
|---|
| 79 |
|
|---|
| 80 | static FT fts[] =
|
|---|
| 81 | {
|
|---|
| 82 | bmp_qft, bmp_rhdr, bmp_rpal, bmp_rdata, bmp_w, bmp_err,
|
|---|
| 83 | gif_qft, gif_rhdr, gif_rpal, gif_rdata, gif_w, gif_err,
|
|---|
| 84 | pcx_qft, pcx_rhdr, pcx_rpal, pcx_rdata, pcx_w, pcx_err,
|
|---|
| 85 | tif_qft, tif_rhdr, tif_rpal, tif_rdata, tif_w, tif_err,
|
|---|
| 86 | tga_qft, tga_rhdr, tga_rpal, tga_rdata, tga_w, tga_err,
|
|---|
| 87 | lbm_qft, lbm_rhdr, lbm_rpal, lbm_rdata, lbm_w, lbm_err,
|
|---|
| 88 | vid_qft, vid_rhdr, vid_rpal, vid_rdata, vid_w, vid_err,
|
|---|
| 89 | pgm_qft, pgm_rhdr, pgm_rpal, pgm_rdata, pgm_w, pgm_err,
|
|---|
| 90 | ppm_qft, ppm_rhdr, ppm_rpal, ppm_rdata, ppm_w, ppm_err,
|
|---|
| 91 | kps_qft, kps_rhdr, kps_rpal, kps_rdata, kps_w, kps_err,
|
|---|
| 92 | iax_qft, iax_rhdr, iax_rpal, iax_rdata, iax_w, iax_err,
|
|---|
| 93 | xbm_qft, xbm_rhdr, xbm_rpal, xbm_rdata, xbm_w, xbm_err,
|
|---|
| 94 | spr_qft, spr_rhdr, spr_rpal, spr_rdata, spr_w, spr_err,
|
|---|
| 95 | psg_qft, psg_rhdr, psg_rpal, psg_rdata, psg_w, psg_err,
|
|---|
| 96 | gem_qft, gem_rhdr, gem_rpal, gem_rdata, gem_w, gem_err,
|
|---|
| 97 | cvp_qft, cvp_rhdr, cvp_rpal, cvp_rdata, cvp_w, cvp_err,
|
|---|
| 98 | #ifdef IJG
|
|---|
| 99 | jpg_qft, jpg_rhdr, jpg_rpal, jpg_rdata, jpg_w, jpg_err,
|
|---|
| 100 | #endif
|
|---|
| 101 | };
|
|---|
| 102 |
|
|---|
| 103 | #define N_FT (sizeof(fts) / sizeof(fts[0]))
|
|---|
| 104 | /*...e*/
|
|---|
| 105 | /*...sextension:0:*/
|
|---|
| 106 | static const char *extension(const char *fn)
|
|---|
| 107 | {
|
|---|
| 108 | const char *dot, *slash;
|
|---|
| 109 |
|
|---|
| 110 | if ( (dot = strrchr(fn, '.')) == NULL )
|
|---|
| 111 | return NULL;
|
|---|
| 112 |
|
|---|
| 113 | if ( (slash = strpbrk(fn, "/\\")) == NULL )
|
|---|
| 114 | return dot + 1;
|
|---|
| 115 |
|
|---|
| 116 | return ( slash < dot ) ? dot + 1 : NULL;
|
|---|
| 117 | }
|
|---|
| 118 | /*...e*/
|
|---|
| 119 |
|
|---|
| 120 | /*...sgbm_init:0:*/
|
|---|
| 121 | GBMEXPORT GBM_ERR GBMENTRY gbm_init(void)
|
|---|
| 122 | {
|
|---|
| 123 | return GBM_ERR_OK;
|
|---|
| 124 | }
|
|---|
| 125 | /*...e*/
|
|---|
| 126 | /*...sgbm_deinit:0:*/
|
|---|
| 127 | GBMEXPORT GBM_ERR GBMENTRY gbm_deinit(void)
|
|---|
| 128 | {
|
|---|
| 129 | return GBM_ERR_OK;
|
|---|
| 130 | }
|
|---|
| 131 | /*...e*/
|
|---|
| 132 | /*...sgbm_io_\42\:0:*/
|
|---|
| 133 | GBMEXPORT GBM_ERR GBMENTRY gbm_io_setup(
|
|---|
| 134 | int (*open )(const char *fn, int mode),
|
|---|
| 135 | int (*create)(const char *fn, int mode),
|
|---|
| 136 | void (*close )(int fd),
|
|---|
| 137 | long (*lseek )(int fd, long pos, int whence),
|
|---|
| 138 | int (*read )(int fd, void *buf, int len),
|
|---|
| 139 | int (*write )(int fd, const void *buf, int len)
|
|---|
| 140 | )
|
|---|
| 141 | {
|
|---|
| 142 | gbm_file_open = open ;
|
|---|
| 143 | gbm_file_create = create;
|
|---|
| 144 | gbm_file_close = close ;
|
|---|
| 145 | gbm_file_lseek = lseek ;
|
|---|
| 146 | gbm_file_read = read ;
|
|---|
| 147 | gbm_file_write = write ;
|
|---|
| 148 | return GBM_ERR_OK;
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | GBMEXPORT int GBMENTRY gbm_io_open(const char *fn, int mode)
|
|---|
| 152 | { return gbm_file_open(fn, mode); }
|
|---|
| 153 | GBMEXPORT int GBMENTRY gbm_io_create(const char *fn, int mode)
|
|---|
| 154 | { return gbm_file_create(fn, mode); }
|
|---|
| 155 | GBMEXPORT void GBMENTRY gbm_io_close(int fd)
|
|---|
| 156 | { gbm_file_close(fd); }
|
|---|
| 157 | GBMEXPORT long GBMENTRY gbm_io_lseek(int fd, long pos, int whence)
|
|---|
| 158 | { return gbm_file_lseek(fd, pos, whence); }
|
|---|
| 159 | GBMEXPORT int GBMENTRY gbm_io_read (int fd, void *buf, int len)
|
|---|
| 160 | { return gbm_file_read(fd, buf, len); }
|
|---|
| 161 | GBMEXPORT int GBMENTRY gbm_io_write(int fd, const void *buf, int len)
|
|---|
| 162 | { return gbm_file_write(fd, buf, len); }
|
|---|
| 163 | /*...e*/
|
|---|
| 164 | /*...sgbm_query_n_filetypes:0:*/
|
|---|
| 165 | GBMEXPORT GBM_ERR GBMENTRY gbm_query_n_filetypes(int *n_ft)
|
|---|
| 166 | {
|
|---|
| 167 | if ( n_ft == NULL )
|
|---|
| 168 | return GBM_ERR_BAD_ARG;
|
|---|
| 169 | *n_ft = N_FT;
|
|---|
| 170 | return GBM_ERR_OK;
|
|---|
| 171 | }
|
|---|
| 172 | /*...e*/
|
|---|
| 173 | /*...sgbm_guess_filetype:0:*/
|
|---|
| 174 | GBMEXPORT GBM_ERR GBMENTRY gbm_guess_filetype(const char *fn, int *ft)
|
|---|
| 175 | {
|
|---|
| 176 | int i;
|
|---|
| 177 | const char *ext;
|
|---|
| 178 |
|
|---|
| 179 | if ( fn == NULL || ft == NULL )
|
|---|
| 180 | return GBM_ERR_BAD_ARG;
|
|---|
| 181 |
|
|---|
| 182 | if ( (ext = extension(fn)) == NULL )
|
|---|
| 183 | ext = "";
|
|---|
| 184 |
|
|---|
| 185 | for ( i = 0; i < N_FT; i++ )
|
|---|
| 186 | {
|
|---|
| 187 | GBMFT gbmft;
|
|---|
| 188 | char buf[100+1], *s;
|
|---|
| 189 |
|
|---|
| 190 | fts[i].query_filetype(&gbmft);
|
|---|
| 191 | for ( s = strtok(strcpy(buf, gbmft.extensions), " \t,");
|
|---|
| 192 | s != NULL;
|
|---|
| 193 | s = strtok(NULL, " \t,") )
|
|---|
| 194 | if ( gbm_same(s, ext, (int) strlen(ext) + 1) )
|
|---|
| 195 | {
|
|---|
| 196 | *ft = i;
|
|---|
| 197 | return GBM_ERR_OK;
|
|---|
| 198 | }
|
|---|
| 199 | }
|
|---|
| 200 | return GBM_ERR_NOT_FOUND;
|
|---|
| 201 | }
|
|---|
| 202 | /*...e*/
|
|---|
| 203 | /*...sgbm_query_filetype:0:*/
|
|---|
| 204 | GBMEXPORT GBM_ERR GBMENTRY gbm_query_filetype(int ft, GBMFT *gbmft)
|
|---|
| 205 | {
|
|---|
| 206 | if ( gbmft == NULL )
|
|---|
| 207 | return GBM_ERR_BAD_ARG;
|
|---|
| 208 | return (*fts[ft].query_filetype)(gbmft);
|
|---|
| 209 | }
|
|---|
| 210 | /*...e*/
|
|---|
| 211 | /*...sgbm_read_header:0:*/
|
|---|
| 212 | GBMEXPORT GBM_ERR GBMENTRY gbm_read_header(const char *fn, int fd, int ft, GBM *gbm, const char *opt)
|
|---|
| 213 | {
|
|---|
| 214 | if ( fn == NULL || opt == NULL || gbm == NULL )
|
|---|
| 215 | return GBM_ERR_BAD_ARG;
|
|---|
| 216 | gbm_file_lseek(fd, 0L, SEEK_SET);
|
|---|
| 217 | return (*fts[ft].read_header)(fn, fd, gbm, opt);
|
|---|
| 218 | }
|
|---|
| 219 | /*...e*/
|
|---|
| 220 | /*...sgbm_read_palette:0:*/
|
|---|
| 221 | GBMEXPORT GBM_ERR GBMENTRY gbm_read_palette(int fd, int ft, GBM *gbm, GBMRGB *gbmrgb)
|
|---|
| 222 | {
|
|---|
| 223 | if ( gbm == NULL || gbmrgb == NULL )
|
|---|
| 224 | return GBM_ERR_BAD_ARG;
|
|---|
| 225 | return (*fts[ft].read_palette)(fd, gbm, gbmrgb);
|
|---|
| 226 | }
|
|---|
| 227 | /*...e*/
|
|---|
| 228 | /*...sgbm_read_data:0:*/
|
|---|
| 229 | GBMEXPORT GBM_ERR GBMENTRY gbm_read_data(int fd, int ft, GBM *gbm, byte *data)
|
|---|
| 230 | {
|
|---|
| 231 | if ( gbm == NULL || data == NULL )
|
|---|
| 232 | return GBM_ERR_BAD_ARG;
|
|---|
| 233 | return (*fts[ft].read_data)(fd, gbm, data);
|
|---|
| 234 | }
|
|---|
| 235 | /*...e*/
|
|---|
| 236 | /*...sgbm_write:0:*/
|
|---|
| 237 | GBMEXPORT GBM_ERR GBMENTRY gbm_write(const char *fn, int fd, int ft, const GBM *gbm, const GBMRGB *gbmrgb, const byte *data, const char *opt)
|
|---|
| 238 | {
|
|---|
| 239 | if ( fn == NULL || opt == NULL )
|
|---|
| 240 | return GBM_ERR_BAD_ARG;
|
|---|
| 241 | return (*fts[ft].write)(fn, fd, gbm, gbmrgb, data, opt);
|
|---|
| 242 | }
|
|---|
| 243 | /*...e*/
|
|---|
| 244 | /*...sgbm_err:0:*/
|
|---|
| 245 | GBMEXPORT const char * GBMENTRY gbm_err(GBM_ERR rc)
|
|---|
| 246 | {
|
|---|
| 247 | int ft;
|
|---|
| 248 |
|
|---|
| 249 | switch ( (int) rc )
|
|---|
| 250 | {
|
|---|
| 251 | case GBM_ERR_OK:
|
|---|
| 252 | return "ok";
|
|---|
| 253 | case GBM_ERR_MEM:
|
|---|
| 254 | return "out of memory";
|
|---|
| 255 | case GBM_ERR_NOT_SUPP:
|
|---|
| 256 | return "not supported";
|
|---|
| 257 | case GBM_ERR_BAD_OPTION:
|
|---|
| 258 | return "bad option(s)";
|
|---|
| 259 | case GBM_ERR_NOT_FOUND:
|
|---|
| 260 | return "not found";
|
|---|
| 261 | case GBM_ERR_BAD_MAGIC:
|
|---|
| 262 | return "bad magic number / signiture block";
|
|---|
| 263 | case GBM_ERR_BAD_SIZE:
|
|---|
| 264 | return "bad bitmap size";
|
|---|
| 265 | case GBM_ERR_READ:
|
|---|
| 266 | return "can't read file";
|
|---|
| 267 | case GBM_ERR_WRITE:
|
|---|
| 268 | return "can't write file";
|
|---|
| 269 | case GBM_ERR_BAD_ARG:
|
|---|
| 270 | return "bad argument to gbm function";
|
|---|
| 271 | }
|
|---|
| 272 |
|
|---|
| 273 | for ( ft = 0; ft < N_FT; ft++ )
|
|---|
| 274 | {
|
|---|
| 275 | const char *s;
|
|---|
| 276 |
|
|---|
| 277 | if ( (s = (*fts[ft].err)(rc)) != NULL )
|
|---|
| 278 | return s;
|
|---|
| 279 | }
|
|---|
| 280 |
|
|---|
| 281 | return "general error";
|
|---|
| 282 | }
|
|---|
| 283 | /*...e*/
|
|---|
| 284 | /*...sgbm_version:0:*/
|
|---|
| 285 | GBMEXPORT int GBMENTRY gbm_version(void)
|
|---|
| 286 | {
|
|---|
| 287 | return 112; /* 1.12 */
|
|---|
| 288 | }
|
|---|
| 289 | /*...e*/
|
|---|
| 290 |
|
|---|
| 291 | #ifdef OS2
|
|---|
| 292 | /*...s_System entrypoints:0:*/
|
|---|
| 293 | /* For GBM.DLL to be callable from IBM Smalltalk under OS/2, the entrypoints
|
|---|
| 294 | must be of _System calling convention. These veneers help out here.
|
|---|
| 295 | I can't just change the usual entrypoints because people depend on them. */
|
|---|
| 296 |
|
|---|
| 297 | GBM_ERR _System Gbm_init(void)
|
|---|
| 298 | { return gbm_init(); }
|
|---|
| 299 |
|
|---|
| 300 | GBM_ERR _System Gbm_deinit(void)
|
|---|
| 301 | { return gbm_deinit(); }
|
|---|
| 302 |
|
|---|
| 303 | /* Gbm_io_setup omitted for now...
|
|---|
| 304 | Implies GBM.DLL must call out to non-_Optlink callback routines. */
|
|---|
| 305 |
|
|---|
| 306 | int _System Gbm_io_open(const char *fn, int mode)
|
|---|
| 307 | { return gbm_io_open(fn, mode); }
|
|---|
| 308 |
|
|---|
| 309 | int _System Gbm_io_create(const char *fn, int mode)
|
|---|
| 310 | { return gbm_io_create(fn, mode); }
|
|---|
| 311 |
|
|---|
| 312 | void _System Gbm_io_close(int fd)
|
|---|
| 313 | { gbm_io_close(fd); }
|
|---|
| 314 |
|
|---|
| 315 | long _System Gbm_io_lseek(int fd, long pos, int whence)
|
|---|
| 316 | { return gbm_file_lseek(fd, pos, whence); }
|
|---|
| 317 |
|
|---|
| 318 | int _System Gbm_io_read (int fd, void *buf, int len)
|
|---|
| 319 | { return gbm_file_read(fd, buf, len); }
|
|---|
| 320 |
|
|---|
| 321 | int _System Gbm_io_write(int fd, const void *buf, int len)
|
|---|
| 322 | { return gbm_file_write(fd, buf, len); }
|
|---|
| 323 |
|
|---|
| 324 | GBM_ERR _System Gbm_query_n_filetypes(int *n_ft)
|
|---|
| 325 | { return gbm_query_n_filetypes(n_ft); }
|
|---|
| 326 |
|
|---|
| 327 | GBM_ERR _System Gbm_guess_filetype(const char *fn, int *ft)
|
|---|
| 328 | { return gbm_guess_filetype(fn, ft); }
|
|---|
| 329 |
|
|---|
| 330 | GBM_ERR _System Gbm_query_filetype(int ft, GBMFT *gbmft)
|
|---|
| 331 | { return gbm_query_filetype(ft, gbmft); }
|
|---|
| 332 |
|
|---|
| 333 | GBM_ERR _System Gbm_read_header(const char *fn, int fd, int ft, GBM *gbm, const char *opt)
|
|---|
| 334 | { return gbm_read_header(fn, fd, ft, gbm, opt); }
|
|---|
| 335 |
|
|---|
| 336 | GBM_ERR _System Gbm_read_palette(int fd, int ft, GBM *gbm, GBMRGB *gbmrgb)
|
|---|
| 337 | { return gbm_read_palette(fd, ft, gbm, gbmrgb); }
|
|---|
| 338 |
|
|---|
| 339 | GBM_ERR _System Gbm_read_data(int fd, int ft, GBM *gbm, byte *data)
|
|---|
| 340 | { return gbm_read_data(fd, ft, gbm, data); }
|
|---|
| 341 |
|
|---|
| 342 | GBM_ERR _System Gbm_write(const char *fn, int fd, int ft, const GBM *gbm, const GBMRGB *gbmrgb, const byte *data, const char *opt)
|
|---|
| 343 | { return gbm_write(fn, fd, ft, gbm, gbmrgb, data, opt); }
|
|---|
| 344 |
|
|---|
| 345 | const char * _System Gbm_err(GBM_ERR rc)
|
|---|
| 346 | { return gbm_err(rc); }
|
|---|
| 347 |
|
|---|
| 348 | int _System Gbm_version(void)
|
|---|
| 349 | { return gbm_version(); }
|
|---|
| 350 | /*...e*/
|
|---|
| 351 | #endif
|
|---|