home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 26 / CD_ASCQ_26_1295.iso / vrac / volume_3.zip / PCX.CPP < prev    next >
C/C++ Source or Header  |  1995-09-30  |  5KB  |  178 lines

  1. //PCX Picture Viewer
  2. //(C) as Freeware 1994 by Marty A. Lineberry
  3. //Only in 480 by 640 but can easily be modified
  4. #ifndef action.cpp
  5. #define action.cpp
  6. #endif
  7.  
  8.  
  9. //*******************************************************************
  10. //*********** Function Photo-Types ***********************************
  11. //*******************************************************************
  12. int Display_PCX(char *PCX_Filename);
  13. void DrawBitPlane(int n, int Vertical_Y_Line, char *Picture_Buffer);
  14.  
  15.  
  16.  
  17.  
  18.  
  19. //**********************************************************
  20. //****************** Global Variables **********************
  21. //**********************************************************
  22. struct PCXHEADER
  23. {
  24.       char           Check_For_10;              // Always 10 for PCX
  25.       char           PCX_Version_Number;       // Version info
  26.       char           RLE_Encoding;             // Set to 1
  27.       char           Bits_Per_Pixel;          // Bits per pixel
  28.       unsigned short Upper_Left_X;             // Image bounds in pixels
  29.       unsigned short Upper_Left_Y;             //     "
  30.       unsigned short Lower_Right_X;            //     "
  31.       unsigned short Lower_Right_Y;         //     "
  32.       unsigned short Horiz_Resolution;         // Image resolution in dpi
  33.       unsigned short Vert_Resolution;           //     "
  34.       unsigned char  Color_Palette_16_[48];     // Palette
  35.       char           BIOS_Video_Mode;           // Junk info
  36.       char           Number_Of_BitPlanes;         // Plane count (v2.5=0)
  37.       unsigned short Bits_Per_Line;            // Bytes per line
  38.       unsigned short Color_or_BW;         // 1 for color, 2 for gray
  39.       unsigned short Screen_Width;           // Screen size in pixels
  40.       unsigned short Screen_Height;           //     "
  41.       char           Junk_54_Zeros[54];        // Junk...just 0's
  42.  
  43. };
  44.  
  45.  
  46. // Default palette.
  47. char Default_Palette[48] =
  48. {
  49.    0x00,0x00,0x0E,0x00,0x52,0x07,0x2C,0x00,
  50.    0x0E,0x00,0x00,0x00,0xF8,0x01,0x2C,0x00,
  51.    0x85,0x0F,0x42,0x00,0x21,0x00,0x00,0x00,
  52.    0x00,0x00,0x6A,0x24,0x9B,0x49,0xA1,0x5E,
  53.    0x90,0x5E,0x18,0x5E,0x84,0x14,0xD9,0x95,
  54.    0xA0,0x14,0x12,0x00,0x06,0x00,0x68,0x1F
  55. };
  56. unsigned char pcxpalette[48];
  57. PCXHEADER pcx_header_info;
  58. char *screenLinePointers[480];
  59.  
  60.  
  61.  
  62.  
  63.  
  64. //***************************************************
  65. //************* Displays PCX Picture ****************
  66. //***************************************************
  67.  
  68. int Display_PCX(char *PCX_Filename)
  69. {
  70. //variables declarations
  71. FILE *PCX_FILE_HANDLE;
  72. char *Picture_Buffer = new char[320];
  73. int PCX_Header_Size = sizeof(PCXHEADER);
  74. int bytesRead;
  75. int byteCount, data, runCount, temp;
  76. int Vertical_Y_Line;
  77.  
  78. // Initialize addresses of screen lines.
  79. for (temp=0; temp<480; ++temp)
  80.    screenLinePointers[temp] = (char *)MK_FP(0xa000, temp*80);
  81.  
  82. //Clears Graphics screen
  83. cleardevice();
  84.  
  85. //Opens File for Reading Only as Binary
  86. PCX_FILE_HANDLE = fopen(PCX_Filename,"rb");
  87.  
  88. //Checks to see if file exist
  89. if(PCX_FILE_HANDLE == NULL)
  90.     {
  91.     cout << "Graphics File Not Found\n";
  92.     fclose(PCX_FILE_HANDLE);
  93.     return FALSE;
  94.     }
  95. //Gets a copy of PCX Header and store it in HEADER Stucture
  96. if(fread(&pcx_header_info,PCX_Header_Size,1,PCX_FILE_HANDLE) != 1)
  97.     {
  98.     cout << "Graphics Header File Corrupt\n";
  99.     fclose(PCX_FILE_HANDLE);
  100.     return FALSE;
  101.     }
  102. //Checks to see if it is PCX File
  103. if(pcx_header_info.Check_For_10 != 10)
  104.     {
  105.     cout << "This is not a PCX Graphics File\n";
  106.     fclose(PCX_FILE_HANDLE);
  107.     return FALSE;
  108.     }
  109. //Checks PCX Version to wether or not use default Palette color info
  110. if(pcx_header_info.PCX_Version_Number == 3)
  111.     memcpy(pcxpalette,Default_Palette,48);
  112. else
  113.     memcpy(pcxpalette,pcx_header_info.Color_Palette_16_ ,48);
  114.  
  115.  
  116. //seek to beggining of picture data
  117. fseek(PCX_FILE_HANDLE,((unsigned long)PCX_Header_Size),SEEK_SET);
  118.  
  119. // Map EGA registers to first 16 DAC registers.
  120. for (temp=0; temp<15; ++temp)
  121.    setpalette(temp, temp);
  122.  
  123. // Store the palette in the DAC registers.
  124. for (temp=0; temp<15; ++temp)
  125.    {
  126.    setrgbpalette(temp, pcxpalette[temp*3]>>2,
  127.    pcxpalette[temp*3+1]>>2, pcxpalette[temp*3+2]>>2);
  128.    }
  129.  
  130.    // Read and display 480 lines with four bit planes each.
  131.    for (Vertical_Y_Line=0; Vertical_Y_Line<480; ++Vertical_Y_Line)
  132.    {
  133.     byteCount = 0;
  134.     while (byteCount < 320)
  135.     {
  136.         // Get a byte of data.
  137.         data = fgetc(PCX_FILE_HANDLE);
  138.  
  139.         // If this is a run-count byte...
  140.         if (data > 192)
  141.         {
  142.             // Calculate the run count.
  143.             runCount = data & 0x3f;
  144.  
  145.             // Get the data byte.
  146.             data = fgetc(PCX_FILE_HANDLE);
  147.  
  148.             // Duplicate the data byte runCount times.
  149.             for (temp=0; temp<runCount; ++temp)
  150.             Picture_Buffer[byteCount++] = data;
  151.         }
  152.         else Picture_Buffer[byteCount++] = data;
  153.     }
  154.  
  155.       // Output four bit planes.
  156.       DrawBitPlane(1, Vertical_Y_Line, Picture_Buffer);
  157.       DrawBitPlane(2, Vertical_Y_Line, &Picture_Buffer[80]);
  158.       DrawBitPlane(4, Vertical_Y_Line, &Picture_Buffer[160]);
  159.       DrawBitPlane(8, Vertical_Y_Line, &Picture_Buffer[240]);
  160.    }
  161.  
  162. outp(0x3c4, 2);
  163. outp(0x3c5, 15);
  164. delete Picture_Buffer;
  165. fclose(PCX_FILE_HANDLE);
  166. return TRUE;
  167. }
  168.  
  169. void DrawBitPlane(int n, int Vertical_Y_Line, char *Picture_Buffer)
  170. {
  171.    outp(0x3c4, 2);
  172.    outp(0x3c5, n);
  173.    for (int byte=0; byte<80; ++byte)
  174.  
  175.       screenLinePointers[Vertical_Y_Line][byte] = Picture_Buffer[byte];
  176. }
  177.  
  178.