home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / gbmsrc.zip / gbmcvp.c < prev    next >
C/C++ Source or Header  |  1996-12-04  |  3KB  |  146 lines

  1. /*
  2.  
  3. gbmcvp.c - Portrait
  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. #include <memory.h>
  14. #include <malloc.h>
  15. #include "gbm.h"
  16. #include "gbmhelp.h"
  17.  
  18. /*...vgbm\46\h:0:*/
  19. /*...vgbmhelp\46\h:0:*/
  20. /*...e*/
  21.  
  22. static GBMFT cvp_gbmft =
  23.     {
  24.     "Portrait",
  25.     "Portrait",
  26.     "CVP",
  27.     GBM_FT_R24|
  28.     GBM_FT_W24,
  29.     };
  30.  
  31. #define    GBM_ERR_CVP_FSIZE    ((GBM_ERR) 1800)
  32. #define    GBM_ERR_CVP_SIZE    ((GBM_ERR) 1801)
  33.  
  34. /*...scvp_qft:0:*/
  35. GBM_ERR cvp_qft(GBMFT *gbmft)
  36.     {
  37.     *gbmft = cvp_gbmft;
  38.     return GBM_ERR_OK;
  39.     }
  40. /*...e*/
  41. /*...scvp_rhdr:0:*/
  42. GBM_ERR cvp_rhdr(const char *fn, int fd, GBM *gbm, const char *opt)
  43.     {
  44.     long length;
  45.  
  46.     fn=fn; fd=fd; opt=opt; /* Suppress 'unref arg' compiler warnings */
  47.  
  48.     length = gbm_file_lseek(fd, 0L, SEEK_END);
  49.     gbm_file_lseek(fd, 0L, SEEK_SET);
  50.  
  51.     if ( length != 512*512*3 )
  52.         return GBM_ERR_CVP_FSIZE;
  53.  
  54.     gbm->w   = 512;
  55.     gbm->h   = 512;
  56.     gbm->bpp = 24;
  57.  
  58.     return GBM_ERR_OK;
  59.     }
  60. /*...e*/
  61. /*...scvp_rpal:0:*/
  62. GBM_ERR cvp_rpal(int fd, GBM *gbm, GBMRGB *gbmrgb)
  63.     {
  64.     fd=fd; gbm=gbm; gbmrgb=gbmrgb; /* Suppress 'unref arg' compiler warnings */
  65.  
  66.     return GBM_ERR_OK;
  67.     }
  68. /*...e*/
  69. /*...scvp_rdata:0:*/
  70. GBM_ERR cvp_rdata(int fd, GBM *gbm, byte *data)
  71.     {
  72.     int p, stride = ((gbm->w*3+3)&~3);
  73.     byte *line;
  74.     if ( (line = malloc((size_t) gbm->w)) == NULL )
  75.         return GBM_ERR_MEM;
  76.     for ( p = 2; p >= 0; p-- )
  77.         {
  78.         int y;
  79.         byte *ptr = data + ( (stride * (gbm->h-1)) + p );
  80.         for ( y = 0; y < gbm->h; y++, ptr-=stride )
  81.             {
  82.             int x;
  83.             if ( gbm_file_read(fd, line, gbm->w) != gbm->w )
  84.                 {
  85.                 free(line);
  86.                 return GBM_ERR_READ;
  87.                 }
  88.             for ( x = 0; x < gbm->w; x++ )
  89.                 ptr[x*3] = line[gbm->w-1-x];
  90.             }
  91.         }
  92.     free(line);
  93.     return GBM_ERR_OK;
  94.     }
  95. /*...e*/
  96. /*...scvp_w:0:*/
  97. GBM_ERR cvp_w(const char *fn, int fd, const GBM *gbm, const GBMRGB *gbmrgb, const byte *data, const char *opt)
  98.     {
  99.     int p, stride = ((gbm->w*3+3)&~3);
  100.     byte *line;
  101.  
  102.     fn=fn; gbmrgb=gbmrgb; opt=opt; /* Suppress 'unref arg' compiler warning */
  103.  
  104.     if ( gbm->bpp != 24 )
  105.         return GBM_ERR_NOT_SUPP;
  106.  
  107.     if ( gbm->w != 512 || gbm->h != 512 )
  108.         return GBM_ERR_CVP_SIZE;
  109.  
  110.     if ( (line = malloc((size_t) gbm->w)) == NULL )
  111.         return GBM_ERR_MEM;
  112.  
  113.     for ( p = 2; p >= 0; p-- )
  114.         {
  115.         int y;
  116.         const byte *ptr = data + ( (stride * (gbm->h-1)) + p );
  117.         for ( y = 0; y < gbm->h; y++, ptr-=stride )
  118.             {
  119.             int x;
  120.             for ( x = 0; x < gbm->w; x++ )
  121.                 line[gbm->w-1-x] = ptr[x*3];
  122.             if ( gbm_file_write(fd, line, gbm->w) != gbm->w )
  123.                 {
  124.                 free(line);
  125.                 return GBM_ERR_WRITE;
  126.                 }
  127.             }
  128.         }
  129.     free(line);
  130.     return GBM_ERR_OK;
  131.     }
  132. /*...e*/
  133. /*...scvp_err:0:*/
  134. const char *cvp_err(GBM_ERR rc)
  135.     {
  136.     switch ( (int) rc )
  137.         {
  138.         case GBM_ERR_CVP_FSIZE:
  139.             return "file is not correct size";
  140.         case GBM_ERR_CVP_SIZE:
  141.             return "portrait files can only hold 512x512 images";
  142.         }
  143.     return NULL;
  144.     }
  145. /*...e*/
  146.