Question

I need to implement DCT transform, but i can't use float or double types. There is a lot of implementations on the internet, but all of them use float.

Does anyone know any source with DCT on integers?

P.S I need it to implement Color Layout Descriptor.

Was it helpful?

Solution

I found a blog post that describes the integer based DCT and IDCT here and gives an implementation in Matlab.

The code he used is on GitHub, available here.

I used the method bink_dct_B2.m. This calculates a scaled 1D-DCT transform, so if you want the 2D-orthogonal DCT tranform, you have have to use following code:

A_tranformed = (bink_dct_B2(bink_dct_B2(A)')')/8

Here A is the matrix you want to transform. This will give approximately the same output as the Matlab built-in dct2-command (this integer implementation will introduce some rounding-errors).

function out=bink_dct_B2(in)
  % extract rows
  i0 = in(1,:);
  i1 = in(2,:);
  i2 = in(3,:);
  i3 = in(4,:);
  i4 = in(5,:);
  i5 = in(6,:);
  i6 = in(7,:);
  i7 = in(8,:);

  % stage 1 - 8A
  a0 = i0 + i7;
  a1 = i1 + i6;
  a2 = i2 + i5;
  a3 = i3 + i4;
  a4 = i0 - i7;
  a5 = i1 - i6;
  a6 = i2 - i5;
  a7 = i3 - i4;

  % even stage 2 - 4A
  b0 = a0 + a3;
  b1 = a1 + a2;
  b2 = a0 - a3;
  b3 = a1 - a2;

  % even stage 3 - 6A 4S
  c0 = b0 + b1;
  c1 = b0 - b1;
  c2 = b2 + b2/4 + b3/2;
  c3 = b2/2 - b3 - b3/4;

  % odd stage 2 - 12A 8S
  % NB a4/4 and a7/4 are each used twice, so this really is 8 shifts, not 10.
  b4 = a7/4 + a4 + a4/4 - a4/16;
  b7 = a4/4 - a7 - a7/4 + a7/16;
  b5 = a5 + a6 - a6/4 - a6/16;
  b6 = a6 - a5 + a5/4 + a5/16;

  % odd stage 3 - 4A
  c4 = b4 + b5;
  c5 = b4 - b5;
  c6 = b6 + b7;
  c7 = b6 - b7;

  % odd stage 4 - 2A
  d4 = c4;
  d5 = c5 + c7;
  d6 = c5 - c7;
  d7 = c6;

  % permute/output
  out = [c0; d4; c2; d6; c1; d5; c3; d7];

  % total: 36A 12S
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top