Skip to Content
go-dicom(3)
Packages

encaps

Parses encapsulated Pixel Data (PS3.5 A.4), the item sequence compressed frames are stored in, and groups its fragments into frames.

import "github.com/amrshadid/go-dicom/encaps" · pkg.go.dev

When to use it

  • You hold the value of an encapsulated Pixel Data element and want its frames as codestreams: NewParser(...).ParseEncapsulatedData, then Frames.
  • You know where the items lie in a file and want each frame's byte ranges without reading the value: FrameRanges.

Most programs need neither. filereader.DICOMFile.FrameReader reads one frame from a file, and the dataset methods decode frames. Building an encapsulated value from frames is compress's EncapsulateFrames.

Key types and functions

NameWhat it is for
NewParser, ParserRead an encapsulated value: offset table and fragments.
FramesGroup fragments into frames: by the offset table, one each, all in one, or by end-of-image markers.
FrameRanges, RangeThe same grouping as byte ranges in a file, using the Extended Offset Table when there is one.
GetStatistics, StatisticsFragment and frame counts, sizes, whether there is an offset table.
NewExtractor, ExtractorOne frame by index.
NewValidator, ValidatorCheck the structure of parsed data.
NewReframerRegroup fragments to a different frame count.

Example

// Split the encapsulated Pixel Data of a compressed file into its frames.
package main

import (
	"bytes"
	"fmt"
	"log"
	"os"

	"github.com/amrshadid/go-dicom/encaps"
	"github.com/amrshadid/go-dicom/filebase"
	"github.com/amrshadid/go-dicom/filereader"
)

func main() {
	f, err := os.Open(os.Args[1]) // a file in a compressed transfer syntax
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()
	df, err := filereader.ReadDICOMFile(filebase.NewFileReader(f))
	if err != nil {
		log.Fatal(err)
	}
	ds := df.GetDataset()
	value, err := ds.RawPixelData() // the items, as stored
	if err != nil {
		log.Fatal(err)
	}

	encData, err := encaps.NewParser(bytes.NewReader(value), true).ParseEncapsulatedData()
	if err != nil {
		log.Fatal(err)
	}
	stats := encaps.GetStatistics(encData)
	fmt.Println("fragments:", stats.FragmentCount, "offset table:", stats.HasBasicOffsetTable)

	info, err := ds.GetPixelDataInfo()
	if err != nil {
		log.Fatal(err)
	}
	frames, err := encaps.Frames(encData, info.NumberOfFrames)
	if err != nil {
		log.Fatal(err)
	}
	for i, fr := range frames {
		fmt.Printf("frame %d: %d bytes, starts % X\n", i+1, len(fr), fr[:4])
	}
}

On SC_rgb_rle_2frame.dcm it prints fragments: 2 offset table: true and two RLE frames of 664 bytes; on JPEG2000.dcm one frame that starts FF 4F FF 51.

Notes

  • Frames refuses a layout it cannot split by its rules rather than guessing: a decoder given half a frame fails later with nothing to say why.
  • A Parser is for one value and one goroutine. Extractors and validators keep no state.
  • Errors from the parser are *errors.EncapsulationError; see errors.

Tip

NewParser's second argument is the byte order. Encapsulated Pixel Data is only defined for little endian transfer syntaxes, so it is true.

See also

compress · filereader · dataset · Pixel data

On this page