tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

enc_external_image_test.cc (2788B)


      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 <jxl/types.h>
      9 
     10 #include <cstddef>
     11 #include <cstdint>
     12 
     13 #include "lib/jxl/base/compiler_specific.h"
     14 #include "lib/jxl/base/span.h"
     15 #include "lib/jxl/color_encoding_internal.h"
     16 #include "lib/jxl/image_bundle.h"
     17 #include "lib/jxl/image_metadata.h"
     18 #include "lib/jxl/test_memory_manager.h"
     19 #include "lib/jxl/testing.h"
     20 
     21 namespace jxl {
     22 namespace {
     23 
     24 TEST(ExternalImageTest, InvalidSize) {
     25  if (JXL_CRASH_ON_ERROR) {
     26    GTEST_SKIP() << "Skipping due to JXL_CRASH_ON_ERROR";
     27  }
     28  ImageMetadata im;
     29  im.SetAlphaBits(8);
     30  ImageBundle ib(jxl::test::MemoryManager(), &im);
     31 
     32  JxlPixelFormat format = {4, JXL_TYPE_UINT16, JXL_BIG_ENDIAN, 0};
     33  const uint8_t buf[10 * 100 * 8] = {};
     34  EXPECT_FALSE(ConvertFromExternal(Bytes(buf, 10), /*xsize=*/10, /*ysize=*/100,
     35                                   /*c_current=*/ColorEncoding::SRGB(),
     36                                   /*bits_per_sample=*/16, format, nullptr,
     37                                   &ib));
     38  EXPECT_FALSE(ConvertFromExternal(
     39      Bytes(buf, sizeof(buf) - 1), /*xsize=*/10, /*ysize=*/100,
     40      /*c_current=*/ColorEncoding::SRGB(),
     41      /*bits_per_sample=*/16, format, nullptr, &ib));
     42  EXPECT_TRUE(
     43      ConvertFromExternal(Bytes(buf, sizeof(buf)), /*xsize=*/10,
     44                          /*ysize=*/100, /*c_current=*/ColorEncoding::SRGB(),
     45                          /*bits_per_sample=*/16, format, nullptr, &ib));
     46 }
     47 
     48 TEST(ExternalImageTest, AlphaMissing) {
     49  ImageMetadata im;
     50  im.SetAlphaBits(0);  // No alpha
     51  ImageBundle ib(jxl::test::MemoryManager(), &im);
     52 
     53  const size_t xsize = 10;
     54  const size_t ysize = 20;
     55  const uint8_t buf[xsize * ysize * 4] = {};
     56 
     57  JxlPixelFormat format = {4, JXL_TYPE_UINT8, JXL_BIG_ENDIAN, 0};
     58  // has_alpha is true but the ImageBundle has no alpha. Alpha channel should
     59  // be ignored.
     60  EXPECT_TRUE(ConvertFromExternal(Bytes(buf, sizeof(buf)), xsize, ysize,
     61                                  /*c_current=*/ColorEncoding::SRGB(),
     62                                  /*bits_per_sample=*/8, format, nullptr, &ib));
     63  EXPECT_FALSE(ib.HasAlpha());
     64 }
     65 
     66 TEST(ExternalImageTest, AlphaPremultiplied) {
     67  ImageMetadata im;
     68  im.SetAlphaBits(8, true);
     69 
     70  ImageBundle ib(jxl::test::MemoryManager(), &im);
     71  const size_t xsize = 10;
     72  const size_t ysize = 20;
     73  const size_t size = xsize * ysize * 8;
     74  const uint8_t buf[size] = {};
     75 
     76  JxlPixelFormat format = {4, JXL_TYPE_UINT16, JXL_BIG_ENDIAN, 0};
     77  EXPECT_TRUE(BufferToImageBundle(format, xsize, ysize, buf, size, nullptr,
     78                                  ColorEncoding::SRGB(), &ib));
     79 }
     80 
     81 }  // namespace
     82 }  // namespace jxl