Skip to content

Commit

Permalink
-Included PNG libs.
Browse files Browse the repository at this point in the history
-Remodelated all the image struct so every part of the code will relate with a generic type of image, and every specific code from each format will be separated...
  • Loading branch information
lucas-zimerman committed May 31, 2017
1 parent fe197a4 commit 04f7f5b
Show file tree
Hide file tree
Showing 19 changed files with 6,911 additions and 86 deletions.
89 changes: 51 additions & 38 deletions Hexen.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
#include <stdio.h>
#include <stdlib.h>
#include "PPM.h"
#include "Hexen.h"
#include "Image.h"
#include <MT2D/MessageBox/MT2D_MessageBox.h>
#include <vector>

extern char str_buffer[200];
/*DataArray loads 8 bytes from the given index

ImageFormat Hexen_GetImageType(unsigned int Width, unsigned int Height) {
ImageFormat _return = TYPE_UNSUPPORTED;
if(Width == 640 && Height == 480){
_return = TYPE_Planar;
}
else if (Width == 4 && Height == 16) {
_return = TYPE_4BitBitmap;
}
else if (Width == 16 && Height == 23) {
_return = TYPE_4BitBitmap;
}
return _return;
}

/*DataArray load 8 bytes from the given index
**/
unsigned char Get_PlanarBitsFilter(unsigned char *DataArray, unsigned char Mask) {
unsigned char Output = 0;
int Moffset = Mask;
int i = 1,j=0;
while (Moffset != 1 /*0xb00000001*/) { //lets get how much we hould move
while (Moffset != 1 /*0xb00000001*/) { //lets get how many steps the offset should be
Moffset = Moffset >> 1;
i++;
}
Expand All @@ -22,49 +36,47 @@ unsigned char Get_PlanarBitsFilter(unsigned char *DataArray, unsigned char Mask)
Output = Output | (DataArray[i] & Mask) << (j - Moffset);
}
else {
Output = Output | (DataArray[i] & Mask) >> abs(j - Moffset);
Output = Output | (DataArray[i] & Mask) >> abs(j - Moffset);
}
// we cant just let << -1 because for the internal code the number after >> is unsigned, so << -1 is << 12u8348932u4 and not >> 1.
}
/* how the data came
DataArray = {12345678,12345678,12345678,12345678,12345678,12345678,12345678,12345678,...};
what the output should look like
Output = 11111111; Output = 22222222; Output = 33333333; ... Output = 88888888;
*/
return Output;
}

Hexen_Startup_Lump *GetPPM_IndexedHexenStartupImage(PPMImage *image, PPMPixel *palette) {
Hexen_Startup_Lump *Hex = (Hexen_Startup_Lump*)malloc(sizeof(Hexen_Startup_Lump));
Hex->Ray_size = image->x * image->y;
Hex->Raw = (unsigned char*)malloc(Hex->Ray_size * sizeof(unsigned char));
Image *Image_Create_IndexedHexenGraphic(Image *image, Pixel *palette) {
int Raw_size = image->Width * image->Height;
unsigned char *Raw = (unsigned char*)malloc(Raw_size * sizeof(unsigned char));
int i = 0, j = 0, k = 0;
//save the color palette struct
Hex->Palette_size = 16;
Hex->Palette = (PPMPixel*)malloc(Hex->Palette_size * sizeof(PPMPixel));
for (i = 0; i < Hex->Palette_size; i++) {
if (palette) {
Hex->Palette[i].red = palette[i].red;
Hex->Palette[i].green = palette[i].green;
Hex->Palette[i].blue = palette[i].blue;
}
else {
//happens when the user load notch or netnotch but not startup palette
Hex->Palette[i].red = 0;
Hex->Palette[i].green = 0;
Hex->Palette[i].blue = 0;
}
}
//save the indexed color
Pixel *tmp;
//save the indexed color in a new struct
j = 0;
for (i = 0; i < image->x * image->y; i++) {
for (k = 0; k < Hex->Palette_size; k++) {
if (palette[k].red == image->data[i].red && palette[k].green == image->data[i].green && palette[k].blue == image->data[i].blue) {
Hex->Raw[j] = k;
for (i = 0; i < Raw_size; i++) {
for (k = 0; k < 16; k++) {
tmp = Image_GetPixel(image, i);
if (palette[k].red == tmp->red && palette[k].green == tmp->green && palette[k].blue == tmp->blue) {
Raw[j] = k;
j++;
break;
}
}

}
return Hex;
Image *img = Image_CreateBlank();
img->Height = image->Height;
img->Width = image->Width;
img->ImagePointer = Raw;
img->Loaded = true;
img->Palette = palette;
img->Type = Hexen_GetImageType(img->Width,img->Height);
return img;
}

void Save_HexenPlanarLump(char *PATH, Hexen_Startup_Lump *HexenLump) {
void Save_HexenPlanarLump(char *PATH, Image *HexenLump) {
FILE *f = fopen(PATH, "wb");
if (!f) {
sprintf(str_buffer, "When trying to create the file %s:Error: %d (%s)", PATH, errno, strerror(errno));
Expand All @@ -74,7 +86,7 @@ void Save_HexenPlanarLump(char *PATH, Hexen_Startup_Lump *HexenLump) {
int x0, y0, PlanarSize;
bool NoPalette = false;

if (HexenLump->Ray_size == 640 * 480) {
if (HexenLump->Width == 640 && HexenLump->Height == 480) {
//check if the file is a startup lump
x0 = 640;
y0 = 480;
Expand Down Expand Up @@ -102,7 +114,7 @@ void Save_HexenPlanarLump(char *PATH, Hexen_Startup_Lump *HexenLump) {
uint8_t* pln1, *pln2, *pln3, *pln4, *read;
size_t plane_size = PlanarSize / 4;

read = HexenLump->Raw;
read = (uint8_t*)HexenLump->ImagePointer;
pln1 = planes; // 80: 10000000 08: 00001000
pln2 = pln1 + plane_size; // 40: 01000000 04: 00000100
pln3 = pln2 + plane_size; // 20: 00100000 02: 00000010
Expand Down Expand Up @@ -132,11 +144,12 @@ void Save_HexenPlanarLump(char *PATH, Hexen_Startup_Lump *HexenLump) {
fclose(f);
}

void Save_HexenBitmapLump(char *PATH, Hexen_Startup_Lump *HexenLump) {
void Save_HexenBitmapLump(char *PATH, Image *HexenLump) {
FILE *f = fopen(PATH, "wb");
int x0, y0, BitmapSize;
unsigned char *Raw = (unsigned char*)HexenLump->ImagePointer;

if (HexenLump->Ray_size == 4 * 16) {
if (HexenLump->Width == 4 && HexenLump->Height == 16) {
//check if the file is a NETNOTCH lump
x0 = 4;
y0 = 16;
Expand All @@ -152,10 +165,10 @@ void Save_HexenBitmapLump(char *PATH, Hexen_Startup_Lump *HexenLump) {
// Create 4-bit bitmap
uint8_t* temp = new uint8_t[BitmapSize];

for (int i = 0; i < HexenLump->Ray_size; i += 2)
for (int i = 0; i < HexenLump->Width * HexenLump->Height; i += 2)
{
// AAAABBBB = 0000AAAA << 4 | 0000BBBB
temp[i / 2] = HexenLump->Raw[i] << 4 | HexenLump->Raw[i + 1];
temp[i / 2] = Raw[i] << 4 | Raw[i + 1];
}

// Write image and cleanup
Expand Down
15 changes: 4 additions & 11 deletions Hexen.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
#pragma once
#include "PPM.h"
#include "Image.h"

typedef struct {
unsigned char *Raw;
int Ray_size;
PPMPixel *Palette;
int Palette_size;
} Hexen_Startup_Lump;

Hexen_Startup_Lump *GetPPM_IndexedHexenStartupImage(PPMImage *image, PPMPixel *palette);
void Save_HexenPlanarLump(char *PATH, Hexen_Startup_Lump *HexenLump);
void Save_HexenBitmapLump(char *PATH, Hexen_Startup_Lump *HexenLump);
Image *Image_Create_IndexedHexenGraphic(Image *image, Pixel *palette);
void Save_HexenPlanarLump(char *PATH, Image *HexenLump);
void Save_HexenBitmapLump(char *PATH, Image *HexenLump);
142 changes: 142 additions & 0 deletions Image.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
#include <stdlib.h>
#include <string.h>
#include "Image.h"
#include "png.h"
#include "PPM.h"
#include "Hexen.h"

ImageFormat PATH_GetType(char *PATH) {
ImageFormat _return= TYPE_UNSUPPORTED;
int i;
i = strlen(PATH);
if (i > 3) {
if (PATH[i - 3] == 'P' || PATH[i - 3] == 'p') {
if (PATH[i - 2] == 'P' || PATH[i - 2] == 'p') {
if (PATH[i - 1] == 'M' || PATH[i - 1] == 'm') {
_return = TYPE_PPM;
}
}
if (PATH[i - 2] == 'N' || PATH[i - 2] == 'n') {
if (PATH[i - 1] == 'G' || PATH[i - 1] == 'g') {
_return = TYPE_PNG;
}
}
}
}
return _return;
}

Image *Image_CreateBlank() {
Image *img = (Image*)malloc(sizeof(Image));
img->Height = 0;
img->Width = 0;
img->ImagePointer = 0;
img->Loaded = false;
img->Palette = 0;
img->Saved = false;
img->Type = TYPE_UNSUPPORTED;
return img;
}

Image *Image_Load(char*PATH) {
Image *Img = 0;
ImageFormat Type;

PPMImage *ppmimg;

if (PATH) {
Type = PATH_GetType(PATH);
switch (Type) {
case TYPE_PPM:
ppmimg = readPPM(PATH);
if (ppmimg) {
Img = Image_CreateBlank();
Img->Height = ppmimg->y;
Img->Width = ppmimg->x;
Img->ImagePointer = ppmimg;
Img->Loaded = true;
Img->Type = Type;
break;
}
case TYPE_PNG:
break;
}
}
return Img;
}
bool Image_Save(Image *img, char *PATH) {
if (img) {
if (PATH) {
if (img->Loaded) {
switch (img->Type)
{
case TYPE_Planar:
Save_HexenPlanarLump(PATH, img);
img->Saved = true;
break;
case TYPE_4BitBitmap:
Save_HexenBitmapLump(PATH, img);
img->Saved = true;
break;
default:
break;
}
}
}
}
return img->Saved;
}
Pixel *Image_GetPixel(Image *img, unsigned int X, unsigned int Y) {
Pixel *p =0;
switch (img->Type) {
case TYPE_PPM:
p = Image_GetPixel(img, X + Y * img->Width);
break;
}
return p;
}

Pixel *Image_GetPixel(Image *img,int offset) {
int X, Y;
Pixel *p =0;
switch (img->Type) {
case TYPE_PPM:
p = PPM_GetPixel((PPMImage*)img->ImagePointer, offset);
break;
default:

Y = offset / img->Width;
X = offset - Y * img->Width;
break;
}

return p;
}


Pixel *Image_GetPalette(Image *image, int paletteLength) {
Pixel *Palette = 0;
switch(image->Type){
case TYPE_PPM:
Palette = Get_Palette((PPMImage*)image->ImagePointer, paletteLength);
break;
case TYPE_PNG:
break;
}
return Palette;
}

Image *Image_ImageConvert(Image *image, Pixel *Palette, ImageFormat ConverTo){
Image *img =0;
if (image) {
if (image->Type != TYPE_UNSUPPORTED) {
switch (ConverTo) {
case TYPE_4BitBitmap:
case TYPE_Planar:
img = Image_Create_IndexedHexenGraphic(image, Palette);
break;
}
}
}
return img;
}
29 changes: 29 additions & 0 deletions Image.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once
#include "Pixel.h"

enum ImageFormat {
TYPE_UNSUPPORTED = -1,
TYPE_PPM,
TYPE_PNG,
TYPE_Planar,
TYPE_4BitBitmap
};

struct Image {
void *ImagePointer;
unsigned int Width;
unsigned int Height;
ImageFormat Type;
Pixel *Palette;
bool Loaded;
bool Saved;

};

Image *Image_CreateBlank();
Image *Image_Load(char*PATH);
bool Image_Save(Image *img,char *PATH);
Pixel *Image_GetPixel(Image *img, unsigned int X, unsigned int Y);
Pixel *Image_GetPixel(Image *img,int offset);
Pixel *Image_GetPalette(Image *image, int paletteLength);
Image *Image_ImageConvert(Image *image,Pixel *Palette, ImageFormat ConverTo);
Loading

0 comments on commit 04f7f5b

Please sign in to comment.