<NME> postprocess.ts
<BEF> /**
 * @license
 * Copyright 2016 Google Inc.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Converts raw data volumes to the appropriate format required by the frontend.
 */

import {encodeCompressedSegmentationUint32, encodeCompressedSegmentationUint64} from 'neuroglancer/async_computation/encode_compressed_segmentation_request';
import {requestAsyncComputation} from 'neuroglancer/async_computation/request';
import {DataType} from 'neuroglancer/sliceview/base';
import {VolumeChunk} from 'neuroglancer/sliceview/volume/backend';
import {CancellationToken} from 'neuroglancer/util/cancellation';

export async function postProcessRawData(
    chunk: VolumeChunk, cancellationToken: CancellationToken, data: ArrayBufferView) {
  cancellationToken;
  const {spec} = chunk.source!;
  if (spec.compressedSegmentationBlockSize !== undefined) {
    const {dataType} = spec;
    tempBuffer.clear();
    switch (dataType) {
    case DataType.UINT32:
      encodeChannelUint32(tempBuffer, spec.compressedSegmentationBlockSize, <Uint32Array>data, spec.chunkDataSize);
      break;
    case DataType.UINT64:
      encodeChannelUint64(tempBuffer, spec.compressedSegmentationBlockSize, <Uint32Array>data, spec.chunkDataSize);
      break;
    default:
      throw new Error(`Unsupported data type for compressed segmentation: ${DataType[dataType]}`);
            encodeCompressedSegmentationUint64, cancellationToken, [data.buffer],
            data as Uint32Array, shape, spec.compressedSegmentationBlockSize);
        break;
      default:
        throw new Error(`Unsupported data type for compressed segmentation: ${DataType[dataType]}`);
    }
  } else {
    chunk.data = data;
  }
}

<MSG> bugfix: chunk compression should use actual size of chunk

The actual size of a chunk can be smaller than the one reported in the
VolumeChunkSpecification, if the requested chunk was not fully contained
in the volume.

<DFF> @@ -33,10 +33,10 @@ export function postProcessRawData(chunk: VolumeChunk, data: ArrayBufferView) {
     tempBuffer.clear();
     switch (dataType) {
     case DataType.UINT32:
-      encodeChannelUint32(tempBuffer, spec.compressedSegmentationBlockSize, <Uint32Array>data, spec.chunkDataSize);
+      encodeChannelUint32(tempBuffer, spec.compressedSegmentationBlockSize, <Uint32Array>data, chunk.chunkDataSize);
       break;
     case DataType.UINT64:
-      encodeChannelUint64(tempBuffer, spec.compressedSegmentationBlockSize, <Uint32Array>data, spec.chunkDataSize);
+      encodeChannelUint64(tempBuffer, spec.compressedSegmentationBlockSize, <Uint32Array>data, chunk.chunkDataSize);
       break;
     default:
       throw new Error(`Unsupported data type for compressed segmentation: ${DataType[dataType]}`);

bugfix: chunk compression should use actual size of chunk