anchor.diff:  //      GraphicExError(Format('quantization table %d not present',[fColorComponents[i].QuantId]));
     end;
   
  -  //now we determine layout of interleaved output
  -  LeastCommonDivider:=1;
  -  for i := 0 to Nf-1 do
  -    LeastCommonDivider := max(LeastCommonDivider, fColorComponents[i].HSampling * fColorComponents[i].VSampling);
  -  //we know that we have at least one channel with VSampling=HSampling=1 (otherwise we'd better lower resolution)
  -  //and also that only 1,2,4 are allowed. So, no problem.
     fInterleavedBlockSize:=0;
  +  maxSamplingFactor:=1;
     for i := 0 to Nf-1 do begin
  -    fColorComponents[i].SampleCount := LeastCommonDivider div (fColorComponents[i].HSampling * fColorComponents[i].VSampling);
       fColorComponents[i].SampleOffset := fInterleavedBlockSize;
       fColorComponents[i].Run := fDest;
       inc(fColorComponents[i].Run,fInterleavedBlockSize);
  -    inc(fInterleavedBlockSize, fColorComponents[i].SampleCount * fBytesPerSample);
  +    inc(fInterleavedBlockSize, fColorComponents[i].HSampling * fColorComponents[i].VSampling * fBytesPerSample);
  +    maxSamplingFactor := max(maxSamplingFactor, fColorComponents[i].HSampling * fColorComponents[i].VSampling);
     end;
     fRowSize := fX * fInterleavedBlockSize;
  -  fBlocksPerRow := (fX + 7) div (8 * LeastCommonDivider);
  +  fBlocksPerRow := (fX + 7) div (8 * maxSamplingFactor);
   
   
     //here we begin scans
  
positive.code:     PRED: array [0..3] of Integer;  //previous DC coefficient for each component.
        //After each RST it must reset to 0
        //no more than 4 components per scan is allowed, so we use static array here
      curCol: array [0..3] of Integer;  //to manage edges of image if part of block is used
      curLine: array [0..3] of Integer;
    //settings of current scan
      Ns: Word; //number of components in current scan
      ScanHeaders: array of TJpegScanHeader;
      exists: Boolean;
      SamplingSum: Integer;
      Ss: Byte; //start of spectral/predictor selection
      Se: Byte; //end of spectral/predictor selection
      Ah: Byte;
      Al: Byte;
  
      function NextByte: Byte;
      function NextWord: Word; //will swap
  
negative.code:     procedure DecodeQuantTable;
      procedure DecodeHuffmanTable;
      procedure DecodeSOF;
      procedure DecodeSequentialDCTScan;
      procedure DecodeDNL;
      procedure DecodeHuffmanBlock(CompNum, QuantID: Integer);
      procedure DecodeArithmBlock(CompNum, QuantID: Integer); //will raise exception so far
    public
      constructor Create(Properties: Pointer);
      destructor Destroy; override;
  