odin-blend2d

Odin bindings to Blend2D
Log | Files | Refs | README | LICENSE

imagedecoder.cpp (5316B)


      1 // This file is part of Blend2D project <https://blend2d.com>
      2 //
      3 // See blend2d.h or LICENSE.md for license and copyright information
      4 // SPDX-License-Identifier: Zlib
      5 
      6 #include "api-build_p.h"
      7 #include "imagedecoder.h"
      8 #include "object_p.h"
      9 #include "runtime_p.h"
     10 
     11 // bl::ImageDecoder - Globals
     12 // ==========================
     13 
     14 namespace bl {
     15 namespace ImageDecoderInternal {
     16 
     17 static BLObjectEternalVirtualImpl<BLImageDecoderImpl, BLImageDecoderVirt> default_decoder;
     18 
     19 } // {ImageDecoderInternal}
     20 } // {bl}
     21 
     22 // bl::ImageDecoder - API - Init & Destroy
     23 // =======================================
     24 
     25 BL_API_IMPL BLResult bl_image_decoder_init(BLImageDecoderCore* self) noexcept {
     26   self->_d = bl_object_defaults[BL_OBJECT_TYPE_IMAGE_DECODER]._d;
     27   return BL_SUCCESS;
     28 }
     29 
     30 BL_API_IMPL BLResult bl_image_decoder_init_move(BLImageDecoderCore* self, BLImageDecoderCore* other) noexcept {
     31   BL_ASSERT(self != other);
     32   BL_ASSERT(other->_d.is_image_decoder());
     33 
     34   self->_d = other->_d;
     35   other->_d = bl_object_defaults[BL_OBJECT_TYPE_IMAGE_DECODER]._d;
     36 
     37   return BL_SUCCESS;
     38 }
     39 
     40 BL_API_IMPL BLResult bl_image_decoder_init_weak(BLImageDecoderCore* self, const BLImageDecoderCore* other) noexcept {
     41   BL_ASSERT(self != other);
     42   BL_ASSERT(other->_d.is_image_decoder());
     43 
     44   return bl_object_private_init_weak_tagged(self, other);
     45 }
     46 
     47 BL_API_IMPL BLResult bl_image_decoder_destroy(BLImageDecoderCore* self) noexcept {
     48   return bl::ObjectInternal::release_virtual_instance(self);
     49 }
     50 
     51 // bl::ImageDecoder - API - Reset
     52 // ==============================
     53 
     54 BL_API_IMPL BLResult bl_image_decoder_reset(BLImageDecoderCore* self) noexcept {
     55   BL_ASSERT(self->_d.is_image_decoder());
     56 
     57   return bl::ObjectInternal::replace_virtual_instance(self, static_cast<BLImageDecoderCore*>(&bl_object_defaults[BL_OBJECT_TYPE_IMAGE_DECODER]));
     58 }
     59 
     60 // bl::ImageDecoder - API - Assign
     61 // ===============================
     62 
     63 BL_API_IMPL BLResult bl_image_decoder_assign_move(BLImageDecoderCore* self, BLImageDecoderCore* other) noexcept {
     64   BL_ASSERT(self->_d.is_image_decoder());
     65   BL_ASSERT(other->_d.is_image_decoder());
     66 
     67   BLImageDecoderCore tmp = *other;
     68   other->_d = bl_object_defaults[BL_OBJECT_TYPE_IMAGE_DECODER]._d;
     69   return bl::ObjectInternal::replace_virtual_instance(self, &tmp);
     70 }
     71 
     72 BL_API_IMPL BLResult bl_image_decoder_assign_weak(BLImageDecoderCore* self, const BLImageDecoderCore* other) noexcept {
     73   BL_ASSERT(self->_d.is_image_decoder());
     74   BL_ASSERT(other->_d.is_image_decoder());
     75 
     76   return bl::ObjectInternal::assign_virtual_instance(self, other);
     77 }
     78 
     79 // bl::ImageDecoder - API - Interface
     80 // ==================================
     81 
     82 BL_API_IMPL BLResult bl_image_decoder_restart(BLImageDecoderCore* self) noexcept {
     83   BL_ASSERT(self->_d.is_image_decoder());
     84   BLImageDecoderImpl* self_impl = self->_impl();
     85 
     86   return self_impl->virt->restart(self_impl);
     87 }
     88 
     89 BL_API_IMPL BLResult bl_image_decoder_read_info(BLImageDecoderCore* self, BLImageInfo* info_out, const uint8_t* data, size_t size) noexcept {
     90   BL_ASSERT(self->_d.is_image_decoder());
     91   BLImageDecoderImpl* self_impl = self->_impl();
     92 
     93   return self_impl->virt->read_info(self_impl, info_out, data, size);
     94 }
     95 
     96 BL_API_IMPL BLResult bl_image_decoder_read_frame(BLImageDecoderCore* self, BLImageCore* image_out, const uint8_t* data, size_t size) noexcept {
     97   BL_ASSERT(self->_d.is_image_decoder());
     98   BLImageDecoderImpl* self_impl = self->_impl();
     99 
    100   return self_impl->virt->read_frame(self_impl, image_out, data, size);
    101 }
    102 
    103 // bl::ImageDecoder - Virtual Functions (Null)
    104 // ===========================================
    105 
    106 static BLResult BL_CDECL bl_image_decoder_impl_destroy(BLObjectImpl* impl) noexcept {
    107   bl_unused(impl);
    108   return BL_SUCCESS;
    109 }
    110 
    111 static uint32_t BL_CDECL bl_image_decoder_impl_restart(BLImageDecoderImpl* impl) noexcept {
    112   bl_unused(impl);
    113   return BL_ERROR_INVALID_STATE;
    114 }
    115 
    116 static BLResult BL_CDECL bl_image_decoder_impl_read_info(BLImageDecoderImpl* impl, BLImageInfo* info_out, const uint8_t* data, size_t size) noexcept {
    117   bl_unused(impl, info_out, data, size);
    118   return BL_ERROR_INVALID_STATE;
    119 }
    120 
    121 static BLResult BL_CDECL bl_image_decoder_impl_read_frame(BLImageDecoderImpl* impl, BLImageCore* image_out, const uint8_t* data, size_t size) noexcept {
    122   bl_unused(impl, image_out, data, size);
    123   return BL_ERROR_INVALID_STATE;
    124 }
    125 
    126 // bl::ImageDecoder - Runtime Registration
    127 // =======================================
    128 
    129 void bl_image_decoder_rt_init(BLRuntimeContext* rt) noexcept {
    130   using namespace bl::ImageDecoderInternal;
    131 
    132   bl_unused(rt);
    133 
    134   // Initialize default BLImageDecoder.
    135   default_decoder.virt.base.destroy = bl_image_decoder_impl_destroy;
    136   default_decoder.virt.base.get_property = bl_object_impl_get_property;
    137   default_decoder.virt.base.set_property = bl_object_impl_set_property;
    138   default_decoder.virt.restart = bl_image_decoder_impl_restart;
    139   default_decoder.virt.read_info = bl_image_decoder_impl_read_info;
    140   default_decoder.virt.read_frame = bl_image_decoder_impl_read_frame;
    141   default_decoder.impl->ctor(
    142     &default_decoder.virt,
    143     static_cast<BLImageCodecCore*>(&bl_object_defaults[BL_OBJECT_TYPE_IMAGE_CODEC]));
    144   default_decoder.impl->last_result = BL_ERROR_NOT_INITIALIZED;
    145 
    146   bl_object_defaults[BL_OBJECT_TYPE_IMAGE_DECODER]._d.init_dynamic(
    147     BLObjectInfo::from_type_with_marker(BL_OBJECT_TYPE_IMAGE_DECODER),
    148     &default_decoder.impl);
    149 }