Logo Search packages:      
Sourcecode: djvulibre version File versions

void GPixmap::init ( ByteStream ref  ) 

Resets the GPixmap by reading PPM data from ByteStream ref#. See {PNM and RLE file formats} for more information.

Definition at line 465 of file GPixmap.cpp.

References GPixel::b, ByteStream::EndOfFile, GPixel::g, init(), GPixel::r, ByteStream::read16(), ByteStream::readall(), GArrayTemplate< TYPE >::resize(), and ByteStream::seek().

{
  // Read header
  bool raw = false;
  bool grey = false;
  int magic = bs.read16();
  switch (magic)
    {
    case ('P'<<8)+'2':
      grey = true;
      break;
    case ('P'<<8)+'3':
      break;
    case ('P'<<8)+'5':
      raw = grey = true;
    case ('P'<<8)+'6':
      raw = true;
      break;
    default:
#ifdef NEED_JPEG_DECODER
      bs.seek(0L);
      JPEGDecoder::decode(bs,*this);
      return;
#else
      
      G_THROW( ERR_MSG("GPixmap.unk_PPM") );
#endif
    }
  // Read image size
  char lookahead = '\n';
  int bytesperrow = 0;
  int bytespercomp = 1;
  int acolumns = read_integer(lookahead, bs);
  int arows = read_integer(lookahead, bs);
  int maxval = read_integer(lookahead, bs);
  if (maxval > 65535)
    G_THROW("Cannot read PPM with depth greater than 48 bits.");
  if (maxval > 255)
    bytespercomp = 2;
  init(arows, acolumns, 0);
  // Prepare ramp
  GTArray<unsigned char> ramp;
  int maxbin = 1 << (8 * bytespercomp);
  ramp.resize(0, maxbin-1);
  for (int i=0; i<maxbin; i++)
    ramp[i] = (i<maxval ? (255*i + maxval/2) / maxval : 255);
  unsigned char *bramp = ramp;
  // Read image data
  if (raw && grey)
    {
      bytesperrow = ncolumns * bytespercomp;
      GTArray<unsigned char> line(bytesperrow);
      for (int y=nrows-1; y>=0; y--) 
        {
          GPixel *p = (*this)[y];
          unsigned char *g = &line[0];
          if ( bs.readall((void*)g, bytesperrow) < (size_t)bytesperrow)
            G_THROW( ByteStream::EndOfFile );
          if (bytespercomp <= 1)
            {
              for (int x=0; x<ncolumns; x+=1, g+=1)
                p[x].r = p[x].g = p[x].b = bramp[g[0]];
            }
          else
            {
              for (int x=0; x<ncolumns; x+=1, g+=2)
                p[x].r = p[x].g = p[x].b = bramp[g[0]*256+g[1]];
            }
        }
    }
  else if (raw)
    {
      bytesperrow = ncolumns * bytespercomp * 3;
      GTArray<unsigned char> line(bytesperrow);
      for (int y=nrows-1; y>=0; y--) 
        {
          GPixel *p = (*this)[y];
          unsigned char *rgb = &line[0];
          if ( bs.readall((void*)rgb, bytesperrow) < (size_t)bytesperrow)
            G_THROW( ByteStream::EndOfFile );
          if (bytespercomp <= 1)
            {
              for (int x=0; x<ncolumns; x+=1, rgb+=3)
                {
                  p[x].r = bramp[rgb[0]];
                  p[x].g = bramp[rgb[1]];
                  p[x].b = bramp[rgb[2]];
                }
            }
          else
            for (int x=0; x<ncolumns; x+=1, rgb+=6)
              {
                p[x].r = bramp[rgb[0]*256+rgb[1]];
                p[x].g = bramp[rgb[2]*256+rgb[3]];
                p[x].b = bramp[rgb[4]*256+rgb[5]];
              }
        }
    }
  else
    {
      for (int y=nrows-1; y>=0; y--) 
        {
          GPixel *p = (*this)[y];
          for (int x=0; x<ncolumns; x++)
            if (grey)
              {
                p[x].g = p[x].b = p[x].r = ramp[(int)read_integer(lookahead, bs)];
              }
            else
              {
                p[x].r = ramp[(int)read_integer(lookahead, bs)];
                p[x].g = ramp[(int)read_integer(lookahead, bs)];
                p[x].b = ramp[(int)read_integer(lookahead, bs)];
              }
        }
    }
}


Generated by  Doxygen 1.6.0   Back to index