Skip to content

Commit

Permalink
Reverted dsp/file.c
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilia Platone committed Aug 11, 2023
1 parent 1000edc commit 6c4256c
Showing 1 changed file with 1 addition and 105 deletions.
106 changes: 1 addition & 105 deletions libs/dsp/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include <locale.h>
#include <unistd.h>
#include <jpeglib.h>
#include <png.h>

dsp_stream_p* dsp_file_read_fits(const char* filename, int *channels, int stretch)
{
Expand Down Expand Up @@ -646,109 +645,6 @@ void dsp_file_write_jpeg_composite(const char* filename, int components, int qua
free(buf);
}

dsp_stream_p* dsp_file_read_png(const char* filename, int *channels, int stretch)
{
int width, height;
int components;
unsigned int type;
unsigned int row_stride;
int bpp;
unsigned char * buf;

FILE *infile = fopen (filename, "r");
if(infile == NULL)
return NULL;
png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png)
return NULL;
png_infop info = png_create_info_struct(png);
if (!info)
return NULL;
png_infop end_info = png_create_info_struct(png);
if (!end_info)
return NULL;
if (setjmp(png_jmpbuf(png)))
return NULL;
png_init_io(png, infile);
png_read_info(png, info);
width = (int)png_get_image_width(png, info);
height = (int)png_get_image_height(png, info);
type = png_get_color_type(png, info);
bpp = png_get_bit_depth(png, info);
if (type & PNG_COLOR_MASK_PALETTE)
png_set_palette_to_rgb(png);
if (type == PNG_COLOR_TYPE_GRAY && bpp < 8) {
png_set_expand_gray_1_2_4_to_8(png);
bpp = 8;
}
png_read_update_info(png, info);
components = (type & (PNG_COLOR_MASK_COLOR|PNG_COLOR_MASK_PALETTE)) ? 3 : 1;
if (type & PNG_COLOR_MASK_ALPHA)
components++;
row_stride = (unsigned int)(width * components * bpp / 8);
buf = (unsigned char *)malloc(row_stride * (size_t)(height));
unsigned char *image = (unsigned char *)buf;
int row;
for (row = 0; row < height; row++) {
png_read_row(png, image, NULL);
image += row_stride;
}
png_destroy_read_struct(&png, &info, &end_info);
fclose(infile);
if (type & PNG_COLOR_MASK_ALPHA)
components--;
*channels = components;
if(bpp == 16)
dsp_buffer_swap(((unsigned short*)buf), width * height * components);
return dsp_buffer_rgb_to_components(buf, 2, (int[]){width, height}, components, bpp, stretch);
}

void dsp_file_write_png_composite(const char* filename, int components, int compression, dsp_stream_p* stream)
{
int bpp = 16;
unsigned int row_stride;
int width = stream[0]->sizes[0];
int height = stream[0]->sizes[1];

png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png)
return;
png_infop info = png_create_info_struct(png);
if (!info)
return;
if (setjmp(png_jmpbuf(png)))
return;
FILE * outfile;
if ((outfile = fopen(filename, "wb")) == NULL) {
perr("can't open %s\n", filename);
return;
}
png_init_io(png, outfile);
png_set_IHDR(png,
info,
(unsigned int)width,
(unsigned int)height,
bpp,
components == 1 ? PNG_COLOR_TYPE_GRAY : PNG_COLOR_TYPE_RGB,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
png_set_compression_level(png, compression);
png_write_info(png, info);
row_stride = (unsigned int)(width * components * bpp / 8);
int row;
void *buf = malloc((size_t)(stream[0]->len*components*bpp/8));
unsigned char *image = (unsigned char *)buf;
dsp_buffer_components_to_rgb(stream, image, components, bpp);
for (row = 0; row < height; row++) {
png_write_row(png, image);
image += row_stride;
}
png_destroy_write_struct(&png, &info);
free(buf);
fclose(outfile);
}

dsp_t* dsp_file_bayer_2_gray(dsp_t *src, int width, int height)
{
int i;
Expand Down Expand Up @@ -1244,7 +1140,7 @@ void dsp_buffer_components_to_rgb(dsp_stream_p *stream, void* rgb, int component
{
ssize_t y;
int len = stream[0]->len * components;
dsp_t max = (dsp_t)((double)((1<<abs(bpp))-1));
dsp_t max = (dsp_t)((double)((1<<(size_t)abs(bpp))-1));
max = Min(max, dsp_t_max);
for(y = 0; y < components; y++) {
dsp_stream_p in = dsp_stream_copy(stream[y]);
Expand Down

0 comments on commit 6c4256c

Please sign in to comment.