#include "header/FileHelper.h" #include #include #include bool FileHelper::ReadInputFile(QByteArray& arr, QString path, quint64 *width, quint64 *height) { QFile file(path); file.open(QIODevice::ReadOnly); char data[32000]; bool first = true; while (file.readLine(data, 32000)) { QString str(data); if(str.length() == 0) break; (*height)++; str = str.remove(str.length() - 1, 1); auto sl = str.split(","); if(first) { *width = sl.length(); first = false; } else if (sl.length() != *width) { qDebug() << "Some thing wrong here " << *width << " " << sl.length(); return false; } for(int i = 0; i < sl.length(); i++) { auto t = sl[i].PARSE; arr.append(static_cast(t)); arr.append(static_cast((t >> 8))); arr.append(static_cast((t >> 16))); arr.append(static_cast((t >> 24))); #ifdef USE_DBL arr.append(static_cast((t >> 32))); arr.append(static_cast((t >> 40))); arr.append(static_cast((t >> 48))); arr.append(static_cast((t >> 56))); #endif } } return true; } bool FileHelper::ReadInputFile(myint *arr, QString path, quint64 *width, quint64 *height) { QFile file(path); file.open(QIODevice::ReadOnly); char data[32000]; bool first = true; int index = 0; while (file.readLine(data, 32000)) { QString str(data); if(str.length() == 0) break; (*height)++; str = str.remove(str.length() - 1, 1); auto sl = str.split(","); if(first) { *width = sl.length(); first = false; } else if (sl.length() != *width) { qDebug() << "Some thing wrong here " << *width << " " << sl.length(); return false; } for(int i = 0; i < sl.length(); i++) { auto x = sl[i].PARSE; arr[index++] = x; } } return true; } void FileHelper::GetFileSIze(QString path, quint64 *width, quint64 *height) { QFile file(path); file.open(QIODevice::ReadOnly); char data[20000]; bool first = true; while (file.readLine(data, 20000)) { QString str(data); if(str.length() == 0) break; (*height)++; str = str.remove(str.length() - 1, 1); auto sl = str.split(","); *width = sl.length(); } } bool FileHelper::WriteOutputFile(char* arr, QString path, quint64 width, quint64 height) { QFile file(path); file.open(QIODevice::WriteOnly); for(int i = 0; i < height;i++) { int index = i * sizeof (myflt) * width; QString str; for(int j = 0; j < width; j++) { #ifdef USE_DBL quint64 temp = 0; temp = (((quint64)(arr[index++] & 0xFF))) | (((quint64)(arr[index++] & 0xFF)) << 8) | (((quint64)(arr[index++] & 0xFF)) << 16) | (((quint64)(arr[index++] & 0xFF)) << 24) | (((quint64)(arr[index++] & 0xFF)) << 32) | (((quint64)(arr[index++] & 0xFF)) << 40) | (((quint64)(arr[index++] & 0xFF)) << 48) | (((quint64)(arr[index++] & 0xFF)) << 56); auto d = *((double*)(&temp)); #else quint32 temp = 0; temp = (((quint64)(arr[index++] & 0xFF))) | (((quint64)(arr[index++] & 0xFF)) << 8) | (((quint64)(arr[index++] & 0xFF)) << 16) | (((quint64)(arr[index++] & 0xFF)) << 24); auto d = *((float*)(&temp)); #endif str += QString("%1").arg(temp, 8, 16, QChar('0')); if(j != width - 1) str += ","; } str += "\n"; file.write(str.toLocal8Bit()); } return true; } bool FileHelper::WriteOutputFile(myflt* arr, QString path, quint64 width, quint64 height) { QFile file(path); file.open(QIODevice::WriteOnly); for(int i = 0; i < height;i++) { int index = i * width; QString str; for(int j = 0; j < width; j++) { str += QString::number(arr[index + j], 'f', 6); if(j != width - 1) str += ","; } str += "\n"; file.write(str.toLocal8Bit()); } return true; }