Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use consistent formatting using clang-format #74

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
AccessModifierOffset: -1

AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlinesLeft: false
AlignOperands: true
AlignTrailingComments: true

AllowAllParametersOfDeclarationOnNextLine: false

AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Inline
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false

AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: false

BinPackArguments: true
BinPackParameters: false
BraceWrapping:
AfterControlStatement: false
AfterEnum: false
AfterFunction: false
AfterStruct: false
AfterUnion: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false

BreakBeforeBinaryOperators: None
BreakBeforeBraces: Custom
BreakBeforeTernaryOperators: true

ColumnLimit: 80
ContinuationIndentWidth: 4
DerivePointerAlignment: false
DisableFormat: false

ExperimentalAutoDetectBinPacking: false

## Not available with clang-format-3.9
# FixNamespaceComments: true

IndentCaseLabels: false
IndentWidth: 2
IndentWrappedFunctionNames: false

KeepEmptyLinesAtTheStartOfBlocks: false

MaxEmptyLinesToKeep: 1

NamespaceIndentation: None

PenaltyBreakBeforeFirstCallParameter: 16
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000
PenaltyReturnTypeOnItsOwnLine: 9000

Cpp11BracedListStyle: true

PointerAlignment: Left

ReflowComments: true

SortIncludes: false

SpaceAfterCStyleCast: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 2
SpacesInAngles: false
SpacesInContainerLiterals: false
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false

TabWidth: 2
UseTab: Never
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ AC_MSG_NOTICE([JPEG output: $have_libjpeg])
AC_MSG_NOTICE([PNG output: $have_libpng])
AC_MSG_NOTICE([---------------------------------------])

AM_EXTRA_RECURSIVE_TARGETS([format])
AC_CONFIG_FILES([Makefile])
AC_CONFIG_FILES([examples/Makefile])
AC_CONFIG_FILES([extra/Makefile])
Expand Down
6 changes: 6 additions & 0 deletions examples/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ test_c_api_LDFLAGS =
test_c_api_LDADD = ../libheif/libheif.la
test_c_api_SOURCES = test_c_api.c

CLANG_FORMAT := clang-format

format-local:
$(CLANG_FORMAT) --style=file -i \
$(wildcard *.c *.cc *.h)

EXTRA_DIST = \
demo.html \
example.heic \
Expand Down
15 changes: 8 additions & 7 deletions examples/encoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,18 @@ static const char kMetadataTypeExif[] = "Exif";

// static
bool Encoder::HasExifMetaData(const struct heif_image_handle* handle) {

heif_item_id metadata_id;
int count = heif_image_handle_get_list_of_metadata_block_IDs(handle, kMetadataTypeExif,
&metadata_id, 1);
int count = heif_image_handle_get_list_of_metadata_block_IDs(
handle, kMetadataTypeExif, &metadata_id, 1);
return count > 0;
}

// static
uint8_t* Encoder::GetExifMetaData(const struct heif_image_handle* handle, size_t* size) {
uint8_t* Encoder::GetExifMetaData(const struct heif_image_handle* handle,
size_t* size) {
heif_item_id metadata_id;
int count = heif_image_handle_get_list_of_metadata_block_IDs(handle, kMetadataTypeExif,
&metadata_id, 1);
int count = heif_image_handle_get_list_of_metadata_block_IDs(
handle, kMetadataTypeExif, &metadata_id, 1);

for (int i = 0; i < count; i++) {
size_t datasize = heif_image_handle_get_metadata_size(handle, metadata_id);
Expand All @@ -57,7 +57,8 @@ uint8_t* Encoder::GetExifMetaData(const struct heif_image_handle* handle, size_t
continue;
}

heif_error error = heif_image_handle_get_metadata(handle, metadata_id, data);
heif_error error =
heif_image_handle_get_metadata(handle, metadata_id, data);
if (error.code != heif_error_Ok) {
free(data);
continue;
Expand Down
11 changes: 7 additions & 4 deletions examples/encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,20 @@ class Encoder {
virtual heif_colorspace colorspace(bool has_alpha) const = 0;
virtual heif_chroma chroma(bool has_alpha) const = 0;

virtual void UpdateDecodingOptions(const struct heif_image_handle* handle,
struct heif_decoding_options *options) const {
virtual void UpdateDecodingOptions(
const struct heif_image_handle* handle,
struct heif_decoding_options* options) const {
// Override if necessary.
}

virtual bool Encode(const struct heif_image_handle* handle,
const struct heif_image* image, const std::string& filename) = 0;
const struct heif_image* image,
const std::string& filename) = 0;

protected:
static bool HasExifMetaData(const struct heif_image_handle* handle);
static uint8_t* GetExifMetaData(const struct heif_image_handle* handle, size_t* size);
static uint8_t* GetExifMetaData(const struct heif_image_handle* handle,
size_t* size);
};

#endif // EXAMPLE_ENCODER_H
61 changes: 30 additions & 31 deletions examples/encoder_jpeg.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ JpegEncoder::JpegEncoder(int quality) : quality_(quality) {
}
}

void JpegEncoder::UpdateDecodingOptions(const struct heif_image_handle* handle,
struct heif_decoding_options *options) const {
void JpegEncoder::UpdateDecodingOptions(
const struct heif_image_handle* handle,
struct heif_decoding_options* options) const {
if (HasExifMetaData(handle)) {
options->ignore_transformations = 1;
}
Expand All @@ -44,29 +45,26 @@ void JpegEncoder::OnJpegError(j_common_ptr cinfo) {
longjmp(handler->setjmp_buffer, 1);
}



#define ICC_MARKER (JPEG_APP0 + 2) /* JPEG marker code for ICC */
#define ICC_OVERHEAD_LEN 14 /* size of non-profile data in APP2 */
#define MAX_BYTES_IN_MARKER 65533 /* maximum data len of a JPEG marker */
#define ICC_MARKER (JPEG_APP0 + 2) /* JPEG marker code for ICC */
#define ICC_OVERHEAD_LEN 14 /* size of non-profile data in APP2 */
#define MAX_BYTES_IN_MARKER 65533 /* maximum data len of a JPEG marker */
#define MAX_DATA_BYTES_IN_MARKER (MAX_BYTES_IN_MARKER - ICC_OVERHEAD_LEN)

/*
* This routine writes the given ICC profile data into a JPEG file. It *must*
* be called AFTER calling jpeg_start_compress() and BEFORE the first call to
* jpeg_write_scanlines(). (This ordering ensures that the APP2 marker(s) will
* appear after the SOI and JFIF or Adobe markers, but before all else.)
*/
/*
* This routine writes the given ICC profile data into a JPEG file. It *must*
* be called AFTER calling jpeg_start_compress() and BEFORE the first call to
* jpeg_write_scanlines(). (This ordering ensures that the APP2 marker(s) will
* appear after the SOI and JFIF or Adobe markers, but before all else.)
*/

/* This function is copied almost as is from libjpeg-turbo */
/* This function is copied almost as is from libjpeg-turbo */

static
void jpeg_write_icc_profile(j_compress_ptr cinfo, const JOCTET *icc_data_ptr,
unsigned int icc_data_len)
{
unsigned int num_markers; /* total number of markers we'll write */
int cur_marker = 1; /* per spec, counting starts at 1 */
unsigned int length; /* number of bytes to write in this marker */
static void jpeg_write_icc_profile(j_compress_ptr cinfo,
const JOCTET* icc_data_ptr,
unsigned int icc_data_len) {
unsigned int num_markers; /* total number of markers we'll write */
int cur_marker = 1; /* per spec, counting starts at 1 */
unsigned int length; /* number of bytes to write in this marker */

/* Calculate the number of markers we'll need, rounding up of course */
num_markers = icc_data_len / MAX_DATA_BYTES_IN_MARKER;
Expand Down Expand Up @@ -115,7 +113,8 @@ void jpeg_write_icc_profile(j_compress_ptr cinfo, const JOCTET *icc_data_ptr,
}

bool JpegEncoder::Encode(const struct heif_image_handle* handle,
const struct heif_image* image, const std::string& filename) {
const struct heif_image* image,
const std::string& filename) {
FILE* fp = fopen(filename.c_str(), "wb");
if (!fp) {
fprintf(stderr, "Can't open %s: %s\n", filename.c_str(), strerror(errno));
Expand Down Expand Up @@ -151,32 +150,32 @@ bool JpegEncoder::Encode(const struct heif_image_handle* handle,
if (exifdata && exifsize > 4) {
static const uint8_t kExifMarker = JPEG_APP0 + 1;
jpeg_write_marker(&cinfo, kExifMarker, exifdata + 4,
static_cast<unsigned int>(exifsize - 4));
static_cast<unsigned int>(exifsize - 4));
free(exifdata);
}

size_t profile_size = heif_image_handle_get_raw_color_profile_size(handle);
if (profile_size > 0){
if (profile_size > 0) {
uint8_t* profile_data = static_cast<uint8_t*>(malloc(profile_size));
heif_image_handle_get_raw_color_profile(handle, profile_data);
jpeg_write_icc_profile(&cinfo, profile_data, (unsigned int)profile_size);
free(profile_data);
}

int stride_y;
const uint8_t* row_y = heif_image_get_plane_readonly(image, heif_channel_Y,
&stride_y);
const uint8_t* row_y =
heif_image_get_plane_readonly(image, heif_channel_Y, &stride_y);
int stride_u;
const uint8_t* row_u = heif_image_get_plane_readonly(image, heif_channel_Cb,
&stride_u);
const uint8_t* row_u =
heif_image_get_plane_readonly(image, heif_channel_Cb, &stride_u);
int stride_v;
const uint8_t* row_v = heif_image_get_plane_readonly(image, heif_channel_Cr,
&stride_v);
const uint8_t* row_v =
heif_image_get_plane_readonly(image, heif_channel_Cr, &stride_v);

JSAMPARRAY buffer = cinfo.mem->alloc_sarray(
reinterpret_cast<j_common_ptr>(&cinfo), JPOOL_IMAGE,
cinfo.image_width * cinfo.input_components, 1);
JSAMPROW row[1] = { buffer[0] };
JSAMPROW row[1] = {buffer[0]};

while (cinfo.next_scanline < cinfo.image_height) {
size_t offset_y = cinfo.next_scanline * stride_y;
Expand Down
16 changes: 8 additions & 8 deletions examples/encoder_jpeg.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,22 @@ class JpegEncoder : public Encoder {
return heif_colorspace_YCbCr;
}

heif_chroma chroma(bool has_alpha) const override {
return heif_chroma_420;
}
heif_chroma chroma(bool has_alpha) const override { return heif_chroma_420; }

void UpdateDecodingOptions(const struct heif_image_handle* handle,
struct heif_decoding_options *options) const override;
void UpdateDecodingOptions(
const struct heif_image_handle* handle,
struct heif_decoding_options* options) const override;

bool Encode(const struct heif_image_handle* handle,
const struct heif_image* image, const std::string& filename) override;
const struct heif_image* image,
const std::string& filename) override;

private:
static const int kDefaultQuality = 90;

struct ErrorHandler {
struct jpeg_error_mgr pub; /* "public" fields */
jmp_buf setjmp_buffer; /* for return to caller */
struct jpeg_error_mgr pub; /* "public" fields */
jmp_buf setjmp_buffer; /* for return to caller */
};

static void OnJpegError(j_common_ptr cinfo);
Expand Down
19 changes: 11 additions & 8 deletions examples/encoder_png.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ inline uint8_t clip(float value) {
}

bool PngEncoder::Encode(const struct heif_image_handle* handle,
const struct heif_image* image, const std::string& filename) {
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr,
nullptr, nullptr);
const struct heif_image* image,
const std::string& filename) {
png_structp png_ptr =
png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
if (!png_ptr) {
fprintf(stderr, "libpng initialization failed (1)\n");
return false;
Expand Down Expand Up @@ -69,18 +70,20 @@ bool PngEncoder::Encode(const struct heif_image_handle* handle,

png_init_io(png_ptr, fp);

bool withAlpha = (heif_image_get_chroma_format(image) == heif_chroma_interleaved_32bit);
bool withAlpha =
(heif_image_get_chroma_format(image) == heif_chroma_interleaved_32bit);

int width = heif_image_get_width(image, heif_channel_interleaved);
int height = heif_image_get_height(image, heif_channel_interleaved);
const int bitDepth = 8;
const int colorType = withAlpha ? PNG_COLOR_TYPE_RGBA : PNG_COLOR_TYPE_RGB;

png_set_IHDR(png_ptr, info_ptr, width, height, bitDepth, colorType,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE,
PNG_FILTER_TYPE_BASE);

size_t profile_size = heif_image_handle_get_raw_color_profile_size(handle);
if (profile_size > 0){
if (profile_size > 0) {
uint8_t* profile_data = static_cast<uint8_t*>(malloc(profile_size));
heif_image_handle_get_raw_color_profile(handle, profile_data);
char profile_name[] = "unknown";
Expand All @@ -98,8 +101,8 @@ bool PngEncoder::Encode(const struct heif_image_handle* handle,
uint8_t** row_pointers = new uint8_t*[height];

int stride_rgb;
const uint8_t* row_rgb = heif_image_get_plane_readonly(image,
heif_channel_interleaved, &stride_rgb);
const uint8_t* row_rgb = heif_image_get_plane_readonly(
image, heif_channel_interleaved, &stride_rgb);

for (int y = 0; y < height; ++y) {
row_pointers[y] = const_cast<uint8_t*>(&row_rgb[y * stride_rgb]);
Expand Down
3 changes: 2 additions & 1 deletion examples/encoder_png.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ class PngEncoder : public Encoder {
}

bool Encode(const struct heif_image_handle* handle,
const struct heif_image* image, const std::string& filename) override;
const struct heif_image* image,
const std::string& filename) override;

private:
};
Expand Down
Loading