/** * Read a DICOM image input format file encoded in a compressed transfer syntax and extract the * compressed bitstreams into a file for each frame. * * <p>The output file will be named with the specified prefix to which will be appended a 6 digit * zero padded frame number starting from 1, followed by an appropriate file name extension for * the transfer syntax. * * @param arg two parameters, the inputFile, the outputFile prefix */ public static void main(String arg[]) { try { if (arg.length == 2) { DecimalFormat sixDigitZeroPaddedFormat = new DecimalFormat("000000"); AttributeList list = new AttributeList(); list.setDecompressPixelData(false); list.read(arg[0]); String fileNameExtension = getFileNameExtensionForCompressedPixelData(list); OtherByteAttributeMultipleCompressedFrames aPixelData = (OtherByteAttributeMultipleCompressedFrames) (list.getPixelData()); byte[][] frames = aPixelData.getFrames(); for (int f = 0; f < frames.length; ++f) { String outputFilename = arg[1] + sixDigitZeroPaddedFormat.format(f + 1) + "." + fileNameExtension; System.err.println("Writing " + outputFilename); FileOutputStream o = new FileOutputStream(outputFilename); o.write(frames[f]); o.close(); } } else { System.err.println("Error: Incorrect number of arguments"); System.err.println("Usage: UnencapsulateCompressedPixelData inputFile outputFilePrefix"); System.exit(1); } } catch (Exception e) { e.printStackTrace(); } }
public void TestSafePrivateGEPACSRelated_FromFile() throws Exception { AttributeList list = new AttributeList(); String testFilePath = System.getProperty("com.pixelmed.test.filepath"); // System.err.println("TestSafePrivateGEPACSRelated.TestSafePrivateGEPACSRelated_FromFile(): // testFilePath = "+testFilePath); list.read(new java.io.File(testFilePath, "philipsctwithgepacsprivateattributes.dcm")); // System.err.print("TestSafePrivateGEPACSRelated.TestSafePrivateGEPACSRelated():\n"+list); list.removeUnsafePrivateAttributes(); assertTrue( "Checking Creator is not removed", list.get(new AttributeTag(0x0903, 0x0010)) != null); assertTrue( "Checking Reject Image Flag is not removed", list.get(new AttributeTag(0x0903, 0x1010)) != null); }
public void TestSafePrivatePhilipsPETRelated_ScaleFactors_FromFile() throws Exception { AttributeList list = new AttributeList(); String testFilePath = System.getProperty("com.pixelmed.test.filepath"); // System.err.println("TestSafePrivatePhilipsPETRelated.TestSafePrivatePhilipsPETRelated_ScaleFactors_FromFile(): testFilePath = "+testFilePath); list.read(new java.io.File(testFilePath, "philipssuvandactivityscalefactors.dcm")); // System.err.print("TestSafePrivatePhilipsPETRelated.TestSafePrivatePhilipsPETRelated_ScaleFactors():\n"+list); list.removeUnsafePrivateAttributes(); assertTrue( "Checking Creator is not removed", list.get(new AttributeTag(0x7053, 0x0010)) != null); assertTrue( "Checking SUV Factor is not removed", list.get(new AttributeTag(0x7053, 0x1000)) != null); assertTrue( "Checking Activity Concentration Factor is not removed", list.get(new AttributeTag(0x7053, 0x1009)) != null); }
/** * @param arg the underlying image file name, the superimposed segmentation object file name, and * optionally the file name basis for a consumer format image rendering */ public static void main(String arg[]) { try { String underlyingFileName = arg[0]; String superimposedFileName = arg[1]; SuperimposedDicomSegments superimposedSegments = new SuperimposedDicomSegments(superimposedFileName); Vector<SuperimposedImage> superimposedImages = superimposedSegments.getSuperimposedImages(); if (arg.length > 2) { String outputFileName = arg[2]; ConsumerFormatImageMaker.convertFileToEightBitImage( underlyingFileName, outputFileName, "jpeg", 0 /*windowCenter*/, 0 /*windowWidth*/, 0 /*imageWidth*/, 0 /*imageHeight*/, 100 /*imageQuality*/, ConsumerFormatImageMaker.ALL_ANNOTATIONS, superimposedImages, null /*arrayOfPerFrameShapes*/, 0 /*debugLevel*/); } else { AttributeList underlyingList = new AttributeList(); underlyingList.read(underlyingFileName); SourceImage underlyingSourceImage = new SourceImage(underlyingList); GeometryOfVolume underlyingGeometry = new GeometryOfVolumeFromAttributeList(underlyingList); SingleImagePanel ip = new SingleImagePanel(underlyingSourceImage, null, underlyingGeometry); ip.setSuperimposedImages(superimposedImages); javax.swing.JFrame frame = new javax.swing.JFrame(); frame.add(ip); frame.setSize(underlyingSourceImage.getWidth(), underlyingSourceImage.getHeight()); frame.setVisible(true); } } catch (Exception e) { e.printStackTrace(System.err); } }
protected void doSomethingWithDicomFileOnMedia(String mediaFileName) { // logLn("OurFirstPassMediaImporter.doSomethingWithDicomFile(): "+mediaFileName); try { DicomInputStream i = new DicomInputStream(new File(mediaFileName)); AttributeList list = new AttributeList(); list.read(i); i.close(); String sopInstanceUID = Attribute.getSingleStringValueOrEmptyString(list, TagFromName.SOPInstanceUID); if (sopInstanceUID.length() > 0) { String studyInstanceUID = Attribute.getSingleStringValueOrEmptyString(list, TagFromName.StudyInstanceUID); if (studyInstanceUID.length() > 0) { mapOfSOPInstanceUIDToStudyInstanceUID.put(sopInstanceUID, studyInstanceUID); } else { throw new DicomException("Missing StudyInstanceUID"); } { CompositeInstanceContext cic = new CompositeInstanceContext(list, false /*forSR*/); // remove all except patient context ... cic.removeStudy(); cic.removeSeries(); cic.removeEquipment(); cic.removeFrameOfReference(); cic.removeInstance(); cic.removeSRDocumentGeneral(); Group group = addToGroups(list); // System.err.println("group = "+group); mergePatientContext(group, cic); } } else { throw new DicomException("Missing SOPInstanceUID"); } } catch (Exception e) { logLn("Error: File " + mediaFileName + " exception " + e); } }
/** * @param filenames * @exception Exception if internal error */ public void loadMultiPanelFromSpecifiedFiles(String filenames[]) throws Exception { int nFiles = filenames.length; SingleImagePanel imagePanels[] = new SingleImagePanel[nFiles]; String orientations[][] = new String[nFiles][]; String views[] = new String[nFiles]; String lateralityViewAndModifiers[] = new String[nFiles]; String lateralities[] = new String[nFiles]; int widths[] = new int[nFiles]; int heights[] = new int[nFiles]; PixelSpacing spacing[] = new PixelSpacing[nFiles]; String rowOrientations[] = new String[nFiles]; String columnOrientations[] = new String[nFiles]; HashMap eventContexts = new HashMap(); double maximumHorizontalExtentInMm = 0; double maximumVerticalExtentInMm = 0; StructuredReport sr[] = new StructuredReport[nFiles]; int nImages = 0; int nCAD = 0; ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); for (int f = 0; f < nFiles; ++f) { try { String filename = filenames[f]; DicomInputStream distream = null; InputStream in = classLoader.getResourceAsStream(filename); if (in != null) { distream = new DicomInputStream(in); } else { distream = new DicomInputStream(new File(filename)); } AttributeList list = new AttributeList(); list.read(distream); if (list.isImage()) { int i = nImages++; System.err.println("IMAGE [" + i + "] is file " + f + " (" + filenames[f] + ")"); orientations[i] = getPatientOrientation(list); // System.err.println("IMAGE ["+i+"] orientation="+(orientations[i] == null && // orientations[i].length == 2 ? "" : (orientations[i][0] + " " + orientations[i][1]))); views[i] = getView(list); // System.err.println("IMAGE ["+i+"] view="+views[i]); lateralityViewAndModifiers[i] = getImageLateralityViewModifierAndViewModifier(list); // System.err.println("IMAGE ["+i+"] // lateralityViewAndModifiers="+lateralityViewAndModifiers[i]); // System.err.println("File "+filenames[f]+": "+lateralityViewAndModifiers[i]); lateralities[i] = getLaterality(list); // System.err.println("IMAGE ["+i+"] laterality="+lateralities[i]); spacing[i] = new PixelSpacing(list); // System.err.println("IMAGE ["+i+"] spacing="+spacing[i]); SourceImage sImg = new SourceImage(list); BufferedImage img = sImg.getBufferedImage(); widths[i] = sImg.getWidth(); heights[i] = sImg.getHeight(); boolean shareVOIEventsInStudy = false; // does not seem to work anyway, since adding VOITransform to panel constructor // :( EventContext eventContext = new EventContext(Integer.toString(i)); SingleImagePanel imagePanel = makeNewImagePanel(sImg, eventContext); imagePanel.setDemographicAndTechniqueAnnotations( new DemographicAndTechniqueAnnotations(list), "SansSerif", Font.PLAIN, 10, Color.pink); imagePanel.setOrientationAnnotations( new OrientationAnnotations(rowOrientations[i], columnOrientations[i]), "SansSerif", Font.PLAIN, 20, Color.pink); imagePanel.setPixelSpacingInSourceImage( spacing[i].getSpacing(), spacing[i].getDescription()); if (Attribute.getSingleStringValueOrEmptyString(list, TagFromName.VOILUTFunction) .equals("SIGMOID")) { imagePanel.setVOIFunctionToLogistic(); } imagePanels[i] = imagePanel; } else { throw new DicomException("Unsupported SOP Class in file " + filenames[f]); } } catch (Exception e) { // FileNotFoundException,IOException,DicomException e.printStackTrace(System.err); } } // int imagesPerRow = nImages; // i.e., 1 -> 1, 2 -> 1, 4 -> 4, 5 -> 4, 8 -> 4 // int imagesPerCol = 1; int imagesPerRow = nImages >= 8 ? 8 : nImages; // i.e., 1 -> 1, 2 -> 1, 4 -> 4, 5 -> 4, 8 -> 4 int imagesPerCol = (nImages - 1) / imagesPerRow + 1; // i.e., 1 -> 1, 2 -> 2, 4 -> 1, 5 -> 2, 8 -> 2 int singleWidth = frameWidth / imagesPerRow; int singleHeight = frameHeight / imagesPerCol; if (nImages == 1 && singleWidth > singleHeight) { singleWidth = singleWidth / 2; // use only half the screen for a single view and a landscape monitor } for (int i = 0; i < nImages; ++i) { DisplayedAreaSelection displayedAreaSelection = null; displayedAreaSelection = new DisplayedAreaSelection( widths[i], heights[i], 0, 0, widths[i], heights[i], true, // in case spacing was not supplied 0, 0, 0, 0, 0, false /*crop*/); imagePanels[i].setDisplayedAreaSelection(displayedAreaSelection); imagePanels[i].setPreTransformImageRelativeCoordinates(null); } SingleImagePanel.deconstructAllSingleImagePanelsInContainer(multiPanel); multiPanel.removeAll(); multiPanel.setLayout(new GridLayout(imagesPerCol, imagesPerRow)); multiPanel.setBackground(Color.black); for (int x = 0; x < imagesPerCol; ++x) { for (int y = 0; y < imagesPerRow; ++y) { int i = x * imagesPerRow + y; if (i < nImages) { imagePanels[i].setPreferredSize(new Dimension(singleWidth, singleHeight)); multiPanel.add(imagePanels[i]); } } } frame.getContentPane().validate(); frame.getContentPane().repaint(); }
/** @param filename */ public SuperimposedDicomSegments(String filename) throws DicomException, IOException { // no need to call super(), does nothing AttributeList list = new AttributeList(); list.read(filename); doCommonConstructorStuff(list); }
protected void doSomethingWithDicomFileOnMedia(String mediaFileName) { // logLn("OurFirstPassMediaImporter.doSomethingWithDicomFile(): "+mediaFileName); try { DicomInputStream i = new DicomInputStream(new File(mediaFileName)); AttributeList list = new AttributeList(); list.read(i); i.close(); String sopInstanceUID = Attribute.getSingleStringValueOrEmptyString(list, TagFromName.SOPInstanceUID); if (sopInstanceUID.length() > 0) { Group group = mapOfSOPInstanceUIDToGroup.get(sopInstanceUID); // System.err.println("group = "+group); // System.err.println("Groups size = "+groups.size()); if (group == null) { if (groups.size() == 1) { group = groups.toArray(singleGroupArray)[0]; } else { throw new DicomException( "Cannot merge context for second set if more than one group"); } } if (group != null) { logLn("In group " + group.identity); if (group.context != null) { CompositeInstanceContext.removePatient( list); // remove anything hanging around, such as empty attributes list.putAll( group.context .getAttributeList()); // overwrite all patient context in list that was read // in } else { throw new DicomException( "Missing group context for SOPInstanceUID on second pass"); // should not be // possible } ClinicalTrialsAttributes.addContributingEquipmentSequence( list, true, new CodedSequenceItem("109103", "DCM", "Modifying Equipment"), "PixelMed", // Manufacturer "PixelMed", // Institution Name "Software Development", // Institutional Department Name "Bangor, PA", // Institution Address null, // Station Name "com.pixelmed.apps.MergeCompositeContext", // Manufacturer's Model Name null, // Device Serial Number "Vers. " + VersionAndConstants.getBuildDate(), // Software Version(s) "Merged patient context"); list.removeGroupLengthAttributes(); list.removeMetaInformationHeaderAttributes(); list.remove(TagFromName.DataSetTrailingPadding); FileMetaInformation.addFileMetaInformation( list, TransferSyntax.ExplicitVRLittleEndian, ourAETitle); File dstFile = new File( dstFolderName, MoveDicomFilesIntoHierarchy.makeHierarchicalPathFromAttributes(list)); if (dstFile.exists()) { throw new DicomException( "\"" + mediaFileName + "\": new file \"" + dstFile + "\" already exists - not overwriting"); } else { File dstParentDirectory = dstFile.getParentFile(); if (!dstParentDirectory.exists()) { if (!dstParentDirectory.mkdirs()) { throw new DicomException( "\"" + mediaFileName + "\": parent directory creation failed for \"" + dstFile + "\""); } } logLn("Writing with new context file " + dstFile); list.write(dstFile, TransferSyntax.ExplicitVRLittleEndian, true, true); } } else { throw new DicomException( "Missing group for SOPInstanceUID on second pass"); // should not be possible for // single set case } } else { throw new DicomException("Missing SOPInstanceUID"); } } catch (Exception e) { logLn("Error: File " + mediaFileName + " exception " + e); } }