home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD1.iso / CDRom / PHOTOC12.LZX / src / photocdaga / wrdisplay.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-18  |  9.9 KB  |  424 lines

  1. /* AGA output driver */
  2. /* written by Günther Röhrich */
  3.  
  4.  
  5. #include "hpcdtoppm.h"
  6. #include <stdlib.h>
  7. #include <signal.h>
  8. #include <string.h>
  9.  
  10. #define HAM8 1
  11.  
  12. int VGAenable = 0;
  13. int DisplayOriginal = 0;
  14. int OverView = 0;
  15. volatile unsigned long rstep, gstep, bstep;
  16. static int GrayEnable=0;
  17. static int PictureNr=0;
  18. static uBYTE *row;
  19. static FILE *ColorMapFile=NULL;
  20. extern char *MapFileName;
  21. extern char *BaseName;
  22. extern sINT bildnr;
  23.  
  24. extern int InitDisplay(int cols, int rows, unsigned long Mode, int NumPlanes);
  25. extern void SetDisplayColor(int ColorNumber, unsigned char r, unsigned char g, unsigned char b);
  26. extern void CloseDisplay(void);
  27. extern void DisplayRow(char *array, int cols);
  28. extern void DisplayRowPos(char *array, int cols, int xpos, int ypos);
  29. extern int CheckButton(void);
  30. extern void FinalWait(void);
  31. uBYTE *OutputBuffer=NULL;
  32.  
  33. extern void EncodeHAM8(char *rorig, char *gorig, char *borig, char *yham, int xsize);
  34. unsigned short Mult_Table[2*256];
  35. unsigned long *ScreenColorTable=NULL;
  36.  
  37. /* NOTE: this array is in brgbrg order */
  38. /* when a mapfile is available it will be overwritten */
  39.  
  40. char *ColorCache;
  41. unsigned char ColorTable[64*3] =
  42.  { 0, 0, 0,  4, 4, 4,  8, 8, 8, 12,12,12,   
  43.   16,16,16, 20,20,20, 24,24,24, 28,28,28,  /* 16 colors */
  44.   32,32,32, 36,36,36, 41,41,41, 46,46,46,
  45.   51,51,51, 55,55,55, 59,59,59, 63,63,63,
  46.  
  47.  
  48.                       17,17,39, 17,17,55, /* 13 colors */ 
  49.   17,29,17,           17,29,39, 17,29,55, 
  50.   17,39,17, 17,39,29, 17,39,39, 17,39,55,
  51.   17,55,17, 17,55,39, 17,55,39, 17,55,55,
  52.  
  53.  
  54.             29,17,29, 29,17,39, 29,17,55, /* 11 colors */
  55.                                 29,29,55,
  56.   29,39,17, 29,39,29,           29,39,55,
  57.   29,55,17, 29,55,29, 29,55,39, 29,55,55,
  58.  
  59.  
  60.   39,17,17, 39,17,29, 39,17,39, 39,17,55, /* 12 colors */
  61.   39,29,17, 39,29,29,           39,29,55,
  62.   39,39,17, 39,39,29,          
  63.   39,55,17, 39,55,29,  
  64.  
  65.   
  66.   55,17,17, 55,17,29, 55,17,39, 55,17,55, /* 13 colors */
  67.   55,29,27, 55,29,29, 55,29,39, 55,29,55,
  68.   55,39,17, 55,39,29, 55,39,39, 
  69.   55,55,17, 55,55,29
  70. };
  71.  
  72.  
  73.  
  74.  
  75. void write_dcolor(FILE *fout,dim w,dim h, 
  76.                uBYTE *rptr,sdim rzeil,sdim rpix,  
  77.                uBYTE *gptr,sdim gzeil,sdim gpix,  
  78.                uBYTE *bptr,sdim bzeil,sdim bpix) 
  79. {
  80.   register uBYTE *pr,*pg,*pb;
  81.   dim y;
  82.   int DisplaySuccess,i;
  83.   char *MapDir;
  84.  
  85.   strcat(MapFileName, ".map");
  86.   ColorMapFile = fopen(MapFileName, "r");
  87.   /* try to find the file in the directory pointed to by the environment */
  88.   /* variable MAPDIR to support read-only devices like CD-ROM */
  89.   if(!ColorMapFile)
  90.   {
  91.     MapDir = getenv("MAPDIR");
  92.     if(MapDir)
  93.      if(strlen(MapDir) != 0)
  94.     {
  95.       char *MapDirName;
  96.       int pos,i;
  97.       MapDirName = malloc(strlen(MapDir)+strlen(MapFileName)+5); /* worst case */
  98.       if(!MapDirName) error(E_MEM);
  99.       strcpy(MapDirName, MapDir);
  100.  
  101.       i = strlen(MapDirName);
  102.       if(MapDirName[i-1] != '/' && MapDirName[i-1] != ':')
  103.       {
  104.         strcat(MapDirName, "/");
  105.         i++;
  106.       }
  107.       i = strlen(MapFileName);
  108.       while(i > 0 && MapFileName[i-1] != '/' && MapFileName[i-1] != ':') i--;
  109.       strcat(MapDirName, &MapFileName[i]);
  110.       /* printf("%s\n", MapDirName); */
  111.       ColorMapFile = fopen(MapDirName, "r");
  112.       free(MapDirName);     
  113.     }
  114.   }
  115.  
  116.  
  117.  
  118.   if(!ColorMapFile)
  119.   {
  120.     int i = strlen(MapFileName) - 4;
  121.     while(i > 0 && MapFileName[i-1] != '.') i--;
  122.     if(MapFileName[i-1] == '.')
  123.     {
  124.       strcpy(&MapFileName[i], "map");
  125.       /* printf("%s\n", MapFileName); */
  126.       ColorMapFile = fopen(MapFileName, "r");
  127.     }
  128.   }
  129.  
  130.   if(!ColorMapFile && BaseName && MapDir)
  131.    if(strlen(MapDir) != 0)
  132.   {
  133.     char *NewName;
  134.     int i;
  135.     NewName = malloc(strlen(MapDir)+strlen(BaseName)+strlen(MapFileName));
  136.     if(!NewName) error(E_MEM);
  137.     strcpy(NewName, MapDir);
  138.     i = strlen(NewName);
  139.     if(NewName[i-1] != '/' && NewName[i-1] != ':')
  140.     {
  141.       strcat(NewName, "/");
  142.       i++;
  143.     }
  144.     strcat(NewName, BaseName);
  145.     i = strlen(MapFileName);
  146.     while(i > 0 && MapFileName[i-1] != '/' && MapFileName[i-1] != ':') i--;
  147.     if(strlen(MapFileName) > i+3)
  148.     {
  149.       strcat(NewName, &MapFileName[i+3]);
  150.       /* printf("%s\n", NewName); */
  151.       ColorMapFile = fopen(NewName, "r");
  152.     }
  153.     free(NewName);
  154.   }
  155.   
  156.  
  157.   if(ColorMapFile)
  158.   {
  159.     unsigned short MagicNumber;
  160.     unsigned int Reserved;
  161.  
  162.     if(fread(&MagicNumber, 2, 1, ColorMapFile) != 1) error(E_MAPFILE);
  163.     if(MagicNumber != 0x1203) error(E_MAPFILE);    
  164.     if(fread(&Reserved,    4, 1, ColorMapFile) != 1) error(E_MAPFILE);
  165.     if(fread(ColorTable,  64*3, 1, ColorMapFile) != 1) error(E_MAPFILE);
  166.  
  167.     fclose(ColorMapFile);
  168.     ColorMapFile = NULL;
  169.   }
  170.   else
  171.   {
  172.     printf("Create a colormap file for better quality!\n");
  173.   }
  174.  
  175.   OutputBuffer = malloc(((w+15)>>4)<<4);
  176.   if(!OutputBuffer) error(E_MEM);
  177.  
  178.   /* create the multiplication table */
  179.   for(i=-255; i<256; i++) Mult_Table[i+255] = (unsigned short)(i*i);
  180.  
  181.   ColorCache = calloc(262145, 1);
  182.   if(ColorCache == NULL) error(E_MEM);
  183.  
  184.   rstep = rpix; gstep = gpix; bstep = bpix;
  185.  
  186. #ifdef __GNUC__
  187.   signal(SIGINT, SIG_IGN); /* disable CTRL-C handling from now on */
  188. #endif
  189.  
  190.   ScreenColorTable = malloc(64*4*3+8);
  191.   if(!ScreenColorTable) error(E_MEM);
  192.  
  193.   for(i=0; i<64; i++)
  194.   {
  195.     ScreenColorTable[i*3+1] = (unsigned long)ColorTable[i*3+1]<<26; 
  196.     ScreenColorTable[i*3+2] = (unsigned long)ColorTable[i*3+2]<<26;
  197.     ScreenColorTable[i*3+3] = (unsigned long)ColorTable[i*3]<<26;
  198.   }
  199.  
  200.   ScreenColorTable[0] = 64L<<16+0;
  201.   ScreenColorTable[64*3+1] = 0;
  202.  
  203.   DisplaySuccess = InitDisplay(w, h, HAM8, 8); 
  204.   if(DisplaySuccess != 1)
  205.   {
  206.       CloseDisplay();
  207.       error(E_DISPLAY);
  208.   }
  209.   
  210.   for(y=0;y<h;y++)
  211.   {
  212.     pr= rptr; rptr+=rzeil;
  213.     pg= gptr; gptr+=gzeil;
  214.     pb= bptr; bptr+=bzeil;
  215.  
  216.     EncodeHAM8((char *)pr, (char *)pg, (char *)pb, OutputBuffer, w);
  217.     DisplayRow(OutputBuffer, w);
  218.     if(CheckButton())
  219.     {
  220.       CloseDisplay();
  221.       error(E_ABORT);
  222.     }
  223.   }
  224.  
  225.   FinalWait();
  226.   CloseDisplay();
  227. }
  228.  
  229.  
  230. void write_dgray(FILE *fout,dim w,dim h, uBYTE *ptr,sdim zeil,sdim pix) 
  231.  {register uBYTE *p;
  232.   dim y,x;
  233.   uBYTE *row;
  234.   uBYTE *rowptr;
  235.   int DisplaySuccess;
  236.   unsigned long i;
  237.  
  238.  
  239.   row = malloc(((w+15)>>4)<<4);
  240.   if(row == NULL) error(E_MEM);
  241.  
  242. #ifdef __GNUC__
  243.   signal(SIGINT, SIG_IGN); /* disable CTRL-C handling from now on */
  244. #endif
  245.  
  246.   ScreenColorTable = malloc(256*4*3+8);
  247.   if(!ScreenColorTable) error(E_MEM);
  248.  
  249.   for(i=0; i<256; i++)
  250.   {
  251.     ScreenColorTable[i*3+1] = (unsigned long)i<<24L;
  252.     ScreenColorTable[i*3+2] = (unsigned long)i<<24L;
  253.     ScreenColorTable[i*3+3] = (unsigned long)i<<24L;
  254.   }
  255.  
  256.   ScreenColorTable[0] = 256L<<16+0;
  257.   ScreenColorTable[256*3+1] = 0;
  258.  
  259.   DisplaySuccess = InitDisplay(w, h, 0, 8);
  260.  
  261.   if(DisplaySuccess != 1)
  262.   {
  263.     CloseDisplay();
  264.     error(E_DISPLAY);
  265.   }
  266.  
  267.   for(y=0; y<h; y++)
  268.   {
  269.     p = ptr; ptr+=zeil; rowptr = row;
  270.  
  271.     for(x=0; x<w; x++)
  272.     {
  273.       *rowptr++ = *p;
  274.       p+=pix;
  275.     }
  276.     DisplayRow(row, w);
  277.     if(CheckButton())
  278.     {
  279.       CloseDisplay();
  280.       error(E_ABORT);
  281.     } 
  282.   }
  283.   FinalWait();
  284.   CloseDisplay();
  285. }
  286.  
  287. void write_dover_color(FILE *fout,dim w,dim h, 
  288.                uBYTE *rptr,sdim rzeil,sdim rpix,  
  289.                uBYTE *gptr,sdim gzeil,sdim gpix,  
  290.                uBYTE *bptr,sdim bzeil,sdim bpix) 
  291. {
  292.   register uBYTE *pr,*pg,*pb;
  293.   dim y;
  294.   int DisplaySuccess, i;
  295.  
  296.   if(PictureNr == 0)
  297.   {
  298.     OutputBuffer = malloc(((w+15)>>4)<<4);
  299.     if(!OutputBuffer) error(E_MEM);
  300.  
  301.     /* create the multiplication table */
  302.     for(i=-255; i<256; i++) Mult_Table[i+255] = (unsigned short)(i*i);
  303.  
  304.     ColorCache = calloc(262145, 1);
  305.     if(ColorCache == NULL) error(E_MEM);
  306.     
  307.     #ifdef __GNUC__
  308.       signal(SIGINT, SIG_IGN); /* disable CTRL-C handling from now on */
  309.     #endif
  310.  
  311.  
  312.     ScreenColorTable = malloc(64*4*3+8);
  313.     if(!ScreenColorTable) error(E_MEM);
  314.  
  315.     for(i=0; i<64; i++)
  316.     {
  317.       ScreenColorTable[i*3+1] = (unsigned long)ColorTable[i*3+1]<<26; 
  318.       ScreenColorTable[i*3+2] = (unsigned long)ColorTable[i*3+2]<<26;
  319.       ScreenColorTable[i*3+3] = (unsigned long)ColorTable[i*3]<<26;
  320.     }
  321.  
  322.     ScreenColorTable[0] = 64L<<16+0;
  323.     ScreenColorTable[64*3+1] = 0;
  324.  
  325.     OverView = 1;
  326.     DisplaySuccess = InitDisplay(772, 516, HAM8, 8); 
  327.     if(DisplaySuccess != 1)
  328.     {
  329.         CloseDisplay();
  330.         error(E_DISPLAY);
  331.     }
  332.   }
  333.  
  334.  
  335.   rstep = rpix; gstep = gpix; bstep = bpix;
  336.   
  337.   for(y=0;y<h;y++)
  338.   {
  339.     int px,py;
  340.     pr= rptr; rptr+=rzeil;
  341.     pg= gptr; gptr+=gzeil;
  342.     pb= bptr; bptr+=bzeil;
  343.  
  344.     px = (PictureNr % 4)*193;
  345.     if(px + w > 771) error(E_ABORT);
  346.     py = (PictureNr / 4)*129 + y;
  347.     if(py > 561) error(E_ABORT);
  348.     EncodeHAM8((char *)pr, (char *)pg, (char *)pb, OutputBuffer, w);
  349.     DisplayRowPos(OutputBuffer, w, px, py);
  350.     if(CheckButton())
  351.     {
  352.       CloseDisplay();
  353.       error(E_ABORT);
  354.     }
  355.   }
  356.  
  357.   PictureNr++;
  358. }
  359.  
  360. void write_dover_gray(FILE *fout,dim w,dim h, uBYTE *ptr,sdim zeil,sdim pix) 
  361.  {register uBYTE *p;
  362.   dim y,x;
  363.   int px, py;
  364.   uBYTE *rowptr;
  365.   int DisplaySuccess;
  366.   unsigned long i;
  367.  
  368.   if(PictureNr == 0)
  369.   {
  370.     row = malloc(((w+15)>>4)<<4);
  371.     if(row == NULL) error(E_MEM);
  372.  
  373.     #ifdef __GNUC__
  374.     signal(SIGINT, SIG_IGN); /* disable CTRL-C handling from now on */
  375.     #endif
  376.  
  377.     ScreenColorTable = malloc(256*4*3+8);
  378.     if(!ScreenColorTable) error(E_MEM);
  379.  
  380.     for(i=0; i<256; i++)
  381.     {
  382.       ScreenColorTable[i*3+1] = (unsigned long)i<<24;
  383.       ScreenColorTable[i*3+2] = (unsigned long)i<<24;
  384.       ScreenColorTable[i*3+3] = (unsigned long)i<<24;
  385.     }
  386.  
  387.     ScreenColorTable[0] = 256L<<16+0;
  388.     ScreenColorTable[256*3+1] = 0;
  389.  
  390.     OverView = 1;
  391.     DisplaySuccess = InitDisplay(772, 516, 0, 8);
  392.  
  393.     if(DisplaySuccess != 1)
  394.     {
  395.       CloseDisplay();
  396.       error(E_DISPLAY);
  397.     }
  398.   }
  399.   
  400.   for(y=0; y<h; y++)
  401.   {
  402.     p = ptr; ptr+=zeil; rowptr = row;
  403.  
  404.     for(x=0; x<w; x++)
  405.     {
  406.       *rowptr++ = *p;
  407.       p+=pix;
  408.     }
  409.     px = (PictureNr % 4)*193;
  410.     if(px + w > 771) error(E_ABORT);
  411.     py = (PictureNr / 4)*129+y;
  412.     if(py > 561) error(E_ABORT);
  413.     DisplayRowPos(row, w, px, py);
  414.  
  415.     if(CheckButton())
  416.     {
  417.       CloseDisplay();
  418.       error(E_ABORT);
  419.     } 
  420.   }
  421.   PictureNr++;
  422.   DisplayNrGray(bildnr+1, px+8, py-112);
  423. }
  424.