Example #1
0
void ParseOBJ::parse(BinaryInput& bi, const ParseOBJ::Options& options, const String& basePath) {
    m_filename = bi.getFilename();

    String bp = basePath;
    if (bp == "<AUTO>") {
        bp = FilePath::parent(FileSystem::resolve(m_filename));
    }    

    parse((const char*)bi.getCArray() + bi.getPosition(),
          size_t(bi.getLength() - bi.getPosition()), bp, options);
}
Example #2
0
GFont::GFont(const String& filename, BinaryInput& b) {    

    int ver = b.readInt32();
    debugAssertM(ver == 1 || ver == 2, "Can't read font files other than version 1");
    (void)ver;

    if (ver == 1) {
        charsetSize = 128;
    } else {
        charsetSize = b.readInt32();
    }

    // Read the widths
    subWidth.resize(charsetSize);
    for (int c = 0; c < charsetSize; ++c) {
        subWidth[c] = b.readUInt16();
    }

    baseline = b.readUInt16();
    int texWidth = b.readUInt16();
    charWidth  = texWidth / 16;
    charHeight = texWidth / 16;

    // The input may not be a power of 2
    int width  = ceilPow2(charWidth * 16);
    int height = ceilPow2(charHeight * (charsetSize / 16));
  
    // Create a texture
    const uint8* ptr = ((uint8*)b.getCArray()) + b.getPosition();
    debugAssertM((b.getLength() - b.getPosition()) >= width * height, 
        "File does not contain enough data for this size texture");

    Texture::Preprocess preprocess;
    preprocess.computeMinMaxMean = false;
    const bool generateMipMaps = true;

    
    m_texture = 
        Texture::fromMemory
        (   filename, 
            ptr,
            ImageFormat::R8(),
            width, 
            height,
            1,
            1,
            ImageFormat::R8(), 
            Texture::DIM_2D,
            generateMipMaps,
            preprocess);
       
    m_textureMatrix[0] = 1.0f / m_texture->width();
    m_textureMatrix[1] = 0;
    m_textureMatrix[2] = 0;
    m_textureMatrix[3] = 0;
    m_textureMatrix[4] = 0;
    m_textureMatrix[5] = 1.0f / m_texture->height();
    m_textureMatrix[6] = 0;
    m_textureMatrix[7] = 0;    
    m_textureMatrix[8] = 0;
    m_textureMatrix[9] = 0;
    m_textureMatrix[10] = 1;
    m_textureMatrix[11] = 0;
    m_textureMatrix[12] = 0;
    m_textureMatrix[13] = 0;
    m_textureMatrix[14] = 0;
    m_textureMatrix[15] = 1;

    m_name = filename;
}
Example #3
0
void GImage::decodeTGA(
    BinaryInput&        input) {

    // This is a simple TGA loader that can handle uncompressed
    // truecolor TGA files (TGA type 2).
    // Verify this is a TGA file by looking for the TRUEVISION tag.
    int pos = input.getPosition();
    input.setPosition(input.size() - 18);
    std::string tag = input.readString(16);
    if (tag != "TRUEVISION-XFILE") {
        throw Error("Not a TGA file", input.getFilename());
    }

    input.setPosition(pos);

    int IDLength     = input.readUInt8();
    int colorMapType = input.readUInt8();
    int imageType    = input.readUInt8();

    (void)colorMapType;

    // 2 is the type supported by this routine.
    if (imageType != 2 && imageType != 10) {
        throw Error("TGA images must be type 2 (Uncompressed truecolor) or 10 (Run-length truecolor)", input.getFilename());
    }

    // Color map specification
    input.skip(5);

    // Image specification

    // Skip x and y offsets
    input.skip(4);

    m_width  = input.readInt16();
    m_height = input.readInt16();

    int colorDepth = input.readUInt8();

    if ((colorDepth != 24) && (colorDepth != 32)) {
        throw Error("TGA files must be 24 or 32 bit.", input.getFilename());
    }

    if (colorDepth == 32) {
        m_channels = 4;
    } else {
        m_channels = 3;
    }

    // Image descriptor contains overlay data as well
    // as data indicating where the origin is
    int imageDescriptor = input.readUInt8();
    (void)imageDescriptor;

    // Image ID
    input.skip(IDLength);

    m_byte = (uint8*)m_memMan->alloc(m_width * m_height * m_channels);
    debugAssert(m_byte);

    // Pixel data
    int x;
    int y;

    if (imageType == 2) {
        // Uncompressed
        if (m_channels == 3) {
            for (y = m_height - 1; y >= 0; --y) {
              for (x = 0; x < m_width; ++x) {
                int i = (x + y * m_width) * 3;
                readBGR(m_byte + i, input);
              }
            }
        } else {
            for (y = m_height - 1; y >= 0; --y) {
              for (x = 0; x < m_width; ++x) {
                 int i = (x + y * m_width) * 4;
                 readBGRA(m_byte + i, input);
              }
            }
        }
    } else if (imageType == 10) {

        // Run-length encoded
        for (y = m_height - 1; y >= 0; --y) {
            for (int x = 0; x < m_width; /* intentionally no x increment */) {
                // The specification guarantees that no packet will wrap past the end of a row
                const uint8 repetitionCount = input.readUInt8();
                const uint8 numValues = (repetitionCount & (~128)) + 1;
                int byteOffset = (x + y * m_width) * 3;

                if (repetitionCount & 128) {
                    // When the high bit is 1, this is a run-length packet
                    if (m_channels == 3) {
                        Color3uint8 value;
                        readBGR((uint8*)(&value), input);
                        for (int i = 0; i < numValues; ++i, ++x) {
                            for (int b = 0; b < 3; ++b, ++byteOffset) {
                                m_byte[byteOffset] = value[b];
                            }
                        }
                    } else {
                        Color4uint8 value;
                        readBGRA((uint8*)(&value), input);
                        for (int i = 0; i < numValues; ++i, ++x) {
                            for (int b = 0; b < 3; ++b, ++byteOffset) {
                                m_byte[byteOffset] = value[b];
                            }
                        }
                    }

                } else {
                    // When the high bit is 0, this is a raw packet
                    for (int i = 0; i < numValues; ++i, ++x, byteOffset += m_channels) {
                        readBGR(m_byte + byteOffset, input);
                    }
                }
            }
        }
    } else {
        alwaysAssertM(false, "Unsupported type");
    }
}
Example #4
0
void GImage::decodeTGA(
    BinaryInput&        input) {

    // This is a simple TGA loader that can handle uncompressed
    // truecolor TGA files (TGA type 2). 
    // Verify this is a TGA file by looking for the TRUEVISION tag.
    int pos = input.getPosition();
    input.setPosition(input.size() - 18);
    std::string tag = input.readString(16);
    if (tag != "TRUEVISION-XFILE") {
        throw Error("Not a TGA file", input.getFilename());
    }

    input.setPosition(pos);

    int IDLength     = input.readUInt8();
    int colorMapType = input.readUInt8();
    int imageType    = input.readUInt8();

    (void)colorMapType;
	
    // 2 is the type supported by this routine.
    if (imageType != 2) {
        throw Error("TGA images must be type 2 (Uncompressed truecolor)", input.getFilename());
    }
	
    // Color map specification
    input.skip(5);

    // Image specification

    // Skip x and y offsets
    input.skip(4); 

    m_width  = input.readInt16();
    m_height = input.readInt16();

    int colorDepth = input.readUInt8();

    if ((colorDepth != 24) && (colorDepth != 32)) {
        throw Error("TGA files must be 24 or 32 bit.", input.getFilename());
    }

    if (colorDepth == 32) {
        m_channels = 4;
    } else {
        m_channels = 3;
    }

    // Image descriptor contains overlay data as well
    // as data indicating where the origin is
    int imageDescriptor = input.readUInt8();
    (void)imageDescriptor;
	
    // Image ID
    input.skip(IDLength);

    m_byte = (uint8*)m_memMan->alloc(m_width * m_height * m_channels);
    debugAssert(m_byte);
	
    // Pixel data
    int x;
    int y;

    if (m_channels == 3) {
        for (y = m_height - 1; y >= 0; --y) {
          for (x = 0; x < m_width; ++x) {
            int b = input.readUInt8();
            int g = input.readUInt8();
            int r = input.readUInt8();
		    
            int i = (x + y * m_width) * 3;
            m_byte[i + 0] = r;
            m_byte[i + 1] = g;
            m_byte[i + 2] = b;
          }
        }
    } else {
        for (y = m_height - 1; y >= 0; --y) {
          for (x = 0; x < m_width; ++x) {
            int b = input.readUInt8();
            int g = input.readUInt8();
            int r = input.readUInt8();
            int a = input.readUInt8();
		    
            int i = (x + y * m_width) * 4;
            m_byte[i + 0] = r;
            m_byte[i + 1] = g;
            m_byte[i + 2] = b;
            m_byte[i + 3] = a;
          }
        }
    }
}