private void UpdateImageBox(DcmImageBox imageBox, String filename, int index) { try { DicomFileFormat ff = new DicomFileFormat(); ff.Load(filename, DicomReadOptions.DefaultWithoutDeferredLoading); if (ff.Dataset != null) { ff.Dataset.ChangeTransferSyntax(DicomTransferSyntax.ImplicitVRLittleEndian, null); DcmPixelData pixelData = new DcmPixelData(ff.Dataset); PhotometricInterpretation pi = PhotometricInterpretation.Lookup(pixelData.PhotometricInterpretation); // Grayscale only printer? if (pi.IsColor == true && _supportsColorPrinting == false) { pixelData.Unload(); return; } // Color only printer? if (pi.IsColor == false && _supportsGrayscalePrinting == false) { pixelData.Unload(); return; } DicomUID imageBoxSOPClassUID = null; DcmItemSequence seq = null; DcmItemSequenceItem item = new DcmItemSequenceItem(); pixelData.UpdateDataset(item.Dataset); if (pi.IsColor == true) { imageBoxSOPClassUID = DicomUID.BasicColorImageBoxSOPClass; seq = new DcmItemSequence(DicomTags.BasicColorImageSequence); } else { imageBoxSOPClassUID = DicomUID.BasicGrayscaleImageBoxSOPClass; seq = new DcmItemSequence(DicomTags.BasicGrayscaleImageSequence); } seq.AddSequenceItem(item); imageBox.Dataset.AddItem(seq); pixelData.Unload(); imageBox.UpdateImageBox(imageBoxSOPClassUID); imageBox.ImageBoxPosition = (ushort)index; } } catch (Exception) { } }
public void Decode(DcmDataset dataset, DcmPixelData oldPixelData, DcmPixelData newPixelData, DcmCodecParameters parameters) { if (oldPixelData.NumberOfFrames == 0) return; // Determine JPEG image precision and assert that the implemented codec supports this precision int precision; try { precision = JpegHelper.ScanHeaderForBitDepth(oldPixelData); } catch (DicomCodecException) { precision = oldPixelData.BitsStored; } AssertImagePrecision(precision); // Ensure consistency in the new pixel data header if (precision > 8) newPixelData.BitsAllocated = 16; else if (newPixelData.BitsStored <= 8) newPixelData.BitsAllocated = 8; // Set up new pixel data specifics newPixelData.PhotometricInterpretation = newPixelData.PhotometricInterpretation.Equals("YBR_FULL_422") || newPixelData.PhotometricInterpretation.Equals("YBR_PARTIAL_422") ? "YBR_FULL" : oldPixelData.PhotometricInterpretation; if (newPixelData.PhotometricInterpretation.Equals("YBR_FULL")) newPixelData.PlanarConfiguration = 1; try { for (int j = 0; j < oldPixelData.NumberOfFrames; ++j) { var frameData = new byte[newPixelData.UncompressedFrameSize]; var jpegStream = new MemoryStream(oldPixelData.GetFrameDataU8(j)); // Decode JPEG from stream var decoder = new JpegDecoder(jpegStream); var jpegDecoded = decoder.Decode(); var img = jpegDecoded.Image; // Init Buffer int w = img.Width; int h = img.Height; var pixelsFromJpeg = img.Raster; // Copy FluxJpeg buffer into frame data array /* int comps = pixelsFromJpeg.GetLength(0); int preIncr = newPixelData.BytesAllocated - comps; if (preIncr < 0) throw new InvalidOperationException( String.Format("Number of JPEG components: {0} exceeds number of bytes allocated: {1}", comps, newPixelData.BytesAllocated)); */ int i = 0; for (int y = 0; y < h; ++y) { for (int x = 0; x < w; ++x) { var pixel = pixelsFromJpeg[0][x, y]; frameData[i++] = (byte)((pixel >> 8) & 0xff); frameData[i++] = (byte)(pixel & 0xff); // for (int k = 0; k < preIncr; ++k) frameData[i++] = 0xff; // for (int k = 0; k < comps; ++k) frameData[i++] = pixelsFromJpeg[k][x, y]; } } oldPixelData.Unload(); if (newPixelData.IsPlanar) DcmCodecHelper.ChangePlanarConfiguration(frameData, frameData.Length / newPixelData.BytesAllocated, newPixelData.BitsAllocated, newPixelData.SamplesPerPixel, 0); newPixelData.AddFrame(frameData); } } catch (Exception e) { Debug.Log.Error("Failed to decode JPEG image: {0}, reason: {1}", e.StackTrace, e.Message); } }