使用fstream读取/写入unsigned char *

时间:2014-09-06 02:17:03

标签: c++ fstream unsigned-char

我的这个功能有问题。它给出的结果与原始数据不匹配。我找不到问题的原因。编码和解码png工作正常,但是当我在编码之间保存并加载像素原始数据时,它无法读取。

#include <stdio.h>
#include <tchar.h>
#include "lodepng.h"
void ProcessImage(const char* scrPath, const char* tmpPath, const char* dstPath)
{
    unsigned error;
    unsigned char* pixelRaw;
    unsigned width, height;

    //load png
    error = lodepng_decode32_file(&pixelRaw, &width, &height, scrPath);
    if(error) 
        printf("error %u: %s\n", error, lodepng_error_text(error));

    //save pixels raw to file
    ofstream* _stream = new ofstream(tmpPath, ios::out|ios::binary);
    _stream->write(reinterpret_cast<const char*>(&pixelRaw), width * height);
    _stream->close();
    delete _stream;

    char* pixelRawTmp;
    unsigned char* pixelRaw2;

    // load pixels raws from file
    ifstream* _stream2 = new ifstream(tmpPath, ios::in|ios::binary);
    _stream2->read(pixelRawTmp,width*height);
    pixelRaw2 = reinterpret_cast<unsigned char*>(pixelRawTmp);
    delete _stream2;

    // saving png
    error = lodepng_encode32_file(dstPath, pixelRaw2, width, height);
    if(error) 
        printf("error %u: %s\n", error, lodepng_error_text(error));

    free(pixelRaw);
    free(pixelRawTmp);
    free(pixelRaw2);
}

0 个答案:

没有答案
相关问题