Skip to content

Commit 25ec607

Browse files
-PNG support implemented and tested
1 parent 04f7f5b commit 25ec607

File tree

4 files changed

+149
-29
lines changed

4 files changed

+149
-29
lines changed

Image.cpp

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
#include <stdlib.h>
22
#include <string.h>
33
#include "Image.h"
4-
#include "png.h"
4+
#include "_png.h"
55
#include "PPM.h"
66
#include "Hexen.h"
7+
#include <MT2D/MessageBox/MT2D_MessageBox.h>
8+
#include <MT2D/MT2D_Display.h>
9+
#include <MT2D/MT2D.h>
10+
11+
extern char str_buffer[200];
712

813
ImageFormat PATH_GetType(char *PATH) {
914
ImageFormat _return= TYPE_UNSUPPORTED;
@@ -43,6 +48,7 @@ Image *Image_Load(char*PATH) {
4348
ImageFormat Type;
4449

4550
PPMImage *ppmimg;
51+
png_info *png;
4652

4753
if (PATH) {
4854
Type = PATH_GetType(PATH);
@@ -58,7 +64,11 @@ Image *Image_Load(char*PATH) {
5864
Img->Type = Type;
5965
break;
6066
}
67+
break;
6168
case TYPE_PNG:
69+
Img = PNG_read_file(PATH);
70+
Img->Loaded = true;
71+
Img->Type = Type;
6272
break;
6373
}
6474
}
@@ -92,6 +102,8 @@ Pixel *Image_GetPixel(Image *img, unsigned int X, unsigned int Y) {
92102
case TYPE_PPM:
93103
p = Image_GetPixel(img, X + Y * img->Width);
94104
break;
105+
case TYPE_PNG:
106+
p = PNG_Get_Pixel(img, X, Y);
95107
}
96108
return p;
97109
}
@@ -103,6 +115,11 @@ Pixel *Image_GetPixel(Image *img,int offset) {
103115
case TYPE_PPM:
104116
p = PPM_GetPixel((PPMImage*)img->ImagePointer, offset);
105117
break;
118+
case TYPE_PNG:
119+
Y = offset / img->Width;
120+
X = offset - Y * img->Width;
121+
p = Image_GetPixel(img, X, Y);
122+
break;
106123
default:
107124

108125
Y = offset / img->Width;
@@ -115,6 +132,51 @@ Pixel *Image_GetPixel(Image *img,int offset) {
115132

116133

117134
Pixel *Image_GetPalette(Image *image, int paletteLength) {
135+
int i = 0, j = 0;
136+
int ImgLength = image->Width * image->Height;
137+
Pixel *palette = (Pixel*)malloc(paletteLength * sizeof(Pixel));//hard coded
138+
Pixel *tmp;
139+
bool *palette_started = (bool*)malloc(paletteLength * sizeof(bool));
140+
for (int i = 0; i < paletteLength; i++) {
141+
palette[i].blue = 0;
142+
palette[i].green = 0;
143+
palette[i].red = 0;
144+
palette_started[i] = false;
145+
}
146+
int disp_pos = 8;
147+
insert_string_on_display("INDEX R ,G ,B", 7, 10, DISPLAY_WINDOW1);
148+
for (i = 0; i < ImgLength; i++) {
149+
for (j = 0; j < paletteLength; j++) {
150+
tmp = Image_GetPixel(image, i);
151+
if (palette[j].red == tmp->red && palette[j].green == tmp->green && palette[j].blue == tmp->blue) {
152+
//we found the same color on index, jump.
153+
break;
154+
}
155+
if (palette_started[j] == false) {
156+
//color not found in the index, jump.
157+
palette[j].red = tmp->red;
158+
palette[j].green = tmp->green;
159+
palette[j].blue = tmp->blue;
160+
palette_started[j] = true;
161+
/*MT2D STUFF*/
162+
sprintf(str_buffer, "%2d %3d ,%3d ,%3d", j, palette[j].red, palette[j].green, palette[j].blue);
163+
insert_string_on_display(str_buffer, disp_pos, 10, DISPLAY_WINDOW1);
164+
disp_pos++;
165+
MT2D_Draw_Window(DISPLAY_WINDOW1);
166+
/*=========*/
167+
break;
168+
}
169+
}
170+
if (j == 16) {
171+
sprintf(str_buffer, "ERROR:this image has more than 16 colors, you can fix that with the gimp software... More info here:https://docs.gimp.org/en/gimp-image-convert-indexed.html");
172+
MT2D_MessageBox(str_buffer);
173+
return 0;
174+
}
175+
}
176+
return palette;
177+
178+
179+
118180
Pixel *Palette = 0;
119181
switch(image->Type){
120182
case TYPE_PPM:

Source.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ void screen_reset() {
2525
insert_string_on_display("+ powered by +", 21, 60, DISPLAY_WINDOW1);
2626
insert_string_on_display("+ MT2D ENGINE +", 22, 60, DISPLAY_WINDOW1);
2727
insert_string_on_display("+++++++++++++++", 23, 60, DISPLAY_WINDOW1);
28-
insert_string_on_display("HexStartup++ v1.01, created by IBM5155", 24, 40, DISPLAY_WINDOW1);
28+
insert_string_on_display("HexStartup++ v2.0, created by IBM5155", 24, 40, DISPLAY_WINDOW1);
2929
MT2D_Draw_Window(DISPLAY_WINDOW1);
3030
}
3131

_PNG.cpp

Lines changed: 74 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1+
#include <stdlib.h>
2+
#include "Image.h"
13
#include <png.h>
4+
#include "_PNG.h"
5+
#include <MT2D/MessageBox/MT2D_MessageBox.h>
6+
#include <MT2D/MT2D_Display.h>
7+
#include <MT2D/MT2D.h>
28

39
/*
410
* A simple libpng example program
@@ -17,63 +23,105 @@
1723
* of the X11 license.
1824
*
1925
*/
20-
/*
21-
void read_png_file(char *filename) {
26+
27+
extern char str_buffer[200];
28+
29+
30+
Image *PNG_read_file(char *filename) {
31+
Image *img = Image_CreateBlank();
32+
PNG_Info *img_struct = (PNG_Info*)malloc(sizeof(PNG_Info));
2233
FILE *fp = fopen(filename, "rb");
2334

24-
png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
25-
if (!png) abort();
35+
img_struct->png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
36+
if (!img_struct->png) {
37+
sprintf(str_buffer, "PNG ERROR:Unable to read image:", filename);
38+
MT2D_MessageBox(str_buffer);
39+
fclose(fp);
40+
return 0;
41+
}
2642

27-
png_infop info = png_create_info_struct(png);
28-
if (!info) abort();
43+
img_struct->info = png_create_info_struct(img_struct->png);
44+
if (!img_struct->info) {
45+
sprintf(str_buffer, "PNG ERROR:Unable to create the info of from this image: ", filename);
46+
MT2D_MessageBox(str_buffer);
47+
fclose(fp);
48+
return 0;
49+
}
2950

30-
if (setjmp(png_jmpbuf(png))) abort();
51+
if (setjmp(png_jmpbuf(img_struct->png))) {
52+
sprintf(str_buffer, "PNG ERROR:I really dont know whats this error is but if the original author said it's an error, so is it. : ", filename);
53+
MT2D_MessageBox(str_buffer);
54+
fclose(fp);
55+
return 0;
56+
}
3157

32-
png_init_io(png, fp);
58+
png_init_io(img_struct->png, fp);
3359

34-
png_read_info(png, info);
60+
png_read_info(img_struct->png, img_struct->info);
3561

36-
width = png_get_image_width(png, info);
37-
height = png_get_image_height(png, info);
38-
color_type = png_get_color_type(png, info);
39-
bit_depth = png_get_bit_depth(png, info);
62+
img->Width = png_get_image_width(img_struct->png, img_struct->info);
63+
img->Height = png_get_image_height(img_struct->png, img_struct->info);
64+
png_byte color_type = png_get_color_type(img_struct->png, img_struct->info);
65+
png_byte bit_depth = png_get_bit_depth(img_struct->png, img_struct->info);
4066

4167
// Read any color_type into 8bit depth, RGBA format.
4268
// See http://www.libpng.org/pub/png/libpng-manual.txt
4369

4470
if (bit_depth == 16)
45-
png_set_strip_16(png);
71+
png_set_strip_16(img_struct->png);
4672

4773
if (color_type == PNG_COLOR_TYPE_PALETTE)
48-
png_set_palette_to_rgb(png);
74+
png_set_palette_to_rgb(img_struct->png);
4975

5076
// PNG_COLOR_TYPE_GRAY_ALPHA is always 8 or 16bit depth.
5177
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
52-
png_set_expand_gray_1_2_4_to_8(png);
78+
png_set_expand_gray_1_2_4_to_8(img_struct->png);
5379

54-
if (png_get_valid(png, info, PNG_INFO_tRNS))
55-
png_set_tRNS_to_alpha(png);
80+
if (png_get_valid(img_struct->png, img_struct->info, PNG_INFO_tRNS))
81+
png_set_tRNS_to_alpha(img_struct->png);
5682

5783
// These color_type don't have an alpha channel then fill it with 0xff.
5884
if (color_type == PNG_COLOR_TYPE_RGB ||
5985
color_type == PNG_COLOR_TYPE_GRAY ||
6086
color_type == PNG_COLOR_TYPE_PALETTE)
61-
png_set_filler(png, 0xFF, PNG_FILLER_AFTER);
87+
png_set_filler(img_struct->png, 0xFF, PNG_FILLER_AFTER);
6288

6389
if (color_type == PNG_COLOR_TYPE_GRAY ||
6490
color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
65-
png_set_gray_to_rgb(png);
91+
png_set_gray_to_rgb(img_struct->png);
6692

67-
png_read_update_info(png, info);
93+
png_read_update_info(img_struct->png, img_struct->info);
6894

69-
row_pointers = (png_bytep*)malloc(sizeof(png_bytep) * height);
70-
for (int y = 0; y < height; y++) {
71-
row_pointers[y] = (png_byte*)malloc(png_get_rowbytes(png, info));
95+
png_bytep *row_pointers = (png_bytep*)malloc(sizeof(png_bytep) * img->Height);
96+
for (int y = 0; y < img->Height; y++) {
97+
row_pointers[y] = (png_byte*)malloc(png_get_rowbytes(img_struct->png, img_struct->info));
7298
}
7399

74-
png_read_image(png, row_pointers);
100+
png_read_image(img_struct->png, row_pointers);
101+
img_struct->row_pointers = row_pointers;
102+
img->ImagePointer = img_struct;
75103

76104
fclose(fp);
105+
return img;
77106
}
78107

79-
*/
108+
Pixel *PNG_Get_Pixel(Image *img, int X, int Y) {
109+
PNG_Info *Pi = (PNG_Info*)img->ImagePointer;
110+
Pixel *P = (Pixel*)malloc(sizeof(Pixel));
111+
if (png_get_color_type(Pi->png, Pi->info) == PNG_COLOR_TYPE_RGB);
112+
// abort_("[process_file] input file is PNG_COLOR_TYPE_RGB but must be PNG_COLOR_TYPE_RGBA "
113+
// "(lacks the alpha channel)");
114+
115+
if (png_get_color_type(Pi->png, Pi->info) != PNG_COLOR_TYPE_RGBA);
116+
// abort_("[process_file] color_type of input file must be PNG_COLOR_TYPE_RGBA (%d) (is %d)",
117+
// PNG_COLOR_TYPE_RGBA, png_get_color_type(img->png, img->info));
118+
119+
png_byte* row = Pi->row_pointers[Y];
120+
png_byte* ptr = &(row[X * 4]);
121+
// printf("Pixel at position [ %d - %d ] has RGBA values: %d - %d - %d - %d\n",
122+
// x, y, ptr[0], ptr[1], ptr[2], ptr[3]);
123+
P->red = ptr[0];
124+
P->green = ptr[1];
125+
P->blue = ptr[2];
126+
return P;
127+
}

_PNG.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,14 @@
33
PPMImage *readPPM(const char *filename);
44
Pixel *Get_Palette(PPMImage *image, int paletteLength);
55
6-
*/
6+
*/
7+
#include <png.h>
8+
9+
struct PNG_Info {
10+
png_structp png;
11+
png_infop info;
12+
png_bytep *row_pointers;
13+
};
14+
15+
Image *PNG_read_file(char *filename);
16+
Pixel *PNG_Get_Pixel(Image *img, int X, int Y);

0 commit comments

Comments
 (0)