src/image.cpp
// TODO: it is not necessary to break a raw chunk for two equal pixels (for the
// matter of the resulting size)
it is not necessary to break a raw chunk for two equal pixels (for the
matter of the resulting size)
it is not necessary to break a raw chunk for two equal pixels ( for the matter of the resulting size )
bool image::unload_rle_data(std::ostream& out) {
  const unsigned char max_chunk_length = 128;
  unsigned long npixels                = width_ * height_;
  unsigned long curpix                 = 0;
  while(curpix < npixels) {
    unsigned long chunkstart = curpix * bytespp_;
    unsigned long curbyte    = curpix * bytespp_;
    unsigned char run_length = 1;
    bool raw                 = true;
    while(curpix + run_length < npixels && run_length < max_chunk_length) {
      bool succ_eq = true;
      for(int t = 0; succ_eq && t < bytespp_; t++) {
        succ_eq = (data_[curbyte + t] == data_[curbyte + t + bytespp_]);
      }
      curbyte += bytespp_;
      if(1 == run_length) {
        raw = !succ_eq;
      }
      if(raw && succ_eq) {
        run_length--;
        break;
      }
      if(!raw && !succ_eq) {
        break;
      }
      run_length++;
    }
    curpix += run_length;
    out.put(raw ? run_length - 1 : run_length + 127);
    if(!out.good()) {
      std::cerr << "can't dump the tga file\n";
      return false;
    }
    out.write(
        (char*)(data_ + chunkstart), (raw ? run_length * bytespp_ : bytespp_));
    if(!out.good()) {
      std::cerr << "can't dump the tga file\n";
      return false;
    }
  }
  return true;
}
bool image :: unload_rle_data ( std :: ostream & out ) { const unsigned char max_chunk_length = 128 ; unsigned long npixels = width_ * height_ ; unsigned long curpix = 0 ; while ( curpix < npixels ) { unsigned long chunkstart = curpix * bytespp_ ; unsigned long curbyte = curpix * bytespp_ ; unsigned char run_length = 1 ; bool raw = true ; while ( curpix + run_length < npixels && run_length < max_chunk_length ) { bool succ_eq = true ; for ( int t = 0 ; succ_eq && t < bytespp_ ; t ++ ) { succ_eq = ( data_ [ curbyte + t ] == data_ [ curbyte + t + bytespp_ ] ) ; } curbyte += bytespp_ ; if ( 1 == run_length ) { raw = ! succ_eq ; } if ( raw && succ_eq ) { run_length -- ; break ; } if ( ! raw && ! succ_eq ) { break ; } run_length ++ ; } curpix += run_length ; out . put ( raw ? run_length - 1 : run_length + 127 ) ; if ( ! out . good ( ) ) { std :: cerr << " \n " ; return false ; } out . write ( ( char * ) ( data_ + chunkstart ) , ( raw ? run_length * bytespp_ : bytespp_ ) ) ; if ( ! out . good ( ) ) { std :: cerr << " \n " ; return false ; } } return true ; }
TODO: it is not necessary to break a raw chunk for two equal pixels (for the
matter of the resulting size)
TODO : it is not necessary to break a raw chunk for two equal pixels ( for the matter of the resulting size )