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 << "."; }
CHARLS_IMEXPORT JLS_ERROR JpegLsVerifyEncode(const void* pdataUncompressed, size_t cbyteUncompressed, const void* pdataCompressed, size_t cbyteBuffer) { JlsParamaters params = JlsParamaters(); JLS_ERROR error = JpegLsReadHeader(pdataCompressed, cbyteBuffer, ¶ms); if (error != OK) return error; error = CheckInput(pdataCompressed, cbyteBuffer, pdataUncompressed, cbyteUncompressed, ¶ms); if (error != OK) return error; Size size = Size(params.width, params.height); LONG cbit = params.bitspersample; JLSOutputStream stream; stream.Init(size, params.bitspersample, params.components); if (params.ilv == ILV_NONE) { LONG cbyteComp = size.cx*size.cy*((cbit +7)/8); for (LONG icomp = 0; icomp < params.components; ++icomp) { const BYTE* pbyteComp = static_cast<const BYTE*>(pdataUncompressed) + icomp*cbyteComp; stream.AddScan(pbyteComp, ¶ms); } } else { stream.AddScan(pdataUncompressed, ¶ms); } std::vector<BYTE> rgbyteCompressed; rgbyteCompressed.resize(cbyteBuffer + 16); memcpy(&rgbyteCompressed[0], pdataCompressed, cbyteBuffer); stream.EnableCompare(true); stream.Write(&rgbyteCompressed[0], cbyteBuffer); 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; 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; }
GDALDataset *JPEGLSDataset::Open( GDALOpenInfo * poOpenInfo ) { int bIsDCOM; if (!Identify(poOpenInfo, bIsDCOM)) return NULL; JlsParameters sParams; JLS_ERROR eError; int nOffset = 0; if (!bIsDCOM) { eError = JpegLsReadHeader(poOpenInfo->pabyHeader, poOpenInfo->nHeaderBytes, &sParams); } else { VSILFILE* fp = VSIFOpenL(poOpenInfo->pszFilename, "rb"); if (fp == NULL) return NULL; GByte abyBuffer[1028]; GByte abySignature[] = { 0xFF, 0xD8, 0xFF, 0xF7 }; while( true ) { if (VSIFReadL(abyBuffer, 1, 1028, fp) != 1028) { VSIFCloseL(fp); return NULL; } int i; for(i=0; i<1024; i++) { if (memcmp(abyBuffer + i, abySignature, 4) == 0) { nOffset += i; break; } } if (i != 1024) break; nOffset += 1024; VSIFSeekL(fp, nOffset, SEEK_SET); } VSIFSeekL(fp, nOffset, SEEK_SET); VSIFReadL(abyBuffer, 1, 1024, fp); eError = JpegLsReadHeader(abyBuffer, 1024, &sParams); VSIFCloseL(fp); if (eError == OK) { CPLDebug("JPEGLS", "JPEGLS image found at offset %d", nOffset); } } if (eError != OK) { CPLError(CE_Failure, CPLE_AppDefined, "Cannot read header : %s", JPEGLSGetErrorAsString(eError)); return NULL; } if (sParams.bitspersample > 16) { CPLError(CE_Failure, CPLE_NotSupported, "Unsupport bitspersample : %d", sParams.bitspersample); return NULL; } /* -------------------------------------------------------------------- */ /* Create a corresponding GDALDataset. */ /* -------------------------------------------------------------------- */ JPEGLSDataset *poDS; int iBand; poDS = new JPEGLSDataset(); poDS->osFilename = poOpenInfo->pszFilename; poDS->nRasterXSize = sParams.width; poDS->nRasterYSize = sParams.height; poDS->nBands = sParams.components; poDS->nBitsPerSample = sParams.bitspersample; poDS->nOffset = nOffset; /* -------------------------------------------------------------------- */ /* Create band information objects. */ /* -------------------------------------------------------------------- */ for( iBand = 1; iBand <= poDS->nBands; iBand++ ) { poDS->SetBand( iBand, new JPEGLSRasterBand( poDS, iBand) ); if (poDS->nBitsPerSample != 8 && poDS->nBitsPerSample != 16) { poDS->GetRasterBand(iBand)->SetMetadataItem( "NBITS", CPLString().Printf( "%d", poDS->nBitsPerSample ), "IMAGE_STRUCTURE" ); } } /* -------------------------------------------------------------------- */ /* Initialize any PAM information. */ /* -------------------------------------------------------------------- */ poDS->SetDescription( poOpenInfo->pszFilename ); poDS->TryLoadXML(); /* -------------------------------------------------------------------- */ /* Check for overviews. */ /* -------------------------------------------------------------------- */ poDS->oOvManager.Initialize( poDS, poOpenInfo->pszFilename ); return( poDS ); }