Skip to content

Commit 154127a

Browse files
committed
Fix MSVC build warnings at warning level 3
including: C4244, C4267, C4305, C4838
1 parent ea5a3ff commit 154127a

8 files changed

Lines changed: 44 additions & 41 deletions

File tree

CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,6 @@ endif()
251251
set(UHDR_WERROR_FLAGS "")
252252
if(MSVC)
253253
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
254-
add_compile_options(/wd26812) # Prefer enum class over enum
255254
elseif(EMSCRIPTEN)
256255
if(NOT UHDR_BUILD_DEPS)
257256
include(CheckCSourceCompiles)
@@ -613,7 +612,9 @@ target_compile_options(${UHDR_CORE_LIB_NAME} PRIVATE ${UHDR_WERROR_FLAGS})
613612
if(NOT JPEG_FOUND)
614613
add_dependencies(${UHDR_CORE_LIB_NAME} ${JPEGTURBO_TARGET_NAME})
615614
endif()
616-
if(NOT MSVC)
615+
if(MSVC)
616+
target_compile_options(${UHDR_CORE_LIB_NAME} PRIVATE /W3)
617+
else()
617618
target_compile_options(${UHDR_CORE_LIB_NAME} PRIVATE -Wall -Wextra -Wshadow)
618619
endif()
619620
if(DEFINED UHDR_MAX_DIMENSION)

lib/include/ultrahdr/gainmapmath.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,10 @@ inline uint16_t floatToHalf(float f) {
167167
const uint32_t m = b & 0x007FFFFF; // mantissa
168168

169169
// sign : normalized : denormalized : saturate
170-
return (b & 0x80000000) >> 16 | (e > 112) * ((((e - 112) << 10) & 0x7C00) | m >> 13) |
171-
((e < 113) & (e > 101)) * ((((0x007FF000 + m) >> (125 - e)) + 1) >> 1) |
172-
(e > 143) * 0x7FFF;
170+
return static_cast<uint16_t>(
171+
(b & 0x80000000) >> 16 | (e > 112) * ((((e - 112) << 10) & 0x7C00) | m >> 13) |
172+
((e < 113) & (e > 101)) * ((((0x007FF000 + m) >> (125 - e)) + 1) >> 1) |
173+
(e > 143) * 0x7FFF);
173174
}
174175

175176
// Taken from frameworks/base/libs/hwui/jni/android_graphics_ColorSpace.cpp

lib/src/gainmapmath.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ void ShepardsIDW::fillShepardsIDW(float* weights, int incR, int incB) {
4545
for (int x = 0; x < mMapScaleFactor; x++) {
4646
float pos_x = ((float)x) / mMapScaleFactor;
4747
float pos_y = ((float)y) / mMapScaleFactor;
48-
int curr_x = floor(pos_x);
49-
int curr_y = floor(pos_y);
48+
int curr_x = static_cast<int>(floor(pos_x));
49+
int curr_y = static_cast<int>(floor(pos_y));
5050
int next_x = curr_x + incR;
5151
int next_y = curr_y + incB;
52-
float e1_distance = euclideanDistance(pos_x, curr_x, pos_y, curr_y);
52+
float e1_distance = euclideanDistance(pos_x, (float)curr_x, pos_y, (float)curr_y);
5353
int index = y * mMapScaleFactor * 4 + x * 4;
5454
if (e1_distance == 0) {
5555
weights[index++] = 1.f;
@@ -59,13 +59,13 @@ void ShepardsIDW::fillShepardsIDW(float* weights, int incR, int incB) {
5959
} else {
6060
float e1_weight = 1.f / e1_distance;
6161

62-
float e2_distance = euclideanDistance(pos_x, curr_x, pos_y, next_y);
62+
float e2_distance = euclideanDistance(pos_x, (float)curr_x, pos_y, (float)next_y);
6363
float e2_weight = 1.f / e2_distance;
6464

65-
float e3_distance = euclideanDistance(pos_x, next_x, pos_y, curr_y);
65+
float e3_distance = euclideanDistance(pos_x, (float)next_x, pos_y, (float)curr_y);
6666
float e3_weight = 1.f / e3_distance;
6767

68-
float e4_distance = euclideanDistance(pos_x, next_x, pos_y, next_y);
68+
float e4_distance = euclideanDistance(pos_x, (float)next_x, pos_y, (float)next_y);
6969
float e4_weight = 1.f / e4_distance;
7070

7171
float total_weight = e1_weight + e2_weight + e3_weight + e4_weight;
@@ -657,7 +657,7 @@ const std::array<float, 9> kYuvBt601ToBt709 = {
657657
// U' = (0.0 * Y) + ( 1.010016 * U) + ( 0.061592 * V)
658658
// V' = (0.0 * Y) + ( 0.086969 * U) + ( 1.029350 * V)
659659
const std::array<float, 9> kYuvBt601ToBt2100 = {
660-
1.0f, -0.128245f, -0.115879, 0.0f, 1.010016f, 0.061592f, 0.0f, 0.086969f, 1.029350f};
660+
1.0f, -0.128245f, -0.115879f, 0.0f, 1.010016f, 0.061592f, 0.0f, 0.086969f, 1.029350f};
661661

662662
// Yuv Bt2100 -> Yuv Bt709
663663
// Y' = (1.0 * Y) + ( 0.018149 * U) + (-0.095132 * V)
@@ -785,7 +785,7 @@ uint8_t affineMapGain(float gainlog2, float mingainlog2, float maxgainlog2, floa
785785
float mappedVal = (gainlog2 - mingainlog2) / (maxgainlog2 - mingainlog2);
786786
if (gamma != 1.0f) mappedVal = pow(mappedVal, gamma);
787787
mappedVal *= 255;
788-
return CLIP3(mappedVal + 0.5f, 0, 255);
788+
return static_cast<uint8_t>(CLIP3(mappedVal + 0.5f, 0.0f, 255.0f));
789789
}
790790

791791
Color applyGain(Color e, float gain, uhdr_gainmap_metadata_ext_t* metadata) {
@@ -1277,9 +1277,9 @@ bool isPixelFormatRgb(uhdr_img_fmt_t format) {
12771277
}
12781278

12791279
uint32_t colorToRgba1010102(Color e_gamma) {
1280-
uint32_t r = CLIP3((e_gamma.r * 1023 + 0.5f), 0.0f, 1023.0f);
1281-
uint32_t g = CLIP3((e_gamma.g * 1023 + 0.5f), 0.0f, 1023.0f);
1282-
uint32_t b = CLIP3((e_gamma.b * 1023 + 0.5f), 0.0f, 1023.0f);
1280+
uint32_t r = static_cast<uint32_t>(CLIP3((e_gamma.r * 1023 + 0.5f), 0.0f, 1023.0f));
1281+
uint32_t g = static_cast<uint32_t>(CLIP3((e_gamma.g * 1023 + 0.5f), 0.0f, 1023.0f));
1282+
uint32_t b = static_cast<uint32_t>(CLIP3((e_gamma.b * 1023 + 0.5f), 0.0f, 1023.0f));
12831283
return (r | (g << 10) | (b << 20) | (0x3 << 30)); // Set alpha to 1.0
12841284
}
12851285

lib/src/icc.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,8 @@ std::shared_ptr<DataStruct> IccHelper::write_cicp_tag(uint32_t color_primaries,
270270
std::shared_ptr<DataStruct> dataStruct = std::make_shared<DataStruct>(kCicpTagSize);
271271
dataStruct->write32(Endian_SwapBE32(kTAG_cicp)); // Type signature
272272
dataStruct->write32(0); // Reserved
273-
dataStruct->write8(color_primaries); // Color primaries
274-
dataStruct->write8(transfer_characteristics); // Transfer characteristics
273+
dataStruct->write8(static_cast<uint8_t>(color_primaries)); // Color primaries
274+
dataStruct->write8(static_cast<uint8_t>(transfer_characteristics)); // Transfer characteristics
275275
dataStruct->write8(0); // RGB matrix
276276
dataStruct->write8(1); // Full range
277277
return dataStruct;
@@ -366,7 +366,7 @@ std::shared_ptr<DataStruct> IccHelper::write_mAB_or_mBA_tag(uint32_t type, bool
366366
}
367367
}
368368

369-
int total_length = b_curves_offset;
369+
size_t total_length = b_curves_offset;
370370
for (size_t i = 0; i < kNumChannels; ++i) {
371371
total_length += b_curves_data[i]->getLength();
372372
}
@@ -382,11 +382,11 @@ std::shared_ptr<DataStruct> IccHelper::write_mAB_or_mBA_tag(uint32_t type, bool
382382
dataStruct->write8(kNumChannels); // Input channels
383383
dataStruct->write8(kNumChannels); // Output channels
384384
dataStruct->write16(0); // Reserved
385-
dataStruct->write32(Endian_SwapBE32(b_curves_offset)); // B curve offset
386-
dataStruct->write32(Endian_SwapBE32(0)); // Matrix offset (ignored)
387-
dataStruct->write32(Endian_SwapBE32(0)); // M curve offset (ignored)
388-
dataStruct->write32(Endian_SwapBE32(clut_offset)); // CLUT offset
389-
dataStruct->write32(Endian_SwapBE32(a_curves_offset)); // A curve offset
385+
dataStruct->write32(Endian_SwapBE32(static_cast<uint32_t>(b_curves_offset))); // B curve offset
386+
dataStruct->write32(Endian_SwapBE32(0)); // Matrix offset (ignored)
387+
dataStruct->write32(Endian_SwapBE32(0)); // M curve offset (ignored)
388+
dataStruct->write32(Endian_SwapBE32(static_cast<uint32_t>(clut_offset))); // CLUT offset
389+
dataStruct->write32(Endian_SwapBE32(static_cast<uint32_t>(a_curves_offset))); // A curve offset
390390
for (size_t i = 0; i < kNumChannels; ++i) {
391391
if (dataStruct->write(b_curves_data[i]->getData(), b_curves_data[i]->getLength())) {
392392
return dataStruct;
@@ -560,8 +560,8 @@ std::shared_ptr<DataStruct> IccHelper::writeIccProfile(uhdr_color_transfer_t tf,
560560
// Write the header.
561561
header.data_color_space = Endian_SwapBE32(Signature_RGB);
562562
header.pcs = Endian_SwapBE32(tf == UHDR_CT_PQ ? Signature_Lab : Signature_XYZ);
563-
header.size = Endian_SwapBE32(profile_size);
564-
header.tag_count = Endian_SwapBE32(tags.size());
563+
header.size = Endian_SwapBE32(static_cast<uint32_t>(profile_size));
564+
header.tag_count = Endian_SwapBE32(static_cast<uint32_t>(tags.size()));
565565

566566
if (!dataStruct->write(&header, sizeof(header))) {
567567
ALOGE("writeIccProfile(): error in header");
@@ -571,11 +571,11 @@ std::shared_ptr<DataStruct> IccHelper::writeIccProfile(uhdr_color_transfer_t tf,
571571
// Write the tag table. Track the offset and size of the previous tag to
572572
// compute each tag's offset. An empty SkData indicates that the previous
573573
// tag is to be reused.
574-
uint32_t last_tag_offset = sizeof(header) + tag_table_size;
574+
uint32_t last_tag_offset = static_cast<uint32_t>(sizeof(header) + tag_table_size);
575575
uint32_t last_tag_size = 0;
576576
for (const auto& tag : tags) {
577577
last_tag_offset = last_tag_offset + last_tag_size;
578-
last_tag_size = tag.second->getLength();
578+
last_tag_size = static_cast<uint32_t>(tag.second->getLength());
579579
uint32_t tag_table_entry[3] = {
580580
Endian_SwapBE32(tag.first),
581581
Endian_SwapBE32(last_tag_offset),

lib/src/jpegdecoderhelper.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -496,9 +496,9 @@ uhdr_error_info_t JpegDecoderHelper::decodeToCSYCbCr(jpeg_decompress_struct* cin
496496
JDIMENSION mcu_scanline_start[kMaxNumComponents];
497497

498498
for (int i = 0; i < cinfo->num_components; i++) {
499-
mcu_scanline_start[i] =
499+
mcu_scanline_start[i] = static_cast<JDIMENSION>(
500500
std::ceil(((float)cinfo->output_scanline * cinfo->comp_info[i].v_samp_factor) /
501-
cinfo->max_v_samp_factor);
501+
cinfo->max_v_samp_factor));
502502

503503
for (int j = 0; j < cinfo->comp_info[i].v_samp_factor * DCTSIZE; j++) {
504504
JDIMENSION scanline = mcu_scanline_start[i] + j;

lib/src/jpegencoderhelper.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@ uhdr_error_info_t JpegEncoderHelper::encode(const uint8_t* planes[3], const unsi
200200
// start compress
201201
jpeg_start_compress(&cinfo, TRUE);
202202
if (iccBuffer != nullptr && iccSize > 0) {
203-
jpeg_write_marker(&cinfo, JPEG_APP0 + 2, static_cast<const JOCTET*>(iccBuffer), iccSize);
203+
jpeg_write_marker(&cinfo, JPEG_APP0 + 2, static_cast<const JOCTET*>(iccBuffer),
204+
static_cast<unsigned int>(iccSize));
204205
}
205206
if (isGainMapImg) {
206207
char comment[255];
@@ -278,9 +279,9 @@ uhdr_error_info_t JpegEncoderHelper::compressYCbCr(jpeg_compress_struct* cinfo,
278279
JDIMENSION mcu_scanline_start[kMaxNumComponents];
279280

280281
for (int i = 0; i < cinfo->num_components; i++) {
281-
mcu_scanline_start[i] =
282+
mcu_scanline_start[i] = static_cast<JDIMENSION>(
282283
std::ceil(((float)cinfo->next_scanline * cinfo->comp_info[i].v_samp_factor) /
283-
cinfo->max_v_samp_factor);
284+
cinfo->max_v_samp_factor));
284285

285286
for (int j = 0; j < cinfo->comp_info[i].v_samp_factor * DCTSIZE; j++) {
286287
JDIMENSION scanline = mcu_scanline_start[i] + j;

lib/src/jpegr.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,7 +1603,7 @@ uhdr_error_info_t JpegR::applyGainMap(uhdr_raw_image_t* sdr_intent, uhdr_raw_ima
16031603
if (map_scale_factor != floorf(map_scale_factor)) {
16041604
gain = sampleMap(gainmap_img, map_scale_factor, x, y);
16051605
} else {
1606-
gain = sampleMap(gainmap_img, map_scale_factor, x, y, idwTable);
1606+
gain = sampleMap(gainmap_img, static_cast<size_t>(map_scale_factor), x, y, idwTable);
16071607
}
16081608

16091609
#if USE_APPLY_GAIN_LUT
@@ -1618,8 +1618,8 @@ uhdr_error_info_t JpegR::applyGainMap(uhdr_raw_image_t* sdr_intent, uhdr_raw_ima
16181618
gain = sampleMap3Channel(gainmap_img, map_scale_factor, x, y,
16191619
gainmap_img->fmt == UHDR_IMG_FMT_32bppRGBA8888);
16201620
} else {
1621-
gain = sampleMap3Channel(gainmap_img, map_scale_factor, x, y, idwTable,
1622-
gainmap_img->fmt == UHDR_IMG_FMT_32bppRGBA8888);
1621+
gain = sampleMap3Channel(gainmap_img, static_cast<size_t>(map_scale_factor), x, y,
1622+
idwTable, gainmap_img->fmt == UHDR_IMG_FMT_32bppRGBA8888);
16231623
}
16241624

16251625
#if USE_APPLY_GAIN_LUT
@@ -1845,7 +1845,7 @@ GlobalTonemapOutputs globalTonemap(const std::array<float, 3>& rgb_in, float hea
18451845
uint8_t ScaleTo8Bit(float value) {
18461846
constexpr float kMaxValFloat = 255.0f;
18471847
constexpr int kMaxValInt = 255;
1848-
return std::clamp(static_cast<int>(std::round(value * kMaxValFloat)), 0, kMaxValInt);
1848+
return static_cast<uint8_t>(std::clamp(static_cast<int>(std::round(value * kMaxValFloat)), 0, kMaxValInt));
18491849
}
18501850

18511851
uhdr_error_info_t JpegR::toneMap(uhdr_raw_image_t* hdr_intent, uhdr_raw_image_t* sdr_intent) {

lib/src/multipictureformat.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,15 @@ std::shared_ptr<DataStruct> generateMpf(size_t primary_image_size, size_t primar
6969

7070
// Write the MP entries for primary image
7171
dataStruct->write32(Endian_SwapBE32(kMPEntryAttributeFormatJpeg | kMPEntryAttributeTypePrimary));
72-
dataStruct->write32(Endian_SwapBE32(primary_image_size));
73-
dataStruct->write32(Endian_SwapBE32(primary_image_offset));
72+
dataStruct->write32(Endian_SwapBE32(static_cast<uint32_t>(primary_image_size)));
73+
dataStruct->write32(Endian_SwapBE32(static_cast<uint32_t>(primary_image_offset)));
7474
dataStruct->write16(0);
7575
dataStruct->write16(0);
7676

7777
// Write the MP entries for secondary image
7878
dataStruct->write32(Endian_SwapBE32(kMPEntryAttributeFormatJpeg));
79-
dataStruct->write32(Endian_SwapBE32(secondary_image_size));
80-
dataStruct->write32(Endian_SwapBE32(secondary_image_offset));
79+
dataStruct->write32(Endian_SwapBE32(static_cast<uint32_t>(secondary_image_size)));
80+
dataStruct->write32(Endian_SwapBE32(static_cast<uint32_t>(secondary_image_offset)));
8181
dataStruct->write16(0);
8282
dataStruct->write16(0);
8383

0 commit comments

Comments
 (0)