Qt リソースから png を読み取るために libpng を使用しようとしています。問題点: 読み取りを行うクラスには、Qt の依存関係があってはなりません。
最初のステップで、 http://www.piko3d.net/tutorials/libpng-tutorial-loading-png-files-from-streams/#CustomReadを読んで、私はすでに関数を書くことに成功しました
read_png(istream& in)
地味な昔ながらのifstreamを渡すことにも成功しました
ifstream in("abs_path_to_png/icon.png");
read_png(..)に変更し、png を正常に読み取らせます。しかし、Qt リソースから (できればプラットフォームに依存しない) istream を取得するにはどうすればよいでしょうか? パフォーマンスは大きな問題ではないので、最初に思いついた
bool Io_Qt::get_istringstream_from_QFile(QFile& qfile, istringstream& iss)
{
// [.. Some checking for existence and the file being open ..]
QString qs(qfile.readAll());
iss.str(qs.toStdString());
// I also tried: QByteArray arr(qfile.readAll()); iss.str(arr.data());
return qfile.isOpen();
}
// Someplace else iss and qfile are created like this:
istringstream iss(std::stringstream::in | std::stringstream::binary);
QFile qfile(":/res/icon.png");
qfile.open(QIODevice::ReadOnly);
実際、これは、一見すると良さそうに見える iss を生成します。
cout << "'" << iss.str().c_str() << "'" << endl;
私は得る
'�PNG
'
ただし、空白の問題があるようです。為に
ifstream in("abs_path_to_png/icon.png");
char c;
cout << "'";
for (int j=0;j<8;j++)
{
in >> c;
cout << c;
}
cout << "'" << endl;
収量
'�PNG'
後者は機能しますが、前者のバリエーションは最終的に libpng チェック関数png_sig_cmp(..)を導き、私の png を無効として拒否します。私の最初の反射は「バイナリ」についてです。でも:
- istringstream iss(std::stringstream::in | std::stringstream::binary); 正しいと感じます。
- QIODevice::ReadOnly にはバイナリ パートナーがないようです。
私が見逃したものが見えますか?