void PLPGMDecoder::readPgmHeader ( PGMHEADER * pPgmHead, // Pointer to PGM header structure PLDataSource * pDataSrc ) { int current = 0; // bool HeaderComplete = false; // Read type m_LastByte = ReadByte (pDataSrc); if (m_LastByte!=0x50) // ASCII P raiseError (PL_ERRFORMAT_UNKNOWN, "PGM decoder: Is not the correct identifier P5 or P2."); m_LastByte = ReadByte (pDataSrc); if (m_LastByte==0x32) // ASCII 2 pPgmHead->ImageType = PGM_P2; else if (m_LastByte==0x35) // ASCII 5 pPgmHead->ImageType = PGM_P5; else raiseError (PL_ERRFORMAT_UNKNOWN, "PGM decoder: Is not the correct identifier P5 or P2."); m_LastByte = ReadByte (pDataSrc); // Search for the with, height and Max gray value while (current<3) { if (m_LastByte==0x23) // # Starts a comment skipComment(pDataSrc); else if ((m_LastByte>=0x30)&&(m_LastByte<=0x39)) // A digit switch (current) { case 0: // looking for the width { pPgmHead->ImageWidth = readASCIIDecimal(pDataSrc); current++; } break; case 1: // looking for the height { pPgmHead->ImageHeight = readASCIIDecimal(pDataSrc); current++; } break; case 2: // looking for the max gray value { pPgmHead->MaxGrayValue = readASCIIDecimal(pDataSrc); if ((pPgmHead->MaxGrayValue>255)||(pPgmHead->MaxGrayValue<=0)) pPgmHead->MaxGrayValue=255; current++; } break; default: continue; } else skipPgmASCIISeparators(pDataSrc); } }
void PLPPMDecoder::readPpmHeader(PPMHEADER *pPpmHead, PLDataSource *pDataSrc) { int current = 0; bool HeaderComplete = false; // Read type m_LastByte = ReadByte(pDataSrc); if(m_LastByte != 'P') raiseError(PL_ERRFORMAT_UNKNOWN,"PPM decoder: Is not the correct identifier P3 or P6."); m_LastByte = ReadByte(pDataSrc); switch(m_LastByte) { case '3': pPpmHead->ImageType = PPM_P3; break; case '6': pPpmHead->ImageType = PPM_P6; break; default: raiseError(PL_ERRFORMAT_UNKNOWN,"PPM decoder: Is not the correct identifier P3 or P6."); } m_LastByte = ReadByte(pDataSrc); // Search for the width, height and Max sample value while(current<3) { if(m_LastByte == '#') // # Starts a comment skipComment(pDataSrc); else if(m_LastByte >='0' && m_LastByte <= '9') // A digit switch(current) { case 0: // looking for the width { pPpmHead->ImageWidth = readASCIIDecimal(pDataSrc); current++; } break; case 1: // looking for the height { pPpmHead->ImageHeight = readASCIIDecimal(pDataSrc); current++; } break; case 2: // looking for the sample value (max = 255) { pPpmHead->MaxSampleValue = readASCIIDecimal(pDataSrc); if(pPpmHead->MaxSampleValue > 255 || pPpmHead->MaxSampleValue <= 0) pPpmHead->MaxSampleValue = 255; current++; } break; default: continue; } else skipPpmASCIISeparators(pDataSrc); } }
PLPixel32 PLPPMDecoder::readASCIIPixel32( int MaxSampleValue, PLDataSource *pDataSrc) { skipPpmASCIISeparators(pDataSrc); PLBYTE r = readASCIIDecimal(pDataSrc); skipPpmASCIISeparators(pDataSrc); PLBYTE g = readASCIIDecimal(pDataSrc); skipPpmASCIISeparators(pDataSrc); PLBYTE b = readASCIIDecimal(pDataSrc); if(MaxSampleValue != 255) { r = (r * 255) / MaxSampleValue; g = (g * 255) / MaxSampleValue; b = (b * 255) / MaxSampleValue; } PLPixel32 Dest; Dest.Set(r, g, b, 0); return Dest; }
PLBYTE PLPGMDecoder::readASCIIPixel8 ( int MaxGrayValue, PLDataSource * pDataSrc ) { PLBYTE Dest; int Value; skipPgmASCIISeparators(pDataSrc); m_UseLastByte = true; Value = readASCIIDecimal(pDataSrc); Dest = (PLBYTE)((Value*255)/MaxGrayValue); return Dest; }