@Override public DicomObject toDataset() throws Exception { DicomObject attrs = super.toDataset(); attrs.putBytes(Tag.EncapsulatedDocument, VR.OB, getDocumentContent()); return attrs; }
private static DicomObject toDicom(BufferedImage image) throws Exception { int numSamples = image.getColorModel().getNumComponents(); if (numSamples == 4) { numSamples = 3; } String pmi = numSamples > 1 ? "RGB" : "MONOCHROME2"; int bits = image.getColorModel().getComponentSize(0); int allocated = 8; if (bits > 8) { allocated = 16; } DicomObject attrs = new BasicDicomObject(); attrs.putString(Tag.TransferSyntaxUID, VR.UI, UID.ImplicitVRLittleEndian); attrs.putInt(Tag.Rows, VR.US, image.getHeight()); attrs.putInt(Tag.Columns, VR.US, image.getWidth()); attrs.putInt(Tag.SamplesPerPixel, VR.US, numSamples); attrs.putInt(Tag.BitsStored, VR.US, bits); attrs.putInt(Tag.BitsAllocated, VR.US, allocated); attrs.putInt(Tag.PixelRepresentation, VR.US, 0); attrs.putString(Tag.PhotometricInterpretation, VR.CS, pmi); if (numSamples > 1) { attrs.putInt(Tag.PlanarConfiguration, VR.US, 0); } int biType = numSamples == 3 ? BufferedImage.TYPE_INT_RGB : allocated > 8 ? BufferedImage.TYPE_USHORT_GRAY : BufferedImage.TYPE_BYTE_GRAY; BufferedImage tmpImage = image; if (image.getType() != biType) { tmpImage = new BufferedImage(image.getWidth(), image.getHeight(), biType); tmpImage.getGraphics().drawImage(image, 0, 0, null); } byte[] pixelData = null; DataBuffer dataBuffer = tmpImage.getRaster().getDataBuffer(); if (dataBuffer instanceof DataBufferInt) { final int[] data = (int[]) ((DataBufferInt) dataBuffer).getData(); pixelData = new byte[data.length * 3]; int index = 0; for (final int i : data) { pixelData[index++] = (byte) ((i >>> 16) & 0xFF); pixelData[index++] = (byte) ((i >>> 8) & 0xFF); pixelData[index++] = (byte) (i & 0xFF); } } else if (dataBuffer instanceof DataBufferUShort) { final short[] data = (short[]) ((DataBufferUShort) dataBuffer).getData(); pixelData = new byte[data.length * 2]; int index = 0; for (final int i : data) { pixelData[index++] = (byte) ((i >>> 8) & 0xFF); pixelData[index++] = (byte) (i & 0xFF); } } else if (dataBuffer instanceof DataBufferByte) { pixelData = ((DataBufferByte) dataBuffer).getData(); } attrs.putBytes(Tag.PixelData, allocated > 8 ? VR.OW : VR.OB, pixelData); return attrs; }