function order = gen_zigzag8(x, filename)

file=fopen(filename, 'w');

%Construct bands
B = 3*ones(8,8);
B(1:4,1:4)=0;
B(1:2,5:8)=1;
B(5:8,1:2)=2;
#imagesc(B);

order=[];
for k=1:3
   mask = B==k;
   [dummy,ind]=sort(-x.*mask'(:));
   order = [order;ind(1:sum(sum(mask)))];
end
%start indexing at 0
order = order-1;

fprintf(file, "/* This file is generated by gen_zigzag8.m */\n");
fprintf(file, "const unsigned char od_zigzag8[%d][2] = {\n  ", length(order));
for k=1:length(order)
   fprintf(file, "{%d, %d}", mod(order(k),8), floor(order(k)/8));
   if (k!=length(order))
      fprintf(file, ",");
   end
   if (mod(k,4)==0)
      fprintf(file, "\n  ");
   else
      fprintf(file, " ");
   end
end
fprintf(file, "};\n");

fclose(file);
