private static VR getVR(String vr) { try { return VR.valueOf(vr); } catch (Exception e) { return VR.OB; } }
@Override public int hashCode() { final int prime = 31; int hashCode = 1; for (Object e : this) hashCode = prime * hashCode + itemHashCode(e); hashCode = prime * hashCode + (bigEndian ? 1231 : 1237); hashCode = prime * hashCode + ((vr == null) ? 0 : vr.hashCode()); return hashCode; }
private static TagType getTypeFromTag(int tagID, VR vr) { if (vr != null) { if (vr.isIntType()) { return TagType.INTEGER; } else if (vr.isTemporalType()) { if (VR.DA.equals(vr)) { return TagType.DICOM_DATE; } else if (VR.TM.equals(vr)) { return TagType.DICOM_TIME; } else if (vr.isStringType()) { if (VR.DS.equals(vr)) { return TagType.DOUBLE; } else if (VR.PN.equals(vr)) { return TagType.DICOM_PERSON_NAME; } else if (VR.UR.equals(vr)) { return TagType.URI; } else if (VR.AS.equals(vr)) { return TagType.DICOM_PERIOD; } else if (Tag.PatientSex == tagID) { return TagType.DICOM_SEX; } else if (VR.LT.equals(vr) || VR.ST.equals(vr) || VR.UT.equals(vr)) { return TagType.TEXT; } else if (VR.SQ.equals(vr)) { return TagType.DICOM_SEQUENCE; } else { if (VR.FD.equals(vr)) { return TagType.DOUBLE;
public void internalizeStringValues(boolean decode) { SpecificCharacterSet cs = getSpecificCharacterSet(); for (int i = 0; i < values.length; i++) { VR vr = vrs[i]; Object value = values[i]; if (vr.isStringType()) { if (value instanceof byte[]) { if (!decode) continue; value = vr.toStrings((byte[]) value, bigEndian, cs); } if (value instanceof String) values[i] = ((String) value).intern(); else if (value instanceof String[]) { String[] ss = (String[]) value; for (int j = 0; j < ss.length; j++) ss[j] = ss[j].intern(); } } else if (value instanceof Sequence) for (Attributes item : (Sequence) value) item.internalizeStringValues(decode); } }
private void writeValues(VR vr, Object val, boolean bigEndian, SpecificCharacterSet cs) throws SAXException { if (vr.isStringType()) val = vr.toStrings(val, bigEndian, cs); int vm = vr.vmOf(val); for (int i = 0; i < vm; i++) { String s = vr.toString(val, bigEndian, i, null); addAttribute("number", Integer.toString(i + 1)); if (vr == VR.PN) { PersonName pn = new PersonName(s, true); startElement("PersonName"); writePNGroup("Alphabetic", pn, PersonName.Group.Alphabetic); writePNGroup("Ideographic", pn, PersonName.Group.Ideographic); writePNGroup("Phonetic", pn, PersonName.Group.Phonetic); endElement("PersonName"); } else { writeElement("Value", s); } } }
if (vr.isStringType()) { value = decodeStringValue(index); int vm = vr.vmOf(value); if (el.maxVM > 0 && vm > el.maxVM || el.minVM > 1 && vm < el.minVM) { if (!vr.isStringType()) { result.addInvalidAttributeValue(el, ValidationResult.Invalid.VR); return; if (vr == VR.IS) value = decodeISValue(index); else if (!vr.isIntType()) { result.addInvalidAttributeValue(el, ValidationResult.Invalid.VR); return; if (!isValidValue(vr.toInts(value, bigEndian), el.valueNumber, (int[]) validVals)) { result.addInvalidAttributeValue(el, ValidationResult.Invalid.Value);
private void writeDoubleValues(VR vr, Object val, boolean bigEndian) { gen.writeStartArray("Value"); int vm = vr.vmOf(val); for (int i = 0; i < vm; i++) { double d = vr.toDouble(val, bigEndian, i, 0); if (Double.isNaN(d)) { LOG.info("encode {} NaN as null", vr); gen.writeNull(); } else { if (d == Double.POSITIVE_INFINITY) { d = Double.MAX_VALUE; LOG.info("encode {} Infinity as {}", vr, d); } else if (d == Double.NEGATIVE_INFINITY) { d = -Double.MAX_VALUE; LOG.info("encode {} -Infinity as {}", vr, d); } gen.write(d); } } gen.writeEnd(); }
private void promptPreamble(byte[] preamble) { if (preamble == null) return; StringBuilder line = new StringBuilder(width); line.append("0: ["); if (VR.OB.prompt(preamble, false, null, width - 5, line)) line.append(']'); System.out.println(line); }
private void writeAttribute(int tag, VR vr, Object value, SpecificCharacterSet cs, Attributes attrs) throws SAXException { if (TagUtils.isGroupLength(tag) || TagUtils.isPrivateCreator(tag)) return; String privateCreator = attrs.getPrivateCreator(tag); addAttributes(tag, vr, privateCreator); startElement("DicomAttribute"); if (value instanceof Value) writeAttribute((Value) value, attrs.bigEndian()); else if (!vr.isInlineBinary()) { writeValues(vr, value, attrs.bigEndian(), attrs.getSpecificCharacterSet(vr)); } else if (value instanceof byte[]) { writeInlineBinary(attrs.bigEndian() ? vr.toggleEndian((byte[]) value, true) : (byte[]) value); } else throw new IllegalArgumentException("vr: " + vr + ", value class: " + value.getClass()); endElement("DicomAttribute"); }
public String getString(String privateCreator, int tag, VR vr, int valueIndex, String defVal) { int index = indexOf(privateCreator, tag); if (index < 0) return defVal; Object value = values[index]; if (value == Value.NULL) return defVal; if (vr == null) vr = vrs[index]; else updateVR(index, vr); if (vr.isStringType()) { value = decodeStringValue(index); if (value == Value.NULL) return defVal; } try { return vr.toString(value, bigEndian, valueIndex, defVal); } catch (UnsupportedOperationException e) { LOG.info("Attempt to access {} {} as string", TagUtils.toString(tag), vr); return defVal; } }
return false; if (vr.isStringType()) { try { if (vr == VR.IS) if (v2 instanceof byte[] && ((byte[]) v1).length == ((byte[]) v2).length) { if (bigEndian != other.bigEndian) v2 = vr.toggleEndian((byte[]) v2, true); return Arrays.equals((byte[]) v1, (byte[]) v2);
buf.append(" ["); //$NON-NLS-1$ VR vr = dcmObj.getVR(tag); buf.append(vr.toString()); buf.append("] "); //$NON-NLS-1$ if (vr.isInlineBinary()) { buf.append("binary data"); //$NON-NLS-1$ printItem(doc, buf.toString(), null);
public void readHeader() throws IOException { byte[] buf = buffer; tagPos = pos; readFully(buf, 0, 8); encodedVR = 0; switch(tag = ByteUtils.bytesToTag(buf, 0, bigEndian)) { case Tag.Item: case Tag.ItemDelimitationItem: case Tag.SequenceDelimitationItem: vr = null; break; default: if (explicitVR) { vr = VR.valueOf(encodedVR = ByteUtils.bytesToVR(buf, 4)); if (vr.headerLength() == 8) { length = ByteUtils.bytesToUShort(buf, 6, bigEndian); return; } readFully(buf, 4, 4); } else { vr = VR.UN; } } length = ByteUtils.bytesToInt(buf, 4, bigEndian); }
public int getDicomValueMultiplicity(Object value) { if (value == null) { return 0; } try { return vr.vmOf(value); } catch (Exception e) { LOGGER.error("Cannot evaluate mulitplicity from DICOM VR", e); //$NON-NLS-1$ } return getValueMultiplicity(value); }
public DataElement setValues(int... values) { if (!vr.isIntType()) throw new IllegalStateException("vr=" + vr); this.values = values; return this; }
private void updateVR(int index, VR vr) { VR prev = vrs[index]; if (vr == prev) return; Object value = values[index]; if (!(value == Value.NULL || value instanceof byte[] || vr.isStringType() && (value instanceof String || value instanceof String[]))) throw new IllegalStateException("value instanceof " + value.getClass()); vrs[index] = vr; }
@Override public void writeTo(DicomOutputStream out, VR vr) throws IOException { InputStream in = openStream(); try { if (this.bigEndian != out.isBigEndian()) StreamUtils.copy(in, out, length, vr.numEndianBytes()); else StreamUtils.copy(in, out, length); if ((length & 1) != 0) out.write(vr.paddingByte()); } finally { in.close(); } }
public Date getDate(String privateCreator, int tag, VR vr, int valueIndex, Date defVal, DatePrecision precision) { int index = indexOf(privateCreator, tag); if (index < 0) return defVal; Object value = values[index]; if (value == Value.NULL) return defVal; if (vr == null) vr = vrs[index]; else updateVR(index, vr); if (!vr.isTemporalType()) { LOG.info("Attempt to access {} {} as date", TagUtils.toString(tag), vr); return defVal; } try { value = decodeStringValue(index); if (value == Value.NULL) return defVal; return vr.toDate(value, getTimeZone(), valueIndex, false, defVal, precision); } catch (IllegalArgumentException e) { LOG.info("Invalid value of {} {}", TagUtils.toString(tag), vr); return defVal; } }
public Date[] getDates(String privateCreator, int tag, VR vr, DatePrecisions precisions) { int index = indexOf(privateCreator, tag); if (index < 0) return null; Object value = values[index]; if (value == Value.NULL) return DateUtils.EMPTY_DATES; if (vr == null) vr = vrs[index]; else updateVR(index, vr); if (!vr.isTemporalType()) { LOG.info("Attempt to access {} {} as date", TagUtils.toString(tag), vr); return DateUtils.EMPTY_DATES; } try { value = decodeStringValue(index); if (value == Value.NULL) return DateUtils.EMPTY_DATES; return vr.toDates(value, getTimeZone(), false, precisions); } catch (IllegalArgumentException e) { LOG.info("Invalid value of {} {}", TagUtils.toString(tag), vr); return DateUtils.EMPTY_DATES; } }