home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 March / VPR9703A.ISO / VPR_DATA / DOGA / SOURCES / REND.LZH / PIC1600 / FILE.C < prev    next >
C/C++ Source or Header  |  1996-07-16  |  3KB  |  146 lines

  1. #include    <stdio.h>
  2. #include    <stdlib.h>
  3. #ifdef X68K
  4. #include    <doslib.h>
  5. #endif
  6. #include    <ctype.h>
  7. #include    <string.h>
  8. #include    "piclib.h"
  9. #ifndef SEEK_SET
  10.     #define SEEK_SET 0
  11. #endif
  12.  
  13. extern PicFunction PicFunction_16_16;
  14. extern PicFunction PicFunction_25_7;
  15. extern PicFunction PicFunction_RGB888;
  16. extern PicFunction PicFunction_RGBT8888;
  17. extern PicFunction PicFunction_PIC;
  18. extern PicFunction PicFunction_BMP;
  19. #ifdef X68K
  20. extern PicFunction PicFunction_Display;
  21. #endif
  22.  
  23. PicFunction *function[] = {
  24. #ifdef X68K
  25.     &PicFunction_Display,
  26. #endif
  27.     &PicFunction_16_16,
  28.     &PicFunction_25_7,
  29.     &PicFunction_RGB888,
  30.     &PicFunction_RGBT8888,
  31.     &PicFunction_PIC,
  32.     &PicFunction_BMP,
  33. };
  34.  
  35. INLINE    void    putlong(int i, FILE *fp)
  36. {
  37.     fputc( i >> 24        , fp);
  38.     fputc((i >> 16) & 0xff, fp);
  39.     fputc((i >>  8) & 0xff, fp);
  40.     fputc( i        & 0xff, fp);
  41. }
  42.  
  43. PicData    *PicWriteOpen(char *filename, int sizex, int sizey, int mode)
  44. {
  45.     PicData    *pd;
  46.     FILE *fp;
  47.     int i;
  48.     char *ext;
  49.     if ((ext = strrchr(filename, '.')) == NULL) {
  50.         ext = filename + strlen(filename);
  51.     } else {
  52.         ext++;
  53.     }
  54.  
  55.     if (pd = malloc(sizeof(PicData)), pd == NULL) {
  56.         return NULL;
  57.     }
  58.     pd->flag = PIC_WRITE;
  59.     pd->pixelX = (unsigned short)sizex;
  60.     pd->pixelY = (unsigned short)sizey;
  61.     strcpy(pd->filename, filename);
  62.     pd->func = NULL;
  63.  
  64. #ifdef X68K
  65.     if (strncmp(filename, "_DISPLAY_", 9) == 0) {
  66.         pd->func = &PicFunction_Display;
  67.         pd->func->PicOpen(pd, NULL, mode);
  68.         return pd;
  69.     }
  70. #endif
  71.     if (fp = fopen(filename, "wb"), fp == NULL) {
  72.         free(pd);
  73.         return FALSE;
  74.     }
  75.     for (i = 0; i < (sizeof(function)/sizeof(function[0])); ++i) {
  76. /*        if (function[i]->ext[0] && strcmpi(function[i]->ext, ext) == 0) {*/
  77.         if (function[i]->ext[0]) {
  78.             char *pext, fext[8];
  79.             int j;
  80.             pext = function[i]->ext;
  81.             j = 0;
  82.             while (1) {
  83.                 if (*pext == '\0' || *pext == '|') {
  84.                     fext[j] = '\0';
  85.                     if (fext[0]
  86.                      && toupper(fext[0]) == toupper(ext[0])
  87.                      && toupper(fext[1]) == toupper(ext[1])
  88.                      && (ext[1] == '\0' || toupper(fext[2]) == toupper(ext[2]))) {
  89.                         pd->func = function[i];
  90.                         break;
  91.                     }
  92.                     j = 0;
  93.                     if (*pext == '\0') break;
  94.                 } else {
  95.                     fext[j++] = *pext;
  96.                 }
  97.                 pext++;
  98.             }
  99.             if (pd->func != NULL) break;
  100.         }
  101.     }
  102.     if (pd->func == NULL) {
  103.         free(pd);
  104.         return FALSE;
  105.     }
  106.     pd->func->PicOpen(pd, fp, mode);
  107.     return pd;
  108. }
  109.  
  110.  
  111. PicData    *PicReadOpen(char *filename)
  112. {
  113.     FILE *fp;
  114.     PicData    *pd;
  115.     int i;
  116.     unsigned char magic[32];
  117.  
  118.     if (pd = malloc(sizeof(PicData)), pd == NULL) {
  119.         return NULL;
  120.     }
  121.     if (fp = fopen(filename, "rb"), fp != NULL) {
  122.         fread(magic, 1, 32, fp);
  123.         fseek(fp, 0, SEEK_SET);
  124.     }
  125.  
  126.     strcpy(pd->filename, filename);
  127.     pd->flag = PIC_READ;
  128.     pd->fullcolor = TRUE;
  129.  
  130.     for (i = 0; i < (sizeof(function)/sizeof(function[0])); ++i) {
  131.         if (function[i]->PicReadHeader(pd, fp, magic) != FALSE) {
  132.             break;
  133.         }
  134.     }
  135.     if (i == (sizeof(function)/sizeof(function[0]))) {
  136.         free(pd);
  137.         return NULL;
  138.     }
  139.     if (pd->func->PicOpen(pd, fp, 0) == FALSE) {
  140.         free(pd);
  141.         return NULL;
  142.     }
  143.     return pd;
  144. }
  145.  
  146.