Nataniel Borges | b810df4 | 2019-02-21 10:54:00 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019, The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Vishnu Nair | 8175a12 | 2019-03-06 13:37:40 -0800 | [diff] [blame] | 17 | /* transform type flags */ |
| 18 | const TRANSLATE_VAL = 0x0001; |
| 19 | const ROTATE_VAL = 0x0002; |
| 20 | const SCALE_VAL = 0x0004; |
Nataniel Borges | b810df4 | 2019-02-21 10:54:00 -0800 | [diff] [blame] | 21 | |
Vishnu Nair | 8175a12 | 2019-03-06 13:37:40 -0800 | [diff] [blame] | 22 | /* orientation flags */ |
| 23 | const FLIP_H_VAL = 0x0100; // (1 << 0 << 8) |
| 24 | const FLIP_V_VAL = 0x0200; // (1 << 1 << 8) |
| 25 | const ROT_90_VAL = 0x0400; // (1 << 2 << 8) |
| 26 | const ROT_INVALID_VAL = 0x8000; // (0x80 << 8) |
Nataniel Borges | b810df4 | 2019-02-21 10:54:00 -0800 | [diff] [blame] | 27 | |
Vishnu Nair | 13f8d14 | 2019-03-19 17:10:48 -0700 | [diff] [blame] | 28 | function is_proto_2(transform) { |
| 29 | /* |
| 30 | * Checks if the loaded file was a stored with ProtoBuf2 or Protobuf3 |
| 31 | * |
| 32 | * Proto2 files don't have a Type for the transform object but all other |
| 33 | * fields of the transform are set. |
| 34 | * |
| 35 | * Proto3 has a type field for the transform but doesn't store default |
| 36 | * values (0 for transform type), also, the framework/native implementation |
| 37 | * doesn't write a transform in case it is an identity matrix. |
| 38 | */ |
| 39 | var propertyNames = Object.getOwnPropertyNames(transform); |
| 40 | return (!propertyNames.includes("type") && propertyNames.includes("dsdx")); |
| 41 | } |
| 42 | |
Vishnu Nair | 8175a12 | 2019-03-06 13:37:40 -0800 | [diff] [blame] | 43 | function is_simple_transform(transform) { |
Vishnu Nair | 13f8d14 | 2019-03-19 17:10:48 -0700 | [diff] [blame] | 44 | transform = transform || {}; |
| 45 | if (is_proto_2(transform)) { |
| 46 | return false; |
| 47 | } |
Vishnu Nair | 8175a12 | 2019-03-06 13:37:40 -0800 | [diff] [blame] | 48 | return is_type_flag_clear(transform, ROT_INVALID_VAL|SCALE_VAL); |
Nataniel Borges | b810df4 | 2019-02-21 10:54:00 -0800 | [diff] [blame] | 49 | } |
| 50 | |
Vishnu Nair | 8175a12 | 2019-03-06 13:37:40 -0800 | [diff] [blame] | 51 | /** |
| 52 | * Converts a transform type into readable format. |
| 53 | * Adapted from the dump function from framework/native |
| 54 | * |
| 55 | * @param {*} transform Transform object ot be converter |
| 56 | */ |
| 57 | function format_transform_type(transform) { |
Vishnu Nair | 13f8d14 | 2019-03-19 17:10:48 -0700 | [diff] [blame] | 58 | if (is_proto_2(transform)) { |
| 59 | return ""; |
| 60 | } |
| 61 | |
Vishnu Nair | 8175a12 | 2019-03-06 13:37:40 -0800 | [diff] [blame] | 62 | if (is_type_flag_clear(transform, SCALE_VAL | ROTATE_VAL | TRANSLATE_VAL)) { |
| 63 | return "IDENTITY"; |
| 64 | } |
| 65 | |
| 66 | var type_flags = []; |
| 67 | if (is_type_flag_set(transform, SCALE_VAL)) { |
| 68 | type_flags.push("SCALE"); |
| 69 | } |
| 70 | if (is_type_flag_set(transform, TRANSLATE_VAL)) { |
| 71 | type_flags.push("TRANSLATE"); |
| 72 | } |
| 73 | |
| 74 | if (is_type_flag_set(transform, ROT_INVALID_VAL)) { |
| 75 | type_flags.push("ROT_INVALID"); |
| 76 | } else if (is_type_flag_set(transform, ROT_90_VAL|FLIP_V_VAL|FLIP_H_VAL)) { |
| 77 | type_flags.push("ROT_270"); |
| 78 | } else if (is_type_flag_set(transform, FLIP_V_VAL|FLIP_H_VAL)) { |
| 79 | type_flags.push("ROT_180"); |
| 80 | } else { |
| 81 | if (is_type_flag_set(transform, ROT_90_VAL)) { |
| 82 | type_flags.push("ROT_90"); |
Nataniel Borges | b810df4 | 2019-02-21 10:54:00 -0800 | [diff] [blame] | 83 | } |
Vishnu Nair | 8175a12 | 2019-03-06 13:37:40 -0800 | [diff] [blame] | 84 | if (is_type_flag_set(transform, FLIP_V_VAL)) { |
| 85 | type_flags.push("FLIP_V"); |
Nataniel Borges | b810df4 | 2019-02-21 10:54:00 -0800 | [diff] [blame] | 86 | } |
Vishnu Nair | 8175a12 | 2019-03-06 13:37:40 -0800 | [diff] [blame] | 87 | if (is_type_flag_set(transform, FLIP_H_VAL)) { |
| 88 | type_flags.push("FLIP_H"); |
Nataniel Borges | b810df4 | 2019-02-21 10:54:00 -0800 | [diff] [blame] | 89 | } |
Vishnu Nair | 8175a12 | 2019-03-06 13:37:40 -0800 | [diff] [blame] | 90 | } |
Nataniel Borges | b810df4 | 2019-02-21 10:54:00 -0800 | [diff] [blame] | 91 | |
Vishnu Nair | 8175a12 | 2019-03-06 13:37:40 -0800 | [diff] [blame] | 92 | if (type_flags.length == 0) { |
| 93 | throw "Unknown transform type " + transform ; |
| 94 | } |
Nataniel Borges | b810df4 | 2019-02-21 10:54:00 -0800 | [diff] [blame] | 95 | |
Vishnu Nair | 8175a12 | 2019-03-06 13:37:40 -0800 | [diff] [blame] | 96 | return type_flags.join(', '); |
Nataniel Borges | b810df4 | 2019-02-21 10:54:00 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Nataniel Borges | b810df4 | 2019-02-21 10:54:00 -0800 | [diff] [blame] | 99 | |
Vishnu Nair | 8175a12 | 2019-03-06 13:37:40 -0800 | [diff] [blame] | 100 | /** |
| 101 | * Ensures all values of the transform object are set. |
| 102 | */ |
| 103 | function fill_transform_data(transform) { |
| 104 | function fill_simple_transform(transform) { |
Nataniel Borges | b810df4 | 2019-02-21 10:54:00 -0800 | [diff] [blame] | 105 | // ROT_270 = ROT_90|FLIP_H|FLIP_V; |
Vishnu Nair | 8175a12 | 2019-03-06 13:37:40 -0800 | [diff] [blame] | 106 | if (is_type_flag_set(transform, ROT_90_VAL|FLIP_V_VAL|FLIP_H_VAL)) { |
| 107 | transform.dsdx = 0.0; |
| 108 | transform.dtdx = -1.0; |
| 109 | transform.dsdy = 1.0; |
| 110 | transform.dtdy = 0.0; |
| 111 | return; |
Nataniel Borges | b810df4 | 2019-02-21 10:54:00 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | // ROT_180 = FLIP_H|FLIP_V; |
Vishnu Nair | 8175a12 | 2019-03-06 13:37:40 -0800 | [diff] [blame] | 115 | if (is_type_flag_set(transform, FLIP_V_VAL|FLIP_H_VAL)) { |
| 116 | transform.dsdx = -1.0; |
| 117 | transform.dtdx = 0.0; |
| 118 | transform.dsdy = 0.0; |
| 119 | transform.dtdy = -1.0; |
| 120 | return; |
Nataniel Borges | b810df4 | 2019-02-21 10:54:00 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | // ROT_90 |
Vishnu Nair | 8175a12 | 2019-03-06 13:37:40 -0800 | [diff] [blame] | 124 | if (is_type_flag_set(transform, ROT_90_VAL)) { |
| 125 | transform.dsdx = 0.0; |
| 126 | transform.dtdx = 1.0; |
| 127 | transform.dsdy = -1.0; |
| 128 | transform.dtdy = 0.0; |
| 129 | return; |
Nataniel Borges | b810df4 | 2019-02-21 10:54:00 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Vishnu Nair | 8175a12 | 2019-03-06 13:37:40 -0800 | [diff] [blame] | 132 | // IDENTITY |
| 133 | if (is_type_flag_clear(transform, SCALE_VAL | ROTATE_VAL)) { |
| 134 | transform.dsdx = 1.0; |
| 135 | transform.dtdx = 0.0; |
| 136 | transform.dsdy = 0.0; |
| 137 | transform.dtdy = 1.0; |
Vishnu Nair | 4c71de2 | 2019-04-19 10:18:08 -0700 | [diff] [blame^] | 138 | transform.type = 0; |
Vishnu Nair | 8175a12 | 2019-03-06 13:37:40 -0800 | [diff] [blame] | 139 | return; |
Nataniel Borges | b810df4 | 2019-02-21 10:54:00 -0800 | [diff] [blame] | 140 | } |
| 141 | |
Vishnu Nair | 8175a12 | 2019-03-06 13:37:40 -0800 | [diff] [blame] | 142 | throw "Unknown transform type " + transform; |
| 143 | } |
| 144 | |
| 145 | if (!transform) { |
| 146 | return; |
| 147 | } |
| 148 | |
Vishnu Nair | 13f8d14 | 2019-03-19 17:10:48 -0700 | [diff] [blame] | 149 | if (is_proto_2(transform)) { |
Vishnu Nair | 8175a12 | 2019-03-06 13:37:40 -0800 | [diff] [blame] | 150 | return; |
| 151 | } |
| 152 | |
| 153 | if (is_simple_transform(transform)){ |
| 154 | fill_simple_transform(transform); |
| 155 | } |
| 156 | |
| 157 | transform.dsdx = transform.dsdx || 0.0; |
| 158 | transform.dtdx = transform.dtdx || 0.0; |
| 159 | transform.dsdy = transform.dsdy || 0.0; |
| 160 | transform.dtdy = transform.dtdy || 0.0; |
Nataniel Borges | b810df4 | 2019-02-21 10:54:00 -0800 | [diff] [blame] | 161 | } |
| 162 | |
Vishnu Nair | 8175a12 | 2019-03-06 13:37:40 -0800 | [diff] [blame] | 163 | function is_type_flag_set(transform, bits) { |
| 164 | transform = transform || {}; |
| 165 | var type = transform.type || 0; |
| 166 | return (type & bits) === bits; |
| 167 | } |
| 168 | |
| 169 | function is_type_flag_clear(transform, bits) { |
| 170 | transform = transform || {}; |
| 171 | var type = transform.type || 0; |
| 172 | return (type & bits) === 0; |
| 173 | } |
| 174 | |
| 175 | export {format_transform_type, fill_transform_data, is_simple_transform}; |