|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * BSD implementations of Bio-Formats readers and writers |
| 4 | + * %% |
| 5 | + * Copyright (C) 2005 - 2023 Open Microscopy Environment: |
| 6 | + * - Board of Regents of the University of Wisconsin-Madison |
| 7 | + * - Glencoe Software, Inc. |
| 8 | + * - University of Dundee |
| 9 | + * %% |
| 10 | + * Redistribution and use in source and binary forms, with or without |
| 11 | + * modification, are permitted provided that the following conditions are met: |
| 12 | + * |
| 13 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 14 | + * this list of conditions and the following disclaimer. |
| 15 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 16 | + * this list of conditions and the following disclaimer in the documentation |
| 17 | + * and/or other materials provided with the distribution. |
| 18 | + * |
| 19 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 20 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 22 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 23 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 24 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 25 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 27 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 28 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 29 | + * POSSIBILITY OF SUCH DAMAGE. |
| 30 | + * #L% |
| 31 | + */ |
| 32 | + |
| 33 | +package loci.formats.dicom; |
| 34 | + |
| 35 | +import java.io.IOException; |
| 36 | +import java.util.ArrayList; |
| 37 | +import java.util.List; |
| 38 | + |
| 39 | +import loci.common.Constants; |
| 40 | +import loci.common.DataTools; |
| 41 | + |
| 42 | +import org.json.JSONArray; |
| 43 | +import org.json.JSONException; |
| 44 | +import org.json.JSONObject; |
| 45 | + |
| 46 | +import org.slf4j.Logger; |
| 47 | +import org.slf4j.LoggerFactory; |
| 48 | + |
| 49 | +/** |
| 50 | + * Provide DICOM tags from a file containing JSON. |
| 51 | + * Formal JSON schema yet to be determined, but the idea is to accept a hierarchy |
| 52 | + * of tags of the form: |
| 53 | + * |
| 54 | + * { |
| 55 | + * "BodyPartExamined": { |
| 56 | + * "Value": "BRAIN", |
| 57 | + * "VR": "CS", |
| 58 | + * "Tag": "(0018,0015)" |
| 59 | + * } |
| 60 | + * "ContributingEquipmentSequence": { |
| 61 | + * "VR": "SQ", |
| 62 | + * "Tag": "(0018,a001)", |
| 63 | + * "Sequence": { |
| 64 | + * "Manufacturer": { |
| 65 | + * "Value": "PixelMed", |
| 66 | + * "VR": "LO", |
| 67 | + * "Tag": "(0008,0070)" |
| 68 | + * }, |
| 69 | + * "ContributionDateTime": { |
| 70 | + * "Value": "20210710234601.105+0000", |
| 71 | + * "VR": "DT", |
| 72 | + * "Tag": "(0018,a002)" |
| 73 | + * } |
| 74 | + * } |
| 75 | + * } |
| 76 | + * } |
| 77 | + * |
| 78 | + * This is similar to JSON examples in https://github.com/QIICR/dcmqi/tree/master/doc/examples, |
| 79 | + * but allows for the VR (type) and tag to be explicitly stated, in addition to |
| 80 | + * the more human-readable description. |
| 81 | + */ |
| 82 | +public class DicomJSONProvider implements ITagProvider { |
| 83 | + |
| 84 | + private static final Logger LOGGER = |
| 85 | + LoggerFactory.getLogger(DicomJSONProvider.class); |
| 86 | + |
| 87 | + private List<DicomTag> tags = new ArrayList<DicomTag>(); |
| 88 | + |
| 89 | + @Override |
| 90 | + public void readTagSource(String location) throws IOException { |
| 91 | + String rawJSON = DataTools.readFile(location); |
| 92 | + try { |
| 93 | + JSONObject root = new JSONObject(rawJSON); |
| 94 | + |
| 95 | + for (String tagKey : root.keySet()) { |
| 96 | + JSONObject tag = root.getJSONObject(tagKey); |
| 97 | + String value = tag.has("Value") ? tag.getString("Value") : null; |
| 98 | + |
| 99 | + Integer intTagCode = lookupTag(tagKey, tag); |
| 100 | + DicomVR vrEnum = lookupVR(intTagCode, tag); |
| 101 | + |
| 102 | + DicomTag dicomTag = new DicomTag(intTagCode, vrEnum); |
| 103 | + dicomTag.value = value; |
| 104 | + LOGGER.debug("Adding tag: {}, VR: {}, value: {}", |
| 105 | + DicomAttribute.formatTag(intTagCode), vrEnum, value); |
| 106 | + dicomTag.validateValue(); |
| 107 | + |
| 108 | + if (vrEnum == DicomVR.SQ && tag.has("Sequence")) { |
| 109 | + readSequence(tag, dicomTag); |
| 110 | + } |
| 111 | + |
| 112 | + ResolutionStrategy rs = vrEnum == DicomVR.SQ ? ResolutionStrategy.APPEND : ResolutionStrategy.REPLACE; |
| 113 | + if (tag.has("ResolutionStrategy")) { |
| 114 | + String strategy = tag.getString("ResolutionStrategy"); |
| 115 | + rs = Enum.valueOf(ResolutionStrategy.class, strategy); |
| 116 | + } |
| 117 | + dicomTag.strategy = rs; |
| 118 | + |
| 119 | + tags.add(dicomTag); |
| 120 | + } |
| 121 | + } |
| 122 | + catch (JSONException e) { |
| 123 | + throw new IOException("Could not parse JSON", e); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public List<DicomTag> getTags() { |
| 129 | + return tags; |
| 130 | + } |
| 131 | + |
| 132 | + private void readSequence(JSONObject rootTag, DicomTag parent) { |
| 133 | + JSONObject sequence = rootTag.getJSONObject("Sequence"); |
| 134 | + for (String key : sequence.keySet()) { |
| 135 | + JSONObject tag = sequence.getJSONObject(key); |
| 136 | + |
| 137 | + Integer intTagCode = lookupTag(key, tag); |
| 138 | + DicomVR vrEnum = lookupVR(intTagCode, tag); |
| 139 | + |
| 140 | + DicomTag dicomTag = new DicomTag(intTagCode, vrEnum); |
| 141 | + |
| 142 | + if (tag.has("Value")) { |
| 143 | + dicomTag.value = tag.get("Value"); |
| 144 | + } |
| 145 | + |
| 146 | + LOGGER.debug("Adding tag: {}, VR: {}, value: {}", intTagCode, vrEnum, dicomTag.value); |
| 147 | + dicomTag.validateValue(); |
| 148 | + |
| 149 | + dicomTag.parent = parent; |
| 150 | + parent.children.add(dicomTag); |
| 151 | + |
| 152 | + if (vrEnum == DicomVR.SQ && tag.get("Sequence") != null) { |
| 153 | + readSequence(tag, dicomTag); |
| 154 | + } |
| 155 | + } |
| 156 | + parent.children.sort(null); |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Get the tag code corresponding to the given JSON object. |
| 161 | + * If "Tag" is not a defined attribute, lookup the default |
| 162 | + * for the object name in the dictionary. |
| 163 | + */ |
| 164 | + private Integer lookupTag(String tagKey, JSONObject tag) { |
| 165 | + Integer intTagCode = null; |
| 166 | + |
| 167 | + if (tag.has("Tag")) { |
| 168 | + String[] tagCode = tag.getString("Tag").replaceAll("[()]", "").split(","); |
| 169 | + |
| 170 | + int tagUpper = Integer.parseInt(tagCode[0], 16); |
| 171 | + int tagLower = Integer.parseInt(tagCode[1], 16); |
| 172 | + |
| 173 | + intTagCode = tagUpper << 16 | tagLower; |
| 174 | + } |
| 175 | + else { |
| 176 | + intTagCode = DicomAttribute.getTag(tagKey); |
| 177 | + |
| 178 | + if (intTagCode == null) { |
| 179 | + throw new IllegalArgumentException( |
| 180 | + "Tag not defined and could not be determined from description '" + |
| 181 | + tagKey + "'"); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + return intTagCode; |
| 186 | + } |
| 187 | + |
| 188 | + /** |
| 189 | + * Get the VR associated with the given tag code and JSON object. |
| 190 | + * If "VR" is not a defined attribute, lookup the default for the |
| 191 | + * given tag code in the dictionary. |
| 192 | + */ |
| 193 | + private DicomVR lookupVR(Integer intTagCode, JSONObject tag) { |
| 194 | + DicomVR vrEnum = DicomAttribute.getDefaultVR(intTagCode); |
| 195 | + if (tag.has("VR")) { |
| 196 | + DicomVR userEnum = DicomVR.valueOf(DicomVR.class, tag.getString("VR")); |
| 197 | + if (!vrEnum.equals(userEnum)) { |
| 198 | + LOGGER.warn("User-defined VR ({}) for {} does not match expected VR ({})", |
| 199 | + userEnum, DicomAttribute.formatTag(intTagCode), vrEnum); |
| 200 | + if (userEnum != null) { |
| 201 | + vrEnum = userEnum; |
| 202 | + } |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + return vrEnum; |
| 207 | + } |
| 208 | + |
| 209 | + |
| 210 | + |
| 211 | +} |
0 commit comments