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

  1. #include    <stdio.h>
  2. #include    <stdlib.h>
  3. #include    <ctype.h>
  4. #include    <string.h>
  5. #include    "piclib.h"
  6.  
  7. typedef struct {
  8.     FILE *fp;
  9.     unsigned long buffer[BUFFERSIZE];
  10.     unsigned int nowpoint;
  11. }    PicWork;
  12.  
  13.  
  14. #ifdef LittleEndian
  15. #define r_short(p)        (((unsigned char*)(p))[0] * 256 + ((unsigned char*)(p))[1])
  16. #define w_short(p,n)    ((((unsigned char*)(p))[0] = n/256), (((unsigned char*)(p))[1] = n%256))
  17. #define r_long(p)        ((((unsigned char*)(p))[0] << 24) |\
  18.                          (((unsigned char*)(p))[1] << 16) |\
  19.                          (((unsigned char*)(p))[2] <<  8) |\
  20.                          (((unsigned char*)(p))[3]      ))
  21. #define w_long(p,n)        ((((unsigned char*)(p))[0] = ((n) >> 24)),\
  22.                          (((unsigned char*)(p))[1] = ((n) >> 16)),\
  23.                          (((unsigned char*)(p))[2] = ((n) >>  8)),\
  24.                          (((unsigned char*)(p))[3] =  (n)       ))
  25. #if 0
  26. #define r_long(p)        (r_short(p) * 65536 + r_short(((unsigned char*)p)+2))
  27. #define w_long(p,n)        (w_short(p, n/65536), w_short(((unsigned char*)p)+2, n%65536))
  28. #endif
  29. #else
  30. #define r_short(p)         (*((unsigned short *)(p)))
  31. #define w_short(p,n)    ((*((unsigned short *)(p))) = (n))
  32. #define r_long(p)        (*((unsigned long *)(p)))
  33. #define w_long(p,n)        ((*((unsigned long *)(p))) = (n))
  34. #endif
  35.  
  36. static int Get4Hex(char *p)
  37. {
  38.     int i;
  39.     int code = 0;;
  40.     for (i = 4; i > 0; --i, ++p) {
  41.         code *= 16;
  42.         if (isdigit(*p)) {
  43.             code += *p - '0';
  44.         } else if (isalpha(*p)) {
  45.             code += tolower(*p) - 'a';
  46.         }
  47.     }
  48.     return code;
  49. }
  50.  
  51. int        PicReadHeader_RGBT8888(PicData *pd, FILE *fp, unsigned char *magic)
  52. {
  53.     char header[1024];
  54.     extern PicFunction PicFunction_RGBT8888;
  55.     if (magic[0] != 0xff || magic[1] != 0xff || magic[2] != 0xff || magic[3] != 0xf0) {
  56.         return FALSE;
  57.     }
  58.     fread(header, 1, 1024, fp);
  59.     pd->mode = NON;
  60.     pd->pixelX = Get4Hex(header+20);    pd->pixelY = Get4Hex(header+24);
  61.     pd->func = &PicFunction_RGBT8888;
  62.     return TRUE;
  63. }
  64.  
  65.  
  66. static inline Pixel Convert_RGBT8888_32(unsigned long color)
  67. {
  68.     return ((color & 0xff000000UL) >> 8)
  69.          | ((color & 0x00ff0000UL) << 8)
  70.          | ((color & 0x0000ffffUL)     ) ;
  71. }
  72.  
  73. int        PicOpen_RGBT8888(PicData *pd, FILE *fp, int flag)
  74. {
  75.     PicWork *work;
  76.     if ((work = malloc(sizeof(PicWork))) == NULL) {
  77.         return FALSE;
  78.     }
  79.     pd->work = work;
  80.     work->fp = fp;
  81.     if (pd->flag == PIC_READ) {
  82.         fread(work->buffer, 1, BUFFERSIZE*sizeof(long), work->fp);
  83.         work->nowpoint = 0;
  84.     } else {
  85. /* Not Implemented*/
  86.         return FALSE;
  87.     }
  88.     return TRUE;
  89. }
  90.  
  91. int        PicOutput_RGBT8888(PicData *pd, Pixel *pixel)
  92. {
  93. /* Not Implemented*/
  94.     return FALSE;
  95. }
  96.  
  97. int        PicInput_RGBT8888(PicData *pd, Pixel *pixel)
  98. {
  99.     int pixels = pd->pixelX;
  100.     PicWork *work = pd->work;
  101.     unsigned long *nowp;
  102.     nowp = work->buffer+work->nowpoint;
  103.     work->nowpoint += pixels;
  104.     for (; pixels > 0; --pixels) {
  105.         *pixel++ = Convert_RGBT8888_32(r_long(nowp));
  106.         nowp++;
  107.     }
  108.     if (work->nowpoint > BUFFERSIZE - pd->pixelX) {
  109.         int rest = BUFFERSIZE - work->nowpoint;
  110.         memcpy(work->buffer, work->buffer+work->nowpoint, rest);
  111.         fread(work->buffer+rest, 1, (work->nowpoint)*sizeof(long), work->fp);
  112.         work->nowpoint = 0;
  113.     }
  114.     return TRUE;
  115. }
  116.  
  117. int        PicClose_RGBT8888(PicData *pd)
  118. {
  119.     PicWork *work = pd->work;
  120.     if (pd->flag == PIC_WRITE) {
  121. /* Not Implemented*/
  122.     }
  123.     fclose(work->fp);
  124.     free(work);
  125.     return TRUE;
  126. }
  127.  
  128. PicFunction PicFunction_RGBT8888 = {
  129.     "",
  130.     PicReadHeader_RGBT8888,
  131.     PicOpen_RGBT8888,
  132.     PicOutput_RGBT8888,
  133.     PicInput_RGBT8888,
  134.     PicClose_RGBT8888
  135. };
  136.  
  137.  
  138.  
  139.