2016-10-11 9 views
1

可変スライスの長さ、画像の高さが640、画像の幅が512のボリュームファイルがあります。高さが620、幅がファイルに変換したい420のファイルを作成し、新しいファイルに書き込みます。私は、高さの上から20ピクセルを取り除き、このサイズ変更を行うために幅の各辺を46から取り除きたいと思います。各ピクセルは符号なし16ビットです。私は実際の操作をどうやって行うのか分かりません。ボリュームファイルの長さと幅をC++でサイズ変更

これは私が私のメインに持っているものです。

FILE* pfile = NULL; 
FILE* pfile2 = NULL; 

pFile = fopen("test.vol", "rb"); 
fseek (pFile , 0 , SEEK_END); 

int fileData = ftell(pFile); 
rewind(pFile); 

char* FileBuffer = new char[fileData]; 
fread(FileBuffer, fileData, 1, pFile); 

int height = 640; 
int width = 512; 
int slizeSize = height * width * 2; 
int sliceCount = fileData/sliceSize; 
uint16_t pixels; 

int newHeight = height - 20; 
int newWidth = width - 92; 
int newSliceSize = newHeight * newWidth * 2; 
int newImageSize = newSliceSize * sliceCount; 

char* NewFileBuffer = new char[newImageSize]; 

for (int i = 0; i < newHeight; i++) { 

    for (int i = 0; i < newWidth; i++) { 

    } 
    // need help in these for loops and after 

} 

fclose (pFile); 
free (FileBuffer); 

pFile2 = fopen("test2.vol", "wb"); 
fwrite(NewFileBuffer, NewImageSize, 1, pFile2); 

答えて

2

あなたがC++でプログラミングしている場合は、C++を使用します。FILE*と他のCのものを取り除きます。

あなたはnewcharの配列を割り当てるが、メモリを解放するfreeを呼び出している:あなたはdelete[]を呼び出す必要があり、またはより良いあなたはstd::vectorを使用すると、それはあなたのためのメモリを管理させてください。

16ビットの値を扱っているので、16ビットの値を読み書き/処理しないのはなぜですか?

まず、ファイル読み込み:今

#include <fstream> 
#include <iterator> 
#include <vector> 

int main() 
{ 
    using data_type = uint16_t; 

    std::basic_ifstream<data_type> file_in{ 
     "test.vol", 
     std::ifstream::binary 
    }; 
    std::vector<data_type> FileBuffer{ 
     std::istreambuf_iterator<data_type>(file_in), 
     std::istreambuf_iterator<data_type>() // end of stream iterator 
    }; 

FileBuffer管理するメモリ割り当てと解放と、"test.vol"の内容で満たされuint16_tの配列です。

次の部分では、ボリュームのサイズを変更します。私はあなたのデータが列、線、スライスの順にパックされていると仮定します。先頭がスライスの最初の行か最後の行かは不明です。ここに私の命題はイテレータとstd::copyを使用している:

size_t height = 640, width = 512; 
    size_t sliceSize = height * width; 
    size_t sliceCount = FileBuffer.size()/sliceSize; 

    size_t newHeight = height - 20, newWidth = width - 92; 
    size_t newSliceSize = newHeight * newWidth; 
    size_t newImageSize = newSliceSize * sliceCount; 

    std::vector<data_type> NewFileBuffer(newImageSize); 

    auto it_buffer = FileBuffer.begin(); 
    auto it_new_buffer = NewFileBuffer.begin(); 

    for (size_t i = 0; i < sliceCount; ++i) 
    { 
     // skip 20 first lines, remove and uncomment the next line 
     // if you want to skip the last ones 
     auto it_line = it_buffer + 20*width; 
     //auto it_line = it_buffer; 

     for (size_t j = 0; j < newHeight; ++j) 
     { 
      auto it_column = it_line + 46; 

      it_new_buffer = std::copy(
       it_column, 
       it_column + newWidth, 
       it_new_buffer 
      ); 

      it_line += width; // go to next line 
     } 

     it_buffer += sliceSize; // go to next slice 
    } 

最後に、ファイル書き込み:私はこれをコンパイルするときおかげで、しかし、イムは、エラーのトンを取得

std::basic_ofstream<data_type> file_out{ 
     "test2.vol", 
     std::ofstream::binary 
    }; 
    std::copy(
     NewFileBuffer.begin(), 
     NewFileBuffer.end(), 
     std::ostreambuf_iterator<data_type>(file_out) 
    ); 
} 
+0

を。エラー: 'data_type'の前に予想されるネストされた名前指定子がエラーの連鎖反応を引き起こしたようです – user1984300

+0

私はC++ 11と98でコンパイルを試みましたが、まだエラーがあります – user1984300

+0

問題を修正しました。現在、私は "std :: bad_cast"のインスタンスをスローした後に呼び出されて終了しています。 何():std :: bad_cast アボート(コアダンプ) – user1984300

関連する問題