void TestRoundTrip(const char* strName, std::vector<BYTE>& rgbyteRaw, Size size, int cbit, int ccomp) { std::vector<BYTE> rgbyteCompressed(size.cx *size.cy * ccomp * cbit / 4); std::vector<BYTE> rgbyteOut(size.cx * size.cy * ((cbit + 7) / 8) * ccomp); double dblstart = getTime(); JlsParameters info = JlsParameters(); info.components = ccomp; info.bitspersample = cbit; info.height = size.cy; info.width = size.cx; if (ccomp == 4) { info.ilv = ILV_LINE; } else if (ccomp == 3) { info.ilv = ILV_LINE; info.colorTransform = COLORXFORM_HP1; } size_t compressedLength; JLS_ERROR err = JpegLsEncode(&rgbyteCompressed[0], rgbyteCompressed.size(), &compressedLength, &rgbyteRaw[0], rgbyteOut.size(), &info); assert(err == OK); double dwtimeEncodeComplete = getTime(); err = JpegLsDecode(&rgbyteOut[0], rgbyteOut.size(), &rgbyteCompressed[0], int(compressedLength), NULL); assert(err == OK); double bitspersample = compressedLength * 8 * 1.0 / (ccomp *size.cy * size.cx); double dwtimeDecodeComplete = getTime(); std::cout << "RoundTrip test for: " << strName << "\n\r"; double decodeTime = dwtimeDecodeComplete - dwtimeEncodeComplete; double symbolRate = (ccomp *size.cy * size.cx)/(1000.0 * decodeTime); printf("Size:%4ldx%4ld Encode:%7.2f Decode:%7.2f Bps:%5.2f Decode rate:%5.1f M/s \n\r", size.cx, size.cy, dwtimeEncodeComplete - dblstart, dwtimeDecodeComplete - dwtimeEncodeComplete, bitspersample, symbolRate); BYTE* pbyteOut = &rgbyteOut[0]; for (size_t i = 0; i < rgbyteOut.size(); ++i) { if (rgbyteRaw[i] != pbyteOut[i]) { assert(false); break; } } }
CHARLS_IMEXPORT(JLS_ERROR) JpegLsVerifyEncode(const void* uncompressedData, size_t uncompressedLength, const void* compressedData, size_t compressedLength) { JlsParameters info = JlsParameters(); JLS_ERROR error = JpegLsReadHeader(compressedData, compressedLength, &info); if (error != OK) return error; ByteStreamInfo rawStreamInfo = FromByteArray(uncompressedData, uncompressedLength); error = CheckInput(compressedData, compressedLength, rawStreamInfo, &info); if (error != OK) return error; Size size = Size(info.width, info.height); JLSOutputStream stream; stream.Init(size, info.bitspersample, info.components); if (info.ilv == ILV_NONE) { LONG fieldLength = size.cx*size.cy*((info.bitspersample +7)/8); for (LONG component = 0; component < info.components; ++component) { stream.AddScan(rawStreamInfo, &info); SkipBytes(&rawStreamInfo, fieldLength); } } else { stream.AddScan(rawStreamInfo, &info); } std::vector<BYTE> rgbyteCompressed(compressedLength + 16); memcpy(&rgbyteCompressed[0], compressedData, compressedLength); stream.EnableCompare(true); stream.Write(&rgbyteCompressed[0], rgbyteCompressed.size()); return OK; }
CHARLS_IMEXPORT(JLS_ERROR) JpegLsVerifyEncode(const void* uncompressedData, size_t uncompressedLength, const void* compressedData, size_t compressedLength) { JlsParameters info = JlsParameters(); JLS_ERROR error = JpegLsReadHeader(compressedData, compressedLength, &info); if (error != OK) return error; error = CheckInput(compressedData, compressedLength, uncompressedData, uncompressedLength, &info); if (error != OK) return error; Size size = Size(info.width, info.height); JLSOutputStream stream; stream.Init(size, info.bitspersample, info.components); if (info.ilv == ILV_NONE) { LONG cbyteComp = size.cx*size.cy*((info.bitspersample +7)/8); for (LONG component = 0; component < info.components; ++component) { const BYTE* compareData = static_cast<const BYTE*>(uncompressedData) + component*cbyteComp; stream.AddScan(compareData, &info); } } else { stream.AddScan(uncompressedData, &info); } std::vector<BYTE> rgbyteCompressed(compressedLength + 16); memcpy(&rgbyteCompressed[0], compressedData, compressedLength); stream.EnableCompare(true); stream.Write(&rgbyteCompressed[0], compressedLength); return OK; }