Skip to content

Commit

Permalink
fix code style
Browse files Browse the repository at this point in the history
  • Loading branch information
LiuYang328 committed Oct 29, 2024
1 parent 2bc1cd7 commit 0e832d7
Showing 1 changed file with 35 additions and 37 deletions.
72 changes: 35 additions & 37 deletions frontend/Interfaces/buddy/DIP/ImgContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
#define FRONTEND_INTERFACES_BUDDY_DIP_IMGCONTAINER

#include "buddy/Core/Container.h"
#include <array>
#include <cstring>
#include <string>
#include <fstream>
#include <memory>
#include <array>
#include <string>
#ifdef BUDDY_ENABLE_PNG
#include <png.h>
#endif
Expand Down Expand Up @@ -75,7 +75,7 @@ template <typename T, size_t N> class Image : public MemRef<T, N> {
Image(std::string filename, ImageModes mode, bool norm = false);

// from data to initialize image
Image(T *data, intptr_t sizes[N]) : MemRef<T, N>(data, sizes) {};
Image(T *data, intptr_t sizes[N]) : MemRef<T, N>(data, sizes){};

// Retrieves the name of the current image format as a string.
std::string getFormatName() const {
Expand Down Expand Up @@ -128,9 +128,6 @@ template <typename T, size_t N> class Image : public MemRef<T, N> {
#endif
};

// template <typename T, std::size_t N>
// Image<T, N>::Image(T *data, intptr_t sizes[N]): MemRef<T, N>(data, sizes) {}

// Image Container Constructor
// Constructs an image container object from the image file path.
template <typename T, std::size_t N>
Expand Down Expand Up @@ -651,42 +648,42 @@ bool Image<T, N>::decodePNG(const std::vector<uint8_t> &fileData) {
}
#endif

template <typename T, size_t N>
int findFormat(const std::string &_ext){
template <typename T, size_t N> int findFormat(const std::string &_ext) {
if (_ext.size() <= 1)
return 0;

const char *ext = strrchr(_ext.c_str(), '.');
if (!ext)
return 0;

if (strcmp(ext, ".bmp") == 0) {
return 1;
} else {
std::cerr << "Unsupported to generate" << ext << "format image" << std::endl;
std::cerr << "Unsupported to generate" << ext << "format image"
<< std::endl;
return 0;
}
}

template <typename T, size_t N>
static void imageWrite(const std::string &filename, Image<T, N> &image){
static void imageWrite(const std::string &filename, Image<T, N> &image) {
int imformat = findFormat<T, N>(filename);
switch (imformat) {
case 1:
BMPEncode(filename, image);
break;
default:
return;
case 1:
BMPEncode(filename, image);
break;
default:
return;
}
return;
}

template <typename T, size_t N>
void BMPEncode(const std::string &filename, Image<T, N> &image){
void BMPEncode(const std::string &filename, Image<T, N> &image) {
std::ofstream bmp(filename, std::ios::binary);
if (!bmp) {
std::cerr << "Failed to create file" << std::endl;
return;
std::cerr << "Failed to create file" << std::endl;
return;
}
int width = image.getSizes()[3];
int height = image.getSizes()[2];
Expand All @@ -706,28 +703,28 @@ void BMPEncode(const std::string &filename, Image<T, N> &image){
// Write file header
bmp.write("BM", 2);
int fileSizeInt = validToInt(fileSize);
bmp.write(reinterpret_cast<char*>(&fileSizeInt), 4);
bmp.write(reinterpret_cast<char*>(&zero), 4);
bmp.write(reinterpret_cast<char*>(&headerSize), 4);
bmp.write(reinterpret_cast<char *>(&fileSizeInt), 4);
bmp.write(reinterpret_cast<char *>(&zero), 4);
bmp.write(reinterpret_cast<char *>(&headerSize), 4);

// Write bitmap header
bmp.write(reinterpret_cast<char*>(&bitmapHeaderSize), 4);
bmp.write(reinterpret_cast<char*>(&width), 4);
bmp.write(reinterpret_cast<char*>(&height), 4);
bmp.write(reinterpret_cast<char*>(&one), 2);
bmp.write(reinterpret_cast<char *>(&bitmapHeaderSize), 4);
bmp.write(reinterpret_cast<char *>(&width), 4);
bmp.write(reinterpret_cast<char *>(&height), 4);
bmp.write(reinterpret_cast<char *>(&one), 2);
int bitDepth = channels << 3;
bmp.write(reinterpret_cast<char*>(&(bitDepth)), 2);
bmp.write(reinterpret_cast<char*>(&zero), 4);
bmp.write(reinterpret_cast<char*>(&zero), 4);
bmp.write(reinterpret_cast<char*>(&zero), 4);
bmp.write(reinterpret_cast<char*>(&zero), 4);
bmp.write(reinterpret_cast<char*>(&zero), 4);
bmp.write(reinterpret_cast<char*>(&zero), 4);
bmp.write(reinterpret_cast<char *>(&(bitDepth)), 2);
bmp.write(reinterpret_cast<char *>(&zero), 4);
bmp.write(reinterpret_cast<char *>(&zero), 4);
bmp.write(reinterpret_cast<char *>(&zero), 4);
bmp.write(reinterpret_cast<char *>(&zero), 4);
bmp.write(reinterpret_cast<char *>(&zero), 4);
bmp.write(reinterpret_cast<char *>(&zero), 4);

// Write palette
if (channels == 1) {
FillPalette(palette, 8);
bmp.write(reinterpret_cast<char*>(&palette), sizeof(palette));
bmp.write(reinterpret_cast<char *>(&palette), sizeof(palette));
}

// Write image data
Expand All @@ -736,14 +733,15 @@ void BMPEncode(const std::string &filename, Image<T, N> &image){
for (int y = height - 1; y >= 0; y--) {
for (int i = 0; i < width; i++) {
for (int t = channels - 1; t >= 0; t--) {
unsigned char pixel= static_cast<unsigned char>(data[y * width + i + t * step]);
bmp.write(reinterpret_cast<char*>(&pixel), 1);
unsigned char pixel =
static_cast<unsigned char>(data[y * width + i + t * step]);
bmp.write(reinterpret_cast<char *>(&pixel), 1);
}
}
if (fileStep > width * channels)
bmp.write(zeropad, fileStep - width * channels);
}

bmp.close();
}

Expand Down

0 comments on commit 0e832d7

Please sign in to comment.