Skip to content

Commit

Permalink
LoadBMP
Browse files Browse the repository at this point in the history
  • Loading branch information
javiergutierrezchamorro committed May 19, 2024
1 parent 26d8c11 commit 437a309
Show file tree
Hide file tree
Showing 26 changed files with 160 additions and 78 deletions.
4 changes: 4 additions & 0 deletions Source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ void main( int argc, char *argv[] )
VGA256_Video = VBE_GetVideoPtr(iMode);


//Todo: Crop
//Rotate 180 / FlipV
//FlipH

DemoDraw();
VGA256GetCh();

Expand Down
147 changes: 80 additions & 67 deletions Source/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,82 +5,95 @@
#include <math.h>
#include <io.h>

void VGA256ScaleImage(unsigned char* pDest, unsigned char* pSource, unsigned int widthd, unsigned int heightd, unsigned int widths, unsigned int heights);


/*------------------------------------------------------------------------------------------------------- */
void main(int argc, char* argv[])
#pragma pack (push, 1)
struct bmp_header
{
unsigned char image[4][10] =
{
{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' },
{ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J' },
{ 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T' },
{ 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd' }
};
unsigned char b[2][5];
VGA256ScaleImage((unsigned char *) b, (unsigned char *) image, 5, 2, 10, 4);
unsigned short type; // Magic identifier: 0x4d42
unsigned long size; // File size in bytes
unsigned short reserved1; // Not used
unsigned short reserved2; // Not used
unsigned long offset; // Offset to image data in bytes from beginning of file (54 bytes)
unsigned long dib_header_size; // DIB Header size in bytes (40 bytes)
long width_px; // Width of the image
long height_px; // Height of image
unsigned short num_planes; // Number of color planes
unsigned short bits_per_pixel; // Bits per pixel
unsigned long compression; // Compression type
unsigned long image_size_bytes; // Image size in bytes
long x_resolution_ppm; // Pixels per meter
long y_resolution_ppm; // Pixels per meter
unsigned long num_colors; // Number of colors
unsigned long important_colors; // Important colors


}
};
#pragma pack (pop)


/*------------------------------------------------------------------------------------------------------- */
int VGA256LoadBMP(char* filename, unsigned char* dest, unsigned char* pal)
{
int readlen = 0;
int infile = -1;
struct bmp_header bmp_header;
int res = -9;


if ((infile = _open(filename, O_BINARY | O_RDONLY)) == -1)
{
res = -1; /* Cannot open */
}
else
{
readlen = _read(infile, &bmp_header, sizeof(bmp_header));
if ((bmp_header.type == 0x4d42) && (bmp_header.bits_per_pixel == 8) && (bmp_header.num_planes == 1) && (bmp_header.num_colors <= 256) && (bmp_header.compression == 0))
{
if (pal)
{
readlen = _read(infile, pal, 3 * 256);
/*
for (i = 0; i < readlen; i++)
{
pal[i] = pal[i] >> 2;
}
*/
}
if (dest)
{
_lseek(infile, bmp_header.offset, SEEK_SET);
readlen = _read(infile, dest, bmp_header.image_size_bytes);
}
res = bmp_header.image_size_bytes;
}
else
{
res = -2; /* Invalid PCX */
}
}
if (infile != -1)
{
_close(infile);
}
return(res);
}


/*------------------------------------------------------------------------------------------------------- */
void VGA256ScaleImage(unsigned char* pDest, unsigned char* pSource, unsigned int widthd, unsigned int heightd, unsigned int widths, unsigned int heights)
void main(int argc, char* argv[])
{
unsigned int h, w;
unsigned char pixel;
unsigned int runw, runh;
unsigned int carryw, carryh;
div_t widthr, heightr;
unsigned char image[4][10] =
{
{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' },
{ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J' },
{ 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T' },
{ 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd' }
};
unsigned char b[2][5];

heightr = div(heightd, heights);
widthr = div(widthd, widths);
unsigned char data[64000];
unsigned char pal[768];
VGA256LoadBMP("perin.bmp", data, pal);

runh = 0;
carryh = 0;
for (h = 0; h < heights; h++)
{
runh += heightr.quot;
carryh += heightr.rem;
if (carryh >= heights)
{
carryh = 0;
runh++;
}
if (runh > 0)
{
runw = 0;
carryw = 0;
for (w = 0; w < widths; w++)
{
runw += widthr.quot;
carryw += widthr.rem;
if (carryw >= widths)
{
carryw = 0;
runw++;
}
pixel = *pSource;
pSource++;
while (runw > 0)
{
*pDest = pixel;
pDest++;
runw--;
}
}
runh--;
while (runh > 0)
{
memcpy(pDest, pDest - widthd, widthd);
pDest += widthd;
runh--;
}
}
else
{
pSource += widths;
}
}
}
65 changes: 65 additions & 0 deletions Source/vga256.c
Original file line number Diff line number Diff line change
Expand Up @@ -1587,6 +1587,71 @@ int VGA256DecodePCX(unsigned char *dest, unsigned char *buffer)
}


/*------------------------------------------------------------------------------------------------------- */
#pragma pack (push, 1)
struct bmp_header
{
unsigned short type; // Magic identifier: 0x4d42
unsigned long size; // File size in bytes
unsigned short reserved1; // Not used
unsigned short reserved2; // Not used
unsigned long offset; // Offset to image data in bytes from beginning of file (54 bytes)
unsigned long dib_header_size; // DIB Header size in bytes (40 bytes)
long width_px; // Width of the image
long height_px; // Height of image
unsigned short num_planes; // Number of color planes
unsigned short bits_per_pixel; // Bits per pixel
unsigned long compression; // Compression type
unsigned long image_size_bytes; // Image size in bytes
long x_resolution_ppm; // Pixels per meter
long y_resolution_ppm; // Pixels per meter
unsigned long num_colors; // Number of colors
unsigned long important_colors; // Important colors

};
#pragma pack (pop)


/*------------------------------------------------------------------------------------------------------- */
int VGA256LoadBMP(char* filename, unsigned char* dest, unsigned char* pal)
{
int infile = -1;
struct bmp_header bmp_header;
int res = -9;


if ((infile = _open(filename, O_BINARY | O_RDONLY)) == -1)
{
res = -1; /* Cannot open */
}
else
{
_read(infile, &bmp_header, sizeof(bmp_header));
if ((bmp_header.type == 0x4d42) && (bmp_header.bits_per_pixel == 8) && (bmp_header.num_planes == 1) && (bmp_header.num_colors <= 256) && (bmp_header.compression == 0))
{
if (pal)
{
_read(infile, pal, 3 * 256);
}
if (dest)
{
_lseek(infile, bmp_header.offset, SEEK_SET);
_read(infile, dest, bmp_header.image_size_bytes);
}
res = bmp_header.image_size_bytes;
}
else
{
res = -2; /* Invalid PCX */
}
}
if (infile != -1)
{
_close(infile);
}
return(res);
}



/*------------------------------------------------------------------------------------------------------- */
Expand Down
1 change: 1 addition & 0 deletions Source/vga256.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ void VGA256OutText2x(void* pVideo, char* string, unsigned int x_cursor, unsigned
void VGA256OutText4x(void* pVideo, char* string, unsigned int x_cursor, unsigned int y_cursor, unsigned int color, unsigned char* font);
int VGA256LoadPCX(char* filename, unsigned char* dest, unsigned char* pal);
int VGA256DecodePCX(unsigned char* dest, unsigned char* buffer);
int VGA256LoadBMP(char* filename, unsigned char* dest, unsigned char* pal);
int VGA256KbHit(void);
int VGA256GetCh(void);

Expand Down
Binary file modified _VC/Test/.vs/Test/v17/.suo
Binary file not shown.
Binary file modified _VC/Test/.vs/Test/v17/Browse.VC.db
Binary file not shown.
2 changes: 1 addition & 1 deletion _VC/Test/.vs/Test/v17/DocumentLayout.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"RelativeDocumentMoniker": "..\\..\\Source\\test.c",
"ToolTip": "D:\\PROYECTOS\\VGA256\\Source\\test.c",
"RelativeToolTip": "..\\..\\Source\\test.c",
"ViewState": "AQIAAAcAAAAAAAAAAAAAABsAAAAAAAAA",
"ViewState": "AQIAADUAAAAAAAAAAAAkwFAAAAAAAAAA",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000423|",
"WhenOpened": "2024-05-11T18:38:58.519Z",
"EditorCaption": ""
Expand Down
Binary file modified _VC/Test/Test/x64/Debug/Test.ilk
Binary file not shown.
2 changes: 1 addition & 1 deletion _VC/Test/Test/x64/Debug/Test.log
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
 test.c
D:\PROYECTOS\VGA256\Source\test.c(75,17): warning C4013: 'memcpy' sin definir; se supone que extern devuelve como resultado int
D:\PROYECTOS\VGA256\Source\test.c(93,16): warning C4101: 'b': variable local sin referencia
Test.vcxproj -> D:\PROYECTOS\VGA256\_VC\Test\x64\Debug\Test.exe
Binary file modified _VC/Test/Test/x64/Debug/test.obj
Binary file not shown.
Binary file modified _VC/Test/Test/x64/Debug/vc143.idb
Binary file not shown.
Binary file modified _VC/Test/Test/x64/Debug/vc143.pdb
Binary file not shown.
Binary file added _VC/Test/perin.bmp
Binary file not shown.
Binary file added _VC/Test/perin.pcx
Binary file not shown.
Binary file modified _VC/Test/x64/Debug/Test.exe
Binary file not shown.
Binary file modified _VC/Test/x64/Debug/Test.pdb
Binary file not shown.
Binary file modified _VC/VGA256/.vs/VGA256/v17/.suo
Binary file not shown.
Binary file modified _VC/VGA256/.vs/VGA256/v17/Browse.VC.db
Binary file not shown.
17 changes: 8 additions & 9 deletions _VC/VGA256/.vs/VGA256/v17/DocumentLayout.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
"AbsoluteMoniker": "D:0:0:{6DC0C79A-7147-40F9-87E3-409F203C2E64}|VGA256.vcxproj|D:\\PROYECTOS\\VGA256\\Source\\vga256.h||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}"
},
{
"AbsoluteMoniker": "D:0:0:{6DC0C79A-7147-40F9-87E3-409F203C2E64}|VGA256.vcxproj|D:\\PROYECTOS\\VGA256\\Source\\data.c||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}"
"AbsoluteMoniker": "D:0:0:{6DC0C79A-7147-40F9-87E3-409F203C2E64}|VGA256.vcxproj|D:\\PROYECTOS\\VGA256\\Source\\data.h||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}"
},
{
"AbsoluteMoniker": "D:0:0:{6DC0C79A-7147-40F9-87E3-409F203C2E64}|VGA256.vcxproj|D:\\PROYECTOS\\VGA256\\Source\\data.h||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}"
"AbsoluteMoniker": "D:0:0:{6DC0C79A-7147-40F9-87E3-409F203C2E64}|VGA256.vcxproj|D:\\PROYECTOS\\VGA256\\Source\\data.c||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}"
}
],
"DocumentGroupContainers": [
Expand All @@ -35,14 +35,13 @@
"RelativeDocumentMoniker": "..\\..\\Source\\vga256.h",
"ToolTip": "D:\\PROYECTOS\\VGA256\\Source\\vga256.h",
"RelativeToolTip": "..\\..\\Source\\vga256.h",
"ViewState": "AQIAAKMAAAAAAAAAAAAwwL0AAACIAAAA",
"ViewState": "AQIAAKQAAAAAAAAAAAAwwMAAAABLAAAA",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000680|",
"WhenOpened": "2024-05-11T03:28:11.846Z",
"EditorCaption": ""
"WhenOpened": "2024-05-11T03:28:11.846Z"
},
{
"$type": "Document",
"DocumentIndex": 4,
"DocumentIndex": 3,
"Title": "data.h",
"DocumentMoniker": "D:\\PROYECTOS\\VGA256\\Source\\data.h",
"RelativeDocumentMoniker": "..\\..\\Source\\data.h",
Expand All @@ -54,7 +53,7 @@
},
{
"$type": "Document",
"DocumentIndex": 3,
"DocumentIndex": 4,
"Title": "data.c",
"DocumentMoniker": "D:\\PROYECTOS\\VGA256\\Source\\data.c",
"RelativeDocumentMoniker": "..\\..\\Source\\data.c",
Expand All @@ -72,7 +71,7 @@
"RelativeDocumentMoniker": "..\\..\\Source\\main.c",
"ToolTip": "D:\\PROYECTOS\\VGA256\\Source\\main.c",
"RelativeToolTip": "..\\..\\Source\\main.c",
"ViewState": "AQIAADwAAAAAAAAAAAAAAEYAAAAsAAAA",
"ViewState": "AQIAACQAAAAAAAAAAAAAAC8AAAAAAAAA",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000423|",
"WhenOpened": "2024-05-12T03:38:17.185Z",
"EditorCaption": ""
Expand All @@ -85,7 +84,7 @@
"RelativeDocumentMoniker": "..\\..\\Source\\vga256.c",
"ToolTip": "D:\\PROYECTOS\\VGA256\\Source\\vga256.c",
"RelativeToolTip": "..\\..\\Source\\vga256.c",
"ViewState": "AQIAAAoFAAAAAAAAAAAuwCAFAAAvAAAA",
"ViewState": "AQIAAFEGAAAAAAAAAAAkwFwGAAACAAAA",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000423|",
"WhenOpened": "2024-05-12T08:43:28.903Z",
"EditorCaption": ""
Expand Down
Binary file not shown.
Binary file not shown.

0 comments on commit 437a309

Please sign in to comment.