function order = gen_zigzag32(x, filename)

file=fopen(filename, 'w');

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