Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/various updates #26

Merged
merged 3 commits into from
Feb 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions src/collect-flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,7 @@ export function collectFlow(
}

if (part instanceof ItemPart && this.inFragments) {
this.currentFragment = new FragmentElement(
part.index,
part.length,
Value.empty(),
part.bigEndian,
);
this.currentFragment = new FragmentElement(part.length, Value.empty(), part.bigEndian);
this.bytes = emptyBuffer;
return [];
}
Expand All @@ -127,7 +122,6 @@ export function collectFlow(
if (this.inFragments && this.currentFragment) {
this.maybeAdd(
new FragmentElement(
this.currentFragment.index,
this.currentFragment.length,
new Value(this.bytes),
this.currentFragment.bigEndian,
Expand Down Expand Up @@ -160,14 +154,14 @@ export function collectFlow(
return [];
}
if (part instanceof ItemPart) {
this.maybeAdd(new ItemElement(part.index, part.length, part.bigEndian));
this.maybeAdd(new ItemElement(part.length, part.bigEndian));
return [];
}
if (part instanceof ItemDelimitationPartMarker) {
return [];
}
if (part instanceof ItemDelimitationPart) {
this.maybeAdd(new ItemDelimitationElement(part.index, part.bigEndian));
this.maybeAdd(new ItemDelimitationElement(part.bigEndian));
return [];
}
if (part === sequenceDelimitationPartMarker) {
Expand Down
92 changes: 43 additions & 49 deletions src/dicom-elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,17 @@ export class ValueElement extends ElementSet {
const vm = strings.length + '';
return (
'ValueElement(' +
tagToString(this.tag) +
' ' +
this.vr.name +
' [' +
s +
'] # ' +
this.length +
', ' +
vm +
' ' +
Lookup.keywordOf(this.tag) +
')'
tagToString(this.tag) +
' ' +
this.vr.name +
' [' +
s +
'] # ' +
this.length +
', ' +
vm +
' ' +
Lookup.keywordOf(this.tag) || '' + ')'
);
}
}
Expand All @@ -147,7 +146,8 @@ export class SequenceElement extends Element {
}
public toString(): string {
return (
'SequenceElement(' + tagToString(this.tag) + ' SQ # ' + this.length + ' ' + Lookup.keywordOf(this.tag) + ')'
'SequenceElement(' + tagToString(this.tag) + ' SQ # ' + this.length + ' ' + Lookup.keywordOf(this.tag) ||
'' + ')'
);
}
}
Expand Down Expand Up @@ -179,15 +179,16 @@ export class FragmentsElement extends Element {
}
public toString(): string {
return (
'FragmentsElement(' + tagToString(this.tag) + ' ' + this.vr.name + ' # ' + Lookup.keywordOf(this.tag) + ')'
'FragmentsElement(' + tagToString(this.tag) + ' ' + this.vr.name + ' # ' + Lookup.keywordOf(this.tag) ||
'' + ')'
);
}
}

export class ItemElement extends Element {
public indeterminate: boolean;

constructor(public readonly index: number, public readonly length = indeterminateLength, bigEndian?: boolean) {
constructor(public readonly length = indeterminateLength, bigEndian?: boolean) {
super(bigEndian);
this.indeterminate = this.length === indeterminateLength;
}
Expand All @@ -196,20 +197,15 @@ export class ItemElement extends Element {
return concat(tagToBytes(Tag.Item, this.bigEndian), intToBytes(this.length, this.bigEndian));
}
public toParts(): DicomPart[] {
return [new ItemPart(this.index, this.length, this.bigEndian, this.toBytes())];
return [new ItemPart(this.length, this.bigEndian, this.toBytes())];
}
public toString(): string {
return 'ItemElement(index = ' + this.index + ', length = ' + this.length + ')';
return 'ItemElement(length = ' + this.length + ')';
}
}

export class FragmentElement extends Element {
constructor(
public readonly index: number,
public readonly length: number,
public readonly value: Value,
bigEndian?: boolean,
) {
constructor(public readonly length: number, public readonly value: Value, bigEndian?: boolean) {
super(bigEndian);
}

Expand All @@ -219,30 +215,30 @@ export class FragmentElement extends Element {
.reduce(concat);
}
public toParts(): DicomPart[] {
const itemParts: DicomPart[] = new ItemElement(this.index, this.value.length, this.bigEndian).toParts();
const itemParts: DicomPart[] = new ItemElement(this.value.length, this.bigEndian).toParts();
if (this.value.length !== 0) {
itemParts.push(new ValueChunk(this.bigEndian, this.value.bytes, true));
}
return itemParts;
}
public toString(): string {
return 'FragmentElement(index = ' + this.index + ', length = ' + this.length + ')';
return 'FragmentElement(length = ' + this.length + ')';
}
}

export class ItemDelimitationElement extends Element {
constructor(public readonly index: number, bigEndian?: boolean) {
constructor(bigEndian?: boolean) {
super(bigEndian);
}

public toBytes(): Buffer {
return concat(tagToBytes(Tag.ItemDelimitationItem, this.bigEndian), Buffer.from([0, 0, 0, 0]));
}
public toParts(): DicomPart[] {
return [new ItemDelimitationPart(this.index, this.bigEndian, this.toBytes())];
return [new ItemDelimitationPart(this.bigEndian, this.toBytes())];
}
public toString(): string {
return 'ItemDelimitationElement(index = ' + this.index + ')';
return 'ItemDelimitationElement';
}
}

Expand Down Expand Up @@ -301,7 +297,7 @@ export class Sequence extends ElementSet {
const elements = [];
elements.push(new SequenceElement(this.tag, this.length, this.bigEndian, this.explicitVR));
for (let i = 1; i <= this.items.length; i++) {
const itemElements = this.item(i).toElements(i);
const itemElements = this.item(i).toElements();
itemElements.forEach((e) => elements.push(e));
}
if (this.indeterminate) {
Expand All @@ -317,14 +313,13 @@ export class Sequence extends ElementSet {
public toString(): string {
return (
'Sequence(' +
tagToString(this.tag) +
' SQ # ' +
this.length +
' ' +
this.size +
' ' +
Lookup.keywordOf(this.tag) +
')'
tagToString(this.tag) +
' SQ # ' +
this.length +
' ' +
this.size +
' ' +
Lookup.keywordOf(this.tag) || '' + ')'
);
}
}
Expand All @@ -340,17 +335,17 @@ export class Item {
this.indeterminate = length === indeterminateLength;
}

public toElements(index: number): Element[] {
public toElements(): Element[] {
const elements: Element[] = [];
elements.push(new ItemElement(index, this.length, this.bigEndian));
elements.push(new ItemElement(this.length, this.bigEndian));
this.elements.toElements(false).forEach((e) => elements.push(e));
if (this.indeterminate) {
elements.push(new ItemDelimitationElement(index, this.bigEndian));
elements.push(new ItemDelimitationElement(this.bigEndian));
}
return elements;
}
public toBytes(): Buffer {
return this.toElements(1)
return this.toElements()
.map((e) => e.toBytes())
.reduce(concat);
}
Expand All @@ -370,8 +365,8 @@ export class Fragment {
public readonly bigEndian: boolean = false,
) {}

public toElement(index: number): Element {
return new FragmentElement(index, this.length, this.value, this.bigEndian);
public toElement(): Element {
return new FragmentElement(this.length, this.value, this.bigEndian);
}
public toString(): string {
return 'Fragment(length = ' + this.length + ', value length = ' + this.value.length + ')';
Expand Down Expand Up @@ -434,7 +429,6 @@ export class Fragments extends ElementSet {
if (this.offsets !== undefined) {
elements.push(
new FragmentElement(
1,
4 * this.offsets.length,
new Value(
this.offsets
Expand All @@ -445,10 +439,10 @@ export class Fragments extends ElementSet {
),
);
} else {
elements.push(new FragmentElement(1, 0, Value.empty()));
elements.push(new FragmentElement(0, Value.empty()));
}
for (let i = 1; i <= this.fragments.length; i++) {
elements.push(this.fragment(i).toElement(i + 1));
elements.push(this.fragment(i).toElement());
}
elements.push(new SequenceDelimitationElement(this.bigEndian));
return elements;
Expand All @@ -459,8 +453,8 @@ export class Fragments extends ElementSet {
return new Fragments(this.tag, this.vr, this.offsets, newFragments, this.bigEndian, this.explicitVR);
}
public toString(): string {
return `Fragments(${tagToString(this.tag)} ${this.vr.name} # ${this.fragments.length} ${Lookup.keywordOf(
this.tag,
)})`;
return `Fragments(${tagToString(this.tag)} ${this.vr.name} # ${this.fragments.length} ${
Lookup.keywordOf(this.tag) || ''
})`;
}
}
23 changes: 15 additions & 8 deletions src/dicom-flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
ValueChunk,
} from './dicom-parts';
import { Tag } from './tag';
import { emptyTagPath, TagPath, TagPathItem } from './tag-path';
import { emptyTagPath, TagPath, TagPathItem, TagPathItemEnd } from './tag-path';

export function createFlow(flow: any): any {
return pipe(flow.baseFlow(), flatMapFlow(flow.handlePart.bind(flow)));
Expand Down Expand Up @@ -241,14 +241,15 @@ class SequenceDelimitationPartMarker extends SequenceDelimitationPart {
export const sequenceDelimitationPartMarker = new SequenceDelimitationPartMarker();

export class ItemDelimitationPartMarker extends ItemDelimitationPart {
constructor(index: number) {
super(index, false, emptyBuffer);
constructor() {
super(false, emptyBuffer);
}

public toString(): string {
return 'ItemDelimitationMarker []';
}
}
export const itemDelimitationPartMarker = new ItemDelimitationPartMarker();

/**
* Depends on InFragments
Expand Down Expand Up @@ -315,7 +316,7 @@ export const GuaranteedDelimitationEvents = (Super: any): any =>
this.partStack = this.partStack.slice(splitIndex);
const out = inactive.map((i) =>
i.part instanceof ItemPart
? this.onItemDelimitation(new ItemDelimitationPartMarker((i.part as ItemPart).index))
? this.onItemDelimitation(itemDelimitationPartMarker)
: this.onSequenceDelimitation(sequenceDelimitationPartMarker),
);
return [].concat(...out);
Expand All @@ -333,18 +334,20 @@ export const InSequence = (Super: any): any =>
class extends Super {
public sequenceStack: SequencePart[] = [];

public sequenceDepth() {
public sequenceDepth(): number {
return this.sequenceStack.length;
}
public inSequence() {
public inSequence(): boolean {
return this.sequenceStack.length > 0;
}
public onSequence(part: SequencePart): DicomPart[] {
this.sequenceStack.unshift(part);
return super.onSequence(part);
}
public onSequenceDelimitation(part: SequenceDelimitationPart): DicomPart[] {
this.sequenceStack.shift();
if (!this.inFragments) {
this.sequenceStack.shift();
}
return super.onSequenceDelimitation(part);
}
};
Expand Down Expand Up @@ -381,7 +384,11 @@ export const TagPathTracking = (Super: any): any =>
public onItem(part: ItemPart): DicomPart[] {
const t = this.tagPath;
if (!this.inFragments) {
this.tagPath = t.previous().thenItem(t.tag(), part.index);
if (t instanceof TagPathItemEnd) {
this.tagPath = t.previous().thenItem(t.tag(), t.item + 1);
} else {
this.tagPath = t.previous().thenItem(t.tag(), 1);
}
}
return super.onItem(part);
}
Expand Down
3 changes: 1 addition & 2 deletions src/dicom-flows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,6 @@ export function toIndeterminateLengthSequences(): any {
return super.onItem(part).map((p: DicomPart) => {
if (p instanceof ItemPart && !this.inFragments && !p.indeterminate) {
return new ItemPart(
part.index,
indeterminateLength,
part.bigEndian,
concat(part.bytes.slice(0, part.bytes.length - 4), this.indeterminateBytes),
Expand All @@ -324,7 +323,7 @@ export function toIndeterminateLengthSequences(): any {
public onItemDelimitation(part: ItemDelimitationPart): DicomPart[] {
const out = super.onItemDelimitation(part);
if (part.bytes.length <= 0) {
out.push(new ItemDelimitationPart(part.index, part.bigEndian, itemDelimitation(part.bigEndian)));
out.push(new ItemDelimitationPart(part.bigEndian, itemDelimitation(part.bigEndian)));
}
return out;
}
Expand Down
8 changes: 4 additions & 4 deletions src/dicom-parts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,23 +149,23 @@ export class DeflatedChunk extends DicomPart {
export class ItemPart extends DicomPart {
public indeterminate = false;

constructor(public readonly index: number, public readonly length: number, bigEndian: boolean, bytes: Buffer) {
constructor(public readonly length: number, bigEndian: boolean, bytes: Buffer) {
super(bigEndian, bytes);
this.indeterminate = length === indeterminateLength;
}

public toString(): string {
return 'Item [length = ' + this.length + ', index = ' + this.index + ']';
return 'Item [length = ' + this.length + ']';
}
}

export class ItemDelimitationPart extends DicomPart {
constructor(public readonly index: number, bigEndian: boolean, bytes: Buffer) {
constructor(bigEndian: boolean, bytes: Buffer) {
super(bigEndian, bytes);
}

public toString(): string {
return 'ItemDelimitation [index = ' + this.index + ']';
return 'ItemDelimitation';
}
}

Expand Down
Loading