Converted the Mode and ECLevel enums in QRCode.java.
[zxing.git] / core / src / com / google / zxing / qrcode / encoder / MatrixUtil.java
1 /*
2  * Copyright 2008 ZXing authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.google.zxing.qrcode.encoder;
18
19 // #include "util/array/array2d-inl.h"
20
21 /**
22  * @author satorux@google.com (Satoru Takabayashi) - creator
23  * @author dswitkin@google.com (Daniel Switkin) - ported from C++
24  */
25 public final class MatrixUtil {
26
27   private static final int kPositionDetectionPattern[][] =  {
28       {1, 1, 1, 1, 1, 1, 1},
29       {1, 0, 0, 0, 0, 0, 1},
30       {1, 0, 1, 1, 1, 0, 1},
31       {1, 0, 1, 1, 1, 0, 1},
32       {1, 0, 1, 1, 1, 0, 1},
33       {1, 0, 0, 0, 0, 0, 1},
34       {1, 1, 1, 1, 1, 1, 1},
35   };
36
37   private static final int kHorizontalSeparationPattern[][] = {
38       {0, 0, 0, 0, 0, 0, 0, 0},
39   };
40
41   private static final int kVerticalSeparationPattern[][] = {
42       {0}, {0}, {0}, {0}, {0}, {0}, {0},
43   };
44
45   private static final int kPositionAdjustmentPattern[][] = {
46       {1, 1, 1, 1, 1},
47       {1, 0, 0, 0, 1},
48       {1, 0, 1, 0, 1},
49       {1, 0, 0, 0, 1},
50       {1, 1, 1, 1, 1},
51   };
52
53   // From Appendix E. Table 1, JIS0510X:2004 (p 71).
54   // The table was double-checked by komatsu.
55   private static final int kPositionAdjustmentPatternCoordinateTable[][] = {
56       {-1, -1, -1, -1,  -1,  -1,  -1},  // Version 1
57       { 6, 18, -1, -1,  -1,  -1,  -1},  // Version 2
58       { 6, 22, -1, -1,  -1,  -1,  -1},  // Version 3
59       { 6, 26, -1, -1,  -1,  -1,  -1},  // Version 4
60       { 6, 30, -1, -1,  -1,  -1,  -1},  // Version 5
61       { 6, 34, -1, -1,  -1,  -1,  -1},  // Version 6
62       { 6, 22, 38, -1,  -1,  -1,  -1},  // Version 7
63       { 6, 24, 42, -1,  -1,  -1,  -1},  // Version 8
64       { 6, 26, 46, -1,  -1,  -1,  -1},  // Version 9
65       { 6, 28, 50, -1,  -1,  -1,  -1},  // Version 10
66       { 6, 30, 54, -1,  -1,  -1,  -1},  // Version 11
67       { 6, 32, 58, -1,  -1,  -1,  -1},  // Version 12
68       { 6, 34, 62, -1,  -1,  -1,  -1},  // Version 13
69       { 6, 26, 46, 66,  -1,  -1,  -1},  // Version 14
70       { 6, 26, 48, 70,  -1,  -1,  -1},  // Version 15
71       { 6, 26, 50, 74,  -1,  -1,  -1},  // Version 16
72       { 6, 30, 54, 78,  -1,  -1,  -1},  // Version 17
73       { 6, 30, 56, 82,  -1,  -1,  -1},  // Version 18
74       { 6, 30, 58, 86,  -1,  -1,  -1},  // Version 19
75       { 6, 34, 62, 90,  -1,  -1,  -1},  // Version 20
76       { 6, 28, 50, 72,  94,  -1,  -1},  // Version 21
77       { 6, 26, 50, 74,  98,  -1,  -1},  // Version 22
78       { 6, 30, 54, 78, 102,  -1,  -1},  // Version 23
79       { 6, 28, 54, 80, 106,  -1,  -1},  // Version 24
80       { 6, 32, 58, 84, 110,  -1,  -1},  // Version 25
81       { 6, 30, 58, 86, 114,  -1,  -1},  // Version 26
82       { 6, 34, 62, 90, 118,  -1,  -1},  // Version 27
83       { 6, 26, 50, 74,  98, 122,  -1},  // Version 28
84       { 6, 30, 54, 78, 102, 126,  -1},  // Version 29
85       { 6, 26, 52, 78, 104, 130,  -1},  // Version 30
86       { 6, 30, 56, 82, 108, 134,  -1},  // Version 31
87       { 6, 34, 60, 86, 112, 138,  -1},  // Version 32
88       { 6, 30, 58, 86, 114, 142,  -1},  // Version 33
89       { 6, 34, 62, 90, 118, 146,  -1},  // Version 34
90       { 6, 30, 54, 78, 102, 126, 150},  // Version 35
91       { 6, 24, 50, 76, 102, 128, 154},  // Version 36
92       { 6, 28, 54, 80, 106, 132, 158},  // Version 37
93       { 6, 32, 58, 84, 110, 136, 162},  // Version 38
94       { 6, 26, 54, 82, 110, 138, 166},  // Version 39
95       { 6, 30, 58, 86, 114, 142, 170},  // Version 40
96   };
97
98   // Type info cells at the left top corner.
99   private static int kTypeInfoCoordinates[][] = {
100       {8, 0},
101       {8, 1},
102       {8, 2},
103       {8, 3},
104       {8, 4},
105       {8, 5},
106       {8, 7},
107       {8, 8},
108       {7, 8},
109       {5, 8},
110       {4, 8},
111       {3, 8},
112       {2, 8},
113       {1, 8},
114       {0, 8},
115   };
116
117   // From Appendix D in JISX0510:2004 (p. 67)
118   private static final uint32 kVersionInfoPoly = 0x1f25;  // 1 1111 0010 0101
119
120   // From Appendix C in JISX0510:2004 (p.65).
121   private static final uint32 kTypeInfoPoly = 0x537;
122   private static final uint32 kTypeInfoMaskPattern = 0x5412;
123
124   // Set all cells to -1.  -1 means that the cell is empty (not set
125   // yet).
126   public static void ClearMatrix(QRCodeMatrix *matrix) {
127     for (int y = 0; y < matrix.height(); ++y) {
128       for (int x = 0; x < matrix.width(); ++x) {
129         (*matrix)(y, x) = -1;
130       }
131     }
132   }
133
134   // Convert "matrix" to ASCII String for debugging.
135   public static String ToASCII(final QRCodeMatrix &matrix) {
136     String result;
137     for (int y = 0; y < matrix.height(); ++y) {
138       for (int x = 0; x < matrix.width(); ++x) {
139         switch (matrix(y, x)) {
140           case 0:
141             result.append(" 0");
142             break;
143           case 1:
144             result.append(" 1");
145             break;
146           default:
147             result.append("  ");
148             break;
149         }
150       }
151       result.append("\n");
152     }
153     return result;
154   }
155
156   // Build 2D matrix of QR Code from "data_bits" with "ec_level",
157   // "version" and "mask_pattern".  On success, store the result in
158   // "matrix" and return true.  On error, return false.
159   public static boolean BuildMatrix(final BitVector &data_bits,
160                                     int ec_level,
161                                     int version,
162                                     int mask_pattern,
163                                     QRCodeMatrix *matrix) {
164     MatrixUtil.ClearMatrix(matrix);
165     if (!EmbedBasicPatterns(version, matrix)) {
166       return false;
167     }
168     // Type information appear with any version.
169     if (!EmbedTypeInfo(ec_level, mask_pattern, matrix)) {
170       return false;
171     }
172     // Version info appear if version >= 7.
173     if (!MaybeEmbedVersionInfo(version, matrix)) {
174       return false;
175     }
176     // Data should be embedded at end.
177     return EmbedDataBits(data_bits,  mask_pattern, matrix);
178   }
179
180   // Embed basic patterns.  On success, modify the matrix and return
181   // true.  On error, return false.  The basic patterns are:
182   // - Position detection patterns
183   // - Timing patterns
184   // - Dark dot at the left bottom corner
185   // - Position adjustment patterns, if need be
186   public static boolean EmbedBasicPatterns(int version,
187                                            QRCodeMatrix *matrix) {
188     // Let's get started with embedding big squares at corners.
189     EmbedPositionDetectionPatternsAndSeparators(matrix);
190     // Then, embed the dark dot at the left bottom corner.
191     EmbedDarkDotAtLeftBottomCorner(matrix);
192
193     // Position adjustment patterns appear if version >= 2.
194     MaybeEmbedPositionAdjustmentPatterns(version, matrix);
195     // Timing patterns should be embedded after position adj. patterns.
196     EmbedTimingPatterns(matrix);
197     return true;
198   }
199
200   // Embed type information.  On success, modify the matrix and return
201   // true.  On error, return false.
202   public static boolean EmbedTypeInfo(int ec_level, int mask_pattern, QRCodeMatrix *matrix) {
203     BitVector type_info_bits;
204     if (!MakeTypeInfoBits(ec_level, mask_pattern, &type_info_bits)) {
205       return false;
206     }
207     Debug.DCHECK_EQ(15, type_info_bits.size());
208
209     for (int i = 0; i < type_info_bits.size(); ++i) {
210       // Place bits in LSB to MSB order.  LSB (least significant bit)
211       // is the last value in "type_info_bits".
212       final int bit = type_info_bits.at(type_info_bits.size() - 1 - i);
213
214       // Type info bits at the left top corner.
215       // See 8.9 of JISX0510:2004 (p.46).
216       final int x1 = kTypeInfoCoordinates[i][0];
217       final int y1 = kTypeInfoCoordinates[i][1];
218       (*matrix)(y1, x1) = bit;
219
220       if (i < 8) {
221         // Right top corner.
222         final int x2 = matrix.width() - i - 1;
223         final int y2 = 8;
224         (*matrix)(y2, x2) = bit;
225       } else {
226         // Left bottom corner.
227         final int x2 = 8;
228         final int y2 = matrix.height() - 7 + (i - 8);
229         (*matrix)(y2, x2) = bit;
230       }
231     }
232     return true;
233   }
234
235   // Embed version information if need be.  On success, modify the
236   // matrix and return true.  On error, return false.
237   // See 8.10 of JISX0510:2004 (p.47) for how to embed version
238   // information.  Return true on success.  Return false otherwise.
239   public static boolean MaybeEmbedVersionInfo(int version,
240                                               QRCodeMatrix *matrix) {
241     if (version < 7) {  // Version info is necessary if version >= 7.
242       return true;  // Don't need version info.
243     }
244     BitVector version_info_bits;
245     if (!MakeVersionInfoBits(version, &version_info_bits)) {
246     return false;
247   }
248
249     Debug.DCHECK_EQ(18, version_info_bits.size());
250     int bit_index = 6 * 3 - 1;  // It will decrease from 17 to 0.
251     for (int i = 0; i < 6; ++i) {
252       for (int j = 0; j < 3; ++j) {
253         // Place bits in LSB (least significant bit) to MSB order.
254         final int bit = version_info_bits.at(bit_index--);
255         // Left bottom corner.
256         (*matrix)(matrix.height() - 11 + j, i) = bit;
257         // Right bottom corner.
258         (*matrix)(i, matrix.height() - 11 + j) = bit;
259       }
260     }
261     return true;
262   }
263
264   // Embed "data_bits" using "mask_pattern".  On success, modify the
265   // matrix and return true.  On error, return false.  For debugging
266   // purpose, it skips masking process if "mask_pattern" is -1.
267   // See 8.7 of JISX0510:2004 (p.38) for how to embed data bits.
268   public static boolean EmbedDataBits(final BitVector &data_bits,
269                                       int mask_pattern,
270                                       QRCodeMatrix *matrix) {
271     int bit_index = 0;
272     int direction = -1;
273     // Start from the right bottom cell.
274     int x = matrix.width() - 1;
275     int y = matrix.height() - 1;
276     while (x > 0) {
277       // Skip the vertical timing pattern.
278       if (x == 6) {
279         x -= 1;
280       }
281       while (y >= 0 && y < matrix.height()) {
282         for (int i = 0; i < 2; ++i) {
283           final int xx = x - i;
284           // Skip the cell if it's not empty.
285           if (!IsEmpty((*matrix)(y, xx))) {
286           continue;
287         }
288           int bit = -1;
289           if (bit_index < data_bits.size()) {
290             bit = data_bits.at(bit_index);
291             ++bit_index;
292           } else {
293             // Padding bit.  If there is no bit left, we'll fill the
294             // left cells with 0, as described in 8.4.9 of
295             // JISX0510:2004 (p. 24).
296             bit = 0;
297           }
298           Debug.DCHECK(IsValidValue(bit));
299
300           // Skip masking if mask_pattern is -1.
301           if (mask_pattern != -1) {
302             final int mask = MaskUtil.GetDataMaskBit(mask_pattern, xx, y);
303             Debug.DCHECK(mask == 0 || mask == 1);
304             bit ^= mask;
305           }
306           (*matrix)(y, xx) = bit;
307         }
308         y += direction;
309       }
310       direction = -direction;  // Reverse the direction.
311       y += direction;
312       x -= 2;  // Move to the left.
313     }
314     // All bits should be consumed.
315     if (bit_index < data_bits.size()) {
316       Debug.LOG_ERROR("Not all bits consumed: " + bit_index + "/" + data_bits.size());
317       return false;
318     }
319     Debug.DCHECK_EQ(bit_index, data_bits.size());
320     return true;
321   }
322
323   // Return the position of the most significant bit set (to one) in
324   // the "value".  The most significant bit is position 32.  If there
325   // is no bit set, return 0.
326   // Examples:
327   // - FindMSBSet(0) => 0
328   // - FindMSBSet(1) => 1
329   // - FindMSBSet(255) => 8
330   public static int FindMSBSet(uint32 value) {
331     int num_digits = 0;
332     while (value != 0) {
333       value >>= 1;
334       ++num_digits;
335     }
336     return num_digits;
337   }
338
339   // Calculate BCH (Bose-Chaudhuri-Hocquenghem) code for "value" using
340   // polynomial "poly".  The BCH code is used for encoding type
341   // information and version information.
342   // Example: Calculation of version information of 7.
343   // f(x) is created from 7.
344   //   - 7 = 000111 in 6 bits
345   //   - f(x) = x^2 + x^2 + x^1
346   // g(x) is given by the standard (p. 67)
347   //   - g(x) = x^12 + x^11 + x^10 + x^9 + x^8 + x^5 + x^2 + 1
348   // Multiply f(x) by x^(18 - 6)
349   //   - f'(x) = f(x) * x^(18 - 6)
350   //   - f'(x) = x^14 + x^13 + x^12
351   // Calculate the remainder of f'(x) / g(x)
352   //         x^2
353   //         __________________________________________________
354   //   g(x) )x^14 + x^13 + x^12
355   //         x^14 + x^13 + x^12 + x^11 + x^10 + x^7 + x^4 + x^2
356   //         --------------------------------------------------
357   //                              x^11 + x^10 + x^7 + x^4 + x^2
358   //
359   // The remainder is x^11 + x^10 + x^7 + x^4 + x^2
360   // Encode it in binary: 110010010100
361   // The return value is 0xc94 (1100 1001 0100)
362   //
363   // Since all coefficients in the polynomials are 1 or 0, we can do the
364   // calculation by bit operations.  We don't care if cofficients are
365   // positive or nagative.
366   public static uint32 CalculateBCHCode(uint32 value, uint32 poly) {
367     // If poly is "1 1111 0010 0101" (version info poly),
368     // msb_set_in_poly is 13.  We'll subtract 1 from 13 to make it 12.
369     final int msb_set_in_poly = FindMSBSet(poly);
370     value <<= msb_set_in_poly - 1;
371     // Do the division business using exclusive-or operations.
372     while (FindMSBSet(value) >= msb_set_in_poly) {
373       value ^= poly << (FindMSBSet(value) - msb_set_in_poly);
374     }
375     // Now the "value" is the remainder (i.e. the BCH code)
376     return value;
377   }
378
379   // Make bit vector of type information.  On success, store the
380   // result in "bits" and return true.  On error, return false.
381   // Encode error correction level and mask pattern.  See 8.9 of
382   // JISX0510:2004 (p.45) for details.
383   public static boolean MakeTypeInfoBits(int ec_level, final int mask_pattern, BitVector *bits) {
384     final int ec_code = QRCode.GetECLevelCode(ec_level);
385     if (ec_code == -1) {
386       return false;
387     }
388     if (!QRCode.IsValidMaskPattern(mask_pattern)) {
389       return false;
390     }
391     final uint32 type_info = (ec_code << 3) | mask_pattern;
392     bits.AppendBits(type_info, 5);
393
394     final uint32 bch_code = MatrixUtil.CalculateBCHCode(type_info,
395         kTypeInfoPoly);
396     bits.AppendBits(bch_code, 10);
397
398     BitVector mask_bits;
399     mask_bits.AppendBits(kTypeInfoMaskPattern, 15);
400     bits.XOR(mask_bits);
401
402     if (bits.size() != 15) {  // Just in case.
403       Debug.LOG_ERROR("should not happen but we got: " + bits.size());
404       return false;
405     }
406     return true;
407   }
408
409   // Make bit vector of version information.  On success, store the
410   // result in "bits" and return true.  On error, return false.
411   // Encode version information.  See 8.10 of JISX0510:2004 (p.45) for
412   // details.
413   public static boolean MakeVersionInfoBits(int version, BitVector *bits) {
414     bits.AppendBits(version, 6);
415     final uint32 bch_code = MatrixUtil.CalculateBCHCode(version,
416         kVersionInfoPoly);
417     bits.AppendBits(bch_code, 12);
418     if (bits.size() != 18) {  // Just in case.
419       Debug.LOG_ERROR("should not happen but we got: " + bits.size());
420       return false;
421     }
422     return true;
423   }
424
425   // Check if "value" is empty.
426   private static boolean IsEmpty(final int value) {
427     return value == -1;
428   }
429
430   // Check if "value" is valid.
431   private static boolean IsValidValue(final int value) {
432     return (value == -1 ||  // Empty.
433         value == 0 ||  // Light (white).
434         value == 1);  // Dark (black).
435   }
436
437   private static void EmbedTimingPatterns(QRCodeMatrix *matrix) {
438     // -8 is for skipping position detection patterns (size 7), and
439     // two horizontal/vertical separation patterns (size 1).
440     // Thus, 8 = 7 + 1.
441     for (int i = 8; i < matrix.width() - 8; ++i) {
442       final int bit = (i + 1) % 2;
443       // Horizontal line.
444       Debug.DCHECK(IsValidValue((*matrix)(6, i)));
445       if (IsEmpty((*matrix)(6, i))) {
446       (*matrix)(6, i) = bit;
447     }
448       // Vertical line.
449       Debug.DCHECK(IsValidValue((*matrix)(i, 6)));
450       if (IsEmpty((*matrix)(i, 6))) {
451       (*matrix)(i, 6) = bit;
452     }
453     }
454   }
455
456   // Embed the lonely dark dot at left bottom corner.
457   // JISX0510:2004 (p.46)
458   private static void EmbedDarkDotAtLeftBottomCorner(
459       QRCodeMatrix *matrix) {
460     Debug.DCHECK((*matrix)(matrix.height() - 8, 8));
461     (*matrix)(matrix.height() - 8, 8) = 1;
462   }
463
464   private static void EmbedHorizontalSeparationPattern(final int x_start,
465                                                        final int y_start,
466                                                        QRCodeMatrix *matrix) {
467     // We know the width and height.
468     Debug.DCHECK_EQ(8, arraysize(kHorizontalSeparationPattern[0]));
469     Debug.DCHECK_EQ(1, arraysize(kHorizontalSeparationPattern));
470     for (int x = 0; x < 8; ++x) {
471       Debug.DCHECK(IsEmpty((*matrix)(y_start, x_start + x)));
472       (*matrix)(y_start, x_start + x) = kHorizontalSeparationPattern[0][x];
473     }
474   }
475
476   private static void EmbedVerticalSeparationPattern(final int x_start,
477                                                      final int y_start,
478                                                      QRCodeMatrix *matrix) {
479     // We know the width and height.
480     Debug.DCHECK_EQ(1, arraysize(kVerticalSeparationPattern[0]));
481     Debug.DCHECK_EQ(7, arraysize(kVerticalSeparationPattern));
482     for (int y = 0; y < 7; ++y) {
483       Debug.DCHECK(IsEmpty((*matrix)(y_start + y, x_start)));
484       (*matrix)(y_start + y, x_start) = kVerticalSeparationPattern[y][0];
485     }
486   }
487
488 // Note that we cannot unify the function with
489   // EmbedPositionDetectionPattern() despite they are almost
490   // identical, since we cannot write a function that takes 2D arrays
491   // in different sizes in C/C++.  We should live with the fact.
492   private static void EmbedPositionAdjustmentPattern(final int x_start,
493                                                      final int y_start,
494                                                      QRCodeMatrix *matrix) {
495     // We know the width and height.
496     Debug.DCHECK_EQ(5, arraysize(kPositionAdjustmentPattern[0]));
497     Debug.DCHECK_EQ(5, arraysize(kPositionAdjustmentPattern));
498     for (int y = 0; y < 5; ++y) {
499       for (int x = 0; x < 5; ++x) {
500         Debug.DCHECK(IsEmpty((*matrix)(y_start + y, x_start + x)));
501         (*matrix)(y_start + y, x_start + x) =
502         kPositionAdjustmentPattern[y][x];
503       }
504     }
505   }
506
507   private static void EmbedPositionDetectionPattern(final int x_start,
508                                                     final int y_start,
509                                                     QRCodeMatrix *matrix) {
510     // We know the width and height.
511     Debug.DCHECK_EQ(7, arraysize(kPositionDetectionPattern[0]));
512     Debug.DCHECK_EQ(7, arraysize(kPositionDetectionPattern));
513     for (int y = 0; y < 7; ++y) {
514       for (int x = 0; x < 7; ++x) {
515         Debug.DCHECK(IsEmpty((*matrix)(y_start + y, x_start + x)));
516         (*matrix)(y_start + y, x_start + x) =
517         kPositionDetectionPattern[y][x];
518       }
519     }
520   }
521
522   // Embed position detection patterns and surrounding
523   // vertical/horizontal separators.
524   private static void EmbedPositionDetectionPatternsAndSeparators(QRCodeMatrix *matrix) {
525     // Embed three big squares at corners.
526     final int pdp_width = arraysize(kPositionDetectionPattern[0]);
527     // Left top corner.
528     EmbedPositionDetectionPattern(0, 0, matrix);
529     // Right top corner.
530     EmbedPositionDetectionPattern(matrix.width() - pdp_width, 0, matrix);
531     // Left bottom corner.
532     EmbedPositionDetectionPattern(0, matrix.width() - pdp_width, matrix);
533
534     // Embed horizontal separation patterns around the squares.
535     final int hsp_width = arraysize(kHorizontalSeparationPattern[0]);
536     // Left top corner.
537     EmbedHorizontalSeparationPattern(0, hsp_width - 1, matrix);
538     // Right top corner.
539     EmbedHorizontalSeparationPattern(matrix.width() - hsp_width,
540         hsp_width - 1, matrix);
541     // Left bottom corner.
542     EmbedHorizontalSeparationPattern(0, matrix.width() - hsp_width, matrix);
543
544     // Embed vertical separation patterns around the squares.
545     final int vsp_height = arraysize(kVerticalSeparationPattern);
546     // Left top corner.
547     EmbedVerticalSeparationPattern(vsp_height, 0, matrix);
548     // Right top corner.
549     EmbedVerticalSeparationPattern(matrix.height() - vsp_height - 1, 0, matrix);
550     // Left bottom corner.
551     EmbedVerticalSeparationPattern(vsp_height, matrix.height() - vsp_height,
552         matrix);
553   }
554
555   // Embed position adjustment patterns if need be.
556   private static void MaybeEmbedPositionAdjustmentPatterns(final int version,
557                                                            QRCodeMatrix *matrix) {
558     if (version < 2) {  // The patterns appear if version >= 2
559       return;
560     }
561     final int index = version - 1;
562     final int *coordinates =
563       kPositionAdjustmentPatternCoordinateTable[index];
564     final int num_coordinates =
565         arraysize(kPositionAdjustmentPatternCoordinateTable[index]);
566     for (int i = 0; i < num_coordinates; ++i) {
567       for (int j = 0; j < num_coordinates; ++j) {
568         final int y = coordinates[i];
569         final int x = coordinates[j];
570         if (x == -1 || y == -1) {
571           continue;
572         }
573         // If the cell is unset, we embed the position adjustment
574         // pattern here.
575         if (IsEmpty((*matrix)(y, x))) {
576         // -2 is necessary since the x/y coordinates point to the
577         // center of the pattern, not the left top corner.
578         EmbedPositionAdjustmentPattern(x - 2, y - 2, matrix);
579       }
580       }
581     }
582   }
583
584 }