From cca340e1b1017f1f565c5c53920e450d75164d51 Mon Sep 17 00:00:00 2001
From: Leo Howell <leo@lwh.jp>
Date: Tue, 3 Nov 2009 15:29:45 +0900
Subject: Fix the output block order

---
 lpg/libqr/code-create.c | 24 ++++++++++++++++++------
 lpg/libqr/rs-encode.c   |  1 -
 2 files changed, 18 insertions(+), 7 deletions(-)

diff --git a/lpg/libqr/code-create.c b/lpg/libqr/code-create.c
index ae23acc..1a38967 100644
--- a/lpg/libqr/code-create.c
+++ b/lpg/libqr/code-create.c
@@ -179,7 +179,7 @@ static struct qr_bitstream * make_data(int version,
         const size_t total_words = total_bits / 8;
         const size_t total_data = QR_DATA_WORD_COUNT[version - 1][ec ^ 0x1];
         size_t total_blocks, block_count[2], data_words, rs_words;
-        size_t i;
+        size_t i, w;
         struct qr_bitstream * dcopy = 0;
         struct qr_bitstream * out = 0;
         struct qr_bitstream ** blocks = 0;
@@ -235,12 +235,24 @@ static struct qr_bitstream * make_data(int version,
         }
 
         /* Finally, write everything out in the correct order */
-        qr_bitstream_seek(dcopy, 0);
-        for (i = 0; i < total_blocks; ++i) {
-                size_t dw = data_words + (i >= block_count[0]);
-                qr_bitstream_copy(out, dcopy, dw * 8);
-                qr_bitstream_cat(out, blocks[i]);
+        for (w = 0; w < data_words + 1; ++w) {
+                for (i = (w == data_words ? block_count[0] : 0); i < total_blocks; ++i) {
+                        size_t di = w + (i < block_count[0] ?
+                                i * data_words :
+                                i * (data_words + 1) - block_count[0]);
+
+                        qr_bitstream_seek(dcopy, di * 8);
+                        qr_bitstream_write(out,
+                                qr_bitstream_read(dcopy, 8), 8);
+                }
         }
+        for (i = 0; i < total_blocks; ++i)
+                qr_bitstream_seek(blocks[i], 0);
+        for (w = 0; w < rs_words; ++w)
+                for (i = 0; i < total_blocks; ++i)
+                        qr_bitstream_write(out,
+                                qr_bitstream_read(blocks[i], 8), 8);
+
         qr_bitstream_write(out, 0, total_bits - total_words * 8);
 
         fputs("Final bitstream:\n", stderr);
diff --git a/lpg/libqr/rs-encode.c b/lpg/libqr/rs-encode.c
index 42b404f..a8ecc73 100644
--- a/lpg/libqr/rs-encode.c
+++ b/lpg/libqr/rs-encode.c
@@ -74,7 +74,6 @@ struct qr_bitstream * rs_generate_words(struct qr_bitstream * data,
                 goto fail;
 
         /* First, prepare the registers (b) with data bits */
-        qr_bitstream_seek(data, 0);
         for (i = 0; i < data_words; ++i) {
                 unsigned int x = b[n-1] ^ qr_bitstream_read(data, 8);
                 for (r = n-1; r > 0; --r)
-- 
cgit v1.2.3-70-g09d2