<template>
  <div>
    <el-button plain @click="$refs.file.click()">选择dicom文件</el-button>
    <input
      ref="file"
      type="file"
      name="file"
      webkitdirectory
      multiple
      @change="beginScanFiles($event)"
    />
  </div>
</template>

<script>
import dicomParser from "dicom-parser";
import dcmjs from 'dcmjs'
const CharLs = require('@cornerstonejs/codec-charls')
const codecWrapper = {
  codec: undefined,
  Decoder: undefined,
  Encoder: undefined,
  encoderName: "JpegLSEncoder",
  decoderName: "JpegLSDecoder",
};
function setCodec(codecConfig, encoderName, decoderName, codec = {}) {
  codecConfig.Encoder = codec[encoderName];
  codecConfig.Decoder = codec[decoderName];
  codecConfig.codec = codec;
}
CharLs().then(res => {
  setCodec(codecWrapper, codecWrapper.encoderName, codecWrapper.decoderName, res);
})
export default {
  data() {
    return {
    }
  },
  methods: {
    async upload(name, file) {
      let res = await this.OSSclient.put(name, file)
      return res
    },
    beginScanFiles(e) {
      var files = e.target.files
      if (files.length > 0) {
        files.forEach(async (item, index) => {
          const reader = new FileReader();
          reader.onload = async (event) => {
            let buffer = event.target.result;
            let data = dicomParser.parseDicom(new Uint8Array(buffer))
            let height = data.uint16('x00280011')
            let width = data.uint16('x00280010')
            let bitsPerSample = data.uint16('x00280100')
            let componentCount = data.string('x00280004')
            let transportSyntax = data.string('x00020010')
            if ((transportSyntax === '1.2.840.10008.1.2.1' || transportSyntax === '1.2.840.10008.1.2') && ~componentCount.indexOf('MONOCHROME')) {
              let dataset = dcmjs.data.DicomMessage.readFile(buffer)
              console.log(new dcmjs.data.DicomMetaDictionary(dataset))
              let pixelDataElement = data.elements.x7fe00010;
              let pixelDataOffset = pixelDataElement.dataOffset;
              let pixelDataLength = pixelDataElement.length;
              let pixelDataBytes = data.byteArray.subarray(pixelDataOffset, pixelDataOffset + pixelDataLength)
              let encoder = new codecWrapper.Encoder();
              let decodedBytes = encoder.getDecodedBuffer({width: width, height: height, bitsPerSample: bitsPerSample, componentCount: 1})
              encoder.setNearLossless(0);
              decodedBytes.set(pixelDataBytes);
              encoder.encode()
              let encodedBytes = encoder.getEncodedBuffer();
              dataset.dict['7FE00010'].Value[0] = encodedBytes.buffer;
              dataset.meta['00020010'].Value[0] = '1.2.840.10008.1.2.4.80';
              let newDicomFile = dataset.write();
              const bufferArray = new Uint8Array(newDicomFile);
              const blob = new Blob([bufferArray], { type: 'application/octet-stream' });
              await this.upload(`test/${index}.dcm`, blob)
            }
          };
          reader.readAsArrayBuffer(item);
        })
      }
    },
    open() {
      this.IsOpen = true
    },
    uploadOver(isSuccess, res) {
      if (isSuccess) {
        this.IsOpen = false
        this.videoUrl = res.url
        this.videoName = res.name
      }
    }
  }
}
</script>

<style scoped>

</style>
