-
Notifications
You must be signed in to change notification settings - Fork 11
Fix encapsulated pixeldata handling #103
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
Open
weanti
wants to merge
8
commits into
ImagingDataCommons:main
Choose a base branch
from
weanti:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
87c0436
remove restriction for bits stored, because its value can be modality…
835ed96
fix offset reading and frame reading for encapsulated pixel data
d190231
fix offset calculation for encapsualted fragments: length was essenti…
0ceb9cc
split regular and encapsulated frame parsing
c02f192
fix offset calculation for non-encapsulated frames: bits allocated wa…
7a08c78
Merge branch 'main' into main
weanti ab8b9f0
coding style, code documentation, review fixes: simplification, error…
ac71ff4
functional tests for encapsulated pixel data handling
weanti File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -954,61 +954,75 @@ bool dcm_parse_pixeldata_offsets(DcmError **error, | |
| // the BOT is missing, we must scan pixeldata to find the position of | ||
| // each frame | ||
|
|
||
| // we could use our generic parser above ^^ but we have a special loop | ||
| // here as an optimisation (we can skip over the pixel data itself) | ||
|
|
||
| dcm_log_info("building Offset Table from Pixel Data"); | ||
|
|
||
| // 0 in the BOT is the offset to the start of frame 1, ie. here | ||
| *first_frame_offset = position; | ||
|
|
||
| position = 0; | ||
| for (int i = 0; i < num_frames; i++) { | ||
| if (!read_tag(&state, &tag, &position) || | ||
| !read_uint32(&state, &length, &position)) { | ||
| return false; | ||
| // each frame may consist of several fragments, so we need to scan each fragment to find the next frame | ||
| if (!read_tag(&state, &tag, &position)) { | ||
| dcm_error_set(error, DCM_ERROR_CODE_PARSE, | ||
| "building BasicOffsetTable failed", | ||
| "failed to read tag"); | ||
| return false; | ||
| } | ||
| // all fragments belong to the only frame | ||
| if ( num_frames == 1 ) { | ||
| // according to the standard the first frame's offset is 0 | ||
| offsets[0] = 0; | ||
| } else { | ||
| // 1 fragment shall contain 1 frame | ||
| int fragment_idx = 0; | ||
| offsets[fragment_idx] = 0; // by definition the first fragment is at offset 0 | ||
| fragment_idx++; | ||
| while( tag != TAG_SQ_DELIM ) { | ||
| if ( tag != TAG_ITEM || !read_uint32(&state, &length, &position)) { | ||
| dcm_error_set(error, DCM_ERROR_CODE_PARSE, | ||
| "building BasicOffsetTable failed", | ||
| "failed to read fragment"); | ||
| return false; | ||
| } | ||
| // skip actual content to find next offset | ||
| dcm_seekcur(&state, length, &position); | ||
| if (!read_tag(&state, &tag, &position)) { | ||
| dcm_error_set(error, DCM_ERROR_CODE_PARSE, | ||
| "building BasicOffsetTable failed", | ||
| "failed to read tag"); | ||
| return false; | ||
| } | ||
| if ( fragment_idx < num_frames ) { | ||
| offsets[fragment_idx] = offsets[fragment_idx-1] + 8/*tag and length field size*/ + length; | ||
jcupitt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| fragment_idx++; | ||
| } | ||
|
|
||
| if (tag == TAG_SQ_DELIM) { | ||
| dcm_error_set(error, DCM_ERROR_CODE_PARSE, | ||
| "reading BasicOffsetTable failed", | ||
| "too few frames in PixelData"); | ||
| // fragment_idx shall equal to num_frames+1 at the end | ||
| fragment_idx--; | ||
| if ( fragment_idx > num_frames ) { | ||
| dcm_error_set(error, DCM_ERROR_CODE_INVALID, | ||
| "building BasicOffsetTable failed", | ||
| "Too many fragments" ); | ||
| return false; | ||
| } | ||
|
|
||
| if (tag != TAG_ITEM) { | ||
| dcm_error_set(error, DCM_ERROR_CODE_PARSE, | ||
| if ( num_frames < fragment_idx ) { | ||
| dcm_error_set(error, DCM_ERROR_CODE_INVALID, | ||
| "building BasicOffsetTable failed", | ||
| "frame Item #%d has wrong tag '%08x'", | ||
| i + 1, | ||
| tag); | ||
| "Too many frames" ); | ||
| return false; | ||
| } | ||
|
|
||
| // step back to the start of the item for this frame | ||
| offsets[i] = position - 8; | ||
|
|
||
| // and seek forward over the value | ||
| if (!dcm_seekcur(&state, length, &position)) { | ||
| if ( !read_uint32(&state, &length, &position ) || length != 0 ) { | ||
| dcm_error_set(error, DCM_ERROR_CODE_PARSE, | ||
| "building BasicOffsetTable failed", | ||
| "Sequence Delimiter Tag failure" ); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| // the next thing should be the end of sequence tag | ||
| if (!read_tag(&state, &tag, &position)) { | ||
| return false; | ||
| } | ||
| if (tag != TAG_SQ_DELIM) { | ||
| dcm_error_set(error, DCM_ERROR_CODE_PARSE, | ||
| "reading BasicOffsetTable failed", | ||
| "too many frames in PixelData"); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
|
|
||
| char *dcm_parse_frame(DcmError **error, | ||
| DcmIO *io, | ||
| bool implicit, | ||
|
|
@@ -1022,33 +1036,84 @@ char *dcm_parse_frame(DcmError **error, | |
| .big_endian = is_big_endian(), | ||
| }; | ||
|
|
||
| *length = desc->rows * desc->columns * desc->samples_per_pixel * (desc->bits_allocated / 8); | ||
|
|
||
| char *value = DCM_MALLOC(error, *length); | ||
| if (value == NULL) { | ||
| return NULL; | ||
| } | ||
| int64_t position = 0; | ||
| if (!dcm_require(&state, value, *length, &position)) { | ||
| free(value); | ||
| return NULL; | ||
| } | ||
|
|
||
| if (dcm_is_encapsulated_transfer_syntax(desc->transfer_syntax_uid)) { | ||
| uint32_t tag; | ||
| if (!read_tag(&state, &tag, &position) || | ||
| !read_uint32(&state, length, &position)) { | ||
| return value; | ||
| } | ||
|
|
||
| /* Read encapsulated frame. Return NULL in case of error. | ||
| */ | ||
| char *dcm_parse_encapsulated_frame(DcmError **error, | ||
| DcmIO *io, | ||
| bool implicit, | ||
| int64_t frame_end_offset, | ||
| uint32_t* length) | ||
| { | ||
| DcmParseState state = { | ||
| .error = error, | ||
| .io = io, | ||
| .implicit = implicit, | ||
| .big_endian = is_big_endian(), | ||
| }; | ||
|
|
||
| int64_t position = 0; | ||
| *length = 0; | ||
| uint32_t tag; | ||
| uint32_t fragment_length = 0; | ||
|
|
||
| // first determine the total length of bytes to be read | ||
| while(position < frame_end_offset) { | ||
| if (!read_tag(&state, &tag, &position)) { | ||
| return NULL; | ||
| } | ||
|
|
||
| if (tag == TAG_SQ_DELIM) { | ||
| break; | ||
| } | ||
| if (tag != TAG_ITEM) { | ||
| dcm_error_set(error, DCM_ERROR_CODE_PARSE, | ||
| "reading frame item failed", | ||
| "no item tag found for frame item"); | ||
| return NULL; | ||
| } | ||
| } else { | ||
| *length = desc->rows * desc->columns * desc->samples_per_pixel * | ||
| (desc->bits_allocated / 8); | ||
| if (!read_uint32(&state, &fragment_length, &position)) { | ||
| return NULL; | ||
| } | ||
| dcm_seekcur(&state, fragment_length, &position); | ||
| *length += fragment_length; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose there's a potential uint32 overflow here. |
||
| } | ||
|
|
||
| char *value = DCM_MALLOC(error, *length); | ||
| if (value == NULL) { | ||
| return NULL; | ||
| } | ||
| if (!dcm_require(&state, value, *length, &position)) { | ||
| free(value); | ||
| return NULL; | ||
| // if frame end is unknown/undefined then update it | ||
| if (frame_end_offset == 0xFFFFFFFF) { | ||
| frame_end_offset = position; | ||
| } | ||
| // reposition to the beginning of encapsulated pixel data | ||
| dcm_seekcur(&state, -position, &position); | ||
|
|
||
| fragment_length = 0; | ||
| char* fragment = value; | ||
| position = 0; | ||
| while(position < frame_end_offset) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! |
||
| read_tag(&state, &tag, &position); | ||
| read_uint32(&state, &fragment_length, &position); | ||
| if (!dcm_require(&state, fragment, fragment_length, &position)) { | ||
| free(value); | ||
| return NULL; | ||
| } | ||
| fragment += fragment_length; | ||
| } | ||
|
|
||
| return value; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you need this complicated loop. I think (I think! I hope!) all you need to do is scan
num_framesitems and collect the offsets. This will collect the right number of frames whether or not frames are split into many items.How about changing this back to:
ie. just removing the check for the end of sequence tag.
I suppose we could check for end-of-sequence in the
num_frames > 1case, but I don't know if it'd add much.