private void readIHDR() throws IOException {
        checkChunkLength(13);
        readChunk(buffer, 0, 13);
        width = readInt(buffer, 0);
        height = readInt(buffer, 4);
        
        if(buffer[8] != 8) {
            throw new IOException("Unsupported bit depth");
        }
        
        colorType = buffer[9] & 255;
        switch (colorType) {
        case COLOR_GREYSCALE:
            bytesPerPixel = 1;
            break;
        case COLOR_TRUECOLOR:
            bytesPerPixel = 3;
            break;
        case COLOR_TRUEALPHA:
            bytesPerPixel = 4;
            break;
        case COLOR_INDEXED:
            bytesPerPixel = 1;
            break;
        default:
            throw new IOException("unsupported color format");  
        }
        
        if(buffer[10] != 0) {
            throw new IOException("unsupported compression method");
        }
        if(buffer[11] != 0) {
            throw new IOException("unsupported filtering method");
        }
        if(buffer[12] != 0) {
            throw new IOException("unsupported interlace method");
        }
    }