void PLTImage::getColorRows(const PLTFile &parent, byte *rows) { for (uint i = 0; i < PLTFile::kLayerMAX; i++, rows += 4 * 256) { Common::SeekableReadStream *tgaFile = 0; try { tgaFile = ResMan.getResource(kPalettes[i], ::Aurora::kFileTypeTGA); if (!tgaFile) throw std::exception(); TGA tga(*tgaFile); if (tga.getFormat() != kPixelFormatBGRA) throw std::exception(); const MipMap &mipMap = tga.getMipMap(0); if (mipMap.width != 256) throw std::exception(); uint8 row = parent._colors[i]; if (row >= mipMap.height) throw std::exception(); row = mipMap.height - 1 - row; memcpy(rows, mipMap.data + (row * 4 * 256), 4 * 256); } catch (...) { memset(rows, 0, 4 * 256); } delete tgaFile; } }
bool QTgaHandler::canRead(QIODevice *device) { if (!device) { qWarning("QTgaHandler::canRead() called with no device"); return false; } QTgaFile tga(device); return tga.isValid(); }
// public bool RFeatures::saveTGA( const cv::Mat& m, const std::string& fname) { if ( m.depth() != CV_8U) { std::cerr << "[ERROR] RFeatures::saveTGA: only works with 8-bit unsigned int arrays!" << std::endl; return false; } // end if if ( m.channels() != 1 && m.channels() != 3 && m.channels() != 4) { std::cerr << "[ERROR] RFeatures::saveTGA: only works with 1, 3 or 4 channel images!" << std::endl; return false; } // end if FILE *bstream = fopen( fname.c_str(), "wb"); if ( !bstream) { std::cerr << "[ERROR] RFeatures::saveTGA(" << fname << "): Unable to open file for writing TGA image!" << std::endl; return false; } // end if // Write the header TGAHeader tga(m); if ( fwrite( tga.barray, 1, 18, bstream) != 18) { std::cerr << "[ERROR] RFeatures::saveTGA: Failed to write TGA header!" << std::endl; return false; } // end if // Write the image bytes row by row (BGA order) int bwrote = 0; const int nc = m.cols * m.channels(); for ( int i = int(m.rows-1); i >= 0; --i) // Write bottom to top bwrote += (int)fwrite( (void*)m.ptr(i), 1, nc, bstream); // Check all bytes written okay if ( bwrote != int(nc * m.rows)) { std::cerr << "[ERROR] RFeatures::saveTGA: Failed to write all " << (nc * m.rows) << " bytes of the image!" << std::endl; return false; } // end if fclose(bstream); // flush & close return true; } // end saveTGA
bool QTgaHandler::canRead(QIODevice *device) { if (!device) { qWarning("QTgaHandler::canRead() called with no device"); return false; } // TGA reader implementation needs a seekable QIODevice, so // sequential devices are not supported if (device->isSequential()) return false; qint64 pos = device->pos(); bool isValid; { QTgaFile tga(device); isValid = tga.isValid(); } device->seek(pos); return isValid; }
size_t tga::decode_header(const void *data,size_t size) { *this=tga(); if(!data || !size) return 0; nya_memory::memory_reader reader(data,size); const char id_length=reader.read<char>(); if(!reader.skip(id_length)) return 0; const char colourmaptype=reader.read<char>(); if(colourmaptype!=0) return 0; const char datatypecode=reader.read<char>(); const short colourmaporigin=reader.read<short>(); if(colourmaporigin!=0) return 0; const short colourmaplength=reader.read<short>(); if(colourmaplength!=0) return 0; const char colourmapdepth=reader.read<char>(); if(colourmapdepth!=0) return 0; //const short x_origin= reader.read<short>(); //const short y_origin= reader.read<short>(); const short width=reader.read<short>(); const short height=reader.read<short>(); const char bitsperpixel=reader.read<char>(); const unsigned char imagedescriptor=reader.read<char>(); color_mode channels; bool rle=false; switch(bitsperpixel) { case 32: if(datatypecode==10) channels=bgra,rle=true; else if(datatypecode==2) channels=bgra; else return false; break; case 24: if(datatypecode==10) channels=bgr,rle=true; else if(datatypecode==2) channels=bgr; else return false; break; case 8: if(datatypecode==11) channels=greyscale,rle=true; else if(datatypecode==3) channels=greyscale; else return false; break; default: return 0; } const size_t color_data_size=width*height*channels; if(!color_data_size) return 0; this->width=width; this->height=height; this->channels=channels; this->rle=rle; this->horisontal_flip=(imagedescriptor&0x10)!=0; this->vertical_flip=(imagedescriptor&0x20)!=0; this->data=reader.get_data(); this->compressed_size=reader.get_remained(); this->uncompressed_size=color_data_size; return reader.get_offset(); }