[LIB-9] Add chesshog-qrcode module
[chesshog.git] / chesshog-qrcode / src / main / java / org / hedgecode / chess / qrcode / ChessQRMatrixUtils.java
diff --git a/chesshog-qrcode/src/main/java/org/hedgecode/chess/qrcode/ChessQRMatrixUtils.java b/chesshog-qrcode/src/main/java/org/hedgecode/chess/qrcode/ChessQRMatrixUtils.java
new file mode 100644 (file)
index 0000000..75df9f9
--- /dev/null
@@ -0,0 +1,137 @@
+/*
+ * Copyright (c) 2018-2019. Developed by Hedgecode.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.hedgecode.chess.qrcode;
+
+import com.google.zxing.common.BitMatrix;
+import com.google.zxing.qrcode.encoder.ByteMatrix;
+
+/**
+ * Chess-specific QR Codes binary matrix accessory utils.
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public final class ChessQRMatrixUtils {
+
+/*
+    private static final int[][] CHESS_LOGO_PATTERN = {
+            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
+            {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},
+            {0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0},
+            {0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1},
+            {0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1},
+            {0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1},
+            {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1},
+            {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1},
+            {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1},
+            {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1},
+            {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
+            {0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1},
+    };
+*/
+
+
+    private static final int[][] CHESS_LOGO_PATTERN = {
+            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
+            {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0},
+            {0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1},
+            {0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1},
+            {0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1},
+            {0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1},
+            {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1},
+            {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1},
+            {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1},
+            {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1},
+            {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1},
+            {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
+            {0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
+    };
+
+/*
+    private static final int[][] CHESS_LOGO_PATTERN = {
+            {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
+            {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
+            {1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1},
+            {1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1},
+            {1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1},
+            {1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1},
+            {1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1},
+            {1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1},
+            {1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1},
+            {1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1},
+            {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
+            {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
+    };
+*/
+
+    private static final int CHESS_LOGO_PATTERN_LENGTH = CHESS_LOGO_PATTERN[0].length;
+
+
+    static ByteMatrix embedChessLogoPattern(ByteMatrix byteMatrix) {
+        if (byteMatrix == null || byteMatrix.getWidth() < CHESS_LOGO_PATTERN_LENGTH + 14) // todo: 14
+            return null;
+
+        int xStart = (byteMatrix.getWidth() - CHESS_LOGO_PATTERN_LENGTH) / 2;
+        int yStart = (byteMatrix.getHeight() - CHESS_LOGO_PATTERN_LENGTH) / 2;
+
+        for (int y = 0; y < CHESS_LOGO_PATTERN_LENGTH; ++y) {
+            int[] patternY = CHESS_LOGO_PATTERN[y];
+            for (int x = 0; x < CHESS_LOGO_PATTERN_LENGTH; ++x) {
+                byteMatrix.set(xStart + x, yStart + y, patternY[x]);
+            }
+        }
+        return byteMatrix;
+    }
+
+
+    static boolean checkChessLogoPattern(ByteMatrix byteMatrix) {
+
+
+        return false;
+    }
+
+    static boolean checkChessLogoPattern(BitMatrix bitMatrix) {
+
+
+        return false;
+    }
+
+
+    static BitMatrix renderMatrix(ByteMatrix byteMatrix, int width, int height, int quietZone) {
+        int inputWidth = byteMatrix.getWidth();
+        int inputHeight = byteMatrix.getHeight();
+        int qrWidth = inputWidth + (quietZone * 2);
+        int qrHeight = inputHeight + (quietZone * 2);
+        int outputWidth = Math.max(width, qrWidth);
+        int outputHeight = Math.max(height, qrHeight);
+
+        int multiple = Math.min(outputWidth / qrWidth, outputHeight / qrHeight);
+        int leftPadding = (outputWidth - (inputWidth * multiple)) / 2;
+        int topPadding = (outputHeight - (inputHeight * multiple)) / 2;
+
+        BitMatrix bitMatrix = new BitMatrix(outputWidth, outputHeight);
+
+        for (int inputY = 0, outputY = topPadding; inputY < inputHeight; inputY++, outputY += multiple) {
+            for (int inputX = 0, outputX = leftPadding; inputX < inputWidth; inputX++, outputX += multiple) {
+                if (byteMatrix.get(inputX, inputY) == 1) {
+                    bitMatrix.setRegion(outputX, outputY, multiple, multiple);
+                }
+            }
+        }
+        return bitMatrix;
+    }
+
+}