1 /************************************************************************ 2 * 3 * This file is part of SuperCollider Qt GUI. 4 * 5 * Copyright 2013 Jakob Leben (jakob.leben@gmail.com) 6 * 7 * This program is free software: you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation, either version 2 of the License, or 10 * (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program. If not, see <http://www.gnu.org/licenses/>. 19 * 20 ************************************************************************/ 21 22 // these functions are useful for the creation of QImage SC objects 23 24 #pragma once 25 26 #include <QImage> 27 #include "../image.h" 28 #include "../type_codec.hpp" 29 30 namespace QC = QtCollider; 31 32 namespace QtCollider { 33 34 inline QC::Image* to_image(struct PyrObject* obj) { 35 SharedImage* shared_image_ptr = reinterpret_cast<SharedImage*>(slotRawPtr(obj->slots)); 36 return shared_image_ptr->data(); 37 } 38 39 inline QC::Image* to_image(PyrSlot* slot) { 40 SharedImage* shared_image_ptr = reinterpret_cast<SharedImage*>(slotRawPtr(slotRawObject(slot)->slots)); 41 return shared_image_ptr->data(); 42 } 43 44 inline QRgb color_to_pixel(const QColor& color) { 45 int r, g, b, a; 46 color.getRgb(&r, &g, &b, &a); 47 qreal k = a / 255.f; 48 r *= k; 49 g *= k; 50 b *= k; 51 QRgb pixel = (a << 24) | (r << 16) | (g << 8) | b; 52 return pixel; 53 } 54 55 inline QColor pixel_to_color(QRgb pixel) { 56 int r, g, b, a; 57 int mask = 0xFF; 58 a = pixel >> 24 & mask; 59 r = pixel >> 16 & mask; 60 g = pixel >> 8 & mask; 61 b = pixel & mask; 62 if (a > 0) { 63 qreal k = a > 0 ? 255.f / a : 0.f; 64 r *= k; 65 g *= k; 66 b *= k; 67 return QColor(r, g, b, a); 68 } else 69 return QColor(0, 0, 0, 0); 70 } 71 72 inline int finalize_image_object(struct VMGlobals* g, struct PyrObject* obj) { 73 SharedImage* shared_image_ptr = reinterpret_cast<SharedImage*>(slotRawPtr(obj->slots)); 74 delete shared_image_ptr; 75 SetNil(obj->slots + 0); 76 return errNone; 77 } 78 79 inline void initialize_image_object(struct VMGlobals* g, struct PyrObject* obj, Image* image) { 80 assert(IsNil(obj->slots) && IsNil(obj->slots + 1)); 81 SharedImage* shared_image_ptr = new SharedImage(image); 82 SetPtr(obj->slots, shared_image_ptr); // dataptr 83 InstallFinalizer(g, obj, 1, finalize_image_object); // finalizer 84 } 85 86 } // namespace QtCollider 87