celia
celia

Reputation: 67

Why can`t I visualize this DICOM file?

I am trying to create a new DICOM file from a jpg + metadata

I tried this code, and I generate a final DICOM file, but when I open it I can see nothing. I am using RadiAnt DICOM viewer.

    BufferedImage jpg = ImageIO.read(new File("myjpg.jpg"));

    // Convert the image to a byte array

    DataBuffer buff = jpg.getData().getDataBuffer();
    DataBufferUShort buffer = new DataBufferUShort(buff.getSize());

    for (int i = 0; i < buffer.getSize(); ++i)
        buffer.setElem(i, buff.getElem(i));

    short[] data = buffer.getData();
    ByteBuffer byteBuf = ByteBuffer.allocate(2 * data.length);
    int i = 0;
    while (data.length > i) {
        byteBuf.putShort(data[i]);
        i++;
    }

    // Copy a header

    Attributes attribs = new Attributes();
    Attributes meta = new Attributes();

    //Metadatos header
    meta.setInt(Tag.FileMetaInformationGroupLength, VR.UL, data.length);
    meta.setString(Tag.TransferSyntaxUID, VR.UI, "1.2.840.10008.1.2.1");


    meta.setString(Tag.MediaStorageSOPClassUID, VR.UI, "1");
    meta.setString(Tag.SourceApplicationEntityTitle, VR.AE, "2");

    meta.setString(Tag.ImplementationClassUID, VR.UI, "3");
    meta.setString(Tag.ImplementationVersionName, VR.SH,"4");


    // Change the rows and columns
    attribs.setInt(Tag.Rows, VR.US, jpg.getHeight());
    attribs.setInt(Tag.Columns, VR.US, jpg.getWidth());
    System.out.println(byteBuf.array().length);

    // Write the file
    attribs.setBytes(Tag.PixelData, VR.OW, byteBuf.array());
    DicomOutputStream dcmo = new DicomOutputStream(new File("myDicom.dcm"));
    dcmo.writeFileMetaInformation(meta);
    attribs.writeTo(dcmo);
    dcmo.close();

I generate MyDicom, and I confirm that all metdata is inside but I can´t see the DICOM image.

Upvotes: 0

Views: 790

Answers (1)

Markus Sabin
Markus Sabin

Reputation: 4013

I cannot see how the buffer is obtained from the JPEG. But if the buffer is still JPEG compressed, the Transfer Syntax should be set appropriately. Supposing that you have a color image, the TS is most probably JPEG Process 1 = 1.2.840.10008.1.2.4.50.

Furthermore, lots of attributes are missing for a valid DICOM file, some of them even refer to the pixel data and are definitely needed for reading it properly:

  • Photometric Interpretation (0028,0004) = YBR_FULL_422
  • Planar Configuration (0028,0006) = 0 (color by pixel)
  • Samples per Pixel (0028,0002) = 3

(again, the values given are based on the assumption that you are writing encapsulated JPEG to the pixel data, they would look quite different for a BMP style structure of the pixel data).

Furthermore, you need at least unique identifiers for the Study Instance UID (0020,000d), Series Instance UID (0020, 000e) and SOP Instance UID (0008,0018)

The SOP Class UID (0008,0016) tells the viewer which type of image it is, you cannot go without it.

And depending from the SOP Class UID additional attribute will become mandatory. Some of them (e.g. Patient Name (0010,0010)) may be left empty ("zero length") but have to be present.

In a whole: like I mentioned earlier, you will not succeed with your task without a little basic knowledge about DICOM. DICOM is much more than just an image format. I do understand that you would like to avoid that, but it is inevitable.

Probably the type of image which is easiest to create from scratch is the Secondary Capture Image (SOP Class UID: 1.2.840.10008.5.1.4.1.1.7), and this page nicely helps you to gather all attributes you need to include.

Upvotes: 1

Related Questions