anchor.diff:  
                   if llid = 0 then
                       let v = if lid >= 64 then lv - data.[lid - 4*64] else lv
  -                    let lSize, lc = encode llid (channel <> 0) (llz % 16) v
  +                    let lSize, lc = encode 0 (channel <> 0) 0 v
                       lCode <- lc
                       lLength <- lSize
   
                   elif lnz then
  -                    let lSize, lc = encode llid (channel <> 0)(llz % 16) lv
  +                    let lSize, lc = encode llid (channel <> 0) (llz % 16) lv
                       lCode <- lc
                       lLength <- lSize
   
  -                elif llz > 0 && llz % 16 = 0 then
  +                elif llz > 0 && llz % 16 = 0 && nonZeroAfterL then
                       lCode <- 0xFAu
                       lLength <- 8
   
  
positive.code:         let lenMask = 0x000000FFu
          let huffMask = 0xFFFF0000u
  
          let encode (index : int) (chroma : bool) (leading : int) (value : float) : int * uint32 =
              let value = int value
              if chroma then
  
negative.code:             
              }
  
  //
  //    [<LocalSize(X = 64)>]
  //    let computer (values : float[]) (res : float[]) =
  //        compute {
  //            let id = getGlobalId().X
  //            let v = values.[id] //if values.[id] > 0.0 then 1 else 0
  //
  //            let lg = scan1 v
  //            res.[id] <- lg
  //        }
  
      let next (v : int) (a : int) =
          if v % a = 0 then v
          else (1 + v / a) * a
  
      let writeImage (size : V2i) (offsets : V3i[]) (counts : V3i[]) (data : uint32[]) =
          let ms = new System.IO.MemoryStream()
          let coder = Jpeg.Huffman.photoshop
  
          let header = [| 0xFFuy; 0xD8uy;  |]
          ms.Write(header, 0, header.Length)
  
          let encode (v : uint16) =
              [| byte (v >>> 8); byte v |]
  
  
          let quant = 
              Array.concat [
                  [| 0xFFuy; 0xDBuy; 0x00uy; 0x84uy |]
                  [| 0x00uy |]
                  Jpeg.QLum |> Jpeg.zigZag |> Array.map byte
                  [| 0x01uy |]
                  Jpeg.QChrom |> Jpeg.zigZag |> Array.map byte
              ]
          ms.Write(quant, 0, quant.Length)
  
          let sof =
              Array.concat [
                  [| 0xFFuy; 0xC0uy; 0x00uy; 0x11uy |]
                  [| 0x08uy |]
                  encode (uint16 size.Y)
                  encode (uint16 size.X)
                  [| 0x03uy; |]
                  [| 0x01uy; 0x11uy; 0x00uy |]
                  [| 0x02uy; 0x11uy; 0x01uy |]
                  [| 0x03uy; 0x11uy; 0x01uy |]
              ]
          ms.Write(sof, 0, sof.Length)
  
          let huff =
              let huffSize (spec : Jpeg.TableSpec)  =
                  uint16 (1 + 16 + spec.values.Length)
  
              let encodeHuff (kind : byte) (spec : Jpeg.TableSpec) =
                  Array.concat [
                      [| kind |]
                      Array.skip 1 spec.counts |> Array.map byte
                      spec.values
                  ]
  
              Array.concat [
                  [| 0xFFuy; 0xC4uy |]
                  encode (2us + huffSize coder.spec.specDCLum + huffSize coder.spec.specDCChroma + huffSize coder.spec.specACLum + huffSize coder.spec.specACChroma)
  
                  encodeHuff 0x00uy coder.spec.specDCLum
                  encodeHuff 0x01uy coder.spec.specDCChroma
                  encodeHuff 0x10uy coder.spec.specACLum
                  encodeHuff 0x11uy coder.spec.specACChroma
              ]
          ms.Write(huff, 0, huff.Length)
  
          let sos = [| 0xFFuy; 0xDAuy; 0x00uy; 0x0Cuy; 0x03uy; 0x01uy; 0x00uy; 0x02uy; 0x11uy; 0x03uy; 0x11uy; 0x00uy; 0x3Fuy; 0x00uy |]
          ms.Write(sos, 0, sos.Length)
  
          let start = ms.Position
          let bs = new Jpeg.BitStream(coder, ms)
  
  
          for bi in 0 .. offsets.Length - 1 do
              for ci in 0 .. 2 do
                  let offset = offsets.[bi].[ci]
                  let size = counts.[bi].[ci]
  
                  let mutable i = 0
                  while i <= size - 32 do
                      bs.Write data.[offset + i / 32]
                      i <- i + 32
  
                  if size > i then
                      let mutable rem = size - i
                      if rem > 16 then
                          let w = Codeword.Create(16, uint16 (data.[offset + i/32] >>> 16))
                          bs.Write(w)
                          rem <- rem - 16
  
                      let w = Codeword.Create(rem, uint16 (data.[offset + i/32] >>> (16 - rem)))
                      bs.Write(w)
  
                          
                  if ci = 0 then
                      bs.WriteACLum(0, 0)
                  else
                      bs.WriteACChrom(0,0)
  
          bs.Flush()
          let len = ms.Position - start
          Log.warn "total scan size: %d (%.3f bpp)" len (float (8L * len) / (float (size.X * size.Y)))
          ms.Write([| 0xFFuy; 0xD9uy |], 0, 2)
  
          ms.ToArray()
  
  
  
  
      let testDCT() =
          use app = new HeadlessVulkanApplication(false)
  