blob: 6d7d44fdb17930bd889ea3b5dba2ab3d8db8f0a0 [file] [log] [blame]
Nataniel Borgesb810df42019-02-21 10:54:00 -08001/*
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 Nair8175a122019-03-06 13:37:40 -080017/* transform type flags */
18const TRANSLATE_VAL = 0x0001;
19const ROTATE_VAL = 0x0002;
20const SCALE_VAL = 0x0004;
Nataniel Borgesb810df42019-02-21 10:54:00 -080021
Vishnu Nair8175a122019-03-06 13:37:40 -080022/* orientation flags */
23const FLIP_H_VAL = 0x0100; // (1 << 0 << 8)
24const FLIP_V_VAL = 0x0200; // (1 << 1 << 8)
25const ROT_90_VAL = 0x0400; // (1 << 2 << 8)
26const ROT_INVALID_VAL = 0x8000; // (0x80 << 8)
Nataniel Borgesb810df42019-02-21 10:54:00 -080027
Vishnu Nair13f8d142019-03-19 17:10:48 -070028function 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 Nair8175a122019-03-06 13:37:40 -080043function is_simple_transform(transform) {
Vishnu Nair13f8d142019-03-19 17:10:48 -070044 transform = transform || {};
45 if (is_proto_2(transform)) {
46 return false;
47 }
Vishnu Nair8175a122019-03-06 13:37:40 -080048 return is_type_flag_clear(transform, ROT_INVALID_VAL|SCALE_VAL);
Nataniel Borgesb810df42019-02-21 10:54:00 -080049}
50
Vishnu Nair8175a122019-03-06 13:37:40 -080051/**
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 */
57function format_transform_type(transform) {
Vishnu Nair13f8d142019-03-19 17:10:48 -070058 if (is_proto_2(transform)) {
59 return "";
60 }
61
Vishnu Nair8175a122019-03-06 13:37:40 -080062 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 Borgesb810df42019-02-21 10:54:00 -080083 }
Vishnu Nair8175a122019-03-06 13:37:40 -080084 if (is_type_flag_set(transform, FLIP_V_VAL)) {
85 type_flags.push("FLIP_V");
Nataniel Borgesb810df42019-02-21 10:54:00 -080086 }
Vishnu Nair8175a122019-03-06 13:37:40 -080087 if (is_type_flag_set(transform, FLIP_H_VAL)) {
88 type_flags.push("FLIP_H");
Nataniel Borgesb810df42019-02-21 10:54:00 -080089 }
Vishnu Nair8175a122019-03-06 13:37:40 -080090 }
Nataniel Borgesb810df42019-02-21 10:54:00 -080091
Vishnu Nair8175a122019-03-06 13:37:40 -080092 if (type_flags.length == 0) {
93 throw "Unknown transform type " + transform ;
94 }
Nataniel Borgesb810df42019-02-21 10:54:00 -080095
Vishnu Nair8175a122019-03-06 13:37:40 -080096 return type_flags.join(', ');
Nataniel Borgesb810df42019-02-21 10:54:00 -080097}
98
Nataniel Borgesb810df42019-02-21 10:54:00 -080099
Vishnu Nair8175a122019-03-06 13:37:40 -0800100/**
101 * Ensures all values of the transform object are set.
102 */
103function fill_transform_data(transform) {
104 function fill_simple_transform(transform) {
Nataniel Borgesb810df42019-02-21 10:54:00 -0800105 // ROT_270 = ROT_90|FLIP_H|FLIP_V;
Vishnu Nair8175a122019-03-06 13:37:40 -0800106 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 Borgesb810df42019-02-21 10:54:00 -0800112 }
113
114 // ROT_180 = FLIP_H|FLIP_V;
Vishnu Nair8175a122019-03-06 13:37:40 -0800115 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 Borgesb810df42019-02-21 10:54:00 -0800121 }
122
123 // ROT_90
Vishnu Nair8175a122019-03-06 13:37:40 -0800124 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 Borgesb810df42019-02-21 10:54:00 -0800130 }
131
Vishnu Nair8175a122019-03-06 13:37:40 -0800132 // 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 Nair4c71de22019-04-19 10:18:08 -0700138 transform.type = 0;
Vishnu Nair8175a122019-03-06 13:37:40 -0800139 return;
Nataniel Borgesb810df42019-02-21 10:54:00 -0800140 }
141
Vishnu Nair8175a122019-03-06 13:37:40 -0800142 throw "Unknown transform type " + transform;
143 }
144
145 if (!transform) {
146 return;
147 }
148
Vishnu Nair13f8d142019-03-19 17:10:48 -0700149 if (is_proto_2(transform)) {
Vishnu Nair8175a122019-03-06 13:37:40 -0800150 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 Borgesb810df42019-02-21 10:54:00 -0800161}
162
Vishnu Nair8175a122019-03-06 13:37:40 -0800163function is_type_flag_set(transform, bits) {
164 transform = transform || {};
165 var type = transform.type || 0;
166 return (type & bits) === bits;
167}
168
169function is_type_flag_clear(transform, bits) {
170 transform = transform || {};
171 var type = transform.type || 0;
172 return (type & bits) === 0;
173}
174
175export {format_transform_type, fill_transform_data, is_simple_transform};