function order = gen_zigzag64(x, filename)

file=fopen(filename, 'w');

%Construct bands
B = 3*ones(64,64);
B(1:32,1:32)=0;
B(1:16,33:64)=1;
B(33:64,1:16)=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_zigzag64.m */\n");
fprintf(file, "#include \"odintrin.h\"\n");
fprintf(file, "OD_EXTERN const unsigned char OD_ZIGZAG64[%d][2] = {\n", length(order));
for k=1:length(order)
   fprintf(file, "{ %d, %d }", mod(order(k),64), floor(order(k)/64));
   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);
