libjxl

FORK: libjxl patches used on blog
git clone https://git.neptards.moe/blog/libjxl.git
Log | Files | Refs | Submodules | README | LICENSE

enc_external_image_test.cc (2591B)


      1 // Copyright (c) the JPEG XL Project Authors. All rights reserved.
      2 //
      3 // Use of this source code is governed by a BSD-style
      4 // license that can be found in the LICENSE file.
      5 
      6 #include "lib/jxl/enc_external_image.h"
      7 
      8 #include <array>
      9 #include <new>
     10 
     11 #include "lib/jxl/base/compiler_specific.h"
     12 #include "lib/jxl/base/data_parallel.h"
     13 #include "lib/jxl/color_encoding_internal.h"
     14 #include "lib/jxl/image_ops.h"
     15 #include "lib/jxl/image_test_utils.h"
     16 #include "lib/jxl/testing.h"
     17 
     18 namespace jxl {
     19 namespace {
     20 
     21 #if !defined(JXL_CRASH_ON_ERROR)
     22 TEST(ExternalImageTest, InvalidSize) {
     23   ImageMetadata im;
     24   im.SetAlphaBits(8);
     25   ImageBundle ib(&im);
     26 
     27   JxlPixelFormat format = {4, JXL_TYPE_UINT16, JXL_BIG_ENDIAN, 0};
     28   const uint8_t buf[10 * 100 * 8] = {};
     29   EXPECT_FALSE(ConvertFromExternal(Bytes(buf, 10), /*xsize=*/10, /*ysize=*/100,
     30                                    /*c_current=*/ColorEncoding::SRGB(),
     31                                    /*bits_per_sample=*/16, format, nullptr,
     32                                    &ib));
     33   EXPECT_FALSE(ConvertFromExternal(
     34       Bytes(buf, sizeof(buf) - 1), /*xsize=*/10, /*ysize=*/100,
     35       /*c_current=*/ColorEncoding::SRGB(),
     36       /*bits_per_sample=*/16, format, nullptr, &ib));
     37   EXPECT_TRUE(
     38       ConvertFromExternal(Bytes(buf, sizeof(buf)), /*xsize=*/10,
     39                           /*ysize=*/100, /*c_current=*/ColorEncoding::SRGB(),
     40                           /*bits_per_sample=*/16, format, nullptr, &ib));
     41 }
     42 #endif
     43 
     44 TEST(ExternalImageTest, AlphaMissing) {
     45   ImageMetadata im;
     46   im.SetAlphaBits(0);  // No alpha
     47   ImageBundle ib(&im);
     48 
     49   const size_t xsize = 10;
     50   const size_t ysize = 20;
     51   const uint8_t buf[xsize * ysize * 4] = {};
     52 
     53   JxlPixelFormat format = {4, JXL_TYPE_UINT8, JXL_BIG_ENDIAN, 0};
     54   // has_alpha is true but the ImageBundle has no alpha. Alpha channel should
     55   // be ignored.
     56   EXPECT_TRUE(ConvertFromExternal(Bytes(buf, sizeof(buf)), xsize, ysize,
     57                                   /*c_current=*/ColorEncoding::SRGB(),
     58                                   /*bits_per_sample=*/8, format, nullptr, &ib));
     59   EXPECT_FALSE(ib.HasAlpha());
     60 }
     61 
     62 TEST(ExternalImageTest, AlphaPremultiplied) {
     63   ImageMetadata im;
     64   im.SetAlphaBits(8, true);
     65 
     66   ImageBundle ib(&im);
     67   const size_t xsize = 10;
     68   const size_t ysize = 20;
     69   const size_t size = xsize * ysize * 8;
     70   const uint8_t buf[size] = {};
     71 
     72   JxlPixelFormat format = {4, JXL_TYPE_UINT16, JXL_BIG_ENDIAN, 0};
     73   EXPECT_TRUE(BufferToImageBundle(format, xsize, ysize, buf, size, nullptr,
     74                                   ColorEncoding::SRGB(), &ib));
     75 }
     76 
     77 }  // namespace
     78 }  // namespace jxl