| 1 | /*************************************************************************** |
| 2 | * fheroes2: https://github.com/ihhub/fheroes2 * |
| 3 | * Copyright (C) 2020 - 2026 * |
| 4 | * * |
| 5 | * This program is free software; you can redistribute it and/or modify * |
| 6 | * it under the terms of the GNU General Public License as published by * |
| 7 | * the Free Software Foundation; either version 2 of the License, or * |
| 8 | * (at your option) any later version. * |
| 9 | * * |
| 10 | * This program is distributed in the hope that it will be useful, * |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 13 | * GNU General Public License for more details. * |
| 14 | * * |
| 15 | * You should have received a copy of the GNU General Public License * |
| 16 | * along with this program; if not, write to the * |
| 17 | * Free Software Foundation, Inc., * |
| 18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 19 | ***************************************************************************/ |
| 20 | |
| 21 | #include "image.h" |
| 22 | |
| 23 | #include <algorithm> |
| 24 | #include <cmath> |
| 25 | #include <cstddef> |
| 26 | #include <cstdlib> |
| 27 | #include <cstring> |
| 28 | |
| 29 | #include "exception.h" |
| 30 | #include "image_color_conversion.h" |
| 31 | #include "image_palette.h" |
| 32 | |
| 33 | #if defined( GENERATE_COLOR_TABLE ) |
| 34 | #include <array> |
| 35 | #endif |
| 36 | |
| 37 | namespace |
| 38 | { |
| 39 | // 0 in shadow part means no shadow, 1 means skip any drawings so to don't waste extra CPU cycles for ( tableId - 2 ) command we just add extra fake tables |
| 40 | // Mirror palette was modified as it was containing 238, 238, 239, 240 values instead of 238, 239, 240, 241 |
| 41 | // !!! WARNING !!! |
| 42 | // If you modify "No cycle" part of the table make sure to update nonCyclingUniqueColorPos variable below (see GetPALColorId() function). |
| 43 | const uint8_t transformTable[256 * 16] = { |
| 44 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 45 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 46 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 47 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 48 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 49 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 50 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 51 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 52 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 53 | |
| 54 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 55 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 56 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 57 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 58 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 59 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 60 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 61 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 62 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 63 | |
| 64 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, |
| 65 | 35, 36, 36, 36, 36, 36, 36, 36, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 62, |
| 66 | 62, 62, 62, 62, 62, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 84, 84, 84, 84, 84, 91, 92, |
| 67 | 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 107, 107, 107, 107, 107, 107, 114, 115, 116, 117, 118, 119, 120, 121, |
| 68 | 122, 123, 124, 125, 126, 127, 128, 129, 130, 130, 130, 130, 130, 130, 130, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, |
| 69 | 150, 151, 151, 151, 151, 151, 151, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 174, 174, 174, 174, 174, |
| 70 | 174, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 197, 197, 197, 197, 197, 202, 203, 204, 205, 206, |
| 71 | 207, 208, 209, 210, 211, 212, 213, 213, 213, 213, 213, 214, 215, 216, 217, 218, 219, 220, 221, 225, 226, 227, 228, 229, 230, 230, 230, 230, 73, |
| 72 | 75, 77, 79, 81, 76, 78, 74, 76, 78, 80, 244, 245, 245, 245, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // First |
| 73 | |
| 74 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, |
| 75 | 33, 34, 35, 36, 36, 36, 36, 36, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, |
| 76 | 62, 62, 62, 62, 62, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 84, 84, 84, 89, 90, |
| 77 | 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 107, 107, 107, 107, 112, 113, 114, 115, 116, 117, 118, 119, |
| 78 | 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 130, 130, 130, 130, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, |
| 79 | 148, 149, 150, 151, 151, 151, 151, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 174, 174, 174, |
| 80 | 174, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 197, 197, 197, 201, 202, 203, 204, 205, |
| 81 | 206, 207, 208, 209, 210, 211, 212, 213, 213, 213, 213, 214, 215, 216, 217, 218, 219, 220, 221, 224, 225, 226, 227, 228, 229, 230, 230, 230, 76, |
| 82 | 76, 76, 76, 76, 76, 76, 76, 76, 76, 78, 244, 245, 245, 245, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Second |
| 83 | |
| 84 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 85 | 31, 32, 33, 34, 35, 36, 36, 36, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, |
| 86 | 60, 61, 62, 62, 62, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 84, 84, 87, 88, |
| 87 | 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 107, 107, 110, 111, 112, 113, 114, 115, 116, 117, |
| 88 | 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 130, 130, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, |
| 89 | 147, 148, 149, 150, 151, 151, 151, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 174, |
| 90 | 174, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 197, 197, 200, 201, 202, 203, 204, |
| 91 | 205, 206, 207, 208, 209, 210, 211, 212, 213, 213, 213, 214, 215, 216, 217, 218, 219, 220, 221, 223, 224, 225, 226, 227, 228, 229, 230, 230, 76, |
| 92 | 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 243, 244, 245, 245, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Third |
| 93 | |
| 94 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, |
| 95 | 30, 31, 32, 33, 34, 35, 36, 36, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, |
| 96 | 59, 60, 61, 62, 62, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 84, 86, 87, |
| 97 | 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 107, 109, 110, 111, 112, 113, 114, 115, 116, |
| 98 | 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 130, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, |
| 99 | 146, 147, 148, 149, 150, 151, 151, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, |
| 100 | 174, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 197, 199, 200, 201, 202, 203, |
| 101 | 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 213, 214, 215, 216, 217, 218, 219, 220, 221, 223, 224, 225, 226, 227, 228, 229, 230, 230, 75, |
| 102 | 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 243, 244, 245, 245, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Fourth |
| 103 | |
| 104 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 11, 11, 11, 12, 13, 13, 13, 14, 14, 15, 15, 15, 16, 17, 17, 17, 18, |
| 105 | 18, 19, 19, 20, 20, 20, 21, 21, 11, 37, 37, 37, 38, 38, 39, 39, 39, 40, 40, 41, 41, 41, 41, 42, 42, 19, 42, 20, 20, |
| 106 | 20, 20, 20, 20, 21, 12, 131, 63, 63, 63, 64, 64, 64, 65, 65, 65, 65, 65, 242, 242, 242, 242, 242, 242, 242, 242, 242, 13, 14, |
| 107 | 15, 15, 16, 85, 17, 85, 85, 85, 85, 19, 86, 20, 20, 20, 21, 21, 21, 21, 21, 21, 21, 10, 108, 108, 109, 109, 109, 110, 110, |
| 108 | 110, 110, 199, 40, 41, 41, 41, 41, 41, 42, 42, 42, 42, 20, 20, 11, 11, 131, 131, 132, 132, 132, 133, 133, 134, 134, 134, 135, 135, |
| 109 | 18, 136, 19, 19, 20, 20, 20, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 15, 15, 15, 16, 17, 17, 17, 18, 18, 19, 19, 20, |
| 110 | 20, 11, 175, 175, 176, 176, 38, 177, 177, 178, 178, 178, 179, 179, 179, 179, 180, 180, 180, 180, 180, 180, 21, 21, 108, 108, 38, 109, 38, |
| 111 | 109, 39, 40, 40, 41, 41, 41, 42, 42, 42, 20, 199, 179, 180, 180, 110, 110, 40, 42, 110, 110, 86, 86, 86, 86, 18, 18, 19, 65, |
| 112 | 65, 65, 66, 65, 66, 65, 152, 155, 65, 242, 15, 16, 17, 19, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Fifth |
| 113 | |
| 114 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 11, 11, 12, 12, 13, 13, 14, 15, 15, 16, 16, 17, 17, 18, 19, 20, 20, 21, |
| 115 | 21, 22, 22, 23, 24, 24, 25, 25, 37, 37, 38, 38, 39, 39, 40, 41, 41, 41, 42, 42, 43, 43, 44, 44, 45, 45, 46, 46, 23, |
| 116 | 24, 24, 24, 24, 24, 131, 63, 63, 64, 64, 65, 65, 66, 66, 242, 67, 67, 68, 68, 243, 243, 243, 243, 243, 243, 243, 243, 15, 15, |
| 117 | 85, 85, 85, 85, 86, 86, 87, 87, 88, 88, 88, 88, 89, 24, 90, 25, 25, 25, 25, 25, 25, 37, 108, 109, 109, 110, 110, 111, 111, |
| 118 | 200, 200, 201, 201, 42, 43, 43, 44, 44, 44, 45, 45, 46, 46, 46, 11, 131, 132, 132, 132, 133, 133, 134, 135, 135, 136, 242, 137, 137, |
| 119 | 138, 243, 243, 243, 243, 243, 24, 152, 152, 153, 153, 154, 154, 155, 156, 156, 157, 158, 158, 159, 18, 19, 19, 20, 20, 21, 22, 22, 23, |
| 120 | 24, 37, 175, 176, 176, 177, 177, 178, 179, 179, 180, 180, 180, 181, 181, 181, 182, 182, 182, 46, 47, 47, 48, 25, 108, 109, 109, 109, 198, |
| 121 | 199, 199, 201, 201, 42, 43, 43, 44, 45, 46, 46, 201, 181, 182, 183, 111, 111, 202, 45, 111, 111, 87, 88, 88, 88, 88, 21, 22, 66, |
| 122 | 66, 68, 68, 67, 68, 68, 152, 157, 66, 69, 16, 18, 20, 21, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Sixth |
| 123 | |
| 124 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 11, 11, 12, 13, 14, 14, 15, 16, 17, 17, 18, 19, 20, 20, 21, 22, 23, 24, |
| 125 | 24, 25, 26, 26, 27, 28, 29, 29, 37, 37, 38, 39, 40, 40, 41, 42, 42, 43, 44, 44, 45, 46, 46, 47, 47, 48, 48, 49, 50, |
| 126 | 50, 27, 28, 28, 28, 63, 63, 64, 65, 65, 66, 67, 67, 68, 69, 69, 69, 70, 70, 70, 244, 71, 244, 244, 244, 244, 245, 16, 85, |
| 127 | 85, 86, 87, 87, 88, 88, 89, 90, 90, 91, 91, 91, 92, 93, 93, 93, 29, 29, 29, 29, 29, 37, 109, 109, 110, 111, 111, 112, 113, |
| 128 | 112, 112, 203, 203, 203, 44, 45, 46, 47, 47, 47, 48, 48, 49, 50, 131, 131, 132, 133, 133, 134, 135, 136, 136, 137, 137, 139, 139, 139, |
| 129 | 141, 141, 141, 143, 143, 245, 245, 152, 152, 153, 154, 155, 155, 156, 157, 158, 158, 159, 160, 161, 162, 163, 163, 164, 165, 165, 166, 26, 26, |
| 130 | 27, 175, 13, 176, 177, 178, 178, 179, 180, 181, 181, 182, 182, 183, 183, 183, 184, 184, 185, 185, 50, 50, 52, 52, 109, 109, 198, 199, 200, |
| 131 | 201, 201, 202, 202, 44, 45, 46, 47, 48, 48, 49, 204, 205, 185, 185, 112, 112, 204, 47, 112, 113, 88, 89, 91, 92, 93, 93, 25, 66, |
| 132 | 68, 69, 69, 68, 69, 69, 153, 159, 68, 71, 18, 242, 243, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Seventh |
| 133 | |
| 134 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 11, 12, 13, 13, 14, 15, 16, 17, 17, 19, 19, 20, 21, 22, 23, 24, 24, 26, |
| 135 | 26, 27, 28, 28, 30, 30, 31, 32, 37, 38, 39, 39, 40, 41, 42, 43, 43, 44, 45, 46, 46, 47, 48, 49, 50, 50, 51, 52, 52, |
| 136 | 53, 54, 54, 30, 31, 63, 64, 64, 65, 66, 67, 68, 69, 69, 70, 71, 71, 71, 72, 72, 72, 73, 73, 73, 168, 168, 168, 85, 85, |
| 137 | 86, 87, 88, 88, 89, 90, 91, 91, 92, 93, 93, 94, 95, 95, 96, 96, 96, 31, 32, 32, 32, 108, 109, 198, 110, 111, 112, 113, 113, |
| 138 | 113, 116, 117, 118, 119, 120, 121, 47, 48, 50, 50, 51, 51, 52, 52, 131, 132, 132, 133, 134, 135, 136, 137, 137, 138, 139, 140, 141, 141, |
| 139 | 143, 143, 144, 145, 146, 147, 30, 152, 153, 153, 154, 155, 156, 157, 158, 158, 159, 160, 161, 162, 163, 164, 165, 165, 166, 167, 168, 169, 28, |
| 140 | 29, 175, 176, 177, 177, 178, 179, 180, 181, 182, 182, 183, 184, 185, 185, 185, 186, 186, 187, 50, 52, 52, 54, 55, 109, 198, 199, 200, 201, |
| 141 | 202, 202, 204, 204, 205, 207, 47, 49, 50, 51, 52, 206, 206, 187, 188, 113, 113, 118, 49, 222, 222, 223, 224, 225, 226, 95, 227, 228, 67, |
| 142 | 68, 70, 71, 69, 71, 70, 153, 65, 69, 73, 242, 22, 243, 244, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 143 | 0, // Eighth |
| 144 | |
| 145 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 11, 11, 12, 12, 13, 14, 14, 15, 16, 16, 17, 18, 18, 19, 20, 242, 242, 22, |
| 146 | 22, 23, 243, 243, 25, 244, 244, 244, 11, 37, 38, 38, 39, 40, 40, 41, 178, 18, 19, 20, 20, 21, 21, 22, 22, 22, 22, 22, 23, |
| 147 | 23, 23, 23, 24, 244, 63, 63, 64, 64, 65, 65, 66, 66, 67, 67, 68, 68, 68, 69, 69, 69, 69, 69, 69, 70, 70, 70, 15, 15, |
| 148 | 16, 85, 86, 18, 19, 19, 20, 159, 21, 21, 161, 22, 163, 163, 163, 23, 23, 165, 165, 244, 244, 37, 108, 38, 109, 109, 110, 199, 200, |
| 149 | 199, 40, 41, 42, 42, 42, 43, 43, 44, 22, 22, 23, 23, 23, 23, 131, 131, 132, 132, 133, 133, 134, 134, 135, 135, 136, 136, 137, 137, |
| 150 | 138, 138, 139, 139, 140, 141, 244, 152, 152, 153, 153, 154, 154, 155, 155, 156, 156, 157, 158, 158, 159, 242, 159, 161, 161, 243, 243, 243, 243, |
| 151 | 164, 11, 175, 176, 176, 177, 177, 178, 179, 179, 180, 180, 181, 181, 182, 182, 182, 182, 183, 22, 23, 23, 23, 23, 108, 38, 38, 39, 39, |
| 152 | 40, 40, 41, 178, 180, 42, 44, 45, 23, 23, 23, 180, 181, 181, 183, 110, 200, 42, 45, 85, 86, 87, 87, 87, 21, 22, 22, 23, 66, |
| 153 | 66, 67, 68, 67, 68, 68, 153, 158, 67, 70, 64, 65, 242, 243, 159, 159, 159, 159, 159, 159, 159, 159, 159, 10, // Ninth |
| 154 | |
| 155 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 11, 11, 12, 13, 14, 14, 15, 16, 16, 17, 18, 19, 19, 20, 242, 22, 22, 243, |
| 156 | 243, 243, 244, 244, 244, 244, 245, 245, 37, 37, 38, 176, 39, 177, 41, 41, 42, 179, 20, 180, 45, 22, 23, 23, 24, 24, 24, 24, 25, |
| 157 | 25, 25, 25, 26, 26, 63, 63, 64, 64, 65, 66, 67, 67, 68, 68, 69, 69, 70, 70, 70, 70, 70, 71, 71, 71, 71, 71, 15, 85, |
| 158 | 85, 86, 86, 87, 20, 88, 89, 22, 161, 162, 163, 163, 164, 164, 165, 165, 166, 166, 167, 167, 167, 37, 108, 109, 109, 110, 199, 111, 111, |
| 159 | 200, 201, 41, 43, 43, 43, 44, 44, 46, 46, 24, 24, 25, 25, 25, 131, 131, 132, 132, 133, 134, 135, 135, 136, 136, 137, 138, 138, 139, |
| 160 | 139, 140, 140, 141, 141, 142, 143, 152, 152, 153, 153, 154, 155, 155, 156, 156, 157, 158, 158, 159, 159, 161, 161, 162, 162, 163, 164, 244, 244, |
| 161 | 244, 175, 175, 176, 177, 177, 178, 179, 179, 180, 181, 181, 182, 182, 183, 183, 183, 184, 184, 184, 25, 25, 25, 25, 108, 109, 109, 39, 40, |
| 162 | 41, 41, 42, 42, 43, 43, 45, 46, 47, 25, 25, 181, 182, 183, 185, 111, 111, 42, 46, 111, 87, 88, 88, 88, 22, 23, 24, 25, 66, |
| 163 | 67, 68, 69, 68, 69, 69, 153, 0, 68, 71, 65, 242, 242, 243, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, // Tenth |
| 164 | |
| 165 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 11, 11, 13, 13, 14, 15, 16, 16, 17, 18, 19, 20, 21, 21, 22, 23, 243, 25, |
| 166 | 244, 244, 244, 28, 245, 245, 245, 31, 37, 38, 38, 39, 40, 41, 41, 42, 42, 180, 45, 46, 46, 47, 47, 25, 26, 26, 26, 26, 27, |
| 167 | 27, 27, 27, 27, 245, 63, 63, 64, 65, 66, 66, 67, 68, 69, 69, 70, 70, 71, 71, 72, 72, 72, 72, 72, 73, 73, 73, 16, 85, |
| 168 | 85, 86, 87, 88, 88, 90, 90, 91, 91, 163, 164, 164, 165, 166, 166, 167, 167, 168, 169, 169, 170, 37, 108, 109, 198, 199, 111, 112, 112, |
| 169 | 201, 202, 202, 43, 44, 45, 45, 46, 46, 47, 48, 26, 27, 27, 27, 131, 131, 132, 133, 134, 135, 135, 136, 137, 137, 138, 139, 139, 140, |
| 170 | 141, 141, 142, 143, 143, 144, 145, 152, 152, 153, 154, 155, 155, 156, 156, 158, 158, 159, 160, 160, 161, 162, 163, 164, 164, 165, 166, 166, 167, |
| 171 | 167, 175, 176, 176, 177, 178, 178, 179, 180, 181, 182, 182, 183, 184, 184, 184, 185, 186, 186, 50, 51, 27, 27, 27, 109, 109, 109, 40, 40, |
| 172 | 41, 42, 43, 43, 44, 45, 46, 47, 49, 27, 27, 182, 183, 184, 187, 112, 112, 43, 47, 112, 87, 89, 90, 91, 91, 24, 26, 26, 67, |
| 173 | 68, 69, 70, 69, 70, 70, 153, 0, 0, 73, 65, 242, 243, 244, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 174 | 10, // Eleventh |
| 175 | |
| 176 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 11, 12, 13, 13, 14, 15, 16, 17, 18, 19, 20, 21, 21, 23, 243, 24, 25, 244, |
| 177 | 244, 27, 245, 245, 31, 170, 149, 149, 37, 38, 38, 39, 40, 41, 42, 42, 44, 45, 46, 46, 47, 48, 49, 50, 51, 28, 28, 28, 29, |
| 178 | 29, 29, 29, 29, 30, 63, 64, 65, 65, 66, 67, 68, 69, 70, 70, 71, 72, 72, 73, 73, 74, 74, 75, 75, 76, 76, 76, 85, 85, |
| 179 | 86, 87, 88, 89, 90, 91, 91, 92, 93, 93, 166, 166, 96, 168, 168, 169, 170, 170, 171, 171, 171, 37, 109, 109, 110, 200, 111, 112, 113, |
| 180 | 202, 202, 203, 44, 45, 46, 46, 47, 48, 49, 50, 51, 52, 29, 29, 131, 132, 133, 133, 134, 135, 136, 137, 138, 139, 139, 140, 141, 142, |
| 181 | 142, 144, 144, 145, 145, 146, 147, 152, 153, 153, 154, 155, 156, 157, 158, 159, 159, 160, 161, 162, 163, 164, 164, 165, 166, 167, 168, 168, 168, |
| 182 | 169, 175, 176, 177, 177, 178, 179, 180, 181, 182, 182, 183, 184, 185, 186, 186, 187, 187, 189, 189, 193, 193, 146, 146, 109, 109, 198, 199, 201, |
| 183 | 201, 201, 44, 205, 45, 46, 47, 48, 50, 52, 29, 183, 185, 186, 189, 112, 112, 205, 49, 222, 88, 89, 91, 92, 93, 26, 27, 28, 67, |
| 184 | 68, 70, 71, 69, 71, 71, 154, 0, 0, 75, 242, 242, 243, 244, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, // Twelfth |
| 185 | |
| 186 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, |
| 187 | 25, 26, 27, 28, 29, 30, 31, 32, 37, 37, 37, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, |
| 188 | 54, 55, 56, 57, 58, 63, 63, 63, 63, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 85, 85, |
| 189 | 85, 85, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 108, 108, 108, 108, 108, 109, 110, 111, |
| 190 | 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 131, 131, 131, 131, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, |
| 191 | 141, 142, 143, 144, 145, 146, 147, 152, 152, 152, 152, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, |
| 192 | 170, 175, 175, 175, 175, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 198, 198, 198, 198, 198, |
| 193 | 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 214, 215, 216, 217, 218, 219, 220, 221, 222, 222, 223, 224, 225, 226, 227, 228, 229, 231, |
| 194 | 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 242, 243, 244, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, // Mirror |
| 195 | |
| 196 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, |
| 197 | 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, |
| 198 | 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, |
| 199 | 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, |
| 200 | 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, |
| 201 | 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, |
| 202 | 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, |
| 203 | 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 188, 188, 188, 188, 118, 118, 118, 118, 222, 223, 224, 225, 226, 227, 228, 229, 230, 69, |
| 204 | 69, 69, 69, 69, 236, 237, 69, 69, 69, 69, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255 // No cycle |
| 205 | }; |
| 206 | |
| 207 | bool Validate( const fheroes2::Image & image, const int32_t x, const int32_t y, const int32_t width, const int32_t height ) |
| 208 | { |
| 209 | if ( image.empty() || width <= 0 || height <= 0 ) { |
| 210 | // What's the reason to work with empty images? |
| 211 | return false; |
| 212 | } |
| 213 | |
| 214 | if ( x < 0 || y < 0 || x + width > image.width() || y + height > image.height() ) { |
| 215 | return false; |
| 216 | } |
| 217 | |
| 218 | return true; |
| 219 | } |
| 220 | |
| 221 | bool Verify( const fheroes2::Image & image, int32_t & x, int32_t & y, int32_t & width, int32_t & height ) |
| 222 | { |
| 223 | if ( image.empty() || width <= 0 || height <= 0 ) { |
| 224 | // What's the reason to work with empty images? |
| 225 | return false; |
| 226 | } |
| 227 | |
| 228 | const int32_t widthOut = image.width(); |
| 229 | const int32_t heightOut = image.height(); |
| 230 | |
| 231 | if ( x < 0 ) { |
| 232 | const int32_t offsetX = -x; |
| 233 | if ( offsetX >= width ) { |
| 234 | return false; |
| 235 | } |
| 236 | |
| 237 | x = 0; |
| 238 | width -= offsetX; |
| 239 | } |
| 240 | |
| 241 | if ( y < 0 ) { |
| 242 | const int32_t offsetY = -y; |
| 243 | if ( offsetY >= height ) { |
| 244 | return false; |
| 245 | } |
| 246 | |
| 247 | y = 0; |
| 248 | height -= offsetY; |
| 249 | } |
| 250 | |
| 251 | if ( x > widthOut || y > heightOut ) { |
| 252 | return false; |
| 253 | } |
| 254 | |
| 255 | if ( x + width > widthOut ) { |
| 256 | const int32_t offsetX = x + width - widthOut; |
| 257 | if ( offsetX >= width ) { |
| 258 | return false; |
| 259 | } |
| 260 | width -= offsetX; |
| 261 | } |
| 262 | |
| 263 | if ( y + height > heightOut ) { |
| 264 | const int32_t offsetY = y + height - heightOut; |
| 265 | if ( offsetY >= height ) { |
| 266 | return false; |
| 267 | } |
| 268 | height -= offsetY; |
| 269 | } |
| 270 | |
| 271 | return true; |
| 272 | } |
| 273 | |
| 274 | bool Verify( int32_t & inX, int32_t & inY, int32_t & outX, int32_t & outY, int32_t & width, int32_t & height, const int32_t widthIn, const int32_t heightIn, |
| 275 | const int32_t widthOut, const int32_t heightOut ) |
| 276 | { |
| 277 | if ( widthIn <= 0 || heightIn <= 0 || widthOut <= 0 || heightOut <= 0 || width <= 0 || height <= 0 ) { |
| 278 | // What's the reason to work with empty images? |
| 279 | return false; |
| 280 | } |
| 281 | |
| 282 | if ( inX < 0 || inY < 0 || inX > widthIn || inY > heightIn ) { |
| 283 | return false; |
| 284 | } |
| 285 | |
| 286 | if ( outX < 0 ) { |
| 287 | const int32_t offsetX = -outX; |
| 288 | if ( offsetX >= width ) { |
| 289 | return false; |
| 290 | } |
| 291 | |
| 292 | inX += offsetX; |
| 293 | outX = 0; |
| 294 | width -= offsetX; |
| 295 | } |
| 296 | |
| 297 | if ( outY < 0 ) { |
| 298 | const int32_t offsetY = -outY; |
| 299 | if ( offsetY >= height ) { |
| 300 | return false; |
| 301 | } |
| 302 | |
| 303 | inY += offsetY; |
| 304 | outY = 0; |
| 305 | height -= offsetY; |
| 306 | } |
| 307 | |
| 308 | if ( outX > widthOut || outY > heightOut ) { |
| 309 | return false; |
| 310 | } |
| 311 | |
| 312 | if ( inX + width > widthIn ) { |
| 313 | const int32_t offsetX = inX + width - widthIn; |
| 314 | if ( offsetX >= width ) { |
| 315 | return false; |
| 316 | } |
| 317 | width -= offsetX; |
| 318 | } |
| 319 | |
| 320 | if ( inY + height > heightIn ) { |
| 321 | const int32_t offsetY = inY + height - heightIn; |
| 322 | if ( offsetY >= height ) { |
| 323 | return false; |
| 324 | } |
| 325 | height -= offsetY; |
| 326 | } |
| 327 | |
| 328 | if ( outX + width > widthOut ) { |
| 329 | const int32_t offsetX = outX + width - widthOut; |
| 330 | if ( offsetX >= width ) { |
| 331 | return false; |
| 332 | } |
| 333 | width -= offsetX; |
| 334 | } |
| 335 | |
| 336 | if ( outY + height > heightOut ) { |
| 337 | const int32_t offsetY = outY + height - heightOut; |
| 338 | if ( offsetY >= height ) { |
| 339 | return false; |
| 340 | } |
| 341 | height -= offsetY; |
| 342 | } |
| 343 | |
| 344 | return true; |
| 345 | } |
| 346 | |
| 347 | bool Verify( const fheroes2::Image & in, int32_t & inX, int32_t & inY, const fheroes2::Image & out, int32_t & outX, int32_t & outY, int32_t & width, |
| 348 | int32_t & height ) |
| 349 | { |
| 350 | return Verify( inX, inY, outX, outY, width, height, in.width(), in.height(), out.width(), out.height() ); |
| 351 | } |
| 352 | |
| 353 | #if defined( GENERATE_COLOR_TABLE ) |
| 354 | // This function is only used when generating a new compressed color conversion table. |
| 355 | void generateColorConversionTable( uint8_t * rgbToId, const size_t size ) |
| 356 | { |
| 357 | assert( rgbToId != nullptr ); |
| 358 | |
| 359 | const uint8_t * gamePalette = fheroes2::getGamePalette(); |
| 360 | |
| 361 | // Use the "No cycle" palette. |
| 362 | // The first 10 and the last 10 colors are undefined in the original palette. We skip them to avoid usage of these colors. |
| 363 | // Plus we exclude all repeated colors. |
| 364 | constexpr uint32_t colorCount{ 219 }; |
| 365 | const std::array<uint8_t, colorCount> nonCyclingUniqueColorPos{ 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, |
| 366 | 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, |
| 367 | 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, |
| 368 | 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, |
| 369 | 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, |
| 370 | 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, |
| 371 | 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, |
| 372 | 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, |
| 373 | 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, |
| 374 | 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, |
| 375 | 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 222, 223, 224, 225, 226, |
| 376 | 227, 228, 229, 230, 236, 237, 242, 243, 244, 245 }; |
| 377 | |
| 378 | const uint8_t * correctorY = nonCyclingUniqueColorPos.data(); |
| 379 | |
| 380 | for ( uint32_t id = 0; id < size; ++id ) { |
| 381 | const int32_t r = static_cast<int32_t>( id & 63 ); |
| 382 | const int32_t g = static_cast<int32_t>( id >> 6 ) & 63; |
| 383 | const int32_t b = static_cast<int32_t>( id >> 12 ); |
| 384 | int32_t minDistance = INT32_MAX; |
| 385 | uint8_t bestPos = 0; |
| 386 | |
| 387 | const uint8_t * correctorX = correctorY; |
| 388 | const uint8_t * correctorXEnd = correctorX + colorCount; |
| 389 | |
| 390 | for ( ; correctorX != correctorXEnd; ++correctorX ) { |
| 391 | const uint8_t * palette = gamePalette + static_cast<ptrdiff_t>( *correctorX ) * 3; |
| 392 | |
| 393 | const int32_t sumRed = static_cast<int32_t>( *palette ) + r; |
| 394 | const int32_t offsetRed = static_cast<int32_t>( *palette ) - r; |
| 395 | ++palette; |
| 396 | const int32_t offsetGreen = static_cast<int32_t>( *palette ) - g; |
| 397 | ++palette; |
| 398 | const int32_t offsetBlue = static_cast<int32_t>( *palette ) - b; |
| 399 | |
| 400 | // Based on "Redmean" color distance calculation (https://www.compuphase.com/cmetric.htm). |
| 401 | const int32_t distance = ( 2 * 2 * 256 + sumRed ) * offsetRed * offsetRed + 4 * 2 * 256 * offsetGreen * offsetGreen |
| 402 | + ( 2 * ( 2 * 256 + 255 ) - sumRed ) * offsetBlue * offsetBlue; |
| 403 | if ( minDistance > distance ) { |
| 404 | minDistance = distance; |
| 405 | bestPos = *correctorX; |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | rgbToId[id] = bestPos; |
| 410 | } |
| 411 | } |
| 412 | #endif |
| 413 | |
| 414 | uint8_t GetPALColorId( const uint8_t red, const uint8_t green, const uint8_t blue ) |
| 415 | { |
| 416 | constexpr uint32_t size = 64 * 64 * 64; |
| 417 | static uint8_t rgbToId[size]; |
| 418 | static bool isInitialized = false; |
| 419 | if ( !isInitialized ) { |
| 420 | isInitialized = true; |
| 421 | |
| 422 | if ( !getColorConversionTable( rgbToId, size ) ) { |
| 423 | throw fheroes2::CorruptedExecutable{ "Application is corrupted." }; |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | return rgbToId[red + ( green << 6U ) + ( blue << 12U )]; |
| 428 | } |
| 429 | |
| 430 | void ApplyRawPalette( const fheroes2::Image & in, int32_t inX, int32_t inY, fheroes2::Image & out, int32_t outX, int32_t outY, int32_t width, int32_t height, |
| 431 | const uint8_t * palette ) |
| 432 | { |
| 433 | if ( !Verify( in, inX, inY, out, outX, outY, width, height ) ) { |
| 434 | return; |
| 435 | } |
| 436 | |
| 437 | const int32_t widthIn = in.width(); |
| 438 | const int32_t widthOut = out.width(); |
| 439 | |
| 440 | const uint8_t * imageInY = in.image() + static_cast<ptrdiff_t>( inY ) * widthIn + inX; |
| 441 | uint8_t * imageOutY = out.image() + static_cast<ptrdiff_t>( outY ) * widthOut + outX; |
| 442 | const uint8_t * imageInYEnd = imageInY + static_cast<ptrdiff_t>( height ) * widthIn; |
| 443 | |
| 444 | if ( in.singleLayer() ) { |
| 445 | // All pixels in a single-layer image do not have any transform values so there is no need to check for them. |
| 446 | for ( ; imageInY != imageInYEnd; imageInY += widthIn, imageOutY += widthOut ) { |
| 447 | const uint8_t * imageInX = imageInY; |
| 448 | uint8_t * imageOutX = imageOutY; |
| 449 | const uint8_t * imageInXEnd = imageInX + width; |
| 450 | |
| 451 | for ( ; imageInX != imageInXEnd; ++imageInX, ++imageOutX ) { |
| 452 | *imageOutX = palette[*imageInX]; |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 | else { |
| 457 | const uint8_t * transformInY = in.transform() + static_cast<ptrdiff_t>( inY ) * widthIn + inX; |
| 458 | |
| 459 | for ( ; imageInY != imageInYEnd; imageInY += widthIn, transformInY += widthIn, imageOutY += widthOut ) { |
| 460 | const uint8_t * imageInX = imageInY; |
| 461 | const uint8_t * transformInX = transformInY; |
| 462 | uint8_t * imageOutX = imageOutY; |
| 463 | const uint8_t * imageInXEnd = imageInX + width; |
| 464 | |
| 465 | for ( ; imageInX != imageInXEnd; ++imageInX, ++imageOutX, ++transformInX ) { |
| 466 | if ( *transformInX == 0 ) { // only modify pixels with data |
| 467 | *imageOutX = palette[*imageInX]; |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | namespace fheroes2 |
| 476 | { |
| 477 | Image::Image( Image && image ) noexcept |
| 478 | : _data( std::move( image._data ) ) |
| 479 | { |
| 480 | std::swap( _width, image._width ); |
| 481 | std::swap( _height, image._height ); |
| 482 | std::swap( _singleLayer, image._singleLayer ); |
| 483 | } |
| 484 | |
| 485 | Image & Image::operator=( const Image & image ) |
| 486 | { |
| 487 | if ( this == &image ) { |
| 488 | return *this; |
| 489 | } |
| 490 | |
| 491 | copy( image ); |
| 492 | |
| 493 | return *this; |
| 494 | } |
| 495 | |
| 496 | Image & Image::operator=( Image && image ) noexcept |
| 497 | { |
| 498 | if ( this == &image ) { |
| 499 | return *this; |
| 500 | } |
| 501 | |
| 502 | std::swap( _width, image._width ); |
| 503 | std::swap( _height, image._height ); |
| 504 | std::swap( _data, image._data ); |
| 505 | std::swap( _singleLayer, image._singleLayer ); |
| 506 | |
| 507 | return *this; |
| 508 | } |
| 509 | |
| 510 | uint8_t * Image::image() |
| 511 | { |
| 512 | return _data.get(); |
| 513 | } |
| 514 | |
| 515 | const uint8_t * Image::image() const |
| 516 | { |
| 517 | return _data.get(); |
| 518 | } |
| 519 | |
| 520 | void Image::clear() |
| 521 | { |
| 522 | _data.reset(); |
| 523 | |
| 524 | _width = 0; |
| 525 | _height = 0; |
| 526 | } |
| 527 | |
| 528 | void Image::fill( const uint8_t value ) |
| 529 | { |
| 530 | if ( !empty() ) { |
| 531 | const size_t totalSize = static_cast<size_t>( _width ) * _height; |
| 532 | memset( image(), value, totalSize ); |
| 533 | |
| 534 | if ( !_singleLayer ) { |
| 535 | memset( transform(), static_cast<uint8_t>( 0 ), totalSize ); |
| 536 | } |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | void Image::resize( const int32_t width_, const int32_t height_ ) |
| 541 | { |
| 542 | if ( width_ == _width && height_ == _height ) { |
| 543 | return; |
| 544 | } |
| 545 | |
| 546 | if ( width_ <= 0 || height_ <= 0 ) { |
| 547 | clear(); |
| 548 | |
| 549 | return; |
| 550 | } |
| 551 | |
| 552 | const size_t size = static_cast<size_t>( width_ ) * height_; |
| 553 | |
| 554 | if ( _singleLayer ) { |
| 555 | _data.reset( new uint8_t[size] ); |
| 556 | } |
| 557 | else { |
| 558 | _data.reset( new uint8_t[size * 2] ); |
| 559 | } |
| 560 | |
| 561 | _width = width_; |
| 562 | _height = height_; |
| 563 | } |
| 564 | |
| 565 | void Image::reset() |
| 566 | { |
| 567 | if ( !empty() ) { |
| 568 | const size_t totalSize = static_cast<size_t>( _width ) * _height; |
| 569 | memset( image(), static_cast<uint8_t>( 0 ), totalSize ); |
| 570 | |
| 571 | if ( !_singleLayer ) { |
| 572 | // Set the transform layer to skip all data. |
| 573 | memset( transform(), static_cast<uint8_t>( 1 ), totalSize ); |
| 574 | } |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | void Image::copy( const Image & image ) |
| 579 | { |
| 580 | if ( !image._data ) { |
| 581 | clear(); |
| 582 | |
| 583 | return; |
| 584 | } |
| 585 | |
| 586 | const size_t imageSize = static_cast<size_t>( image._width ) * image._height; |
| 587 | |
| 588 | _singleLayer = image._singleLayer; |
| 589 | |
| 590 | if ( image._width != _width || image._height != _height ) { |
| 591 | if ( _singleLayer ) { |
| 592 | _data.reset( new uint8_t[imageSize] ); |
| 593 | } |
| 594 | else { |
| 595 | _data.reset( new uint8_t[imageSize * 2] ); |
| 596 | } |
| 597 | |
| 598 | _width = image._width; |
| 599 | _height = image._height; |
| 600 | } |
| 601 | |
| 602 | memcpy( _data.get(), image._data.get(), _singleLayer ? imageSize : imageSize * 2 ); |
| 603 | } |
| 604 | |
| 605 | Sprite::Sprite( Sprite && sprite ) noexcept |
| 606 | : Image( std::move( sprite ) ) |
| 607 | { |
| 608 | std::swap( _x, sprite._x ); |
| 609 | std::swap( _y, sprite._y ); |
| 610 | } |
| 611 | |
| 612 | Sprite & Sprite::operator=( const Sprite & sprite ) |
| 613 | { |
| 614 | if ( this == &sprite ) { |
| 615 | return *this; |
| 616 | } |
| 617 | |
| 618 | Image::operator=( sprite ); |
| 619 | |
| 620 | _x = sprite._x; |
| 621 | _y = sprite._y; |
| 622 | |
| 623 | return *this; |
| 624 | } |
| 625 | |
| 626 | Sprite & Sprite::operator=( Sprite && sprite ) noexcept |
| 627 | { |
| 628 | if ( this == &sprite ) { |
| 629 | return *this; |
| 630 | } |
| 631 | |
| 632 | std::swap( _x, sprite._x ); |
| 633 | std::swap( _y, sprite._y ); |
| 634 | |
| 635 | Image::operator=( std::move( sprite ) ); |
| 636 | |
| 637 | return *this; |
| 638 | } |
| 639 | |
| 640 | Sprite & Sprite::operator=( Image && image ) noexcept |
| 641 | { |
| 642 | Image::operator=( std::move( image ) ); |
| 643 | |
| 644 | _x = 0; |
| 645 | _y = 0; |
| 646 | |
| 647 | return *this; |
| 648 | } |
| 649 | |
| 650 | void Sprite::setPosition( const int32_t x_, const int32_t y_ ) |
| 651 | { |
| 652 | _x = x_; |
| 653 | _y = y_; |
| 654 | } |
| 655 | |
| 656 | ImageRestorer::ImageRestorer( Image & image ) |
| 657 | : _image( image ) |
| 658 | , _width( image.width() ) |
| 659 | , _height( image.height() ) |
| 660 | { |
| 661 | _updateRoi(); |
| 662 | |
| 663 | if ( _image.singleLayer() ) { |
| 664 | // The restorer is used to restore the image without the transform layer so we make a single-layer copy. |
| 665 | _copy._disableTransformLayer(); |
| 666 | } |
| 667 | |
| 668 | _copy.resize( _width, _height ); |
| 669 | |
| 670 | Copy( _image, 0, 0, _copy, 0, 0, _width, _height ); |
| 671 | } |
| 672 | |
| 673 | ImageRestorer::ImageRestorer( Image & image, const int32_t x_, const int32_t y_, const int32_t width, const int32_t height ) |
| 674 | : _image( image ) |
| 675 | , _x( x_ ) |
| 676 | , _y( y_ ) |
| 677 | , _width( width ) |
| 678 | , _height( height ) |
| 679 | { |
| 680 | _updateRoi(); |
| 681 | |
| 682 | if ( _image.singleLayer() ) { |
| 683 | // The restorer is used to restore the image without the transform layer so we make a single-layer copy. |
| 684 | _copy._disableTransformLayer(); |
| 685 | } |
| 686 | |
| 687 | _copy.resize( _width, _height ); |
| 688 | |
| 689 | Copy( _image, _x, _y, _copy, 0, 0, _width, _height ); |
| 690 | } |
| 691 | |
| 692 | void ImageRestorer::update( const int32_t x_, const int32_t y_, const int32_t width, const int32_t height ) |
| 693 | { |
| 694 | _isRestored = false; |
| 695 | _x = x_; |
| 696 | _y = y_; |
| 697 | _width = width; |
| 698 | _height = height; |
| 699 | _updateRoi(); |
| 700 | |
| 701 | _copy.resize( _width, _height ); |
| 702 | Copy( _image, _x, _y, _copy, 0, 0, _width, _height ); |
| 703 | } |
| 704 | |
| 705 | void ImageRestorer::restore() |
| 706 | { |
| 707 | _isRestored = true; |
| 708 | Copy( _copy, 0, 0, _image, _x, _y, _width, _height ); |
| 709 | } |
| 710 | |
| 711 | void ImageRestorer::_updateRoi() |
| 712 | { |
| 713 | if ( _width < 0 ) { |
| 714 | _width = 0; |
| 715 | } |
| 716 | |
| 717 | if ( _height < 0 ) { |
| 718 | _height = 0; |
| 719 | } |
| 720 | |
| 721 | if ( _x < 0 ) { |
| 722 | const int32_t offset = -_x; |
| 723 | _x = 0; |
| 724 | _width = _width < offset ? 0 : _width - offset; |
| 725 | } |
| 726 | |
| 727 | if ( _y < 0 ) { |
| 728 | const int32_t offset = -_y; |
| 729 | _y = 0; |
| 730 | _height = _height < offset ? 0 : _height - offset; |
| 731 | } |
| 732 | |
| 733 | if ( _x >= _image.width() || _y >= _image.height() ) { |
| 734 | _x = 0; |
| 735 | _y = 0; |
| 736 | _width = 0; |
| 737 | _height = 0; |
| 738 | return; |
| 739 | } |
| 740 | |
| 741 | if ( _x + _width > _image.width() ) { |
| 742 | const int32_t offsetX = _x + _width - _image.width(); |
| 743 | if ( offsetX >= _width ) { |
| 744 | _x = 0; |
| 745 | _y = 0; |
| 746 | _width = 0; |
| 747 | _height = 0; |
| 748 | return; |
| 749 | } |
| 750 | _width -= offsetX; |
| 751 | } |
| 752 | |
| 753 | if ( _y + _height > _image.height() ) { |
| 754 | const int32_t offsetY = _y + _height - _image.height(); |
| 755 | if ( offsetY >= _height ) { |
| 756 | _x = 0; |
| 757 | _y = 0; |
| 758 | _width = 0; |
| 759 | _height = 0; |
| 760 | return; |
| 761 | } |
| 762 | _height -= offsetY; |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | void addGradientShadow( const Sprite & in, Image & out, const Point & outPos, const Point & shadowOffset ) |
| 767 | { |
| 768 | if ( in.empty() || out.empty() || ( shadowOffset.x == 0 && shadowOffset.y == 0 ) || ( outPos.x < 0 ) || ( outPos.y < 0 ) ) { |
| 769 | return; |
| 770 | } |
| 771 | |
| 772 | const int32_t outWidth = out.width(); |
| 773 | int32_t inWidth = in.width(); |
| 774 | if ( outWidth < ( inWidth + outPos.x + std::max<int32_t>( shadowOffset.x, 0 ) ) ) { |
| 775 | inWidth = outWidth - ( outPos.x + std::max<int32_t>( shadowOffset.x, 0 ) ); |
| 776 | } |
| 777 | |
| 778 | int32_t inHeight = in.height(); |
| 779 | if ( out.height() < ( inHeight + outPos.y + std::max<int32_t>( shadowOffset.y, 0 ) ) ) { |
| 780 | inHeight = out.height() - ( outPos.y + std::max<int32_t>( shadowOffset.y, 0 ) ); |
| 781 | } |
| 782 | |
| 783 | if ( inWidth <= 1 || inHeight <= 1 ) { |
| 784 | return; |
| 785 | } |
| 786 | |
| 787 | const int32_t shadowOffsetX = std::min<int32_t>( shadowOffset.x, 0 ); |
| 788 | const int32_t shadowOffsetY = std::min<int32_t>( shadowOffset.y, 0 ); |
| 789 | const int32_t outStartOffset = outPos.x + shadowOffsetX + in.x() + ( outPos.y + shadowOffsetY + in.y() ) * outWidth; |
| 790 | |
| 791 | // The shadow should not be outside of 'out' image. |
| 792 | assert( outStartOffset >= 0 ); |
| 793 | |
| 794 | const int32_t absOffsetX = std::abs( shadowOffset.x ); |
| 795 | const int32_t absOffsetY = std::abs( shadowOffset.y ); |
| 796 | |
| 797 | std::vector<Point> shadowLine; |
| 798 | shadowLine.reserve( std::max( absOffsetX, absOffsetY ) + 1 ); |
| 799 | |
| 800 | // Calculate shadow line from the object. |
| 801 | if ( shadowOffset.x == 0 ) { |
| 802 | const int32_t maxY = absOffsetY + shadowOffsetY; |
| 803 | for ( int32_t y = shadowOffsetY; y <= maxY; ++y ) { |
| 804 | shadowLine.emplace_back( 0, y ); |
| 805 | } |
| 806 | } |
| 807 | else { |
| 808 | const double slopeFactor = static_cast<double>( shadowOffset.y ) / shadowOffset.x; |
| 809 | |
| 810 | if ( absOffsetX >= absOffsetY ) { |
| 811 | const int32_t maxX = absOffsetX + shadowOffsetX; |
| 812 | for ( int32_t x = shadowOffsetX; x <= maxX; ++x ) { |
| 813 | shadowLine.emplace_back( x, static_cast<int32_t>( std::round( x * slopeFactor ) ) ); |
| 814 | } |
| 815 | } |
| 816 | else { |
| 817 | const int32_t maxY = absOffsetY + shadowOffsetY; |
| 818 | for ( int32_t y = shadowOffsetY; y <= maxY; ++y ) { |
| 819 | shadowLine.emplace_back( static_cast<int32_t>( std::round( y / slopeFactor ) ), y ); |
| 820 | } |
| 821 | } |
| 822 | } |
| 823 | |
| 824 | const int32_t maxX = inWidth + absOffsetX; |
| 825 | const int32_t maxY = inHeight + absOffsetY; |
| 826 | |
| 827 | // If image is single-layer then pointer to its transform layer is 'nullptr' |
| 828 | const uint8_t * transformIn = in.singleLayer() ? nullptr : in.transform(); |
| 829 | uint8_t * transformOut = out.singleLayer() ? nullptr : ( out.transform() + outStartOffset ); |
| 830 | |
| 831 | uint8_t * imageOut = out.image() + outStartOffset; |
| 832 | |
| 833 | const auto isTransparent = [inWidth, inHeight, transformIn]( const int32_t offsetX, const int32_t offsetY ) { |
| 834 | if ( ( offsetX < 0 ) || ( offsetY < 0 ) || ( offsetX >= inWidth ) || ( offsetY >= inHeight ) ) { |
| 835 | // The space out of image boundaries is considered as transparent. |
| 836 | return true; |
| 837 | } |
| 838 | |
| 839 | if ( transformIn == nullptr ) { |
| 840 | // Single-layer images are non-transparent. |
| 841 | return false; |
| 842 | } |
| 843 | |
| 844 | // Image is transparent when its transform layer data is equal to 1. |
| 845 | return ( *( transformIn + offsetX + static_cast<ptrdiff_t>( offsetY ) * inWidth ) == 1 ); |
| 846 | }; |
| 847 | |
| 848 | for ( int32_t y = 0; y < maxY; ++y ) { |
| 849 | const int32_t offsetY = y + shadowOffsetY; |
| 850 | for ( int32_t x = 0; x < maxX; ++x ) { |
| 851 | const int32_t offsetX = x + shadowOffsetX; |
| 852 | |
| 853 | if ( !isTransparent( offsetX, offsetY ) ) { |
| 854 | // We add shadow only to visible parts of the background image. |
| 855 | continue; |
| 856 | } |
| 857 | |
| 858 | // There are 4 shadow tables: 2, 3, 4, 5. The strongest shadow is in table ID 2. |
| 859 | uint8_t transformTableId = 6; |
| 860 | for ( const Point & shadowLineOffset : shadowLine ) { |
| 861 | const int32_t shadowLineOffsetX = offsetX - shadowLineOffset.x; |
| 862 | const int32_t shadowLineOffsetY = offsetY - shadowLineOffset.y; |
| 863 | |
| 864 | if ( !isTransparent( shadowLineOffsetX, shadowLineOffsetY ) ) { |
| 865 | // Increase the strength of shadow by reducing the table ID. |
| 866 | --transformTableId; |
| 867 | |
| 868 | if ( transformTableId == 2 ) { |
| 869 | // We reached the strongest shadow table ID. |
| 870 | break; |
| 871 | } |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | if ( transformTableId == 6 ) { |
| 876 | continue; |
| 877 | } |
| 878 | |
| 879 | // The transformTableId is less than 6 so the shadow has to be applied. |
| 880 | const int32_t outOffset = x + y * outWidth; |
| 881 | |
| 882 | if ( transformOut != nullptr ) { |
| 883 | // 'out' is double-layer image because transformOut pointer is not `nullptr`. |
| 884 | |
| 885 | uint8_t * transformOutX = transformOut + outOffset; |
| 886 | |
| 887 | if ( *transformOutX == 0 ) { |
| 888 | // Apply shadow transform to the out image. |
| 889 | uint8_t * imageOutX = imageOut + outOffset; |
| 890 | *imageOutX = *( transformTable + transformTableId * ptrdiff_t{ 256 } + *imageOutX ); |
| 891 | } |
| 892 | else if ( *transformOutX > 1 && *transformOutX < 6 ) { |
| 893 | // Out image transform layer already has shadow data. We add the shadow strength by subtract the 'transformTableId', limited to 2. |
| 894 | *transformOutX = ( *transformOutX < 2 + transformTableId ) ? 2 : ( *transformOutX - transformTableId ); |
| 895 | } |
| 896 | else { |
| 897 | *transformOutX = transformTableId; |
| 898 | } |
| 899 | } |
| 900 | else { |
| 901 | // For single-layer 'out' image apply shadow transform to the image data. |
| 902 | uint8_t * imageOutX = imageOut + outOffset; |
| 903 | *imageOutX = *( transformTable + transformTableId * ptrdiff_t{ 256 } + *imageOutX ); |
| 904 | } |
| 905 | } |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | void addGradientShadowForArea( Image & out, const Point & outPos, const int32_t areaWidth, const int32_t areaHeight, const int32_t shadowOffset ) |
| 910 | { |
| 911 | if ( out.empty() || outPos.x < 0 || outPos.y < 0 || shadowOffset < 1 ) { |
| 912 | return; |
| 913 | } |
| 914 | |
| 915 | // Render shadow at the left side of the area |
| 916 | int32_t offsetY = outPos.y + shadowOffset; |
| 917 | ApplyTransform( out, outPos.x - shadowOffset, offsetY, shadowOffset, 1, 5 ); |
| 918 | ++offsetY; |
| 919 | ApplyTransform( out, outPos.x - shadowOffset, offsetY, 1, areaHeight, 5 ); |
| 920 | ApplyTransform( out, outPos.x - shadowOffset + 1, offsetY, shadowOffset - 1, 1, 4 ); |
| 921 | ++offsetY; |
| 922 | ApplyTransform( out, outPos.x - shadowOffset + 1, offsetY, 1, areaHeight - 4, 4 ); |
| 923 | ApplyTransform( out, outPos.x - shadowOffset + 2, offsetY, shadowOffset - 2, 1, 3 ); |
| 924 | ++offsetY; |
| 925 | ApplyTransform( out, outPos.x - shadowOffset + 2, offsetY, 1, areaHeight - 6, 3 ); |
| 926 | ApplyTransform( out, outPos.x - shadowOffset + 3, offsetY, shadowOffset - 3, areaHeight - shadowOffset - 3, 2 ); |
| 927 | |
| 928 | // Render shadow at the bottom side of the area |
| 929 | offsetY = outPos.y + areaHeight; |
| 930 | const int32_t shadowBottomEdge = outPos.y + areaHeight + shadowOffset; |
| 931 | ApplyTransform( out, outPos.x - shadowOffset + 3, offsetY, areaWidth - 6, shadowOffset - 3, 2 ); |
| 932 | ApplyTransform( out, outPos.x - shadowOffset + 2, shadowBottomEdge - 3, areaWidth - 4, 1, 3 ); |
| 933 | ApplyTransform( out, outPos.x - shadowOffset + areaWidth - 3, offsetY, 1, shadowOffset - 3, 3 ); |
| 934 | ApplyTransform( out, outPos.x - shadowOffset + 1, shadowBottomEdge - 2, areaWidth - 2, 1, 4 ); |
| 935 | ApplyTransform( out, outPos.x - shadowOffset + areaWidth - 2, offsetY, 1, shadowOffset - 2, 4 ); |
| 936 | ApplyTransform( out, outPos.x - shadowOffset, shadowBottomEdge - 1, areaWidth, 1, 5 ); |
| 937 | ApplyTransform( out, outPos.x - shadowOffset + areaWidth - 1, offsetY, 1, shadowOffset, 5 ); |
| 938 | } |
| 939 | |
| 940 | Sprite addShadow( const Sprite & in, const Point & shadowOffset, const uint8_t transformId ) |
| 941 | { |
| 942 | if ( in.empty() || shadowOffset.x > 0 || shadowOffset.y < 0 ) { |
| 943 | return in; |
| 944 | } |
| 945 | |
| 946 | Sprite out = makeShadow( in, shadowOffset, transformId ); |
| 947 | Blit( in, out, -shadowOffset.x, 0 ); |
| 948 | |
| 949 | return out; |
| 950 | } |
| 951 | |
| 952 | void AlphaBlit( const Image & in, Image & out, const uint8_t alphaValue, const bool flip /* = false */ ) |
| 953 | { |
| 954 | AlphaBlit( in, 0, 0, out, 0, 0, in.width(), in.height(), alphaValue, flip ); |
| 955 | } |
| 956 | |
| 957 | void AlphaBlit( const Image & in, Image & out, int32_t outX, int32_t outY, const uint8_t alphaValue, const bool flip /* = false */ ) |
| 958 | { |
| 959 | AlphaBlit( in, 0, 0, out, outX, outY, in.width(), in.height(), alphaValue, flip ); |
| 960 | } |
| 961 | |
| 962 | void AlphaBlit( const Image & in, int32_t inX, int32_t inY, Image & out, int32_t outX, int32_t outY, int32_t width, int32_t height, const uint8_t alphaValue, |
| 963 | const bool flip /* = false */ ) |
| 964 | { |
| 965 | if ( alphaValue == 0 ) { |
| 966 | // There is nothing we need to do. |
| 967 | return; |
| 968 | } |
| 969 | |
| 970 | if ( alphaValue == 255 ) { |
| 971 | Blit( in, inX, inY, out, outX, outY, width, height, flip ); |
| 972 | return; |
| 973 | } |
| 974 | |
| 975 | if ( !Verify( in, inX, inY, out, outX, outY, width, height ) ) { |
| 976 | return; |
| 977 | } |
| 978 | |
| 979 | const int32_t widthIn = in.width(); |
| 980 | const int32_t widthOut = out.width(); |
| 981 | |
| 982 | const uint8_t behindValue = 255 - alphaValue; |
| 983 | |
| 984 | const uint8_t * gamePalette = getGamePalette(); |
| 985 | |
| 986 | if ( flip ) { |
| 987 | const int32_t offsetInY = inY * widthIn + widthIn - 1 - inX; |
| 988 | const uint8_t * imageInY = in.image() + offsetInY; |
| 989 | |
| 990 | const int32_t offsetOutY = outY * widthOut + outX; |
| 991 | uint8_t * imageOutY = out.image() + offsetOutY; |
| 992 | const uint8_t * imageOutYEnd = imageOutY + static_cast<ptrdiff_t>( height ) * widthOut; |
| 993 | |
| 994 | if ( in.singleLayer() ) { |
| 995 | for ( ; imageOutY != imageOutYEnd; imageInY += widthIn, imageOutY += widthOut ) { |
| 996 | const uint8_t * imageInX = imageInY; |
| 997 | uint8_t * imageOutX = imageOutY; |
| 998 | const uint8_t * imageOutXEnd = imageOutX + width; |
| 999 | |
| 1000 | for ( ; imageOutX != imageOutXEnd; --imageInX, ++imageOutX ) { |
| 1001 | const uint8_t * inPAL = gamePalette + static_cast<ptrdiff_t>( *imageInX ) * 3; |
| 1002 | const uint8_t * outPAL = gamePalette + static_cast<ptrdiff_t>( *imageOutX ) * 3; |
| 1003 | |
| 1004 | const uint32_t red = static_cast<uint32_t>( *inPAL ) * alphaValue + static_cast<uint32_t>( *outPAL ) * behindValue; |
| 1005 | const uint32_t green = static_cast<uint32_t>( *( inPAL + 1 ) ) * alphaValue + static_cast<uint32_t>( *( outPAL + 1 ) ) * behindValue; |
| 1006 | const uint32_t blue = static_cast<uint32_t>( *( inPAL + 2 ) ) * alphaValue + static_cast<uint32_t>( *( outPAL + 2 ) ) * behindValue; |
| 1007 | *imageOutX = GetPALColorId( static_cast<uint8_t>( red / 255 ), static_cast<uint8_t>( green / 255 ), static_cast<uint8_t>( blue / 255 ) ); |
| 1008 | } |
| 1009 | } |
| 1010 | } |
| 1011 | else { |
| 1012 | const uint8_t * transformInY = in.transform() + offsetInY; |
| 1013 | |
| 1014 | for ( ; imageOutY != imageOutYEnd; imageInY += widthIn, transformInY += widthIn, imageOutY += widthOut ) { |
| 1015 | const uint8_t * imageInX = imageInY; |
| 1016 | const uint8_t * transformInX = transformInY; |
| 1017 | uint8_t * imageOutX = imageOutY; |
| 1018 | const uint8_t * imageOutXEnd = imageOutX + width; |
| 1019 | |
| 1020 | for ( ; imageOutX != imageOutXEnd; --imageInX, --transformInX, ++imageOutX ) { |
| 1021 | if ( *transformInX == 1 ) { // skip pixel |
| 1022 | continue; |
| 1023 | } |
| 1024 | |
| 1025 | uint8_t inValue = *imageInX; |
| 1026 | if ( *transformInX > 1 ) { |
| 1027 | inValue = *( transformTable + static_cast<ptrdiff_t>( *transformInX ) * 256 + *imageOutX ); |
| 1028 | } |
| 1029 | |
| 1030 | const uint8_t * inPAL = gamePalette + static_cast<ptrdiff_t>( inValue ) * 3; |
| 1031 | const uint8_t * outPAL = gamePalette + static_cast<ptrdiff_t>( *imageOutX ) * 3; |
| 1032 | |
| 1033 | const uint32_t red = static_cast<uint32_t>( *inPAL ) * alphaValue + static_cast<uint32_t>( *outPAL ) * behindValue; |
| 1034 | const uint32_t green = static_cast<uint32_t>( *( inPAL + 1 ) ) * alphaValue + static_cast<uint32_t>( *( outPAL + 1 ) ) * behindValue; |
| 1035 | const uint32_t blue = static_cast<uint32_t>( *( inPAL + 2 ) ) * alphaValue + static_cast<uint32_t>( *( outPAL + 2 ) ) * behindValue; |
| 1036 | *imageOutX = GetPALColorId( static_cast<uint8_t>( red / 255 ), static_cast<uint8_t>( green / 255 ), static_cast<uint8_t>( blue / 255 ) ); |
| 1037 | } |
| 1038 | } |
| 1039 | } |
| 1040 | } |
| 1041 | else { |
| 1042 | const int32_t offsetInY = inY * widthIn + inX; |
| 1043 | const uint8_t * imageInY = in.image() + offsetInY; |
| 1044 | |
| 1045 | uint8_t * imageOutY = out.image() + static_cast<ptrdiff_t>( outY ) * widthOut + outX; |
| 1046 | const uint8_t * imageInYEnd = imageInY + static_cast<ptrdiff_t>( height ) * widthIn; |
| 1047 | |
| 1048 | if ( in.singleLayer() ) { |
| 1049 | for ( ; imageInY != imageInYEnd; imageInY += widthIn, imageOutY += widthOut ) { |
| 1050 | const uint8_t * imageInX = imageInY; |
| 1051 | uint8_t * imageOutX = imageOutY; |
| 1052 | const uint8_t * imageInXEnd = imageInX + width; |
| 1053 | |
| 1054 | for ( ; imageInX != imageInXEnd; ++imageInX, ++imageOutX ) { |
| 1055 | const uint8_t * inPAL = gamePalette + static_cast<ptrdiff_t>( *imageInX ) * 3; |
| 1056 | const uint8_t * outPAL = gamePalette + static_cast<ptrdiff_t>( *imageOutX ) * 3; |
| 1057 | |
| 1058 | const uint32_t red = static_cast<uint32_t>( *inPAL ) * alphaValue + static_cast<uint32_t>( *outPAL ) * behindValue; |
| 1059 | const uint32_t green = static_cast<uint32_t>( *( inPAL + 1 ) ) * alphaValue + static_cast<uint32_t>( *( outPAL + 1 ) ) * behindValue; |
| 1060 | const uint32_t blue = static_cast<uint32_t>( *( inPAL + 2 ) ) * alphaValue + static_cast<uint32_t>( *( outPAL + 2 ) ) * behindValue; |
| 1061 | *imageOutX = GetPALColorId( static_cast<uint8_t>( red / 255 ), static_cast<uint8_t>( green / 255 ), static_cast<uint8_t>( blue / 255 ) ); |
| 1062 | } |
| 1063 | } |
| 1064 | } |
| 1065 | else { |
| 1066 | const uint8_t * transformInY = in.transform() + offsetInY; |
| 1067 | |
| 1068 | for ( ; imageInY != imageInYEnd; imageInY += widthIn, transformInY += widthIn, imageOutY += widthOut ) { |
| 1069 | const uint8_t * imageInX = imageInY; |
| 1070 | const uint8_t * transformInX = transformInY; |
| 1071 | uint8_t * imageOutX = imageOutY; |
| 1072 | const uint8_t * imageInXEnd = imageInX + width; |
| 1073 | |
| 1074 | for ( ; imageInX != imageInXEnd; ++imageInX, ++transformInX, ++imageOutX ) { |
| 1075 | if ( *transformInX == 1 ) { // skip pixel |
| 1076 | continue; |
| 1077 | } |
| 1078 | |
| 1079 | uint8_t inValue = *imageInX; |
| 1080 | if ( *transformInX > 1 ) { |
| 1081 | inValue = *( transformTable + static_cast<ptrdiff_t>( *transformInX ) * 256 + *imageOutX ); |
| 1082 | } |
| 1083 | |
| 1084 | const uint8_t * inPAL = gamePalette + static_cast<ptrdiff_t>( inValue ) * 3; |
| 1085 | const uint8_t * outPAL = gamePalette + static_cast<ptrdiff_t>( *imageOutX ) * 3; |
| 1086 | |
| 1087 | const uint32_t red = static_cast<uint32_t>( *inPAL ) * alphaValue + static_cast<uint32_t>( *outPAL ) * behindValue; |
| 1088 | const uint32_t green = static_cast<uint32_t>( *( inPAL + 1 ) ) * alphaValue + static_cast<uint32_t>( *( outPAL + 1 ) ) * behindValue; |
| 1089 | const uint32_t blue = static_cast<uint32_t>( *( inPAL + 2 ) ) * alphaValue + static_cast<uint32_t>( *( outPAL + 2 ) ) * behindValue; |
| 1090 | *imageOutX = GetPALColorId( static_cast<uint8_t>( red / 255 ), static_cast<uint8_t>( green / 255 ), static_cast<uint8_t>( blue / 255 ) ); |
| 1091 | } |
| 1092 | } |
| 1093 | } |
| 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | void ApplyPalette( Image & image, const std::vector<uint8_t> & palette ) |
| 1098 | { |
| 1099 | ApplyPalette( image, image, palette ); |
| 1100 | } |
| 1101 | |
| 1102 | void ApplyPalette( const Image & in, Image & out, const std::vector<uint8_t> & palette ) |
| 1103 | { |
| 1104 | if ( palette.size() != 256 ) { |
| 1105 | return; |
| 1106 | } |
| 1107 | |
| 1108 | ApplyRawPalette( in, 0, 0, out, 0, 0, in.width(), in.height(), palette.data() ); |
| 1109 | } |
| 1110 | |
| 1111 | void ApplyPalette( Image & image, const uint8_t paletteId ) |
| 1112 | { |
| 1113 | ApplyPalette( image, image, paletteId ); |
| 1114 | } |
| 1115 | |
| 1116 | void ApplyPalette( const Image & in, Image & out, const uint8_t paletteId ) |
| 1117 | { |
| 1118 | if ( paletteId > 15 ) { |
| 1119 | return; |
| 1120 | } |
| 1121 | |
| 1122 | ApplyRawPalette( in, 0, 0, out, 0, 0, in.width(), in.height(), transformTable + paletteId * 256 ); |
| 1123 | } |
| 1124 | |
| 1125 | void ApplyPalette( const Image & in, int32_t inX, int32_t inY, Image & out, int32_t outX, int32_t outY, int32_t width, int32_t height, uint8_t paletteId ) |
| 1126 | { |
| 1127 | if ( paletteId > 15 ) { |
| 1128 | return; |
| 1129 | } |
| 1130 | |
| 1131 | ApplyRawPalette( in, inX, inY, out, outX, outY, width, height, transformTable + paletteId * 256 ); |
| 1132 | } |
| 1133 | |
| 1134 | void ApplyPalette( const Image & in, int32_t inX, int32_t inY, Image & out, int32_t outX, int32_t outY, int32_t width, int32_t height, |
| 1135 | const std::vector<uint8_t> & palette ) |
| 1136 | { |
| 1137 | if ( palette.size() != 256 ) { |
| 1138 | return; |
| 1139 | } |
| 1140 | |
| 1141 | ApplyRawPalette( in, inX, inY, out, outX, outY, width, height, palette.data() ); |
| 1142 | } |
| 1143 | |
| 1144 | void ApplyAlpha( const Image & in, int32_t inX, int32_t inY, Image & out, int32_t outX, int32_t outY, int32_t width, int32_t height, const uint8_t alpha ) |
| 1145 | { |
| 1146 | std::vector<uint8_t> palette( 256 ); |
| 1147 | |
| 1148 | const uint8_t * value = getGamePalette(); |
| 1149 | |
| 1150 | for ( uint32_t i = 0; i < 256; ++i ) { |
| 1151 | const uint32_t red = static_cast<uint32_t>( *value ) * alpha / 255; |
| 1152 | ++value; |
| 1153 | const uint32_t green = static_cast<uint32_t>( *value ) * alpha / 255; |
| 1154 | ++value; |
| 1155 | const uint32_t blue = static_cast<uint32_t>( *value ) * alpha / 255; |
| 1156 | ++value; |
| 1157 | palette[i] = GetPALColorId( static_cast<uint8_t>( red ), static_cast<uint8_t>( green ), static_cast<uint8_t>( blue ) ); |
| 1158 | } |
| 1159 | |
| 1160 | ApplyPalette( in, inX, inY, out, outX, outY, width, height, palette ); |
| 1161 | } |
| 1162 | |
| 1163 | void ApplyTransform( Image & image, int32_t x, int32_t y, int32_t width, int32_t height, const uint8_t transformId ) |
| 1164 | { |
| 1165 | if ( !Verify( image, x, y, width, height ) ) { |
| 1166 | return; |
| 1167 | } |
| 1168 | |
| 1169 | const int32_t imageWidth = image.width(); |
| 1170 | |
| 1171 | uint8_t * imageY = image.image() + y * imageWidth + x; |
| 1172 | const uint8_t * imageYEnd = imageY + height * imageWidth; |
| 1173 | |
| 1174 | const uint32_t transformOffset = transformId * 256; |
| 1175 | |
| 1176 | if ( image.singleLayer() ) { |
| 1177 | for ( ; imageY != imageYEnd; imageY += imageWidth ) { |
| 1178 | uint8_t * imageX = imageY; |
| 1179 | const uint8_t * imageXEnd = imageX + width; |
| 1180 | |
| 1181 | for ( ; imageX != imageXEnd; ++imageX ) { |
| 1182 | *imageX = *( transformTable + transformOffset + *imageX ); |
| 1183 | } |
| 1184 | } |
| 1185 | } |
| 1186 | else { |
| 1187 | const uint8_t * transformY = image.transform() + y * imageWidth + x; |
| 1188 | |
| 1189 | for ( ; imageY != imageYEnd; imageY += imageWidth, transformY += imageWidth ) { |
| 1190 | uint8_t * imageX = imageY; |
| 1191 | const uint8_t * transformX = transformY; |
| 1192 | const uint8_t * imageXEnd = imageX + width; |
| 1193 | |
| 1194 | for ( ; imageX != imageXEnd; ++imageX, ++transformX ) { |
| 1195 | if ( *transformX == 0 ) { |
| 1196 | *imageX = *( transformTable + transformOffset + *imageX ); |
| 1197 | } |
| 1198 | } |
| 1199 | } |
| 1200 | } |
| 1201 | } |
| 1202 | |
| 1203 | void Blit( const Image & in, Image & out, const bool flip /* = false */ ) |
| 1204 | { |
| 1205 | Blit( in, 0, 0, out, 0, 0, in.width(), in.height(), flip ); |
| 1206 | } |
| 1207 | |
| 1208 | void Blit( const Image & in, Image & out, const Rect & outRoi, const bool flip /* = false */ ) |
| 1209 | { |
| 1210 | Blit( in, 0, 0, out, outRoi.x, outRoi.y, outRoi.width, outRoi.height, flip ); |
| 1211 | } |
| 1212 | |
| 1213 | void Blit( const Image & in, Image & out, const int32_t outX, const int32_t outY, const bool flip /* = false */ ) |
| 1214 | { |
| 1215 | Blit( in, 0, 0, out, outX, outY, in.width(), in.height(), flip ); |
| 1216 | } |
| 1217 | |
| 1218 | void Blit( const Image & in, const Point & inPos, Image & out, const Point & outPos, const Size & size, const bool flip /* = false */ ) |
| 1219 | { |
| 1220 | Blit( in, inPos.x, inPos.y, out, outPos.x, outPos.y, size.width, size.height, flip ); |
| 1221 | } |
| 1222 | |
| 1223 | void Blit( const Image & in, int32_t inX, int32_t inY, Image & out, int32_t outX, int32_t outY, int32_t width, int32_t height, const bool flip /* = false */ ) |
| 1224 | { |
| 1225 | if ( in.singleLayer() && !flip ) { |
| 1226 | Copy( in, inX, inY, out, outX, outY, width, height ); |
| 1227 | return; |
| 1228 | } |
| 1229 | |
| 1230 | if ( !Verify( in, inX, inY, out, outX, outY, width, height ) ) { |
| 1231 | return; |
| 1232 | } |
| 1233 | |
| 1234 | const int32_t widthIn = in.width(); |
| 1235 | const int32_t widthOut = out.width(); |
| 1236 | |
| 1237 | if ( flip ) { |
| 1238 | const int32_t offsetInY = inY * widthIn + widthIn - 1 - inX; |
| 1239 | const uint8_t * imageInY = in.image() + offsetInY; |
| 1240 | const uint8_t * transformInY = in.transform() + offsetInY; |
| 1241 | |
| 1242 | const int32_t offsetOutY = outY * widthOut + outX; |
| 1243 | uint8_t * imageOutY = out.image() + offsetOutY; |
| 1244 | const uint8_t * imageOutYEnd = imageOutY + height * widthOut; |
| 1245 | |
| 1246 | if ( out.singleLayer() ) { |
| 1247 | assert( !in.singleLayer() ); |
| 1248 | for ( ; imageOutY != imageOutYEnd; imageInY += widthIn, transformInY += widthIn, imageOutY += widthOut ) { |
| 1249 | const uint8_t * imageInX = imageInY; |
| 1250 | const uint8_t * transformInX = transformInY; |
| 1251 | uint8_t * imageOutX = imageOutY; |
| 1252 | const uint8_t * imageOutXEnd = imageOutX + width; |
| 1253 | |
| 1254 | for ( ; imageOutX != imageOutXEnd; --imageInX, --transformInX, ++imageOutX ) { |
| 1255 | if ( *transformInX > 0 ) { // apply a transformation |
| 1256 | if ( *transformInX != 1 ) { // skip pixel |
| 1257 | *imageOutX = *( transformTable + ( *transformInX ) * 256 + *imageOutX ); |
| 1258 | } |
| 1259 | } |
| 1260 | else { // copy a pixel |
| 1261 | *imageOutX = *imageInX; |
| 1262 | } |
| 1263 | } |
| 1264 | } |
| 1265 | } |
| 1266 | else { |
| 1267 | uint8_t * transformOutY = out.transform() + offsetOutY; |
| 1268 | |
| 1269 | for ( ; imageOutY != imageOutYEnd; imageInY += widthIn, transformInY += widthIn, imageOutY += widthOut, transformOutY += widthOut ) { |
| 1270 | const uint8_t * imageInX = imageInY; |
| 1271 | const uint8_t * transformInX = transformInY; |
| 1272 | uint8_t * imageOutX = imageOutY; |
| 1273 | uint8_t * transformOutX = transformOutY; |
| 1274 | const uint8_t * imageOutXEnd = imageOutX + width; |
| 1275 | |
| 1276 | for ( ; imageOutX != imageOutXEnd; --imageInX, --transformInX, ++imageOutX, ++transformOutX ) { |
| 1277 | if ( *transformInX == 1 ) { // skip pixel |
| 1278 | continue; |
| 1279 | } |
| 1280 | |
| 1281 | if ( *transformInX > 0 && *transformOutX == 0 ) { // apply a transformation |
| 1282 | *imageOutX = *( transformTable + ( *transformInX ) * 256 + *imageOutX ); |
| 1283 | } |
| 1284 | else { // copy a pixel |
| 1285 | *transformOutX = *transformInX; |
| 1286 | *imageOutX = *imageInX; |
| 1287 | } |
| 1288 | } |
| 1289 | } |
| 1290 | } |
| 1291 | } |
| 1292 | else { |
| 1293 | const int32_t offsetInY = inY * widthIn + inX; |
| 1294 | const uint8_t * imageInY = in.image() + offsetInY; |
| 1295 | const uint8_t * transformInY = in.transform() + offsetInY; |
| 1296 | |
| 1297 | const int32_t offsetOutY = outY * widthOut + outX; |
| 1298 | uint8_t * imageOutY = out.image() + offsetOutY; |
| 1299 | const uint8_t * imageInYEnd = imageInY + height * widthIn; |
| 1300 | |
| 1301 | if ( out.singleLayer() ) { |
| 1302 | assert( !in.singleLayer() ); |
| 1303 | for ( ; imageInY != imageInYEnd; imageInY += widthIn, transformInY += widthIn, imageOutY += widthOut ) { |
| 1304 | const uint8_t * imageInX = imageInY; |
| 1305 | const uint8_t * transformInX = transformInY; |
| 1306 | uint8_t * imageOutX = imageOutY; |
| 1307 | const uint8_t * imageInXEnd = imageInX + width; |
| 1308 | |
| 1309 | for ( ; imageInX != imageInXEnd; ++imageInX, ++transformInX, ++imageOutX ) { |
| 1310 | if ( *transformInX > 0 ) { // apply a transformation |
| 1311 | if ( *transformInX != 1 ) { // skip pixel |
| 1312 | *imageOutX = *( transformTable + ( *transformInX ) * 256 + *imageOutX ); |
| 1313 | } |
| 1314 | } |
| 1315 | else { // copy a pixel |
| 1316 | *imageOutX = *imageInX; |
| 1317 | } |
| 1318 | } |
| 1319 | } |
| 1320 | } |
| 1321 | else { |
| 1322 | uint8_t * transformOutY = out.transform() + offsetOutY; |
| 1323 | |
| 1324 | for ( ; imageInY != imageInYEnd; imageInY += widthIn, transformInY += widthIn, imageOutY += widthOut, transformOutY += widthOut ) { |
| 1325 | const uint8_t * imageInX = imageInY; |
| 1326 | const uint8_t * transformInX = transformInY; |
| 1327 | uint8_t * imageOutX = imageOutY; |
| 1328 | uint8_t * transformOutX = transformOutY; |
| 1329 | const uint8_t * imageInXEnd = imageInX + width; |
| 1330 | |
| 1331 | for ( ; imageInX != imageInXEnd; ++imageInX, ++transformInX, ++imageOutX, ++transformOutX ) { |
| 1332 | if ( *transformInX == 1 ) { // skip pixel |
| 1333 | continue; |
| 1334 | } |
| 1335 | |
| 1336 | if ( *transformInX > 0 && *transformOutX == 0 ) { // apply a transformation |
| 1337 | *imageOutX = *( transformTable + ( *transformInX ) * 256 + *imageOutX ); |
| 1338 | } |
| 1339 | else { // copy a pixel |
| 1340 | *transformOutX = *transformInX; |
| 1341 | *imageOutX = *imageInX; |
| 1342 | } |
| 1343 | } |
| 1344 | } |
| 1345 | } |
| 1346 | } |
| 1347 | } |
| 1348 | |
| 1349 | void Copy( const Image & in, Image & out ) |
| 1350 | { |
| 1351 | if ( !out.singleLayer() && !in.singleLayer() ) { |
| 1352 | // Both images have transform layer. Copy using the assignment operator. |
| 1353 | out = in; |
| 1354 | return; |
| 1355 | } |
| 1356 | |
| 1357 | const int32_t width = in.width(); |
| 1358 | const int32_t height = in.height(); |
| 1359 | |
| 1360 | out.resize( width, height ); |
| 1361 | |
| 1362 | // We do a full copy of an image. |
| 1363 | const size_t size = static_cast<size_t>( width ) * height; |
| 1364 | if ( out.singleLayer() ) { |
| 1365 | // Copy only image layer. Input image can be single- or double-layer. |
| 1366 | memcpy( out.image(), in.image(), size ); |
| 1367 | } |
| 1368 | else { |
| 1369 | assert( in.singleLayer() ); |
| 1370 | // Copy image layer and set transform to non-transparent mode. |
| 1371 | memcpy( out.image(), in.image(), size ); |
| 1372 | memset( out.transform(), static_cast<uint8_t>( 0 ), size ); |
| 1373 | } |
| 1374 | } |
| 1375 | |
| 1376 | void Copy( const Image & in, const int32_t inX, const int32_t inY, Image & out, const Rect & outRoi ) |
| 1377 | { |
| 1378 | Copy( in, inX, inY, out, outRoi.x, outRoi.y, outRoi.width, outRoi.height ); |
| 1379 | } |
| 1380 | |
| 1381 | void Copy( const Image & in, int32_t inX, int32_t inY, Image & out, int32_t outX, int32_t outY, int32_t width, int32_t height ) |
| 1382 | { |
| 1383 | if ( !Verify( in, inX, inY, out, outX, outY, width, height ) ) { |
| 1384 | return; |
| 1385 | } |
| 1386 | |
| 1387 | const int32_t widthIn = in.width(); |
| 1388 | const int32_t widthOut = out.width(); |
| 1389 | |
| 1390 | if ( inX == 0 && inY == 0 && outX == 0 && outY == 0 && width == widthIn && width == widthOut && height == in.height() && height == out.height() ) { |
| 1391 | // Both images have identical width and height and a full copy is requested. |
| 1392 | Copy( in, out ); |
| 1393 | return; |
| 1394 | } |
| 1395 | |
| 1396 | const int32_t offsetInY = inY * widthIn + inX; |
| 1397 | const uint8_t * imageInY = in.image() + offsetInY; |
| 1398 | |
| 1399 | const int32_t offsetOutY = outY * widthOut + outX; |
| 1400 | uint8_t * imageOutY = out.image() + offsetOutY; |
| 1401 | const uint8_t * imageOutYEnd = imageOutY + height * widthOut; |
| 1402 | |
| 1403 | if ( out.singleLayer() ) { |
| 1404 | for ( ; imageOutY != imageOutYEnd; imageInY += widthIn, imageOutY += widthOut ) { |
| 1405 | memcpy( imageOutY, imageInY, static_cast<size_t>( width ) ); |
| 1406 | } |
| 1407 | } |
| 1408 | else if ( in.singleLayer() ) { |
| 1409 | uint8_t * transformOutY = out.transform() + offsetOutY; |
| 1410 | |
| 1411 | for ( ; imageOutY != imageOutYEnd; imageInY += widthIn, imageOutY += widthOut, transformOutY += widthOut ) { |
| 1412 | memcpy( imageOutY, imageInY, static_cast<size_t>( width ) ); |
| 1413 | memset( transformOutY, static_cast<uint8_t>( 0 ), width ); |
| 1414 | } |
| 1415 | } |
| 1416 | else { |
| 1417 | const uint8_t * transformInY = in.transform() + offsetInY; |
| 1418 | uint8_t * transformOutY = out.transform() + offsetOutY; |
| 1419 | |
| 1420 | for ( ; imageOutY != imageOutYEnd; imageInY += widthIn, transformInY += widthIn, imageOutY += widthOut, transformOutY += widthOut ) { |
| 1421 | memcpy( imageOutY, imageInY, static_cast<size_t>( width ) ); |
| 1422 | memcpy( transformOutY, transformInY, static_cast<size_t>( width ) ); |
| 1423 | } |
| 1424 | } |
| 1425 | } |
| 1426 | |
| 1427 | void copyTransformLayer( const Image & in, Image & out ) |
| 1428 | { |
| 1429 | if ( in.empty() || out.empty() || in.singleLayer() || in.width() != out.width() || in.height() != out.height() ) { |
| 1430 | assert( 0 ); |
| 1431 | return; |
| 1432 | } |
| 1433 | |
| 1434 | if ( out.singleLayer() ) { |
| 1435 | // Add the transform layer. |
| 1436 | Image temp; |
| 1437 | Copy( out, temp ); |
| 1438 | out = std::move( temp ); |
| 1439 | } |
| 1440 | |
| 1441 | memcpy( out.transform(), in.transform(), in.width() * in.height() ); |
| 1442 | } |
| 1443 | |
| 1444 | void copyTransformLayer( const Image & in, int32_t inX, int32_t inY, Image & out, int32_t outX, int32_t outY, int32_t width, int32_t height ) |
| 1445 | { |
| 1446 | if ( in.empty() || out.empty() || in.singleLayer() ) { |
| 1447 | assert( 0 ); |
| 1448 | return; |
| 1449 | } |
| 1450 | |
| 1451 | if ( !Verify( in, inX, inY, out, outX, outY, width, height ) ) { |
| 1452 | return; |
| 1453 | } |
| 1454 | |
| 1455 | if ( out.singleLayer() ) { |
| 1456 | // Add the transform layer. |
| 1457 | Image temp; |
| 1458 | Copy( out, temp ); |
| 1459 | out = std::move( temp ); |
| 1460 | } |
| 1461 | |
| 1462 | const int32_t widthIn = in.width(); |
| 1463 | const int32_t widthOut = out.width(); |
| 1464 | |
| 1465 | if ( inX == 0 && inY == 0 && outX == 0 && outY == 0 && width == widthIn && width == widthOut && height == in.height() && height == out.height() ) { |
| 1466 | // Both images have identical width and height and a full copy is requested. |
| 1467 | copyTransformLayer( in, out ); |
| 1468 | return; |
| 1469 | } |
| 1470 | |
| 1471 | const int32_t offsetInY = inY * widthIn + inX; |
| 1472 | const int32_t offsetOutY = outY * widthOut + outX; |
| 1473 | |
| 1474 | const uint8_t * transformInY = in.transform() + offsetInY; |
| 1475 | uint8_t * transformOutY = out.transform() + offsetOutY; |
| 1476 | const uint8_t * transformInYEnd = transformInY + static_cast<ptrdiff_t>( height ) * widthIn; |
| 1477 | |
| 1478 | for ( ; transformInY != transformInYEnd; transformInY += widthIn, transformOutY += widthOut ) { |
| 1479 | memcpy( transformOutY, transformInY, static_cast<size_t>( width ) ); |
| 1480 | } |
| 1481 | } |
| 1482 | |
| 1483 | Sprite CreateContour( const Image & image, const uint8_t value ) |
| 1484 | { |
| 1485 | if ( image.empty() || image.singleLayer() ) { |
| 1486 | // A contour can be created only for non-empty images with the transform layer. |
| 1487 | assert( 0 ); |
| 1488 | return {}; |
| 1489 | } |
| 1490 | |
| 1491 | const int32_t width = image.width(); |
| 1492 | const int32_t height = image.height(); |
| 1493 | |
| 1494 | Sprite contour( width, height ); |
| 1495 | contour.reset(); |
| 1496 | if ( width < 2 || height < 2 ) { |
| 1497 | return contour; |
| 1498 | } |
| 1499 | |
| 1500 | // This assertion is needed to convince SonarQube that we will not dereference the null pointer to 'contour' image data. |
| 1501 | assert( !contour.empty() ); |
| 1502 | |
| 1503 | const uint8_t * inY = image.transform(); |
| 1504 | uint8_t * outImageY = contour.image(); |
| 1505 | uint8_t * outTransformY = contour.transform(); |
| 1506 | |
| 1507 | const int32_t reducedWidth = width - 1; |
| 1508 | const int32_t reducedHeight = height - 1; |
| 1509 | |
| 1510 | for ( int32_t y = 0; y < height; ++y, inY += width, outImageY += width, outTransformY += width ) { |
| 1511 | const uint8_t * inX = inY; |
| 1512 | |
| 1513 | const bool isNotTopRow = ( y > 0 ); |
| 1514 | const bool isNotBottomRow = ( y < reducedHeight ); |
| 1515 | |
| 1516 | for ( int32_t x = 0; x < width; ++x, ++inX ) { |
| 1517 | // Draw contour only on transparent pixel ( 1 ) or shadow ( 2 - 5 ) |
| 1518 | if ( *inX > 0 && *inX < 6 |
| 1519 | && ( ( x > 0 && *( inX - 1 ) == 0 ) || ( x < reducedWidth && *( inX + 1 ) == 0 ) || ( isNotTopRow && *( inX - width ) == 0 ) |
| 1520 | || ( isNotBottomRow && *( inX + width ) == 0 ) ) ) { |
| 1521 | outImageY[x] = value; |
| 1522 | outTransformY[x] = 0; |
| 1523 | } |
| 1524 | } |
| 1525 | } |
| 1526 | |
| 1527 | return contour; |
| 1528 | } |
| 1529 | |
| 1530 | void CreateDitheringTransition( const Image & in, int32_t inX, int32_t inY, Image & out, int32_t outX, int32_t outY, int32_t width, int32_t height, |
| 1531 | const bool isVertical, const bool isReverse ) |
| 1532 | { |
| 1533 | if ( !Verify( in, inX, inY, out, outX, outY, width, height ) ) { |
| 1534 | return; |
| 1535 | } |
| 1536 | |
| 1537 | const int32_t widthIn = in.width(); |
| 1538 | const int32_t offsetIn = inY * widthIn + inX; |
| 1539 | const uint8_t * imageIn = in.image() + offsetIn; |
| 1540 | const uint8_t * transformIn = in.singleLayer() ? nullptr : in.transform() + offsetIn; |
| 1541 | |
| 1542 | const int32_t widthOut = out.width(); |
| 1543 | const int32_t offsetOut = outY * widthOut + outX; |
| 1544 | uint8_t * imageOut = out.image() + offsetOut; |
| 1545 | uint8_t * transformOut = out.singleLayer() ? nullptr : out.transform() + offsetOut; |
| 1546 | |
| 1547 | if ( isVertical ) { |
| 1548 | // We also go in a loop from the right part of the image to its center. |
| 1549 | const uint8_t * imageInRightPoint = imageIn + width - 1; |
| 1550 | uint8_t * imageOutRightPoint = imageOut + width - 1; |
| 1551 | |
| 1552 | // If image is single-layer then pointer to its transform layer is 'nullptr' |
| 1553 | const uint8_t * transformInRightPoint = ( transformIn == nullptr ) ? nullptr : transformIn + width - 1; |
| 1554 | uint8_t * transformOutRightPoint = ( transformOut == nullptr ) ? nullptr : transformOut + width - 1; |
| 1555 | |
| 1556 | const int32_t halfWidth = width / 2; |
| 1557 | |
| 1558 | // We make a symmetric transition and if width is odd we shift one line right. |
| 1559 | if ( ( width % 2 ) == 1 ) { |
| 1560 | if ( isReverse ) { |
| 1561 | --imageOutRightPoint; |
| 1562 | --imageInRightPoint; |
| 1563 | |
| 1564 | if ( transformOutRightPoint != nullptr ) { |
| 1565 | --transformOutRightPoint; |
| 1566 | } |
| 1567 | if ( transformInRightPoint != nullptr ) { |
| 1568 | --transformInRightPoint; |
| 1569 | } |
| 1570 | } |
| 1571 | else { |
| 1572 | ++imageOut; |
| 1573 | ++imageIn; |
| 1574 | |
| 1575 | if ( transformOut != nullptr ) { |
| 1576 | ++transformOut; |
| 1577 | } |
| 1578 | if ( transformIn != nullptr ) { |
| 1579 | ++transformIn; |
| 1580 | } |
| 1581 | } |
| 1582 | } |
| 1583 | |
| 1584 | for ( int32_t x = 0; x < halfWidth; ++x ) { |
| 1585 | // The step is 2 to the power, which decreases by 1 every second line and is 1 in the center. |
| 1586 | // We limit the step power to 30 to not overflow the 32 bit 'stepY'. |
| 1587 | const int32_t stepPower = std::min<int32_t>( 30, ( halfWidth - x ) / 2 + 1 ); |
| 1588 | const int32_t stepY = 1 << stepPower; |
| 1589 | |
| 1590 | // The point position in dithered pattern. |
| 1591 | const int32_t patternPoint = stepY / 2 * ( ( x + halfWidth ) % 2 ); |
| 1592 | |
| 1593 | for ( int32_t y = 0; y < height; ++y ) { |
| 1594 | const int32_t offsetOutX = y * widthOut; |
| 1595 | const int32_t offsetInX = y * widthIn; |
| 1596 | const int32_t offsetY = y % stepY; |
| 1597 | |
| 1598 | if ( isReverse == ( patternPoint != offsetY ) ) { |
| 1599 | if ( transformIn != nullptr && transformOut == nullptr && ( *( transformIn + offsetInX ) == 1 ) ) { |
| 1600 | // Skip pixel. |
| 1601 | continue; |
| 1602 | } |
| 1603 | |
| 1604 | // First part of transition: we copy single pixels. |
| 1605 | *( imageOut + offsetOutX ) = *( imageIn + offsetInX ); |
| 1606 | |
| 1607 | if ( transformOut == nullptr ) { |
| 1608 | continue; |
| 1609 | } |
| 1610 | if ( transformIn == nullptr ) { |
| 1611 | // Set the copied pixel visible. |
| 1612 | *( transformOut + offsetOutX ) = 0; |
| 1613 | } |
| 1614 | else { |
| 1615 | // 'in' and 'out' images have the transform layer. |
| 1616 | *( transformOut + offsetOutX ) = *( transformIn + offsetInX ); |
| 1617 | } |
| 1618 | } |
| 1619 | else { |
| 1620 | if ( transformInRightPoint != nullptr && transformOutRightPoint == nullptr && ( *( transformInRightPoint + offsetInX ) == 1 ) ) { |
| 1621 | // Skip pixel. |
| 1622 | continue; |
| 1623 | } |
| 1624 | |
| 1625 | // Second part of transition: we copy image excluding single pixels. |
| 1626 | *( imageOutRightPoint + offsetOutX ) = *( imageInRightPoint + offsetInX ); |
| 1627 | |
| 1628 | if ( transformOutRightPoint == nullptr ) { |
| 1629 | continue; |
| 1630 | } |
| 1631 | |
| 1632 | if ( transformInRightPoint == nullptr ) { |
| 1633 | // Set the copied pixel visible. |
| 1634 | *( transformOutRightPoint + offsetOutX ) = 0; |
| 1635 | } |
| 1636 | else { |
| 1637 | // 'in' and 'out' images have the transform layer. |
| 1638 | *( transformOutRightPoint + offsetOutX ) = *( transformInRightPoint + offsetInX ); |
| 1639 | } |
| 1640 | } |
| 1641 | } |
| 1642 | |
| 1643 | ++imageOut; |
| 1644 | --imageOutRightPoint; |
| 1645 | ++imageIn; |
| 1646 | --imageInRightPoint; |
| 1647 | |
| 1648 | if ( transformOut != nullptr ) { |
| 1649 | ++transformOut; |
| 1650 | --transformOutRightPoint; |
| 1651 | } |
| 1652 | |
| 1653 | if ( transformIn != nullptr ) { |
| 1654 | ++transformIn; |
| 1655 | --transformInRightPoint; |
| 1656 | } |
| 1657 | } |
| 1658 | } |
| 1659 | else { |
| 1660 | // We also go in a loop from the bottom part of the image to its center. |
| 1661 | const int32_t offsetInBottomOffset = ( height - 1 ) * widthIn; |
| 1662 | const int32_t offsetOutYBottomOffset = ( height - 1 ) * widthOut; |
| 1663 | const uint8_t * imageInBottomPoint = imageIn + offsetInBottomOffset; |
| 1664 | uint8_t * imageOutBottomPoint = imageOut + offsetOutYBottomOffset; |
| 1665 | |
| 1666 | // If image is single-layer then pointer to its transform layer is 'nullptr'. |
| 1667 | const uint8_t * transformInBottomPoint = ( transformIn == nullptr ) ? nullptr : transformIn + offsetInBottomOffset; |
| 1668 | uint8_t * transformOutBottomPoint = ( transformOut == nullptr ) ? nullptr : transformOut + offsetOutYBottomOffset; |
| 1669 | |
| 1670 | const int32_t halfHeight = height / 2; |
| 1671 | |
| 1672 | // We make a symmetric transition and if width is odd we shift one line down. |
| 1673 | if ( ( height % 2 ) == 1 ) { |
| 1674 | if ( isReverse ) { |
| 1675 | imageOutBottomPoint -= widthOut; |
| 1676 | imageInBottomPoint -= widthIn; |
| 1677 | |
| 1678 | if ( transformOutBottomPoint != nullptr ) { |
| 1679 | transformOutBottomPoint -= widthOut; |
| 1680 | } |
| 1681 | if ( transformInBottomPoint != nullptr ) { |
| 1682 | transformInBottomPoint -= widthIn; |
| 1683 | } |
| 1684 | } |
| 1685 | else { |
| 1686 | imageOut += widthOut; |
| 1687 | imageIn += widthIn; |
| 1688 | |
| 1689 | if ( transformOut != nullptr ) { |
| 1690 | transformOut += widthOut; |
| 1691 | } |
| 1692 | if ( transformIn != nullptr ) { |
| 1693 | transformIn += widthIn; |
| 1694 | } |
| 1695 | } |
| 1696 | } |
| 1697 | |
| 1698 | for ( int32_t y = 0; y < halfHeight; ++y ) { |
| 1699 | // The step is 2 to the power, which decreases by 1 every second line and is 1 in the center. |
| 1700 | // We limit the step power to 30 to not overflow the 32 bit 'stepX'. |
| 1701 | const int32_t stepPower = std::min<int32_t>( 30, ( halfHeight - y ) / 2 + 1 ); |
| 1702 | const int32_t stepX = 1 << stepPower; |
| 1703 | |
| 1704 | // The point position in dithered pattern. |
| 1705 | const int32_t patternPoint = stepX / 2 * ( ( y + halfHeight ) % 2 ); |
| 1706 | |
| 1707 | for ( int32_t x = 0; x < width; ++x ) { |
| 1708 | const int32_t offsetX = x % stepX; |
| 1709 | |
| 1710 | if ( isReverse == ( patternPoint != offsetX ) ) { |
| 1711 | if ( transformIn != nullptr && transformOut == nullptr && ( *( transformIn + x ) == 1 ) ) { |
| 1712 | // Skip pixel. |
| 1713 | continue; |
| 1714 | } |
| 1715 | |
| 1716 | // First part of transition: we copy single pixels. |
| 1717 | *( imageOut + x ) = *( imageIn + x ); |
| 1718 | |
| 1719 | if ( transformOut == nullptr ) { |
| 1720 | continue; |
| 1721 | } |
| 1722 | if ( transformIn == nullptr ) { |
| 1723 | // Set the copied pixel visible. |
| 1724 | *( transformOut + x ) = 0; |
| 1725 | } |
| 1726 | else { |
| 1727 | // 'in' and 'out' images have the transform layer. |
| 1728 | *( transformOut + x ) = *( transformIn + x ); |
| 1729 | } |
| 1730 | } |
| 1731 | else { |
| 1732 | if ( transformInBottomPoint != nullptr && transformOutBottomPoint == nullptr && ( *( transformInBottomPoint + x ) == 1 ) ) { |
| 1733 | // Skip pixel. |
| 1734 | continue; |
| 1735 | } |
| 1736 | |
| 1737 | // Second part of transition: we copy image excluding single pixels. |
| 1738 | *( imageOutBottomPoint + x ) = *( imageInBottomPoint + x ); |
| 1739 | |
| 1740 | if ( transformOutBottomPoint == nullptr ) { |
| 1741 | continue; |
| 1742 | } |
| 1743 | if ( transformInBottomPoint == nullptr ) { |
| 1744 | // Set the copied pixel visible. |
| 1745 | *( transformOutBottomPoint + x ) = 0; |
| 1746 | } |
| 1747 | else { |
| 1748 | // 'in' and 'out' images have the transform layer. |
| 1749 | *( transformOutBottomPoint + x ) = *( transformInBottomPoint + x ); |
| 1750 | } |
| 1751 | } |
| 1752 | } |
| 1753 | |
| 1754 | imageOut += widthOut; |
| 1755 | imageOutBottomPoint -= widthOut; |
| 1756 | imageIn += widthIn; |
| 1757 | imageInBottomPoint -= widthIn; |
| 1758 | |
| 1759 | if ( transformOut != nullptr ) { |
| 1760 | transformOut += widthOut; |
| 1761 | transformOutBottomPoint -= widthOut; |
| 1762 | } |
| 1763 | |
| 1764 | if ( transformIn != nullptr ) { |
| 1765 | transformIn += widthIn; |
| 1766 | transformInBottomPoint -= widthIn; |
| 1767 | } |
| 1768 | } |
| 1769 | } |
| 1770 | } |
| 1771 | |
| 1772 | Sprite Crop( const Image & image, int32_t x, int32_t y, int32_t width, int32_t height ) |
| 1773 | { |
| 1774 | if ( image.empty() || width <= 0 || height <= 0 ) { |
| 1775 | return {}; |
| 1776 | } |
| 1777 | |
| 1778 | if ( x < 0 ) { |
| 1779 | const int32_t offsetX = -x; |
| 1780 | if ( offsetX >= width ) { |
| 1781 | return {}; |
| 1782 | } |
| 1783 | |
| 1784 | x = 0; |
| 1785 | width -= offsetX; |
| 1786 | } |
| 1787 | |
| 1788 | if ( y < 0 ) { |
| 1789 | const int32_t offsetY = -y; |
| 1790 | if ( offsetY >= height ) { |
| 1791 | return {}; |
| 1792 | } |
| 1793 | |
| 1794 | y = 0; |
| 1795 | height -= offsetY; |
| 1796 | } |
| 1797 | |
| 1798 | if ( x > image.width() || y > image.height() ) { |
| 1799 | return {}; |
| 1800 | } |
| 1801 | |
| 1802 | if ( x + width > image.width() ) { |
| 1803 | const int32_t offsetX = x + width - image.width(); |
| 1804 | width -= offsetX; |
| 1805 | } |
| 1806 | |
| 1807 | if ( y + height > image.height() ) { |
| 1808 | const int32_t offsetY = y + height - image.height(); |
| 1809 | height -= offsetY; |
| 1810 | } |
| 1811 | |
| 1812 | Sprite out; |
| 1813 | if ( image.singleLayer() ) { |
| 1814 | // The result of Crop should have the same layers as the input image. |
| 1815 | out._disableTransformLayer(); |
| 1816 | } |
| 1817 | out.resize( width, height ); |
| 1818 | |
| 1819 | Copy( image, x, y, out, 0, 0, width, height ); |
| 1820 | out.setPosition( x, y ); |
| 1821 | return out; |
| 1822 | } |
| 1823 | |
| 1824 | void DrawBorder( Image & image, const uint8_t value, const uint32_t skipFactor /* =0 */ ) |
| 1825 | { |
| 1826 | if ( image.empty() || image.width() < 2 || image.height() < 2 ) { |
| 1827 | return; |
| 1828 | } |
| 1829 | |
| 1830 | const int32_t width = image.width(); |
| 1831 | const int32_t height = image.height(); |
| 1832 | |
| 1833 | uint8_t * dataPointer = image.image(); |
| 1834 | // If image is single-layer then pointer to its transform layer is 'nullptr'. |
| 1835 | uint8_t * transformPointer = image.singleLayer() ? nullptr : image.transform(); |
| 1836 | |
| 1837 | if ( skipFactor < 2 ) { |
| 1838 | // top side |
| 1839 | uint8_t * data = dataPointer; |
| 1840 | const uint8_t * dataEnd = data + width; |
| 1841 | if ( transformPointer == nullptr ) { |
| 1842 | for ( ; data != dataEnd; ++data ) { |
| 1843 | *data = value; |
| 1844 | } |
| 1845 | } |
| 1846 | else { |
| 1847 | uint8_t * transform = transformPointer; |
| 1848 | for ( ; data != dataEnd; ++data, ++transform ) { |
| 1849 | *data = value; |
| 1850 | *transform = 0; |
| 1851 | } |
| 1852 | } |
| 1853 | |
| 1854 | // bottom side |
| 1855 | data = dataPointer + width * static_cast<ptrdiff_t>( height - 1 ); |
| 1856 | dataEnd = data + width; |
| 1857 | if ( transformPointer == nullptr ) { |
| 1858 | for ( ; data != dataEnd; ++data ) { |
| 1859 | *data = value; |
| 1860 | } |
| 1861 | } |
| 1862 | else { |
| 1863 | uint8_t * transform = transformPointer + width * static_cast<ptrdiff_t>( height - 1 ); |
| 1864 | for ( ; data != dataEnd; ++data, ++transform ) { |
| 1865 | *data = value; |
| 1866 | *transform = 0; |
| 1867 | } |
| 1868 | } |
| 1869 | |
| 1870 | // left side |
| 1871 | data = dataPointer + width; |
| 1872 | dataEnd = data + width * ( height - 2 ); |
| 1873 | if ( transformPointer == nullptr ) { |
| 1874 | for ( ; data != dataEnd; data += width ) { |
| 1875 | *data = value; |
| 1876 | } |
| 1877 | } |
| 1878 | else { |
| 1879 | uint8_t * transform = transformPointer + width; |
| 1880 | for ( ; data != dataEnd; data += width, transform += width ) { |
| 1881 | *data = value; |
| 1882 | *transform = 0; |
| 1883 | } |
| 1884 | } |
| 1885 | |
| 1886 | // right side |
| 1887 | data = dataPointer + width + width - 1; |
| 1888 | dataEnd = data + width * ( height - 2 ); |
| 1889 | if ( transformPointer == nullptr ) { |
| 1890 | for ( ; data != dataEnd; data += width ) { |
| 1891 | *data = value; |
| 1892 | } |
| 1893 | } |
| 1894 | else { |
| 1895 | uint8_t * transform = transformPointer + width + width - 1; |
| 1896 | for ( ; data != dataEnd; data += width, transform += width ) { |
| 1897 | *data = value; |
| 1898 | *transform = 0; |
| 1899 | } |
| 1900 | } |
| 1901 | } |
| 1902 | else { |
| 1903 | uint32_t counter = 1; |
| 1904 | |
| 1905 | // top side |
| 1906 | uint8_t * data = dataPointer; |
| 1907 | |
| 1908 | const uint8_t * dataEnd = data + width; |
| 1909 | if ( transformPointer == nullptr ) { |
| 1910 | for ( ; data != dataEnd; ++data ) { |
| 1911 | if ( counter % skipFactor != 0 ) { |
| 1912 | *data = value; |
| 1913 | } |
| 1914 | ++counter; |
| 1915 | } |
| 1916 | } |
| 1917 | else { |
| 1918 | uint8_t * transform = transformPointer; |
| 1919 | for ( ; data != dataEnd; ++data, ++transform ) { |
| 1920 | if ( counter % skipFactor != 0 ) { |
| 1921 | *data = value; |
| 1922 | *transform = 0; |
| 1923 | } |
| 1924 | ++counter; |
| 1925 | } |
| 1926 | } |
| 1927 | |
| 1928 | // right side |
| 1929 | data = dataPointer + width + width - 1; |
| 1930 | dataEnd = data + width * ( height - 2 ); |
| 1931 | if ( transformPointer == nullptr ) { |
| 1932 | for ( ; data != dataEnd; data += width ) { |
| 1933 | if ( counter % skipFactor != 0 ) { |
| 1934 | *data = value; |
| 1935 | } |
| 1936 | ++counter; |
| 1937 | } |
| 1938 | } |
| 1939 | else { |
| 1940 | uint8_t * transform = transformPointer + width + width - 1; |
| 1941 | for ( ; data != dataEnd; data += width, transform += width ) { |
| 1942 | if ( counter % skipFactor != 0 ) { |
| 1943 | *data = value; |
| 1944 | *transform = 0; |
| 1945 | } |
| 1946 | ++counter; |
| 1947 | } |
| 1948 | } |
| 1949 | |
| 1950 | // bottom side |
| 1951 | data = dataPointer + width * static_cast<ptrdiff_t>( height - 1 ) + width - 1; |
| 1952 | dataEnd = data - width; |
| 1953 | if ( transformPointer == nullptr ) { |
| 1954 | for ( ; data != dataEnd; --data ) { |
| 1955 | if ( counter % skipFactor != 0 ) { |
| 1956 | *data = value; |
| 1957 | } |
| 1958 | ++counter; |
| 1959 | } |
| 1960 | } |
| 1961 | else { |
| 1962 | uint8_t * transform = transformPointer + width * static_cast<ptrdiff_t>( height - 1 ) + width - 1; |
| 1963 | for ( ; data != dataEnd; --data, --transform ) { |
| 1964 | if ( counter % skipFactor != 0 ) { |
| 1965 | *data = value; |
| 1966 | *transform = 0; |
| 1967 | } |
| 1968 | ++counter; |
| 1969 | } |
| 1970 | } |
| 1971 | |
| 1972 | // left side |
| 1973 | data = dataPointer + width * static_cast<ptrdiff_t>( height - 2 ); |
| 1974 | dataEnd = dataPointer; |
| 1975 | if ( transformPointer == nullptr ) { |
| 1976 | for ( ; data != dataEnd; data -= width ) { |
| 1977 | if ( counter % skipFactor != 0 ) { |
| 1978 | *data = value; |
| 1979 | } |
| 1980 | ++counter; |
| 1981 | } |
| 1982 | } |
| 1983 | else { |
| 1984 | uint8_t * transform = transformPointer + width * static_cast<ptrdiff_t>( height - 2 ); |
| 1985 | for ( ; data != dataEnd; data -= width, transform -= width ) { |
| 1986 | if ( counter % skipFactor != 0 ) { |
| 1987 | *data = value; |
| 1988 | *transform = 0; |
| 1989 | } |
| 1990 | ++counter; |
| 1991 | } |
| 1992 | } |
| 1993 | } |
| 1994 | } |
| 1995 | |
| 1996 | void DrawLine( Image & image, const Point & start, const Point & end, const uint8_t value, const Rect & roi /* = Rect() */ ) |
| 1997 | { |
| 1998 | if ( image.empty() ) { |
| 1999 | return; |
| 2000 | } |
| 2001 | |
| 2002 | const int32_t width = image.width(); |
| 2003 | const int32_t height = image.height(); |
| 2004 | |
| 2005 | int32_t x1 = start.x; |
| 2006 | int32_t y1 = start.y; |
| 2007 | const int32_t x2 = end.x; |
| 2008 | const int32_t y2 = end.y; |
| 2009 | |
| 2010 | const int32_t dx = std::abs( x2 - x1 ); |
| 2011 | const int32_t dy = std::abs( y2 - y1 ); |
| 2012 | |
| 2013 | const bool isValidRoi = roi.width > 0 && roi.height > 0; |
| 2014 | |
| 2015 | const int32_t minX = isValidRoi ? std::max<int32_t>( roi.x, 0 ) : 0; |
| 2016 | const int32_t minY = isValidRoi ? std::max<int32_t>( roi.y, 0 ) : 0; |
| 2017 | int32_t maxX = isValidRoi ? roi.x + roi.width : width; |
| 2018 | int32_t maxY = isValidRoi ? roi.y + roi.height : height; |
| 2019 | |
| 2020 | if ( minX >= width || minY >= height ) { |
| 2021 | return; |
| 2022 | } |
| 2023 | |
| 2024 | if ( maxX >= width ) { |
| 2025 | maxX = width; |
| 2026 | } |
| 2027 | |
| 2028 | if ( maxY >= height ) { |
| 2029 | maxY = height; |
| 2030 | } |
| 2031 | |
| 2032 | uint8_t * data = image.image(); |
| 2033 | |
| 2034 | if ( image.singleLayer() ) { |
| 2035 | if ( dx >= dy ) { |
| 2036 | int32_t ns = dx / 2; |
| 2037 | |
| 2038 | for ( int32_t i = 0; i <= dx; ++i ) { |
| 2039 | if ( x1 >= minX && x1 < maxX && y1 >= minY && y1 < maxY ) { |
| 2040 | const int32_t offset = x1 + y1 * width; |
| 2041 | *( data + offset ) = value; |
| 2042 | } |
| 2043 | x1 < x2 ? ++x1 : --x1; |
| 2044 | ns -= dy; |
| 2045 | if ( ns < 0 ) { |
| 2046 | y1 < y2 ? ++y1 : --y1; |
| 2047 | ns += dx; |
| 2048 | } |
| 2049 | } |
| 2050 | } |
| 2051 | else { |
| 2052 | int32_t ns = dy / 2; |
| 2053 | |
| 2054 | for ( int32_t i = 0; i <= dy; ++i ) { |
| 2055 | if ( x1 >= minX && x1 < maxX && y1 >= minY && y1 < maxY ) { |
| 2056 | const int32_t offset = x1 + y1 * width; |
| 2057 | *( data + offset ) = value; |
| 2058 | } |
| 2059 | y1 < y2 ? ++y1 : --y1; |
| 2060 | ns -= dx; |
| 2061 | if ( ns < 0 ) { |
| 2062 | x1 < x2 ? ++x1 : --x1; |
| 2063 | ns += dy; |
| 2064 | } |
| 2065 | } |
| 2066 | } |
| 2067 | } |
| 2068 | else { |
| 2069 | uint8_t * transform = image.transform(); |
| 2070 | |
| 2071 | if ( dx >= dy ) { |
| 2072 | int32_t ns = dx / 2; |
| 2073 | |
| 2074 | for ( int32_t i = 0; i <= dx; ++i ) { |
| 2075 | if ( x1 >= minX && x1 < maxX && y1 >= minY && y1 < maxY ) { |
| 2076 | const int32_t offset = x1 + y1 * width; |
| 2077 | *( data + offset ) = value; |
| 2078 | *( transform + offset ) = 0; |
| 2079 | } |
| 2080 | x1 < x2 ? ++x1 : --x1; |
| 2081 | ns -= dy; |
| 2082 | if ( ns < 0 ) { |
| 2083 | y1 < y2 ? ++y1 : --y1; |
| 2084 | ns += dx; |
| 2085 | } |
| 2086 | } |
| 2087 | } |
| 2088 | else { |
| 2089 | int32_t ns = dy / 2; |
| 2090 | |
| 2091 | for ( int32_t i = 0; i <= dy; ++i ) { |
| 2092 | if ( x1 >= minX && x1 < maxX && y1 >= minY && y1 < maxY ) { |
| 2093 | const int32_t offset = x1 + y1 * width; |
| 2094 | *( data + offset ) = value; |
| 2095 | *( transform + offset ) = 0; |
| 2096 | } |
| 2097 | y1 < y2 ? ++y1 : --y1; |
| 2098 | ns -= dx; |
| 2099 | if ( ns < 0 ) { |
| 2100 | x1 < x2 ? ++x1 : --x1; |
| 2101 | ns += dy; |
| 2102 | } |
| 2103 | } |
| 2104 | } |
| 2105 | } |
| 2106 | } |
| 2107 | |
| 2108 | void DrawRect( Image & image, const Rect & roi, const uint8_t value ) |
| 2109 | { |
| 2110 | if ( image.empty() || roi.width < 1 || roi.height < 1 ) { |
| 2111 | return; |
| 2112 | } |
| 2113 | |
| 2114 | DrawLine( image, { roi.x, roi.y }, { roi.x + roi.width, roi.y }, value, roi ); |
| 2115 | DrawLine( image, { roi.x, roi.y }, { roi.x, roi.y + roi.height }, value, roi ); |
| 2116 | DrawLine( image, { roi.x + roi.width - 1, roi.y }, { roi.x + roi.width - 1, roi.y + roi.height }, value, roi ); |
| 2117 | DrawLine( image, { roi.x, roi.y + roi.height - 1 }, { roi.x + roi.width, roi.y + roi.height - 1 }, value, roi ); |
| 2118 | } |
| 2119 | |
| 2120 | void DivideImageBySquares( const Point & spriteOffset, const Image & original, const int32_t squareSize, std::vector<Point> & outputSquareId, |
| 2121 | std::vector<std::pair<Point, Rect>> & outputImageInfo ) |
| 2122 | { |
| 2123 | if ( original.empty() ) { |
| 2124 | return; |
| 2125 | } |
| 2126 | |
| 2127 | if ( squareSize <= 0 ) { |
| 2128 | assert( 0 ); |
| 2129 | return; |
| 2130 | } |
| 2131 | |
| 2132 | Point offset{ spriteOffset.x / squareSize, spriteOffset.y / squareSize }; |
| 2133 | |
| 2134 | // The start of a square must be before image offset so in case of negative offset we need to decrease the ID of the start square. |
| 2135 | if ( ( spriteOffset.x < 0 ) && ( offset.x * squareSize != spriteOffset.x ) ) { |
| 2136 | --offset.x; |
| 2137 | } |
| 2138 | |
| 2139 | if ( ( spriteOffset.y < 0 ) && ( offset.y * squareSize != spriteOffset.y ) ) { |
| 2140 | --offset.y; |
| 2141 | } |
| 2142 | |
| 2143 | const Point spriteRelativeOffset{ spriteOffset.x - offset.x * squareSize, spriteOffset.y - offset.y * squareSize }; |
| 2144 | const Point stepPerDirection{ ( original.width() + spriteRelativeOffset.x + squareSize - 1 ) / squareSize, |
| 2145 | ( original.height() + spriteRelativeOffset.y + squareSize - 1 ) / squareSize }; |
| 2146 | assert( stepPerDirection.x > 0 && stepPerDirection.y > 0 ); |
| 2147 | |
| 2148 | const Rect relativeROI( spriteRelativeOffset.x, spriteRelativeOffset.y, original.width(), original.height() ); |
| 2149 | |
| 2150 | for ( int32_t y = 0; y < stepPerDirection.y; ++y ) { |
| 2151 | for ( int32_t x = 0; x < stepPerDirection.x; ++x ) { |
| 2152 | const Rect roi( x * squareSize, y * squareSize, squareSize, squareSize ); |
| 2153 | const Rect intersection = relativeROI ^ roi; |
| 2154 | assert( intersection.width > 0 && intersection.height > 0 ); |
| 2155 | |
| 2156 | outputSquareId.emplace_back( offset + Point( x, y ) ); |
| 2157 | |
| 2158 | outputImageInfo.emplace_back( fheroes2::Point( intersection.x - roi.x, intersection.y - roi.y ), |
| 2159 | fheroes2::Rect( intersection.x - spriteRelativeOffset.x, intersection.y - spriteRelativeOffset.y, intersection.width, |
| 2160 | intersection.height ) ); |
| 2161 | } |
| 2162 | } |
| 2163 | } |
| 2164 | |
| 2165 | Image ExtractCommonPattern( const std::vector<const Image *> & input ) |
| 2166 | { |
| 2167 | if ( input.empty() ) { |
| 2168 | return {}; |
| 2169 | } |
| 2170 | |
| 2171 | assert( input.front() != nullptr ); |
| 2172 | |
| 2173 | if ( input.size() == 1 ) { |
| 2174 | return *input.front(); |
| 2175 | } |
| 2176 | |
| 2177 | if ( input.front()->empty() ) { |
| 2178 | return {}; |
| 2179 | } |
| 2180 | |
| 2181 | const int32_t inputFrontWidth = input.front()->width(); |
| 2182 | const int32_t inputFrontHeight = input.front()->height(); |
| 2183 | |
| 2184 | for ( const Image * image : input ) { |
| 2185 | assert( image != nullptr ); |
| 2186 | if ( image->width() != inputFrontWidth || image->height() != inputFrontHeight ) { |
| 2187 | return {}; |
| 2188 | } |
| 2189 | } |
| 2190 | |
| 2191 | std::vector<const uint8_t *> imageIn( input.size() ); |
| 2192 | std::vector<const uint8_t *> transformIn( input.size() ); |
| 2193 | |
| 2194 | for ( size_t i = 0; i < input.size(); ++i ) { |
| 2195 | imageIn[i] = input[i]->image(); |
| 2196 | |
| 2197 | transformIn[i] = input[i]->singleLayer() ? nullptr : input[i]->transform(); |
| 2198 | } |
| 2199 | |
| 2200 | Image out( inputFrontWidth, inputFrontHeight ); |
| 2201 | out.reset(); |
| 2202 | |
| 2203 | uint8_t * imageOut = out.image(); |
| 2204 | uint8_t * transformOut = out.transform(); |
| 2205 | const uint8_t * imageOutEnd = imageOut + out.width() * out.height(); |
| 2206 | |
| 2207 | bool isEqual = false; |
| 2208 | |
| 2209 | for ( ; imageOut != imageOutEnd; ++imageOut, ++transformOut ) { |
| 2210 | isEqual = true; |
| 2211 | |
| 2212 | for ( size_t i = 1; i < input.size(); ++i ) { |
| 2213 | if ( *imageIn[0] != *imageIn[i] || ( transformIn[0] != nullptr && transformIn[i] != nullptr && ( *transformIn[0] != *transformIn[i] ) ) ) { |
| 2214 | isEqual = false; |
| 2215 | break; |
| 2216 | } |
| 2217 | } |
| 2218 | |
| 2219 | if ( isEqual ) { |
| 2220 | *imageOut = *imageIn[0]; |
| 2221 | *transformOut = ( transformIn[0] == nullptr ) ? 0 : *transformIn[0]; |
| 2222 | } |
| 2223 | |
| 2224 | for ( size_t i = 0; i < input.size(); ++i ) { |
| 2225 | ++imageIn[i]; |
| 2226 | if ( transformIn[i] != nullptr ) { |
| 2227 | ++transformIn[i]; |
| 2228 | } |
| 2229 | } |
| 2230 | } |
| 2231 | |
| 2232 | return out; |
| 2233 | } |
| 2234 | |
| 2235 | void Fill( Image & image, int32_t x, int32_t y, int32_t width, int32_t height, const uint8_t colorId ) |
| 2236 | { |
| 2237 | if ( !Verify( image, x, y, width, height ) ) { |
| 2238 | return; |
| 2239 | } |
| 2240 | |
| 2241 | if ( image.width() == width && image.height() == height ) { |
| 2242 | // We fill the whole image. |
| 2243 | image.fill( colorId ); |
| 2244 | return; |
| 2245 | } |
| 2246 | |
| 2247 | const int32_t imageWidth = image.width(); |
| 2248 | |
| 2249 | uint8_t * imageY = image.image() + y * imageWidth + x; |
| 2250 | const uint8_t * imageYEnd = imageY + height * imageWidth; |
| 2251 | |
| 2252 | if ( image.singleLayer() ) { |
| 2253 | for ( ; imageY != imageYEnd; imageY += imageWidth ) { |
| 2254 | memset( imageY, colorId, width ); |
| 2255 | } |
| 2256 | } |
| 2257 | else { |
| 2258 | uint8_t * transformY = image.transform() + static_cast<ptrdiff_t>( y ) * imageWidth + x; |
| 2259 | for ( ; imageY != imageYEnd; imageY += imageWidth, transformY += imageWidth ) { |
| 2260 | memset( imageY, colorId, width ); |
| 2261 | memset( transformY, static_cast<uint8_t>( 0 ), width ); |
| 2262 | } |
| 2263 | } |
| 2264 | } |
| 2265 | |
| 2266 | void FillTransform( Image & image, int32_t x, int32_t y, int32_t width, int32_t height, const uint8_t transformId ) |
| 2267 | { |
| 2268 | if ( !Verify( image, x, y, width, height ) || image.singleLayer() ) { |
| 2269 | return; |
| 2270 | } |
| 2271 | |
| 2272 | const int32_t imageWidth = image.width(); |
| 2273 | |
| 2274 | uint8_t * imageY = image.image() + y * imageWidth + x; |
| 2275 | uint8_t * transformY = image.transform() + y * imageWidth + x; |
| 2276 | const uint8_t * imageYEnd = imageY + height * imageWidth; |
| 2277 | |
| 2278 | for ( ; imageY != imageYEnd; imageY += imageWidth, transformY += imageWidth ) { |
| 2279 | memset( imageY, static_cast<uint8_t>( 0 ), width ); |
| 2280 | memset( transformY, transformId, width ); |
| 2281 | } |
| 2282 | } |
| 2283 | |
| 2284 | Image FilterOnePixelNoise( const Image & input ) |
| 2285 | { |
| 2286 | if ( input.width() < 3 || input.height() < 3 ) { |
| 2287 | return input; |
| 2288 | } |
| 2289 | |
| 2290 | const int32_t width = input.width(); |
| 2291 | const int32_t height = input.height(); |
| 2292 | const bool isSingleLayer = input.singleLayer(); |
| 2293 | |
| 2294 | Image output; |
| 2295 | if ( isSingleLayer ) { |
| 2296 | output._disableTransformLayer(); |
| 2297 | } |
| 2298 | output.resize( width, height ); |
| 2299 | output.reset(); |
| 2300 | |
| 2301 | const uint8_t * imageInY = input.image(); |
| 2302 | uint8_t * imageOutY = output.image(); |
| 2303 | |
| 2304 | if ( isSingleLayer ) { |
| 2305 | for ( int32_t y = 0; y < height; ++y ) { |
| 2306 | for ( int32_t x = 0; x < width; ++x ) { |
| 2307 | if ( ( x == 0 || x == width - 1 ) && ( y == 0 || y == height - 1 ) ) { |
| 2308 | *( imageOutY + x ) = *( imageInY + x ); |
| 2309 | } |
| 2310 | } |
| 2311 | |
| 2312 | imageInY += width; |
| 2313 | |
| 2314 | imageOutY += width; |
| 2315 | } |
| 2316 | } |
| 2317 | else { |
| 2318 | const uint8_t * transformInY = input.transform(); |
| 2319 | uint8_t * transformOutY = output.transform(); |
| 2320 | |
| 2321 | for ( int32_t y = 0; y < height; ++y ) { |
| 2322 | const uint8_t * transformInX = transformInY; |
| 2323 | |
| 2324 | for ( int32_t x = 0; x < width; ++x ) { |
| 2325 | if ( *transformInX == 0 && ( x == 0 || x == width - 1 || *( transformInX - 1 ) == 0 || *( transformInX + 1 ) == 0 ) |
| 2326 | && ( y == 0 || y == height - 1 || *( transformInX - width ) == 0 || *( transformInX + width ) == 0 ) ) { |
| 2327 | *( transformOutY + x ) = 0; |
| 2328 | *( imageOutY + x ) = *( imageInY + x ); |
| 2329 | } |
| 2330 | |
| 2331 | ++transformInX; |
| 2332 | } |
| 2333 | |
| 2334 | imageInY += width; |
| 2335 | transformInY += width; |
| 2336 | |
| 2337 | imageOutY += width; |
| 2338 | transformOutY += width; |
| 2339 | } |
| 2340 | } |
| 2341 | |
| 2342 | return output; |
| 2343 | } |
| 2344 | |
| 2345 | bool FitToRoi( const Image & in, Point & inPos, const Image & out, Point & outPos, Size & outputSize, const Rect & outputRoi ) |
| 2346 | { |
| 2347 | if ( !Validate( out, outputRoi.x, outputRoi.y, outputRoi.width, outputRoi.height ) ) { |
| 2348 | return false; |
| 2349 | } |
| 2350 | |
| 2351 | outPos.x -= outputRoi.x; |
| 2352 | outPos.y -= outputRoi.y; |
| 2353 | |
| 2354 | if ( !Verify( inPos.x, inPos.y, outPos.x, outPos.y, outputSize.width, outputSize.height, in.width(), in.height(), outputRoi.width, outputRoi.height ) ) { |
| 2355 | return false; |
| 2356 | } |
| 2357 | |
| 2358 | outPos.x += outputRoi.x; |
| 2359 | outPos.y += outputRoi.y; |
| 2360 | |
| 2361 | return true; |
| 2362 | } |
| 2363 | |
| 2364 | Image Flip( const Image & in, const bool horizontally, const bool vertically ) |
| 2365 | { |
| 2366 | if ( in.empty() ) { |
| 2367 | return {}; |
| 2368 | } |
| 2369 | |
| 2370 | const int32_t width = in.width(); |
| 2371 | const int32_t height = in.height(); |
| 2372 | |
| 2373 | Image out; |
| 2374 | |
| 2375 | if ( in.singleLayer() ) { |
| 2376 | // Make the result of Flip to have the same layers as the input image. |
| 2377 | out._disableTransformLayer(); |
| 2378 | } |
| 2379 | |
| 2380 | out.resize( width, height ); |
| 2381 | |
| 2382 | if ( !horizontally && !vertically ) { |
| 2383 | Copy( in, out ); |
| 2384 | return out; |
| 2385 | } |
| 2386 | |
| 2387 | Flip( in, 0, 0, out, 0, 0, width, height, horizontally, vertically ); |
| 2388 | return out; |
| 2389 | } |
| 2390 | |
| 2391 | void Flip( const Image & in, int32_t inX, int32_t inY, Image & out, int32_t outX, int32_t outY, int32_t width, int32_t height, const bool horizontally, |
| 2392 | const bool vertically ) |
| 2393 | { |
| 2394 | if ( !Verify( in, inX, inY, out, outX, outY, width, height ) ) { |
| 2395 | return; |
| 2396 | } |
| 2397 | |
| 2398 | if ( !horizontally && !vertically ) { |
| 2399 | // No flip is required. |
| 2400 | Copy( in, inX, inY, out, outX, outY, width, height ); |
| 2401 | return; |
| 2402 | } |
| 2403 | |
| 2404 | // This assertion is needed to convince SonarQube that we will not dereference the null pointer to 'in' and 'out' images data. |
| 2405 | assert( !in.empty() && !out.empty() ); |
| 2406 | |
| 2407 | const int32_t widthIn = in.width(); |
| 2408 | const int32_t widthOut = out.width(); |
| 2409 | |
| 2410 | const int32_t offsetOut = outY * widthOut + outX; |
| 2411 | const int32_t offsetIn = inY * widthIn + inX; |
| 2412 | |
| 2413 | uint8_t * imageOutY = out.image() + offsetOut; |
| 2414 | const uint8_t * imageOutYEnd = imageOutY + static_cast<ptrdiff_t>( widthOut ) * height; |
| 2415 | |
| 2416 | // If image is single-layer then pointer to its transform layer is 'nullptr'. |
| 2417 | uint8_t * transformOutY = out.singleLayer() ? nullptr : out.transform() + offsetOut; |
| 2418 | |
| 2419 | if ( horizontally && !vertically ) { |
| 2420 | const uint8_t * imageInY = in.image() + offsetIn + width - 1; |
| 2421 | |
| 2422 | if ( in.singleLayer() ) { |
| 2423 | for ( ; imageOutY != imageOutYEnd; imageOutY += widthOut, imageInY += widthIn ) { |
| 2424 | uint8_t * imageOutX = imageOutY; |
| 2425 | const uint8_t * imageOutXEnd = imageOutX + width; |
| 2426 | const uint8_t * imageInX = imageInY; |
| 2427 | |
| 2428 | for ( ; imageOutX != imageOutXEnd; ++imageOutX, --imageInX ) { |
| 2429 | *imageOutX = *imageInX; |
| 2430 | } |
| 2431 | |
| 2432 | if ( transformOutY != nullptr ) { |
| 2433 | // Set 'out' image transform data not to skip image data. |
| 2434 | memset( transformOutY, static_cast<uint8_t>( 0 ), width ); |
| 2435 | |
| 2436 | transformOutY += widthOut; |
| 2437 | } |
| 2438 | } |
| 2439 | } |
| 2440 | else { |
| 2441 | const uint8_t * transformInY = in.transform() + offsetIn + width - 1; |
| 2442 | |
| 2443 | for ( ; imageOutY != imageOutYEnd; imageOutY += widthOut, imageInY += widthIn, transformInY += widthIn ) { |
| 2444 | uint8_t * imageOutX = imageOutY; |
| 2445 | const uint8_t * imageOutXEnd = imageOutX + width; |
| 2446 | const uint8_t * imageInX = imageInY; |
| 2447 | const uint8_t * transformInX = transformInY; |
| 2448 | |
| 2449 | if ( transformOutY == nullptr ) { |
| 2450 | for ( ; imageOutX != imageOutXEnd; ++imageOutX, --imageInX, --transformInX ) { |
| 2451 | // Copy image data only for non-transparent pixels. |
| 2452 | if ( *transformInX == 0 ) { |
| 2453 | *imageOutX = *imageInX; |
| 2454 | } |
| 2455 | } |
| 2456 | } |
| 2457 | else { |
| 2458 | // 'in' and 'out' images are with transform layer. |
| 2459 | uint8_t * transformOutX = transformOutY; |
| 2460 | |
| 2461 | for ( ; imageOutX != imageOutXEnd; ++imageOutX, ++transformOutX, --imageInX, --transformInX ) { |
| 2462 | *imageOutX = *imageInX; |
| 2463 | *transformOutX = *transformInX; |
| 2464 | } |
| 2465 | |
| 2466 | transformOutY += widthOut; |
| 2467 | } |
| 2468 | } |
| 2469 | } |
| 2470 | } |
| 2471 | else if ( !horizontally && vertically ) { |
| 2472 | const uint8_t * imageInY = in.image() + offsetIn + static_cast<ptrdiff_t>( height - 1 ) * widthIn; |
| 2473 | |
| 2474 | if ( in.singleLayer() ) { |
| 2475 | for ( ; imageOutY != imageOutYEnd; imageOutY += widthOut, imageInY -= widthIn ) { |
| 2476 | memcpy( imageOutY, imageInY, static_cast<size_t>( width ) ); |
| 2477 | |
| 2478 | if ( transformOutY != nullptr ) { |
| 2479 | // Set 'out' image transform data not to skip image data. |
| 2480 | memset( transformOutY, static_cast<uint8_t>( 0 ), width ); |
| 2481 | |
| 2482 | transformOutY += widthOut; |
| 2483 | } |
| 2484 | } |
| 2485 | } |
| 2486 | else { |
| 2487 | const uint8_t * transformInY = in.transform() + offsetIn + static_cast<ptrdiff_t>( height - 1 ) * widthIn; |
| 2488 | |
| 2489 | if ( transformOutY == nullptr ) { |
| 2490 | for ( ; imageOutY != imageOutYEnd; imageOutY += widthOut, imageInY -= widthIn, transformInY -= widthIn ) { |
| 2491 | uint8_t * imageOutX = imageOutY; |
| 2492 | const uint8_t * imageOutXEnd = imageOutX + width; |
| 2493 | const uint8_t * imageInX = imageInY; |
| 2494 | const uint8_t * transformInX = transformInY; |
| 2495 | |
| 2496 | for ( ; imageOutX != imageOutXEnd; ++imageOutX, ++imageInX, ++transformInX ) { |
| 2497 | // Copy image data only for non-transparent pixels. |
| 2498 | if ( *transformInX == 0 ) { |
| 2499 | *imageOutX = *imageInX; |
| 2500 | } |
| 2501 | } |
| 2502 | } |
| 2503 | } |
| 2504 | else { |
| 2505 | // 'in' and 'out' images are with transform layer. |
| 2506 | for ( ; imageOutY != imageOutYEnd; imageOutY += widthOut, transformOutY += widthOut, imageInY -= widthIn, transformInY -= widthIn ) { |
| 2507 | memcpy( imageOutY, imageInY, static_cast<size_t>( width ) ); |
| 2508 | memcpy( transformOutY, transformInY, static_cast<size_t>( width ) ); |
| 2509 | } |
| 2510 | } |
| 2511 | } |
| 2512 | } |
| 2513 | else { |
| 2514 | // Flip horizontally and vertically. |
| 2515 | if ( in.singleLayer() ) { |
| 2516 | const uint8_t * imageInY = in.image() + offsetIn + static_cast<ptrdiff_t>( height - 1 ) * widthIn + widthIn - 1; |
| 2517 | |
| 2518 | for ( ; imageOutY != imageOutYEnd; imageOutY += widthOut, imageInY -= widthIn ) { |
| 2519 | uint8_t * imageOutX = imageOutY; |
| 2520 | const uint8_t * imageOutXEnd = imageOutX + width; |
| 2521 | const uint8_t * imageInX = imageInY; |
| 2522 | |
| 2523 | for ( ; imageOutX != imageOutXEnd; ++imageOutX, --imageInX ) { |
| 2524 | *imageOutX = *imageInX; |
| 2525 | } |
| 2526 | |
| 2527 | if ( transformOutY != nullptr ) { |
| 2528 | // 'in' image is double-layer: set transform data not to skip image data. |
| 2529 | memset( transformOutY, static_cast<uint8_t>( 0 ), width ); |
| 2530 | |
| 2531 | transformOutY += widthOut; |
| 2532 | } |
| 2533 | } |
| 2534 | } |
| 2535 | else { |
| 2536 | const uint8_t * imageInY = in.image() + offsetIn + static_cast<ptrdiff_t>( height - 1 ) * widthIn + widthIn - 1; |
| 2537 | const uint8_t * transformInY = in.transform() + offsetIn + static_cast<ptrdiff_t>( height - 1 ) * widthIn + widthIn - 1; |
| 2538 | |
| 2539 | for ( ; imageOutY != imageOutYEnd; imageOutY += widthOut, imageInY -= widthIn, transformInY -= widthIn ) { |
| 2540 | uint8_t * imageOutX = imageOutY; |
| 2541 | const uint8_t * imageOutXEnd = imageOutX + width; |
| 2542 | const uint8_t * imageInX = imageInY; |
| 2543 | const uint8_t * transformInX = transformInY; |
| 2544 | |
| 2545 | if ( transformOutY == nullptr ) { |
| 2546 | for ( ; imageOutX != imageOutXEnd; ++imageOutX, --imageInX, --transformInX ) { |
| 2547 | // Copy image data only for non-transparent pixels. |
| 2548 | if ( *transformInX == 0 ) { |
| 2549 | *imageOutX = *imageInX; |
| 2550 | } |
| 2551 | } |
| 2552 | } |
| 2553 | else { |
| 2554 | // 'in' and 'out' images are with transform layer. |
| 2555 | for ( uint8_t * transformOutX = transformOutY; imageOutX != imageOutXEnd; ++imageOutX, ++transformOutX, --imageInX, --transformInX ) { |
| 2556 | *imageOutX = *imageInX; |
| 2557 | *transformOutX = *transformInX; |
| 2558 | } |
| 2559 | |
| 2560 | transformOutY += widthOut; |
| 2561 | } |
| 2562 | } |
| 2563 | } |
| 2564 | } |
| 2565 | } |
| 2566 | |
| 2567 | Rect GetActiveROI( const Image & image, const uint8_t minTransformValue ) |
| 2568 | { |
| 2569 | if ( image.empty() || image.singleLayer() ) { |
| 2570 | return {}; |
| 2571 | } |
| 2572 | |
| 2573 | const int32_t width = image.width(); |
| 2574 | const int32_t height = image.height(); |
| 2575 | |
| 2576 | Rect area( -1, -1, -1, -1 ); |
| 2577 | |
| 2578 | // Top border |
| 2579 | const uint8_t * inY = image.transform(); |
| 2580 | for ( int32_t y = 0; y < height; ++y, inY += width ) { |
| 2581 | const uint8_t * inX = inY; |
| 2582 | |
| 2583 | for ( int32_t x = 0; x < width; ++x, ++inX ) { |
| 2584 | if ( *inX == 0 || *inX >= minTransformValue ) { |
| 2585 | area.y = y; |
| 2586 | break; |
| 2587 | } |
| 2588 | } |
| 2589 | |
| 2590 | if ( area.y >= 0 ) { |
| 2591 | break; |
| 2592 | } |
| 2593 | } |
| 2594 | |
| 2595 | if ( area.y < 0 ) { |
| 2596 | return {}; |
| 2597 | } |
| 2598 | |
| 2599 | // Bottom border |
| 2600 | inY = image.transform() + width * ( height - 1 ); |
| 2601 | for ( int32_t y = height - 1; y > area.y; --y, inY -= width ) { |
| 2602 | const uint8_t * inX = inY; |
| 2603 | |
| 2604 | for ( int32_t x = 0; x < width; ++x, ++inX ) { |
| 2605 | if ( *inX == 0 || *inX >= minTransformValue ) { |
| 2606 | area.height = y - area.y + 1; |
| 2607 | break; |
| 2608 | } |
| 2609 | } |
| 2610 | |
| 2611 | if ( area.height >= 0 ) { |
| 2612 | break; |
| 2613 | } |
| 2614 | } |
| 2615 | |
| 2616 | // Left border |
| 2617 | const uint8_t * inX = image.transform() + width * area.y; |
| 2618 | for ( int32_t x = 0; x < width; ++x, ++inX ) { |
| 2619 | inY = inX; |
| 2620 | |
| 2621 | for ( int32_t y = 0; y < area.height; ++y, inY += width ) { |
| 2622 | if ( *inY == 0 || *inY >= minTransformValue ) { |
| 2623 | area.x = x; |
| 2624 | break; |
| 2625 | } |
| 2626 | } |
| 2627 | |
| 2628 | if ( area.x >= 0 ) { |
| 2629 | break; |
| 2630 | } |
| 2631 | } |
| 2632 | |
| 2633 | // Right border |
| 2634 | inX = image.transform() + width * area.y + width - 1; |
| 2635 | for ( int32_t x = width - 1; x >= area.x; --x, --inX ) { |
| 2636 | inY = inX; |
| 2637 | |
| 2638 | for ( int32_t y = 0; y < area.height; ++y, inY += width ) { |
| 2639 | if ( *inY == 0 || *inY >= minTransformValue ) { |
| 2640 | area.width = x - area.x + 1; |
| 2641 | break; |
| 2642 | } |
| 2643 | } |
| 2644 | |
| 2645 | if ( area.width >= 0 ) { |
| 2646 | break; |
| 2647 | } |
| 2648 | } |
| 2649 | |
| 2650 | return area; |
| 2651 | } |
| 2652 | |
| 2653 | uint8_t GetColorId( const uint8_t red, const uint8_t green, const uint8_t blue ) |
| 2654 | { |
| 2655 | return GetPALColorId( red / 4, green / 4, blue / 4 ); |
| 2656 | } |
| 2657 | |
| 2658 | Sprite makeShadow( const Sprite & in, const Point & shadowOffset, const uint8_t transformId ) |
| 2659 | { |
| 2660 | if ( in.empty() || shadowOffset.x > 0 || shadowOffset.y < 0 ) { |
| 2661 | return {}; |
| 2662 | } |
| 2663 | |
| 2664 | const int32_t width = in.width(); |
| 2665 | const int32_t height = in.height(); |
| 2666 | |
| 2667 | // Shadow has (-x, +y) offset. |
| 2668 | Sprite out( width - shadowOffset.x, height + shadowOffset.y, in.x() + shadowOffset.x, in.y() ); |
| 2669 | out.reset(); |
| 2670 | |
| 2671 | assert( !out.empty() ); |
| 2672 | |
| 2673 | if ( in.singleLayer() ) { |
| 2674 | // In this case we add a shadow of the fully non-transparent rectangular 'in' image. |
| 2675 | FillTransform( out, 0, shadowOffset.y, width, height, transformId ); |
| 2676 | |
| 2677 | return out; |
| 2678 | } |
| 2679 | |
| 2680 | const int32_t widthOut = out.width(); |
| 2681 | |
| 2682 | const uint8_t * transformInY = in.transform(); |
| 2683 | const uint8_t * transformInYEnd = transformInY + width * height; |
| 2684 | uint8_t * transformOutY = out.transform() + shadowOffset.y * widthOut; |
| 2685 | |
| 2686 | for ( ; transformInY != transformInYEnd; transformInY += width, transformOutY += widthOut ) { |
| 2687 | const uint8_t * transformInX = transformInY; |
| 2688 | uint8_t * transformOutX = transformOutY; |
| 2689 | const uint8_t * transformInXEnd = transformInX + width; |
| 2690 | |
| 2691 | for ( ; transformInX != transformInXEnd; ++transformInX, ++transformOutX ) { |
| 2692 | if ( *transformInX == 0 ) { |
| 2693 | *transformOutX = transformId; |
| 2694 | } |
| 2695 | } |
| 2696 | } |
| 2697 | |
| 2698 | return out; |
| 2699 | } |
| 2700 | |
| 2701 | void ReplaceColorId( Image & image, const uint8_t oldColorId, const uint8_t newColorId ) |
| 2702 | { |
| 2703 | if ( image.empty() ) { |
| 2704 | return; |
| 2705 | } |
| 2706 | |
| 2707 | uint8_t * data = image.image(); |
| 2708 | const uint8_t * dataEnd = data + image.width() * image.height(); |
| 2709 | |
| 2710 | for ( ; data != dataEnd; ++data ) { |
| 2711 | if ( *data == oldColorId ) { |
| 2712 | *data = newColorId; |
| 2713 | } |
| 2714 | } |
| 2715 | } |
| 2716 | |
| 2717 | void ReplaceColorIdByTransformId( Image & image, const uint8_t colorId, const uint8_t transformId ) |
| 2718 | { |
| 2719 | if ( transformId > 15 || image.singleLayer() ) { |
| 2720 | return; |
| 2721 | } |
| 2722 | |
| 2723 | const int32_t width = image.width(); |
| 2724 | const int32_t height = image.height(); |
| 2725 | |
| 2726 | const uint8_t * imageIn = image.image(); |
| 2727 | uint8_t * transformIn = image.transform(); |
| 2728 | const uint8_t * imageInEnd = imageIn + height * width; |
| 2729 | for ( ; imageIn != imageInEnd; ++imageIn, ++transformIn ) { |
| 2730 | if ( *transformIn == 0 && *imageIn == colorId ) { // modify pixels with transform value 0 |
| 2731 | *transformIn = transformId; |
| 2732 | } |
| 2733 | } |
| 2734 | } |
| 2735 | |
| 2736 | void ReplaceTransformIdByColorId( Image & image, const uint8_t transformId, const uint8_t colorId ) |
| 2737 | { |
| 2738 | if ( image.empty() || image.singleLayer() ) { |
| 2739 | return; |
| 2740 | } |
| 2741 | |
| 2742 | const int32_t size = image.width() * image.height(); |
| 2743 | |
| 2744 | uint8_t * imageIn = image.image(); |
| 2745 | uint8_t * transformIn = image.transform(); |
| 2746 | const uint8_t * imageInEnd = imageIn + size; |
| 2747 | for ( ; imageIn != imageInEnd; ++imageIn, ++transformIn ) { |
| 2748 | if ( *transformIn == transformId ) { |
| 2749 | *transformIn = 0U; |
| 2750 | *imageIn = colorId; |
| 2751 | } |
| 2752 | } |
| 2753 | } |
| 2754 | |
| 2755 | void Resize( const Image & in, Image & out ) |
| 2756 | { |
| 2757 | if ( in.empty() || out.empty() ) { |
| 2758 | return; |
| 2759 | } |
| 2760 | |
| 2761 | Resize( in, 0, 0, in.width(), in.height(), out, 0, 0, out.width(), out.height() ); |
| 2762 | } |
| 2763 | |
| 2764 | void Resize( const Image & in, const int32_t inX, const int32_t inY, const int32_t widthRoiIn, const int32_t heightRoiIn, Image & out, const int32_t outX, |
| 2765 | const int32_t outY, const int32_t widthRoiOut, const int32_t heightRoiOut ) |
| 2766 | { |
| 2767 | if ( !Validate( in, inX, inY, widthRoiIn, heightRoiIn ) || !Validate( out, outX, outY, widthRoiOut, heightRoiOut ) ) { |
| 2768 | return; |
| 2769 | } |
| 2770 | |
| 2771 | if ( widthRoiIn == widthRoiOut && heightRoiIn == heightRoiOut ) { |
| 2772 | Copy( in, inX, inY, out, outX, outY, widthRoiIn, heightRoiIn ); |
| 2773 | return; |
| 2774 | } |
| 2775 | |
| 2776 | const int32_t widthIn = in.width(); |
| 2777 | const int32_t widthOut = out.width(); |
| 2778 | |
| 2779 | const int32_t offsetInY = inY * widthIn + inX; |
| 2780 | const int32_t offsetOutY = outY * widthOut + outX; |
| 2781 | |
| 2782 | const uint8_t * imageInY = in.image() + offsetInY; |
| 2783 | uint8_t * imageOutY = out.image() + offsetOutY; |
| 2784 | |
| 2785 | const uint8_t * imageOutYEnd = imageOutY + static_cast<ptrdiff_t>( widthOut ) * heightRoiOut; |
| 2786 | int32_t idY = 0; |
| 2787 | |
| 2788 | // Pre-calculation of X position |
| 2789 | std::vector<int32_t> positionX( widthRoiOut ); |
| 2790 | for ( int32_t x = 0; x < widthRoiOut; ++x ) { |
| 2791 | positionX[x] = ( x * widthRoiIn ) / widthRoiOut; |
| 2792 | } |
| 2793 | |
| 2794 | if ( in.singleLayer() ) { |
| 2795 | if ( !out.singleLayer() ) { |
| 2796 | // In this case we make the output image fully non-transparent in the given output area. |
| 2797 | |
| 2798 | uint8_t * transformY = out.transform() + static_cast<ptrdiff_t>( outY ) * widthOut + outX; |
| 2799 | const uint8_t * transformYEnd = transformY + static_cast<ptrdiff_t>( heightRoiOut ) * widthOut; |
| 2800 | |
| 2801 | for ( ; transformY != transformYEnd; transformY += widthOut ) { |
| 2802 | memset( transformY, static_cast<uint8_t>( 0 ), widthRoiOut ); |
| 2803 | } |
| 2804 | } |
| 2805 | |
| 2806 | for ( ; imageOutY != imageOutYEnd; imageOutY += widthOut, ++idY ) { |
| 2807 | uint8_t * imageOutX = imageOutY; |
| 2808 | |
| 2809 | const int32_t offset = ( ( idY * heightRoiIn ) / heightRoiOut ) * widthIn; |
| 2810 | const uint8_t * imageInX = imageInY + offset; |
| 2811 | |
| 2812 | for ( const int32_t posX : positionX ) { |
| 2813 | *imageOutX = *( imageInX + posX ); |
| 2814 | ++imageOutX; |
| 2815 | } |
| 2816 | } |
| 2817 | } |
| 2818 | else if ( out.singleLayer() ) { |
| 2819 | const uint8_t * transformInY = in.transform() + offsetInY; |
| 2820 | |
| 2821 | for ( ; imageOutY != imageOutYEnd; imageOutY += widthOut, ++idY ) { |
| 2822 | uint8_t * imageOutX = imageOutY; |
| 2823 | |
| 2824 | const int32_t offset = ( ( idY * heightRoiIn ) / heightRoiOut ) * widthIn; |
| 2825 | const uint8_t * imageInX = imageInY + offset; |
| 2826 | const uint8_t * transformInX = transformInY + offset; |
| 2827 | |
| 2828 | for ( const int32_t posX : positionX ) { |
| 2829 | const uint8_t * transformIn = transformInX + posX; |
| 2830 | if ( *transformIn > 0 ) { |
| 2831 | if ( *transformIn != 1 ) { |
| 2832 | // Apply a transformation. |
| 2833 | *imageOutX = *( transformTable + static_cast<ptrdiff_t>( *transformIn ) * 256 + *imageOutX ); |
| 2834 | } |
| 2835 | } |
| 2836 | else { |
| 2837 | *imageOutX = *( imageInX + posX ); |
| 2838 | } |
| 2839 | |
| 2840 | ++imageOutX; |
| 2841 | } |
| 2842 | } |
| 2843 | } |
| 2844 | else { |
| 2845 | // Both 'in' and 'out' are double-layer. |
| 2846 | const uint8_t * transformInY = in.transform() + offsetInY; |
| 2847 | uint8_t * transformOutY = out.transform() + offsetOutY; |
| 2848 | |
| 2849 | for ( ; imageOutY != imageOutYEnd; imageOutY += widthOut, transformOutY += widthOut, ++idY ) { |
| 2850 | uint8_t * imageOutX = imageOutY; |
| 2851 | uint8_t * transformOutX = transformOutY; |
| 2852 | |
| 2853 | const int32_t offset = ( ( idY * heightRoiIn ) / heightRoiOut ) * widthIn; |
| 2854 | const uint8_t * imageInX = imageInY + offset; |
| 2855 | const uint8_t * transformInX = transformInY + offset; |
| 2856 | |
| 2857 | for ( const int32_t posX : positionX ) { |
| 2858 | *imageOutX = *( imageInX + posX ); |
| 2859 | *transformOutX = *( transformInX + posX ); |
| 2860 | ++imageOutX; |
| 2861 | ++transformOutX; |
| 2862 | } |
| 2863 | } |
| 2864 | } |
| 2865 | } |
| 2866 | |
| 2867 | void SetPixel( Image & image, const int32_t x, const int32_t y, const uint8_t value ) |
| 2868 | { |
| 2869 | if ( image.empty() || x >= image.width() || y >= image.height() || x < 0 || y < 0 ) { |
| 2870 | return; |
| 2871 | } |
| 2872 | |
| 2873 | const int32_t offset = y * image.width() + x; |
| 2874 | *( image.image() + offset ) = value; |
| 2875 | if ( !image.singleLayer() ) { |
| 2876 | *( image.transform() + offset ) = 0; |
| 2877 | } |
| 2878 | } |
| 2879 | |
| 2880 | void SetPixel( Image & image, const std::vector<Point> & points, const uint8_t value ) |
| 2881 | { |
| 2882 | if ( image.empty() ) { |
| 2883 | return; |
| 2884 | } |
| 2885 | |
| 2886 | const int32_t width = image.width(); |
| 2887 | const int32_t height = image.height(); |
| 2888 | const bool isDoubleLayer = !image.singleLayer(); |
| 2889 | |
| 2890 | for ( const Point & point : points ) { |
| 2891 | if ( point.x >= width || point.y >= height || point.x < 0 || point.y < 0 ) { |
| 2892 | continue; |
| 2893 | } |
| 2894 | |
| 2895 | const int32_t offset = point.y * width + point.x; |
| 2896 | *( image.image() + offset ) = value; |
| 2897 | if ( isDoubleLayer ) { |
| 2898 | *( image.transform() + offset ) = 0; |
| 2899 | } |
| 2900 | } |
| 2901 | } |
| 2902 | |
| 2903 | void SetTransformPixel( Image & image, const int32_t x, const int32_t y, const uint8_t value ) |
| 2904 | { |
| 2905 | if ( image.empty() || image.singleLayer() || x >= image.width() || y >= image.height() || x < 0 || y < 0 ) { |
| 2906 | return; |
| 2907 | } |
| 2908 | |
| 2909 | const int32_t offset = y * image.width() + x; |
| 2910 | *( image.image() + offset ) = 0; |
| 2911 | *( image.transform() + offset ) = value; |
| 2912 | } |
| 2913 | |
| 2914 | Image Stretch( const Image & in, int32_t inX, int32_t inY, int32_t widthIn, int32_t heightIn, const int32_t widthOut, const int32_t heightOut ) |
| 2915 | { |
| 2916 | if ( !Validate( in, inX, inY, widthIn, heightIn ) || widthOut <= 0 || heightOut <= 0 ) { |
| 2917 | return {}; |
| 2918 | } |
| 2919 | |
| 2920 | Image out; |
| 2921 | if ( in.singleLayer() ) { |
| 2922 | out._disableTransformLayer(); |
| 2923 | } |
| 2924 | out.resize( widthOut, heightOut ); |
| 2925 | |
| 2926 | const int32_t minWidth = widthIn < widthOut ? widthIn : widthOut; |
| 2927 | const int32_t minHeight = heightIn < heightOut ? heightIn : heightOut; |
| 2928 | |
| 2929 | const int32_t cornerWidth = minWidth / 3; |
| 2930 | const int32_t cornerHeight = minHeight / 3; |
| 2931 | const int32_t cornerX = inX + ( widthIn - cornerWidth ) / 2; |
| 2932 | const int32_t cornerY = inY + ( heightIn - cornerHeight ) / 2; |
| 2933 | const int32_t bodyWidth = minWidth - 2 * cornerWidth; |
| 2934 | const int32_t bodyHeight = minHeight - 2 * cornerHeight; |
| 2935 | |
| 2936 | const int32_t outX = ( widthOut - ( widthOut / bodyWidth ) * bodyWidth ) / 2; |
| 2937 | const int32_t outY = ( heightOut - ( heightOut / bodyHeight ) * bodyHeight ) / 2; |
| 2938 | |
| 2939 | // Create internal area |
| 2940 | if ( bodyWidth < widthOut && bodyHeight < heightOut ) { |
| 2941 | for ( int32_t y = 0; y < ( heightOut / bodyHeight ); ++y ) { |
| 2942 | for ( int32_t x = 0; x < ( widthOut / bodyWidth ); ++x ) { |
| 2943 | Copy( in, cornerX, cornerY, out, outX + x * bodyWidth, outY + y * bodyHeight, bodyWidth, bodyHeight ); |
| 2944 | } |
| 2945 | } |
| 2946 | } |
| 2947 | |
| 2948 | // Create sides |
| 2949 | // top and bottom side |
| 2950 | for ( int32_t x = 0; x < ( widthOut / bodyWidth ); ++x ) { |
| 2951 | const int32_t offsetX = outX + x * bodyWidth; |
| 2952 | Copy( in, cornerX, inY, out, offsetX, 0, bodyWidth, cornerHeight ); |
| 2953 | Copy( in, cornerX, inY + heightIn - cornerHeight, out, offsetX, heightOut - cornerHeight, bodyWidth, cornerHeight ); |
| 2954 | } |
| 2955 | |
| 2956 | // left and right sides |
| 2957 | for ( int32_t y = 0; y < ( heightOut / bodyHeight ); ++y ) { |
| 2958 | const int32_t offsetY = outY + y * bodyHeight; |
| 2959 | Copy( in, inX, cornerY, out, 0, offsetY, cornerWidth, bodyHeight ); |
| 2960 | Copy( in, inX + widthIn - cornerWidth, cornerY, out, widthOut - cornerWidth, offsetY, cornerWidth, bodyHeight ); |
| 2961 | } |
| 2962 | |
| 2963 | // Create corners |
| 2964 | // top-left corner |
| 2965 | Copy( in, inX, inY, out, 0, 0, cornerWidth, cornerHeight ); |
| 2966 | |
| 2967 | // top-right corner |
| 2968 | Copy( in, inX + widthIn - cornerWidth, inY, out, widthOut - cornerWidth, 0, cornerWidth, cornerHeight ); |
| 2969 | |
| 2970 | // bottom-left corner |
| 2971 | Copy( in, inX, inY + heightIn - cornerHeight, out, 0, heightOut - cornerHeight, cornerWidth, cornerHeight ); |
| 2972 | |
| 2973 | // bottom-right corner |
| 2974 | Copy( in, inX + widthIn - cornerWidth, inY + heightIn - cornerHeight, out, widthOut - cornerWidth, heightOut - cornerHeight, cornerWidth, cornerHeight ); |
| 2975 | |
| 2976 | return out; |
| 2977 | } |
| 2978 | |
| 2979 | void SubpixelResize( const Image & in, Image & out ) |
| 2980 | { |
| 2981 | if ( in.empty() || out.empty() ) { |
| 2982 | return; |
| 2983 | } |
| 2984 | |
| 2985 | SubpixelResize( in, 0, 0, in.width(), in.height(), out, 0, 0, out.width(), out.height() ); |
| 2986 | } |
| 2987 | |
| 2988 | void SubpixelResize( const Image & in, const int32_t inX, const int32_t inY, const int32_t widthRoiIn, const int32_t heightRoiIn, Image & out, const int32_t outX, |
| 2989 | const int32_t outY, const int32_t widthRoiOut, const int32_t heightRoiOut ) |
| 2990 | { |
| 2991 | if ( !Validate( in, inX, inY, widthRoiIn, heightRoiIn ) || !Validate( out, outX, outY, widthRoiOut, heightRoiOut ) ) { |
| 2992 | return; |
| 2993 | } |
| 2994 | |
| 2995 | if ( widthRoiIn == widthRoiOut && heightRoiIn == heightRoiOut ) { |
| 2996 | Copy( in, inX, inY, out, outX, outY, widthRoiIn, heightRoiIn ); |
| 2997 | return; |
| 2998 | } |
| 2999 | |
| 3000 | const int32_t widthIn = in.width(); |
| 3001 | const int32_t widthOut = out.width(); |
| 3002 | |
| 3003 | const int32_t offsetInY = inY * widthIn + inX; |
| 3004 | const int32_t offsetOutY = outY * widthOut + outX; |
| 3005 | |
| 3006 | const uint8_t * imageInY = in.image() + offsetInY; |
| 3007 | uint8_t * imageOutY = out.image() + offsetOutY; |
| 3008 | |
| 3009 | std::vector<double> positionX( widthRoiOut ); |
| 3010 | for ( int32_t x = 0; x < widthRoiOut; ++x ) { |
| 3011 | positionX[x] = static_cast<double>( x * widthRoiIn ) / widthRoiOut; |
| 3012 | } |
| 3013 | |
| 3014 | const uint8_t * gamePalette = getGamePalette(); |
| 3015 | |
| 3016 | if ( in.singleLayer() ) { |
| 3017 | if ( !out.singleLayer() ) { |
| 3018 | // In this case we make the output image fully non-transparent in the given output area. |
| 3019 | |
| 3020 | uint8_t * transformY = out.transform() + static_cast<ptrdiff_t>( outY ) * widthOut + outX; |
| 3021 | const uint8_t * transformYEnd = transformY + static_cast<ptrdiff_t>( heightRoiOut ) * widthOut; |
| 3022 | |
| 3023 | for ( ; transformY != transformYEnd; transformY += widthOut ) { |
| 3024 | memset( transformY, static_cast<uint8_t>( 0 ), widthRoiOut ); |
| 3025 | } |
| 3026 | } |
| 3027 | |
| 3028 | for ( int32_t y = 0; y < heightRoiOut; ++y, imageOutY += widthOut ) { |
| 3029 | const double posY = static_cast<double>( y * heightRoiIn ) / heightRoiOut; |
| 3030 | const int32_t startY = static_cast<int32_t>( posY ) * widthIn; |
| 3031 | const double coeffY = posY - static_cast<int32_t>( posY ); |
| 3032 | |
| 3033 | uint8_t * imageOutX = imageOutY; |
| 3034 | |
| 3035 | for ( int32_t x = 0; x < widthRoiOut; ++x, ++imageOutX ) { |
| 3036 | const double posX = positionX[x]; |
| 3037 | const int32_t startX = static_cast<int32_t>( posX ); |
| 3038 | const int32_t offsetIn = startY + startX; |
| 3039 | |
| 3040 | const uint8_t * imageInX = imageInY + offsetIn; |
| 3041 | |
| 3042 | if ( posX < widthRoiIn - 1 && posY < heightRoiIn - 1 ) { |
| 3043 | const double coeffX = posX - startX; |
| 3044 | const double coeff1 = ( 1 - coeffX ) * ( 1 - coeffY ); |
| 3045 | const double coeff2 = coeffX * ( 1 - coeffY ); |
| 3046 | const double coeff3 = ( 1 - coeffX ) * coeffY; |
| 3047 | const double coeff4 = coeffX * coeffY; |
| 3048 | |
| 3049 | const uint8_t * id1 = gamePalette + static_cast<size_t>( *imageInX ) * 3; |
| 3050 | const uint8_t * id2 = gamePalette + static_cast<size_t>( *( imageInX + 1 ) ) * 3; |
| 3051 | const uint8_t * id3 = gamePalette + static_cast<size_t>( *( imageInX + widthIn ) ) * 3; |
| 3052 | const uint8_t * id4 = gamePalette + static_cast<size_t>( *( imageInX + widthIn + 1 ) ) * 3; |
| 3053 | |
| 3054 | const double red = *id1 * coeff1 + *id2 * coeff2 + *id3 * coeff3 + *id4 * coeff4 + 0.5; |
| 3055 | const double green = *( id1 + 1 ) * coeff1 + *( id2 + 1 ) * coeff2 + *( id3 + 1 ) * coeff3 + *( id4 + 1 ) * coeff4 + 0.5; |
| 3056 | const double blue = *( id1 + 2 ) * coeff1 + *( id2 + 2 ) * coeff2 + *( id3 + 2 ) * coeff3 + *( id4 + 2 ) * coeff4 + 0.5; |
| 3057 | |
| 3058 | *imageOutX = GetPALColorId( static_cast<uint8_t>( red ), static_cast<uint8_t>( green ), static_cast<uint8_t>( blue ) ); |
| 3059 | } |
| 3060 | else { |
| 3061 | *imageOutX = *imageInX; |
| 3062 | } |
| 3063 | } |
| 3064 | } |
| 3065 | } |
| 3066 | else { |
| 3067 | const uint8_t * transformInY = in.transform() + offsetInY; |
| 3068 | const bool isOutNotSingleLayer = !out.singleLayer(); |
| 3069 | uint8_t * transformOutY = isOutNotSingleLayer ? ( out.transform() + offsetOutY ) : nullptr; |
| 3070 | |
| 3071 | for ( int32_t y = 0; y < heightRoiOut; ++y, imageOutY += widthOut ) { |
| 3072 | const double posY = static_cast<double>( y * heightRoiIn ) / heightRoiOut; |
| 3073 | const int32_t startY = static_cast<int32_t>( posY ) * widthIn; |
| 3074 | const double coeffY = posY - static_cast<int32_t>( posY ); |
| 3075 | |
| 3076 | uint8_t * imageOutX = imageOutY; |
| 3077 | uint8_t * transformOutX = transformOutY; |
| 3078 | |
| 3079 | for ( int32_t x = 0; x < widthRoiOut; ++x, ++imageOutX ) { |
| 3080 | const double posX = positionX[x]; |
| 3081 | const int32_t startX = static_cast<int32_t>( posX ); |
| 3082 | const int32_t offsetIn = startY + startX; |
| 3083 | |
| 3084 | const uint8_t * imageInX = imageInY + offsetIn; |
| 3085 | const uint8_t * transformInX = transformInY + offsetIn; |
| 3086 | |
| 3087 | if ( posX < widthRoiIn - 1 && posY < heightRoiIn - 1 && *transformInX == 0 |
| 3088 | && ( *( transformInX + 1 ) == 0 || *( transformInX + widthRoiIn ) == 0 ) ) { |
| 3089 | if ( *( transformInX + 1 ) == 0 && *( transformInX + widthRoiIn ) == 0 && *( transformInX + widthRoiIn + 1 ) == 0 ) { |
| 3090 | const double coeffX = posX - startX; |
| 3091 | const double coeff1 = ( 1 - coeffX ) * ( 1 - coeffY ); |
| 3092 | const double coeff2 = coeffX * ( 1 - coeffY ); |
| 3093 | const double coeff3 = ( 1 - coeffX ) * coeffY; |
| 3094 | const double coeff4 = coeffX * coeffY; |
| 3095 | |
| 3096 | const uint8_t * id1 = gamePalette + static_cast<size_t>( *imageInX ) * 3; |
| 3097 | const uint8_t * id2 = gamePalette + static_cast<size_t>( *( imageInX + 1 ) ) * 3; |
| 3098 | const uint8_t * id3 = gamePalette + static_cast<size_t>( *( imageInX + widthIn ) ) * 3; |
| 3099 | const uint8_t * id4 = gamePalette + static_cast<size_t>( *( imageInX + widthIn + 1 ) ) * 3; |
| 3100 | |
| 3101 | const double red = *id1 * coeff1 + *id2 * coeff2 + *id3 * coeff3 + *id4 * coeff4 + 0.5; |
| 3102 | const double green = *( id1 + 1 ) * coeff1 + *( id2 + 1 ) * coeff2 + *( id3 + 1 ) * coeff3 + *( id4 + 1 ) * coeff4 + 0.5; |
| 3103 | const double blue = *( id1 + 2 ) * coeff1 + *( id2 + 2 ) * coeff2 + *( id3 + 2 ) * coeff3 + *( id4 + 2 ) * coeff4 + 0.5; |
| 3104 | |
| 3105 | *imageOutX = GetPALColorId( static_cast<uint8_t>( red ), static_cast<uint8_t>( green ), static_cast<uint8_t>( blue ) ); |
| 3106 | } |
| 3107 | else if ( *( transformInX + 1 ) != 0 && *( transformInX + widthRoiIn ) == 0 ) { |
| 3108 | // The pixel to the right is transparent, do only vertical interpolation. |
| 3109 | const double coeff1 = 1 - coeffY; |
| 3110 | |
| 3111 | const uint8_t * id1 = gamePalette + static_cast<size_t>( *imageInX ) * 3; |
| 3112 | const uint8_t * id3 = gamePalette + static_cast<size_t>( *( imageInX + widthIn ) ) * 3; |
| 3113 | |
| 3114 | const double red = *id1 * coeff1 + *id3 * coeffY + 0.5; |
| 3115 | const double green = *( id1 + 1 ) * coeff1 + *( id3 + 1 ) * coeffY + 0.5; |
| 3116 | const double blue = *( id1 + 2 ) * coeff1 + *( id3 + 2 ) * coeffY + 0.5; |
| 3117 | |
| 3118 | *imageOutX = GetPALColorId( static_cast<uint8_t>( red ), static_cast<uint8_t>( green ), static_cast<uint8_t>( blue ) ); |
| 3119 | } |
| 3120 | else if ( *( transformInX + 1 ) == 0 && *( transformInX + widthRoiIn ) != 0 ) { |
| 3121 | // The pixel to the bottom is transparent, do only horizontal interpolation. |
| 3122 | const double coeff2 = posX - startX; |
| 3123 | const double coeff1 = 1 - coeff2; |
| 3124 | |
| 3125 | const uint8_t * id1 = gamePalette + static_cast<size_t>( *imageInX ) * 3; |
| 3126 | const uint8_t * id2 = gamePalette + static_cast<size_t>( *( imageInX + 1 ) ) * 3; |
| 3127 | |
| 3128 | const double red = *id1 * coeff1 + *id2 * coeff2 + 0.5; |
| 3129 | const double green = *( id1 + 1 ) * coeff1 + *( id2 + 1 ) * coeff2 + 0.5; |
| 3130 | const double blue = *( id1 + 2 ) * coeff1 + *( id2 + 2 ) * coeff2 + 0.5; |
| 3131 | |
| 3132 | *imageOutX = GetPALColorId( static_cast<uint8_t>( red ), static_cast<uint8_t>( green ), static_cast<uint8_t>( blue ) ); |
| 3133 | } |
| 3134 | else if ( *( transformInX + 1 ) == 0 && *( transformInX + widthRoiIn ) == 0 && *( transformInX + widthRoiIn + 1 ) != 0 ) { |
| 3135 | // Interpolation by three pixels: current, the right one and the bottom one. |
| 3136 | const double coeffX = posX - startX; |
| 3137 | double coeff1 = ( 1 - coeffX ) * ( 1 - coeffY ); |
| 3138 | double coeff2 = coeffX * ( 1 - coeffY ); |
| 3139 | double coeff3 = ( 1 - coeffX ) * coeffY; |
| 3140 | const double coeffSumm = coeff1 + coeff2 + coeff3; |
| 3141 | coeff1 /= coeffSumm; |
| 3142 | coeff2 /= coeffSumm; |
| 3143 | coeff3 /= coeffSumm; |
| 3144 | |
| 3145 | const uint8_t * id1 = gamePalette + static_cast<size_t>( *imageInX ) * 3; |
| 3146 | const uint8_t * id2 = gamePalette + static_cast<size_t>( *( imageInX + 1 ) ) * 3; |
| 3147 | const uint8_t * id3 = gamePalette + static_cast<size_t>( *( imageInX + widthIn ) ) * 3; |
| 3148 | |
| 3149 | const double red = *id1 * coeff1 + *id2 * coeff2 + *id3 * coeff3 + 0.5; |
| 3150 | const double green = *( id1 + 1 ) * coeff1 + *( id2 + 1 ) * coeff2 + *( id3 + 1 ) * coeff3 + 0.5; |
| 3151 | const double blue = *( id1 + 2 ) * coeff1 + *( id2 + 2 ) * coeff2 + *( id3 + 2 ) * coeff3 + 0.5; |
| 3152 | |
| 3153 | *imageOutX = GetPALColorId( static_cast<uint8_t>( red ), static_cast<uint8_t>( green ), static_cast<uint8_t>( blue ) ); |
| 3154 | } |
| 3155 | } |
| 3156 | else { |
| 3157 | if ( isOutNotSingleLayer || *transformInX == 0 ) { |
| 3158 | // Output image is double-layer or single-layer with non-transparent current pixel. |
| 3159 | *imageOutX = *imageInX; |
| 3160 | } |
| 3161 | else if ( *transformInX != 1 ) { |
| 3162 | // Apply a transformation. |
| 3163 | *imageOutX = *( transformTable + static_cast<ptrdiff_t>( *transformInX ) * 256 + *imageOutX ); |
| 3164 | } |
| 3165 | } |
| 3166 | |
| 3167 | if ( isOutNotSingleLayer ) { |
| 3168 | *transformOutX = *transformInX; |
| 3169 | ++transformOutX; |
| 3170 | } |
| 3171 | } |
| 3172 | |
| 3173 | if ( isOutNotSingleLayer ) { |
| 3174 | transformOutY += widthOut; |
| 3175 | } |
| 3176 | } |
| 3177 | } |
| 3178 | } |
| 3179 | |
| 3180 | void Transpose( const Image & in, Image & out ) |
| 3181 | { |
| 3182 | assert( !out.empty() ); |
| 3183 | |
| 3184 | if ( in.empty() || in.width() != out.height() || in.height() != out.width() ) { |
| 3185 | out.reset(); |
| 3186 | return; |
| 3187 | } |
| 3188 | const int32_t width = in.width(); |
| 3189 | const int32_t height = in.height(); |
| 3190 | |
| 3191 | const uint8_t * imageInY = in.image(); |
| 3192 | const uint8_t * imageInYEnd = imageInY + width * height; |
| 3193 | uint8_t * imageOutX = out.image(); |
| 3194 | |
| 3195 | if ( in.singleLayer() ) { |
| 3196 | if ( out.singleLayer() ) { |
| 3197 | for ( ; imageInY != imageInYEnd; imageInY += width, ++imageOutX ) { |
| 3198 | const uint8_t * imageInX = imageInY; |
| 3199 | const uint8_t * imageInXEnd = imageInX + width; |
| 3200 | uint8_t * imageOutY = imageOutX; |
| 3201 | |
| 3202 | for ( ; imageInX != imageInXEnd; ++imageInX, imageOutY += height ) { |
| 3203 | *imageOutY = *imageInX; |
| 3204 | } |
| 3205 | } |
| 3206 | } |
| 3207 | else { |
| 3208 | uint8_t * transformOutX = out.transform(); |
| 3209 | |
| 3210 | for ( ; imageInY != imageInYEnd; imageInY += width, ++imageOutX, ++transformOutX ) { |
| 3211 | const uint8_t * imageInX = imageInY; |
| 3212 | const uint8_t * imageInXEnd = imageInX + width; |
| 3213 | uint8_t * imageOutY = imageOutX; |
| 3214 | |
| 3215 | uint8_t * transformOutY = transformOutX; |
| 3216 | for ( ; imageInX != imageInXEnd; ++imageInX, imageOutY += height, transformOutY += height ) { |
| 3217 | *imageOutY = *imageInX; |
| 3218 | *transformOutY = 0; |
| 3219 | } |
| 3220 | } |
| 3221 | } |
| 3222 | } |
| 3223 | else { |
| 3224 | const uint8_t * transformInY = in.transform(); |
| 3225 | |
| 3226 | if ( out.singleLayer() ) { |
| 3227 | for ( ; imageInY != imageInYEnd; imageInY += width, transformInY += width, ++imageOutX ) { |
| 3228 | const uint8_t * imageInX = imageInY; |
| 3229 | const uint8_t * imageInXEnd = imageInX + width; |
| 3230 | uint8_t * imageOutY = imageOutX; |
| 3231 | |
| 3232 | const uint8_t * transformInX = transformInY; |
| 3233 | for ( ; imageInX != imageInXEnd; ++imageInX, ++transformInX, imageOutY += height ) { |
| 3234 | // Copy image data only for non-transparent pixels. |
| 3235 | if ( *transformInX == 0 ) { |
| 3236 | *imageOutY = *imageInX; |
| 3237 | } |
| 3238 | } |
| 3239 | } |
| 3240 | } |
| 3241 | else { |
| 3242 | uint8_t * transformOutX = out.transform(); |
| 3243 | |
| 3244 | for ( ; imageInY != imageInYEnd; imageInY += width, transformInY += width, ++imageOutX, ++transformOutX ) { |
| 3245 | const uint8_t * imageInX = imageInY; |
| 3246 | const uint8_t * imageInXEnd = imageInX + width; |
| 3247 | uint8_t * imageOutY = imageOutX; |
| 3248 | |
| 3249 | const uint8_t * transformInX = transformInY; |
| 3250 | uint8_t * transformOutY = transformOutX; |
| 3251 | for ( ; imageInX != imageInXEnd; ++imageInX, ++transformInX, imageOutY += height, transformOutY += height ) { |
| 3252 | *imageOutY = *imageInX; |
| 3253 | *transformOutY = *transformInX; |
| 3254 | } |
| 3255 | } |
| 3256 | } |
| 3257 | } |
| 3258 | } |
| 3259 | |
| 3260 | void updateShadow( Image & image, const Point & shadowOffset, const uint8_t transformId, const bool connectCorners ) |
| 3261 | { |
| 3262 | const int32_t imageWidth = image.width(); |
| 3263 | const int32_t imageHeight = image.height(); |
| 3264 | |
| 3265 | if ( image.empty() || image.singleLayer() || ( std::abs( shadowOffset.x ) >= imageWidth ) || ( std::abs( shadowOffset.y ) >= imageHeight ) |
| 3266 | || shadowOffset == Point() ) { |
| 3267 | return; |
| 3268 | } |
| 3269 | |
| 3270 | const int32_t width = imageWidth - std::abs( shadowOffset.x ); |
| 3271 | const int32_t height = imageHeight - std::abs( shadowOffset.y ); |
| 3272 | |
| 3273 | const uint8_t * transformInY = image.transform(); |
| 3274 | uint8_t * transformOutY = image.transform(); |
| 3275 | |
| 3276 | int32_t cornerOffsetX; |
| 3277 | int32_t cornerOffsetY; |
| 3278 | |
| 3279 | if ( shadowOffset.x > 0 ) { |
| 3280 | transformOutY += shadowOffset.x; |
| 3281 | cornerOffsetX = 1; |
| 3282 | } |
| 3283 | else { |
| 3284 | transformInY -= shadowOffset.x; |
| 3285 | cornerOffsetX = -1; |
| 3286 | } |
| 3287 | |
| 3288 | if ( shadowOffset.y > 0 ) { |
| 3289 | transformOutY += imageWidth * shadowOffset.y; |
| 3290 | cornerOffsetY = imageWidth; |
| 3291 | } |
| 3292 | else { |
| 3293 | transformInY -= imageWidth * shadowOffset.y; |
| 3294 | cornerOffsetY = -imageWidth; |
| 3295 | } |
| 3296 | |
| 3297 | const uint8_t * transformOutYEnd = transformOutY + imageWidth * height; |
| 3298 | |
| 3299 | for ( ; transformOutY != transformOutYEnd; transformInY += imageWidth, transformOutY += imageWidth ) { |
| 3300 | const uint8_t * transformInX = transformInY; |
| 3301 | uint8_t * transformOutX = transformOutY; |
| 3302 | const uint8_t * transformOutXEnd = transformOutX + width; |
| 3303 | |
| 3304 | for ( ; transformOutX != transformOutXEnd; ++transformInX, ++transformOutX ) { |
| 3305 | if ( *transformOutX == 1 |
| 3306 | && ( *transformInX == 0 || ( connectCorners && *( transformInX + cornerOffsetX ) == 0 && *( transformInX + cornerOffsetY ) == 0 ) ) ) { |
| 3307 | // If 'connectCorners' is 'true' and when there are two pixels adjacent diagonally, |
| 3308 | // we also create a "shadow" pixel in the corner that is closer to the image. |
| 3309 | // Doing so there will be no "empty" pixels between the image and its shadow (for 1 pixel offset case). |
| 3310 | *transformOutX = transformId; |
| 3311 | } |
| 3312 | } |
| 3313 | } |
| 3314 | } |
| 3315 | } |