1 /**************************************************************************** 2 ** 3 ** DQt - D bindings for the Qt Toolkit 4 ** 5 ** GNU Lesser General Public License Usage 6 ** This file may be used under the terms of the GNU Lesser 7 ** General Public License version 3 as published by the Free Software 8 ** Foundation and appearing in the file LICENSE.LGPL3 included in the 9 ** packaging of this file. Please review the following information to 10 ** ensure the GNU Lesser General Public License version 3 requirements 11 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. 12 ** 13 ****************************************************************************/ 14 module qt.gui.pixelformat; 15 extern(C++): 16 17 import qt.config; 18 import qt.core.global; 19 import qt.core.typeinfo; 20 import qt.helpers; 21 22 @Q_PRIMITIVE_TYPE extern(C++, class) struct QPixelFormat 23 { 24 private: 25 // QPixelFormat basically is a glorified quint64, split into several fields. 26 // We could use bit-fields, but GCC at least generates horrible, horrible code for them, 27 // so we do the bit-twiddling ourselves. 28 enum FieldWidth { 29 ModelFieldWidth = 4, 30 FirstFieldWidth = 6, 31 SecondFieldWidth = FieldWidth.FirstFieldWidth, 32 ThirdFieldWidth = FieldWidth.FirstFieldWidth, 33 FourthFieldWidth = FieldWidth.FirstFieldWidth, 34 FifthFieldWidth = FieldWidth.FirstFieldWidth, 35 AlphaFieldWidth = FieldWidth.FirstFieldWidth, 36 AlphaUsageFieldWidth = 1, 37 AlphaPositionFieldWidth = 1, 38 PremulFieldWidth = 1, 39 TypeInterpretationFieldWidth = 4, 40 ByteOrderFieldWidth = 2, 41 SubEnumFieldWidth = 6, 42 UnusedFieldWidth = 9, 43 44 TotalFieldWidthByWidths = FieldWidth.ModelFieldWidth + FieldWidth.FirstFieldWidth + FieldWidth.SecondFieldWidth + FieldWidth.ThirdFieldWidth + 45 FieldWidth.FourthFieldWidth + FieldWidth.FifthFieldWidth + FieldWidth.AlphaFieldWidth + FieldWidth.AlphaUsageFieldWidth + 46 FieldWidth.AlphaPositionFieldWidth + FieldWidth.PremulFieldWidth + FieldWidth.TypeInterpretationFieldWidth + 47 FieldWidth.ByteOrderFieldWidth + FieldWidth.SubEnumFieldWidth + FieldWidth.UnusedFieldWidth 48 } 49 50 enum Field { 51 ModelField = 0, 52 // work around bug in old clang versions: when building webkit 53 // with XCode 4.6 and older this fails compilation, thus cast to int 54 FirstField = Field.ModelField + int(FieldWidth.ModelFieldWidth), 55 SecondField = Field.FirstField + int(FieldWidth.FirstFieldWidth), 56 ThirdField = Field.SecondField + int(FieldWidth.SecondFieldWidth), 57 FourthField = Field.ThirdField + int(FieldWidth.ThirdFieldWidth), 58 FifthField = Field.FourthField + int(FieldWidth.FourthFieldWidth), 59 AlphaField = Field.FifthField + int(FieldWidth.FifthFieldWidth), 60 AlphaUsageField = Field.AlphaField + int(FieldWidth.AlphaFieldWidth), 61 AlphaPositionField = Field.AlphaUsageField + int(FieldWidth.AlphaUsageFieldWidth), 62 PremulField = Field.AlphaPositionField + int(FieldWidth.AlphaPositionFieldWidth), 63 TypeInterpretationField = Field.PremulField + int(FieldWidth.PremulFieldWidth), 64 ByteOrderField = Field.TypeInterpretationField + int(FieldWidth.TypeInterpretationFieldWidth), 65 SubEnumField = Field.ByteOrderField + int(FieldWidth.ByteOrderFieldWidth), 66 UnusedField = Field.SubEnumField + int(FieldWidth.SubEnumFieldWidth), 67 68 TotalFieldWidthByOffsets = Field.UnusedField + int(FieldWidth.UnusedFieldWidth) 69 } 70 71 mixin(Q_STATIC_ASSERT(q{uint(QPixelFormat.FieldWidth.TotalFieldWidthByWidths) == uint(QPixelFormat.Field.TotalFieldWidthByOffsets)})); 72 mixin(Q_STATIC_ASSERT(q{uint(QPixelFormat.FieldWidth.TotalFieldWidthByWidths) == 8 * quint64.sizeof})); 73 74 pragma(inline, true) uchar get(Field offset, FieldWidth width) const/+ noexcept+/ 75 { return cast(uchar)((data >> uint(offset)) & ((1uL << uint(width)) - 1uL)); } 76 pragma(inline, true) static quint64 set(Field offset, FieldWidth width, uchar value) 77 { return (quint64(value) & ((1uL << uint(width)) - 1uL)) << uint(offset); } 78 79 public: 80 enum ColorModel { 81 RGB, 82 BGR, 83 Indexed, 84 Grayscale, 85 CMYK, 86 HSL, 87 HSV, 88 YUV, 89 Alpha 90 } 91 92 enum AlphaUsage { 93 UsesAlpha, 94 IgnoresAlpha 95 } 96 97 enum AlphaPosition { 98 AtBeginning, 99 AtEnd 100 } 101 102 enum AlphaPremultiplied { 103 NotPremultiplied, 104 Premultiplied 105 } 106 107 enum TypeInterpretation { 108 UnsignedInteger, 109 UnsignedShort, 110 UnsignedByte, 111 FloatingPoint 112 } 113 114 enum YUVLayout { 115 YUV444, 116 YUV422, 117 YUV411, 118 YUV420P, 119 YUV420SP, 120 YV12, 121 UYVY, 122 YUYV, 123 NV12, 124 NV21, 125 IMC1, 126 IMC2, 127 IMC3, 128 IMC4, 129 Y8, 130 Y16 131 } 132 133 enum ByteOrder { 134 LittleEndian, 135 BigEndian, 136 CurrentSystemEndian 137 } 138 139 @disable this(); 140 /+pragma(inline, true) this()/+ noexcept+/ 141 { 142 this.data = 0; 143 }+/ 144 /+ pragma(inline, true) this(ColorModel mdl, 145 uchar firstSize, 146 uchar secondSize, 147 uchar thirdSize, 148 uchar fourthSize, 149 uchar fifthSize, 150 uchar alfa, 151 AlphaUsage usage, 152 AlphaPosition position, 153 AlphaPremultiplied premult, 154 TypeInterpretation typeInterp, 155 ByteOrder b_order = ByteOrder.CurrentSystemEndian, 156 uchar s_enum = 0)/+ noexcept+/ 157 { 158 this.data = set(Field.ModelField, FieldWidth.ModelFieldWidth, cast(uchar)(mdl)) | 159 set(Field.FirstField, FieldWidth.FirstFieldWidth, firstSize) | 160 set(Field.SecondField, FieldWidth.SecondFieldWidth, secondSize) | 161 set(Field.ThirdField, FieldWidth.ThirdFieldWidth, thirdSize) | 162 set(Field.FourthField, FieldWidth.FourthFieldWidth, fourthSize) | 163 set(Field.FifthField, FieldWidth.FifthFieldWidth, fifthSize) | 164 set(Field.AlphaField, FieldWidth.AlphaFieldWidth, alfa) | 165 set(Field.AlphaUsageField, FieldWidth.AlphaUsageFieldWidth, cast(uchar)(usage)) | 166 set(Field.AlphaPositionField, FieldWidth.AlphaPositionFieldWidth, cast(uchar)(position)) | 167 set(Field.PremulField, FieldWidth.PremulFieldWidth, cast(uchar)(premult)) | 168 set(Field.TypeInterpretationField, FieldWidth.TypeInterpretationFieldWidth, cast(uchar)(typeInterp)) | 169 set(Field.ByteOrderField, FieldWidth.ByteOrderFieldWidth, cast(uchar)(resolveByteOrder(b_order))) | 170 set(Field.SubEnumField, FieldWidth.SubEnumFieldWidth, s_enum) | 171 set(Field.UnusedField, FieldWidth.UnusedFieldWidth, 0); 172 }+/ 173 174 pragma(inline, true) ColorModel colorModel() const/+ noexcept+/ { return cast(ColorModel)(get(Field.ModelField, FieldWidth.ModelFieldWidth)); } 175 pragma(inline, true) uchar channelCount() const/+ noexcept+/ { return cast(uchar)((get(Field.FirstField, FieldWidth.FirstFieldWidth) > 0) + 176 (get(Field.SecondField, FieldWidth.SecondFieldWidth) > 0) + 177 (get(Field.ThirdField, FieldWidth.ThirdFieldWidth) > 0) + 178 (get(Field.FourthField, FieldWidth.FourthFieldWidth) > 0) + 179 (get(Field.FifthField, FieldWidth.FifthFieldWidth) > 0) + 180 (get(Field.AlphaField, FieldWidth.AlphaFieldWidth) > 0)); } 181 182 pragma(inline, true) uchar redSize() const/+ noexcept+/ { return get(Field.FirstField, FieldWidth.FirstFieldWidth); } 183 pragma(inline, true) uchar greenSize() const/+ noexcept+/ { return get(Field.SecondField, FieldWidth.SecondFieldWidth); } 184 pragma(inline, true) uchar blueSize() const/+ noexcept+/ { return get(Field.ThirdField, FieldWidth.ThirdFieldWidth); } 185 186 pragma(inline, true) uchar cyanSize() const/+ noexcept+/ { return get(Field.FirstField, FieldWidth.FirstFieldWidth); } 187 pragma(inline, true) uchar magentaSize() const/+ noexcept+/ { return get(Field.SecondField, FieldWidth.SecondFieldWidth); } 188 pragma(inline, true) uchar yellowSize() const/+ noexcept+/ { return get(Field.ThirdField, FieldWidth.ThirdFieldWidth); } 189 pragma(inline, true) uchar blackSize() const/+ noexcept+/ { return get(Field.FourthField, FieldWidth.FourthFieldWidth); } 190 191 pragma(inline, true) uchar hueSize() const/+ noexcept+/ { return get(Field.FirstField, FieldWidth.FirstFieldWidth); } 192 pragma(inline, true) uchar saturationSize() const/+ noexcept+/ { return get(Field.SecondField, FieldWidth.SecondFieldWidth); } 193 pragma(inline, true) uchar lightnessSize() const/+ noexcept+/ { return get(Field.ThirdField, FieldWidth.ThirdFieldWidth); } 194 pragma(inline, true) uchar brightnessSize() const/+ noexcept+/ { return get(Field.ThirdField, FieldWidth.ThirdFieldWidth); } 195 196 pragma(inline, true) uchar alphaSize() const/+ noexcept+/ { return get(Field.AlphaField, FieldWidth.AlphaFieldWidth); } 197 198 pragma(inline, true) uchar bitsPerPixel() const/+ noexcept+/ { return cast(uchar)(get(Field.FirstField, FieldWidth.FirstFieldWidth) + 199 get(Field.SecondField, FieldWidth.SecondFieldWidth) + 200 get(Field.ThirdField, FieldWidth.ThirdFieldWidth) + 201 get(Field.FourthField, FieldWidth.FourthFieldWidth) + 202 get(Field.FifthField, FieldWidth.FifthFieldWidth) + 203 get(Field.AlphaField, FieldWidth.AlphaFieldWidth)); } 204 205 pragma(inline, true) AlphaUsage alphaUsage() const/+ noexcept+/ { return cast(AlphaUsage)(get(Field.AlphaUsageField, FieldWidth.AlphaUsageFieldWidth)); } 206 pragma(inline, true) AlphaPosition alphaPosition() const/+ noexcept+/ { return cast(AlphaPosition)(get(Field.AlphaPositionField, FieldWidth.AlphaPositionFieldWidth)); } 207 pragma(inline, true) AlphaPremultiplied premultiplied() const/+ noexcept+/ { return cast(AlphaPremultiplied)(get(Field.PremulField, FieldWidth.PremulFieldWidth)); } 208 pragma(inline, true) TypeInterpretation typeInterpretation() const/+ noexcept+/ { return cast(TypeInterpretation)(get(Field.TypeInterpretationField, FieldWidth.TypeInterpretationFieldWidth)); } 209 pragma(inline, true) ByteOrder byteOrder() const/+ noexcept+/ { return cast(ByteOrder)(get(Field.ByteOrderField, FieldWidth.ByteOrderFieldWidth)); } 210 211 pragma(inline, true) YUVLayout yuvLayout() const/+ noexcept+/ { return cast(YUVLayout)(get(Field.SubEnumField, FieldWidth.SubEnumFieldWidth)); } 212 pragma(inline, true) uchar subEnum() const/+ noexcept+/ { return get(Field.SubEnumField, FieldWidth.SubEnumFieldWidth); } 213 214 private: 215 pragma(inline, true) static ByteOrder resolveByteOrder(ByteOrder bo) 216 { 217 return bo == ByteOrder.CurrentSystemEndian ? versionIsSet!"LittleEndian" ? ByteOrder.LittleEndian : ByteOrder.BigEndian : bo ; 218 } 219 220 private: 221 quint64 data = 0; 222 223 /+ friend Q_DECL_CONST_FUNCTION inline bool operator==(QPixelFormat fmt1, QPixelFormat fmt2) 224 { return fmt1.data == fmt2.data; } +/ 225 226 /+ friend Q_DECL_CONST_FUNCTION inline bool operator!=(QPixelFormat fmt1, QPixelFormat fmt2) 227 { return !(fmt1 == fmt2); } +/ 228 } 229 /+ Q_STATIC_ASSERT(sizeof(QPixelFormat) == sizeof(quint64)); 230 Q_DECLARE_TYPEINFO(QPixelFormat, Q_PRIMITIVE_TYPE); +/ 231 232 233 extern(C++, "QtPrivate") { 234 QPixelFormat /+ Q_GUI_EXPORT +/ QPixelFormat_createYUV(QPixelFormat.YUVLayout yuvLayout, 235 uchar alphaSize, 236 QPixelFormat.AlphaUsage alphaUsage, 237 QPixelFormat.AlphaPosition alphaPosition, 238 QPixelFormat.AlphaPremultiplied premultiplied, 239 QPixelFormat.TypeInterpretation typeInterpretation, 240 QPixelFormat.ByteOrder byteOrder); 241 } 242 243 /+pragma(inline, true) QPixelFormat qPixelFormatRgba(uchar red, 244 uchar green, 245 uchar blue, 246 uchar alfa, 247 QPixelFormat.AlphaUsage usage, 248 QPixelFormat.AlphaPosition position, 249 QPixelFormat.AlphaPremultiplied pmul=QPixelFormat.AlphaPremultiplied.NotPremultiplied, 250 QPixelFormat.TypeInterpretation typeInt=QPixelFormat.TypeInterpretation.UnsignedInteger)/+ noexcept+/ 251 { 252 return QPixelFormat(QPixelFormat.ColorModel.RGB, 253 red, 254 green, 255 blue, 256 0, 257 0, 258 alfa, 259 usage, 260 position, 261 pmul, 262 typeInt); 263 } 264 265 pragma(inline, true) QPixelFormat qPixelFormatGrayscale(uchar channelSize, 266 QPixelFormat.TypeInterpretation typeInt=QPixelFormat.TypeInterpretation.UnsignedInteger)/+ noexcept+/ 267 { 268 return QPixelFormat(QPixelFormat.ColorModel.Grayscale, 269 channelSize, 270 0, 271 0, 272 0, 273 0, 274 0, 275 QPixelFormat.AlphaUsage.IgnoresAlpha, 276 QPixelFormat.AlphaPosition.AtBeginning, 277 QPixelFormat.AlphaPremultiplied.NotPremultiplied, 278 typeInt); 279 } 280 281 pragma(inline, true) QPixelFormat qPixelFormatAlpha(uchar channelSize, 282 QPixelFormat.TypeInterpretation typeInt=QPixelFormat.TypeInterpretation.UnsignedInteger)/+ noexcept+/ 283 { 284 return QPixelFormat(QPixelFormat.ColorModel.Alpha, 285 0, 286 0, 287 0, 288 0, 289 0, 290 channelSize, 291 QPixelFormat.AlphaUsage.UsesAlpha, 292 QPixelFormat.AlphaPosition.AtBeginning, 293 QPixelFormat.AlphaPremultiplied.NotPremultiplied, 294 typeInt); 295 } 296 297 pragma(inline, true) QPixelFormat qPixelFormatCmyk(uchar channelSize, 298 uchar alfa=0, 299 QPixelFormat.AlphaUsage usage=QPixelFormat.AlphaUsage.IgnoresAlpha, 300 QPixelFormat.AlphaPosition position=QPixelFormat.AlphaPosition.AtBeginning, 301 QPixelFormat.TypeInterpretation typeInt=QPixelFormat.TypeInterpretation.UnsignedInteger)/+ noexcept+/ 302 { 303 return QPixelFormat(QPixelFormat.ColorModel.CMYK, 304 channelSize, 305 channelSize, 306 channelSize, 307 channelSize, 308 0, 309 alfa, 310 usage, 311 position, 312 QPixelFormat.AlphaPremultiplied.NotPremultiplied, 313 typeInt); 314 } 315 316 pragma(inline, true) QPixelFormat qPixelFormatHsl(uchar channelSize, 317 uchar alfa=0, 318 QPixelFormat.AlphaUsage usage=QPixelFormat.AlphaUsage.IgnoresAlpha, 319 QPixelFormat.AlphaPosition position=QPixelFormat.AlphaPosition.AtBeginning, 320 QPixelFormat.TypeInterpretation typeInt=QPixelFormat.TypeInterpretation.FloatingPoint)/+ noexcept+/ 321 { 322 return QPixelFormat(QPixelFormat.ColorModel.HSL, 323 channelSize, 324 channelSize, 325 channelSize, 326 0, 327 0, 328 alfa, 329 usage, 330 position, 331 QPixelFormat.AlphaPremultiplied.NotPremultiplied, 332 typeInt); 333 } 334 335 pragma(inline, true) QPixelFormat qPixelFormatHsv(uchar channelSize, 336 uchar alfa=0, 337 QPixelFormat.AlphaUsage usage=QPixelFormat.AlphaUsage.IgnoresAlpha, 338 QPixelFormat.AlphaPosition position=QPixelFormat.AlphaPosition.AtBeginning, 339 QPixelFormat.TypeInterpretation typeInt=QPixelFormat.TypeInterpretation.FloatingPoint)/+ noexcept+/ 340 { 341 return QPixelFormat(QPixelFormat.ColorModel.HSV, 342 channelSize, 343 channelSize, 344 channelSize, 345 0, 346 0, 347 alfa, 348 usage, 349 position, 350 QPixelFormat.AlphaPremultiplied.NotPremultiplied, 351 typeInt); 352 } 353 354 pragma(inline, true) QPixelFormat qPixelFormatYuv(QPixelFormat.YUVLayout layout, 355 uchar alfa=0, 356 QPixelFormat.AlphaUsage usage=QPixelFormat.AlphaUsage.IgnoresAlpha, 357 QPixelFormat.AlphaPosition position=QPixelFormat.AlphaPosition.AtBeginning, 358 QPixelFormat.AlphaPremultiplied p_mul=QPixelFormat.AlphaPremultiplied.NotPremultiplied, 359 QPixelFormat.TypeInterpretation typeInt=QPixelFormat.TypeInterpretation.UnsignedByte, 360 QPixelFormat.ByteOrder b_order=QPixelFormat.ByteOrder.LittleEndian) 361 { 362 return /+ QtPrivate:: +/QPixelFormat_createYUV(layout, 363 alfa, 364 usage, 365 position, 366 p_mul, 367 typeInt, 368 b_order); 369 } 370 +/ 371