void TestDicomSampleImage(const char* name) { std::vector<BYTE> data; bool success = ReadFile(name, &data, 9); Assert::IsTrue(success); BYTE pixeldataStart[] = { 0x00, 0x00, 0x01, 0x00, 0xFF, 0xD8, 0xFF, 0xF7 }; int offset = findstring(data, pixeldataStart, COUNT(pixeldataStart)); data.erase(data.begin(), data.begin() + offset - 4); // remove the dicom fragment headers (in the concerned images they occur every 64k) for (unsigned int i = 0; i < data.size(); i+= 64 * 1024) { data.erase(data.begin() + i, data.begin() + i + 8); } JlsParameters info; auto error = JpegLsReadHeader(&data[0], data.size(), &info, nullptr); // 0xFE, 0xFF, 0x00, 0xE0, 0x00, 0x00, 0x01, 0x00 std::vector<BYTE> dataUnc; dataUnc.resize(info.bytesperline * info.height); error = JpegLsDecode(&dataUnc[0], dataUnc.size(), &data[0], data.size(), nullptr, nullptr); Assert::IsTrue(error == charls::ApiResult::OK); std::cout << "."; }
CPLErr JPEGLSDataset::Uncompress() { if (bHasUncompressed) return CE_None; bHasUncompressed = TRUE; VSILFILE* fp = VSIFOpenL(osFilename, "rb"); if (!fp) return CE_Failure; VSIFSeekL(fp, 0, SEEK_END); int nFileSize = (int)VSIFTellL(fp) - nOffset; VSIFSeekL(fp, 0, SEEK_SET); GByte* pabyCompressedData = (GByte*)VSIMalloc(nFileSize); if (pabyCompressedData == NULL) { VSIFCloseL(fp); return CE_Failure; } VSIFSeekL(fp, nOffset, SEEK_SET); VSIFReadL(pabyCompressedData, 1, nFileSize, fp); VSIFCloseL(fp); fp = NULL; int nUncompressedSize = nRasterXSize * nRasterYSize * nBands * (GDALGetDataTypeSize(GetRasterBand(1)->GetRasterDataType()) / 8); pabyUncompressedData = (GByte*)VSI_MALLOC_VERBOSE(nUncompressedSize); if (pabyUncompressedData == NULL) { VSIFree(pabyCompressedData); return CE_Failure; } JLS_ERROR eError = JpegLsDecode( pabyUncompressedData, nUncompressedSize, pabyCompressedData, nFileSize, NULL); if (eError != OK) { CPLError( CE_Failure, CPLE_AppDefined, "Decompression of data failed : %s", JPEGLSGetErrorAsString(eError) ); VSIFree(pabyCompressedData); VSIFree(pabyUncompressedData); pabyUncompressedData = NULL; return CE_Failure; } VSIFree(pabyCompressedData); return CE_None; }
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; } } }