Skip to content
This repository has been archived by the owner on Apr 8, 2024. It is now read-only.

Commit

Permalink
Add support for consuming chroma sampling 4:1:1 and 4:4:0
Browse files Browse the repository at this point in the history
Summary:
Adding support for 4:1:1 and 4:4:0 sub-sampling as discussed in #8

**Test plan**

Followed the reproduction steps from the file with a file captured in 4:1:1 as input.

Before:
 ```
SpectrumException{
  name='decompressor_unexpected_chroma_sampling',
  message='decompressor_unexpected_chroma_sampling',
  location='image::ChromaSamplingMode     facebook::spectrum::plugins::jpeg::LibJpegDecompressor::_chromaSamplingMode():103',
  ...
```

After:
```
SpectrumResult{
  ruleName='base',
  inputImageSpecification=ImageSpecification{size=ImageSize{width=1431, height=2688}, format=jpeg, pixelSpecification=ImagePixelSpecification{colorModel=RGB, bytesPerPixel=3, alphaInfo=NONE, componentsOrder=NATURAL}, orientation=UP, chromaSamplingMode=S411, metadata=ImageMetadata{}},
  outputImageSpecification=ImageSpecification{size=ImageSize{width=533, height=1000}, format=jpeg, pixelSpecification=ImagePixelSpecification{colorModel=RGB, bytesPerPixel=3, alphaInfo=NONE, componentsOrder=NATURAL}, orientation=UP, chromaSamplingMode=S420, metadata=ImageMetadata{}},
  totalBytesRead=1854587,
  totalBytesWritten=58664
}
```

Reviewed By: cuva

Differential Revision: D13233286

fbshipit-source-id: 408cf5f74a6c1dedefa35b92449923e3c73b8dc6
  • Loading branch information
Daniel Hugenroth authored and facebook-github-bot committed Nov 28, 2018
1 parent 8a67810 commit d36ba0e
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@ public enum ImageChromaSamplingMode {
/** Sampling mode 4:2:0 uses 4 luma information per chroma information */
S420(1),

/** Sampling mode 4:2:2 uses 2 vertical luma information per chroma information */
S422(2);
/** Sampling mode 4:2:2 uses 2 horizontal luma information per chroma information */
S422(2),

/** Sampling mode 4:1:1 uses 4 horizontal luma information per chroma information */
S411(3),

/** Sampling mode 4:4:0 uses 2 vertical luma information per chroma information */
S440(4);

@DoNotStrip public final int value;

Expand Down
14 changes: 14 additions & 0 deletions cpp/spectrum/Configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ std::string Configuration::General::chromaSamplingModeOverrideStringFromValue(
return "S420";
case ChromaSamplingModeOverride::S422:
return "S422";
case ChromaSamplingModeOverride::S440:
return "S440";
case ChromaSamplingModeOverride::S411:
return "S411";
default:
return core::makeUnknownWithValue<std::uint8_t>(
chromaSamplingModeOverride);
Expand All @@ -78,6 +82,10 @@ Configuration::General::chromaSamplingModeOverrideFromChromaSamplingMode(
return Configuration::General::ChromaSamplingModeOverride::S420;
case image::ChromaSamplingMode::S422:
return Configuration::General::ChromaSamplingModeOverride::S422;
case image::ChromaSamplingMode::S440:
return Configuration::General::ChromaSamplingModeOverride::S440;
case image::ChromaSamplingMode::S411:
return Configuration::General::ChromaSamplingModeOverride::S411;
}
}

Expand All @@ -94,6 +102,10 @@ Configuration::General::chromaSamplingModeOverrideToChromaSamplingMode(
return image::ChromaSamplingMode::S420;
case Configuration::General::ChromaSamplingModeOverride::S422:
return image::ChromaSamplingMode::S422;
case Configuration::General::ChromaSamplingModeOverride::S440:
return image::ChromaSamplingMode::S440;
case Configuration::General::ChromaSamplingModeOverride::S411:
return image::ChromaSamplingMode::S411;
}
}

Expand All @@ -106,6 +118,8 @@ Configuration::General::chromaSamplingModeOverrideFromValue(const int value) {
case ChromaSamplingModeOverride::S444:
case ChromaSamplingModeOverride::S420:
case ChromaSamplingModeOverride::S422:
case ChromaSamplingModeOverride::S440:
case ChromaSamplingModeOverride::S411:
return chromaSamplingModeOverride;
default:
SPECTRUM_UNREACHABLE_CONFIGURATION_CHROMA_SAMPLING_MODE_OVERRIDE(
Expand Down
2 changes: 2 additions & 0 deletions cpp/spectrum/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ struct Configuration {
S444,
S420,
S422,
S411,
S440,
};

static std::string chromaSamplingModeOverrideStringFromValue(
Expand Down
6 changes: 6 additions & 0 deletions cpp/spectrum/image/Specification.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ std::string chromaSamplingModeStringValue(
return "420";
case ChromaSamplingMode::S422:
return "422";
case ChromaSamplingMode::S411:
return "411";
case ChromaSamplingMode::S440:
return "440";
}
}

Expand All @@ -30,6 +34,8 @@ ChromaSamplingMode chromaSamplingModeFromValue(const int value) {
case image::ChromaSamplingMode::S444:
case image::ChromaSamplingMode::S420:
case image::ChromaSamplingMode::S422:
case image::ChromaSamplingMode::S411:
case image::ChromaSamplingMode::S440:
return chromaSamplingMode;
default:
SPECTRUM_UNREACHABLE_IMAGE_CHROMA_SAMPLING_MODE(chromaSamplingMode);
Expand Down
2 changes: 2 additions & 0 deletions cpp/spectrum/image/Specification.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ enum class ChromaSamplingMode {
S444 = 0,
S420 = 1,
S422 = 2,
S411 = 3,
S440 = 4,
};

#define SPECTRUM_UNREACHABLE_IMAGE_CHROMA_SAMPLING_MODE(chromaSamplingMode) \
Expand Down
8 changes: 8 additions & 0 deletions cpp/spectrum/plugins/jpeg/LibJpegDecompressor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ image::ChromaSamplingMode LibJpegDecompressor::_chromaSamplingMode() {
libJpegDecompressInfo.max_h_samp_factor == 2 &&
libJpegDecompressInfo.max_v_samp_factor == 1) {
return image::ChromaSamplingMode::S422;
} else if (
libJpegDecompressInfo.max_h_samp_factor == 4 &&
libJpegDecompressInfo.max_v_samp_factor == 1) {
return image::ChromaSamplingMode::S411;
} else if (
libJpegDecompressInfo.max_h_samp_factor == 1 &&
libJpegDecompressInfo.max_v_samp_factor == 2) {
return image::ChromaSamplingMode::S440;
} else {
SPECTRUM_ERROR(codecs::error::DecompressorUnexpectedChromaSamplingMode);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ typedef NS_ENUM(NSUInteger, FSPConfigurationChromaSamplingModeOverride) {
FSPConfigurationChromaSamplingModeOverride444,
FSPConfigurationChromaSamplingModeOverride420,
FSPConfigurationChromaSamplingModeOverride422,
FSPConfigurationChromaSamplingModeOverride411,
FSPConfigurationChromaSamplingModeOverride440,
} NS_SWIFT_NAME(ConfigurationChromaSamplingMode);

NS_SWIFT_NAME(ConfigurationGeneral)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ static FSPConfigurationChromaSamplingModeOverride FSPConfigurationChromaSampling
return FSPConfigurationChromaSamplingModeOverride420;
case Configuration::General::ChromaSamplingModeOverride::S422:
return FSPConfigurationChromaSamplingModeOverride422;
case Configuration::General::ChromaSamplingModeOverride::S411:
return FSPConfigurationChromaSamplingModeOverride411;
case Configuration::General::ChromaSamplingModeOverride::S440:
return FSPConfigurationChromaSamplingModeOverride440;
}
}

Expand All @@ -166,6 +170,10 @@ static FSPConfigurationChromaSamplingModeOverride FSPConfigurationChromaSampling
return Configuration::General::ChromaSamplingModeOverride::S420;
case FSPConfigurationChromaSamplingModeOverride422:
return Configuration::General::ChromaSamplingModeOverride::S422;
case FSPConfigurationChromaSamplingModeOverride411:
return Configuration::General::ChromaSamplingModeOverride::S411;
case FSPConfigurationChromaSamplingModeOverride440:
return Configuration::General::ChromaSamplingModeOverride::S440;
}
}

Expand Down
2 changes: 2 additions & 0 deletions ios/SpectrumKit/SpectrumKit/Image/FSPImageSpecification.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ typedef NS_ENUM(NSUInteger, FSPImageChromaSamplingMode) {
FSPImageChromaSamplingMode444 = 0,
FSPImageChromaSamplingMode420 = 1,
FSPImageChromaSamplingMode422 = 2,
FSPImageChromaSamplingMode411 = 3,
FSPImageChromaSamplingMode440 = 4,
} NS_SWIFT_NAME(ImageChromaSamplingMode);

/**
Expand Down
8 changes: 8 additions & 0 deletions ios/SpectrumKit/SpectrumKit/Image/FSPImageSpecification.mm
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ + (instancetype)imageSpecificationFromImage:(UIImage *)image
return image::ChromaSamplingMode::S420;
case FSPImageChromaSamplingMode422:
return image::ChromaSamplingMode::S422;
case FSPImageChromaSamplingMode411:
return image::ChromaSamplingMode::S411;
case FSPImageChromaSamplingMode440:
return image::ChromaSamplingMode::S440;
}
}

Expand All @@ -153,6 +157,10 @@ static FSPImageChromaSamplingMode FSPImageChromaSamplingModeFromInternalChromaSa
return FSPImageChromaSamplingMode420;
case image::ChromaSamplingMode::S422:
return FSPImageChromaSamplingMode422;
case image::ChromaSamplingMode::S411:
return FSPImageChromaSamplingMode411;
case image::ChromaSamplingMode::S440:
return FSPImageChromaSamplingMode440;
}
}

Expand Down

0 comments on commit d36ba0e

Please sign in to comment.