function order = gen_zigzag16(x, filename)

file=fopen(filename, 'w');

%Construct bands
B = 3*ones(16,16);
B(1:8,1:8)=0;
B(1:4,9:16)=1;
B(9:16,1:4)=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_zigzag16.m */\n");
fprintf(file, "const unsigned char od_zigzag16[%d][2] = {\n  ", length(order));
for k=1:length(order)
   fprintf(file, "{%d, %d}", mod(order(k),16), floor(order(k)/16));
   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);
