Path: blob/main_old/samples/sample_util/tga_utils.cpp
1695 views
//1// Copyright 2014 The ANGLE Project Authors. All rights reserved.2// Use of this source code is governed by a BSD-style license that can be3// found in the LICENSE file.4//56#include "tga_utils.h"78#include <fstream>9#include <iostream>10#include <limits.h>11#include <stdint.h>12#include <string>1314TGAImage::TGAImage()15: width(0), height(0), data(0)16{17}1819struct TGAHeader20{21uint8_t idSize;22uint8_t mapType;23uint8_t imageType;24uint16_t paletteStart;25uint16_t paletteSize;26uint8_t paletteEntryDepth;27uint16_t x;28uint16_t y;29uint16_t width;30uint16_t height;31uint8_t colorDepth;32uint8_t descriptor;33};3435#define INVERTED_BIT (1 << 5)3637template <typename dataType>38void readBinary(std::ifstream &stream, dataType &item)39{40stream.read(reinterpret_cast<char *>(&item), sizeof(dataType));41}4243template <typename dataType>44void readBinary(std::ifstream &stream, std::vector<dataType> &items)45{46stream.read(reinterpret_cast<char *>(items.data()), sizeof(dataType) * items.size());47}4849bool LoadTGAImageFromFile(const std::string &path, TGAImage *image)50{51std::ifstream stream(path, std::ios::binary);52if (!stream)53{54std::cerr << "error opening tga file " << path << " for reading.\n";55return false;56}5758TGAHeader header;59readBinary(stream, header.idSize);60readBinary(stream, header.mapType);61readBinary(stream, header.imageType);62readBinary(stream, header.paletteStart);63readBinary(stream, header.paletteSize);64readBinary(stream, header.paletteEntryDepth);65readBinary(stream, header.x);66readBinary(stream, header.y);67readBinary(stream, header.width);68readBinary(stream, header.height);69readBinary(stream, header.colorDepth);70readBinary(stream, header.descriptor);7172image->width = header.width;73image->height = header.height;7475size_t pixelComponentCount = header.colorDepth / CHAR_BIT;76std::vector<unsigned char> buffer(header.width * header.height * pixelComponentCount);77readBinary(stream, buffer);7879image->data.reserve(header.width * header.height);8081for (size_t y = 0; y < header.height; y++)82{83size_t rowIdx = ((header.descriptor & INVERTED_BIT) ? (header.height - 1 - y) : y) * header.width * pixelComponentCount;84for (size_t x = 0; x < header.width; x++)85{86size_t pixelIdx = rowIdx + x * pixelComponentCount;8788Byte4 pixel;89pixel[0] = (pixelComponentCount > 2) ? buffer[pixelIdx + 2] : 0;90pixel[2] = (pixelComponentCount > 0) ? buffer[pixelIdx + 0] : 0;91pixel[1] = (pixelComponentCount > 1) ? buffer[pixelIdx + 1] : 0;92pixel[3] = (pixelComponentCount > 3) ? buffer[pixelIdx + 3] : 255;9394image->data.push_back(pixel);95}96}9798std::cout << "loaded image " << path << ".\n";99100return true;101}102103GLuint LoadTextureFromTGAImage(const TGAImage &image)104{105if (image.width > 0 && image.height > 0)106{107GLuint texture;108glGenTextures(1, &texture);109glBindTexture(GL_TEXTURE_2D, texture);110glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);111glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);112glPixelStorei(GL_UNPACK_ALIGNMENT, 1);113glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, static_cast<GLsizei>(image.width), static_cast<GLsizei>(image.height), 0,114GL_RGBA, GL_UNSIGNED_BYTE, image.data.data());115glGenerateMipmap(GL_TEXTURE_2D);116return texture;117}118else119{120return 0;121}122}123124125