From f030154fe727e25a2afe1f78b3998c2d2dba95e4 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Tue, 18 Aug 2009 09:24:15 +0000 Subject: astyle cleanup, corrects not loading of material textures --- src/render/tgafile.cc | 222 +++++++++++++++++++++++++------------------------- 1 file changed, 111 insertions(+), 111 deletions(-) (limited to 'src/render/tgafile.cc') diff --git a/src/render/tgafile.cc b/src/render/tgafile.cc index d4d5df8..db54645 100644 --- a/src/render/tgafile.cc +++ b/src/render/tgafile.cc @@ -21,7 +21,7 @@ 9 Colormapped image data Yes Yes 10 Truecolor image data No Yes 11 Monochrome image data No Yes - + TGA multi-byte integer values have LSB first */ @@ -57,7 +57,7 @@ Image *TGA::load(const char *filename) // TGA header unsigned char header[18]; - memset(header, 0, sizeof(header)); + memset(header, 0, sizeof(header)); if (!tga_file->read(header, 18)) { con_warn << "Error reading " << filename << std::endl; @@ -65,38 +65,38 @@ Image *TGA::load(const char *filename) return 0; } - // byte 0 - image ID field length + // byte 0 - image ID field length unsigned int tga_idlength = header[0]; - // byte 1 - color map type + // byte 1 - color map type unsigned int tga_colormap = header[1]; - // byte 2 - image type + // byte 2 - image type unsigned int tga_type = header[2]; - // byte 3+4 - color map first entry index + // byte 3+4 - color map first entry index //unsigned int tga_colormap_first = header[3] + (header[4] << 8 ); // byte 5+6 - color map length (in bits) - unsigned int tga_color_map_length = header[5] +(header[6] << 8 ); + unsigned int tga_color_map_length = header[5] + (header[6] << 8); // byte 7 - color map entry length unsigned int tga_colormap_entry = header[7]; // byte 8+9 - image x origin // byte 10+11 - image y origin - // byte 12+13 - image width (LSB first) + // byte 12+13 - image width (LSB first) unsigned int tga_width = header[12] + (header[13] << 8); - // byte 14+15 - image height (LSB first) + // byte 14+15 - image height (LSB first) unsigned int tga_height = header[14] + (header[15] << 8); - // byte 16 - image color depth (in bits) + // byte 16 - image color depth (in bits) unsigned int tga_depth = header[16]; - // byte 17 - image descriptor byte + // byte 17 - image descriptor byte unsigned int tga_descriptor = header[17]; - + // read the image id if there is one if (tga_idlength) tga_file->skip(tga_idlength); @@ -105,7 +105,7 @@ Image *TGA::load(const char *filename) if (tga_colormap) { if (tga_colormap > 1) con_warn << filename << ": invalid color map type!" << std::endl; - + tga_file->skip(tga_color_map_length*tga_colormap_entry); } @@ -113,92 +113,92 @@ Image *TGA::load(const char *filename) unsigned int index = 0; unsigned int channels = tga_depth / 8; - switch(tga_type) { + switch (tga_type) { - case TGA_NONE: - con_warn << "Error reading " << filename + case TGA_NONE: + con_warn << "Error reading " << filename << ": no image data!" << std::endl; - filesystem::close(tga_file); - return 0; - break; + filesystem::close(tga_file); + return 0; + break; - case TGA_TRUECOLOR: - if ((tga_depth == 24) || (tga_depth == 32)) { + case TGA_TRUECOLOR: + if ((tga_depth == 24) || (tga_depth == 32)) { - image = new Image(tga_width, tga_height, channels); + image = new Image(tga_width, tga_height, channels); - for (size_t i = 0; i < tga_width * tga_height; i++) { - tga_file->read((void *)(*image)[i*(size_t)channels], channels); - } + for (size_t i = 0; i < tga_width * tga_height; i++) { + tga_file->read((void *)(*image)[i*(size_t)channels], channels); + } - image->swap_channels(); + image->swap_channels(); - } else if (tga_depth == 16) { - - channels = 3; - image = new Image(tga_width, tga_height,channels); - - for (size_t i =0; i < tga_width * tga_height; i++) { - // unpack one pixel - unsigned char pixel_data[2]; - tga_file->read((void *)pixel_data, 2); - unsigned int unpacked = pixel_data[0] + pixel_data[1]* 0xff; - - unsigned int b = (unpacked & 0x1f) << 3; - unsigned int g = ((unpacked >> 5) & 0x1f) << 3; - unsigned int r = ((unpacked >> 10) & 0x1f) << 3; - - // store it - image->data()[i * channels] = (unsigned char) b; - image->data()[i * channels+1] = (unsigned char) g; - image->data()[i * channels+2] = (unsigned char) r; - } - } else { - con_warn << "Error reading " << filename + } else if (tga_depth == 16) { + + channels = 3; + image = new Image(tga_width, tga_height, channels); + + for (size_t i = 0; i < tga_width * tga_height; i++) { + // unpack one pixel + unsigned char pixel_data[2]; + tga_file->read((void *)pixel_data, 2); + unsigned int unpacked = pixel_data[0] + pixel_data[1] * 0xff; + + unsigned int b = (unpacked & 0x1f) << 3; + unsigned int g = ((unpacked >> 5) & 0x1f) << 3; + unsigned int r = ((unpacked >> 10) & 0x1f) << 3; + + // store it + image->data()[i * channels] = (unsigned char) b; + image->data()[i * channels+1] = (unsigned char) g; + image->data()[i * channels+2] = (unsigned char) r; + } + } else { + con_warn << "Error reading " << filename << ": unsupported image depth '" << tga_depth << "'!" << std::endl; - filesystem::close(tga_file); - return 0; - } - - break; + filesystem::close(tga_file); + return 0; + } - case TGA_TRUECOLOR_RLE: + break; - image = new Image(tga_width, tga_height, channels); + case TGA_TRUECOLOR_RLE: - while (index < tga_width * tga_height) { - unsigned char rle = 0; - unsigned char pixel_data[4]; + image = new Image(tga_width, tga_height, channels); - // read RLE packet byte - tga_file->read(&rle, 1); - - if (rle < 128) { - rle++; // rle contains the number of pixels-1 - tga_file->read((void *)(*image)[index*channels], rle*channels); - index += rle; + while (index < tga_width * tga_height) { + unsigned char rle = 0; + unsigned char pixel_data[4]; - } else { - rle -= 127; // rle contains 128 + the number of identical pixels-1 - tga_file->read(pixel_data, channels); - - while (rle > 0 ) { - memcpy((void *)(*image)[index*channels], (void *)pixel_data, channels); - index++; - rle--; + // read RLE packet byte + tga_file->read(&rle, 1); + + if (rle < 128) { + rle++; // rle contains the number of pixels-1 + tga_file->read((void *)(*image)[index*channels], rle*channels); + index += rle; + + } else { + rle -= 127; // rle contains 128 + the number of identical pixels-1 + tga_file->read(pixel_data, channels); + + while (rle > 0) { + memcpy((void *)(*image)[index*channels], (void *)pixel_data, channels); + index++; + rle--; + } } } - } - image->swap_channels(); + image->swap_channels(); + + break; - break; - - default: - con_warn << "Error reading " << filename + default: + con_warn << "Error reading " << filename << ": unsupported TGA type '" << (int) tga_type << "'!" << std::endl; - filesystem::close(tga_file); - return 0; + filesystem::close(tga_file); + return 0; } filesystem::close(tga_file); @@ -221,35 +221,35 @@ void TGA::save(const char *filename, Image & image) if (!filename) return; - std::ofstream ofs(filename, std::ios_base::out | std::ios_base::binary ); + std::ofstream ofs(filename, std::ios_base::out | std::ios_base::binary); - if (!ofs.is_open()) { - con_warn << "Could not write " << filename << std::endl; - return; - } + if (!ofs.is_open()) { + con_warn << "Could not write " << filename << std::endl; + return; + } // write TGA header unsigned char header[18]; memset(header, 0, sizeof(header)); // byte 0 - image ID field length = 0 (no image ID field present) - // byte 1 - color map type = 0 (no palette present) - // byte 2 - image type = 10 (truecolor RLE encoded) - header[2] = TGA_TRUECOLOR_RLE; - // byte 3-11 - palette data (not used) - // byte 12+13 - image width - header[12] = (image.width() & 0xff); - header[13] = ((image.width() >> 8) & 0xff); - // byte 14+15 - image height - header[14] = (image.height() & 0xff); - header[15] = ((image.height() >> 8) & 0xff); - // byte 16 - image color depth = 24 (RGB) or 32 (RGBA) - header[16] = image.channels() * 8; - // byte 17 - image descriptor byte = 0x20 (origin at bottom left) - header[17] = 0x20; + // byte 1 - color map type = 0 (no palette present) + // byte 2 - image type = 10 (truecolor RLE encoded) + header[2] = TGA_TRUECOLOR_RLE; + // byte 3-11 - palette data (not used) + // byte 12+13 - image width + header[12] = (image.width() & 0xff); + header[13] = ((image.width() >> 8) & 0xff); + // byte 14+15 - image height + header[14] = (image.height() & 0xff); + header[15] = ((image.height() >> 8) & 0xff); + // byte 16 - image color depth = 24 (RGB) or 32 (RGBA) + header[16] = image.channels() * 8; + // byte 17 - image descriptor byte = 0x20 (origin at bottom left) + header[17] = 0x20; // write header - ofs.write((char *)header, sizeof(header)); + ofs.write((char *)header, sizeof(header)); // write image data // TGA has the R and B channels switched @@ -259,16 +259,16 @@ void TGA::save(const char *filename, Image & image) bool compress = false; size_t block_length = 0; - for (int y = image.height()-1; y >= 0; y--) { + for (int y = image.height() - 1; y >= 0; y--) { for (size_t x = 0; x < image.width(); x++) { - size_t index = y*image.width()*image.channels() + x * image.channels(); + size_t index = y * image.width() * image.channels() + x * image.channels(); pixel_data[0] = *image[index+2]; pixel_data[1] = *image[index+1]; pixel_data[2] = *image[index]; if (image.channels() == 4) - pixel_data[3] = *image[index+3]; - + pixel_data[3] = *image[index+3]; + if (block_length == 0) { memcpy(block_data, pixel_data, image.channels()); block_length++; @@ -284,12 +284,12 @@ void TGA::save(const char *filename, Image & image) block_length++; } else { - // uncompressed block and pixel data is identical - if (block_length > 1 ) { - // write the uncompressed block + // uncompressed block and pixel data is identical + if (block_length > 1) { + // write the uncompressed block rle_packet = block_length - 2; ofs.write((char *)&rle_packet, 1); - ofs.write((char *)block_data, (block_length-1) * image.channels()); + ofs.write((char *)block_data, (block_length - 1) * image.channels()); block_length = 1; } memcpy(block_data, pixel_data, image.channels()); @@ -305,9 +305,9 @@ void TGA::save(const char *filename, Image & image) } else { - // compressed block and pixel data differs + // compressed block and pixel data differs if (block_length > 1) { - // write the compressed block + // write the compressed block rle_packet = block_length + 127; ofs.write((char *)&rle_packet, 1); ofs.write((char *)block_data, image.channels()); -- cgit v1.2.3