summaryrefslogtreecommitdiff
path: root/trunk/reader.c
blob: 3ac5260053f04399c7b0b5be143457af4b561eb9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "stdio.h"
#include "stdlib.h"
#include "stdint.h"

/* method for 16bpp and above (>1 byte per pixel) */
void sprite_high_colour()
{
	/*uint32_t imgSize = 8;*/ /* in bytes */
	/*               0xddccbbaa              0xeeeeffff */
	uint8_t img[] = {0xaa, 0xbb, 0xcc, 0xdd, 0xff, 0xff, 0xee, 0xee};
	uint32_t widthPixels = 2;
	uint32_t currentByteIndex = 0; /* only for standalone test -- fread() will maintain this */

	uint32_t bpp = 24;
	uint32_t bytesPerPixel = bpp >> 3; /* divide by 8 */
	
	
	for (uint32_t x = 0; x < widthPixels; x++) {
		uint32_t pixel = 0;
		for (uint32_t j = 0; j < bytesPerPixel; j++) {
			uint8_t b = img[currentByteIndex++];
			pixel = pixel | (b << (j * 8));
		}
		printf("%x\n", pixel);
	}
}

void sprite_low_colour()
{
	uint8_t img[] = {0xab, 0xcd, 0x12, 0x34 };
	uint32_t widthInBits = 32;
	uint32_t currentByteIndex = 0; /* only for standalone test -- fread() will maintain this */

	uint32_t bpp = 8;

	uint32_t bitmask = (1 << bpp) - 1; /* creates a mask of 1s that is bpp bits wide */
	uint8_t currentbyte = img[currentByteIndex++];
	
	for (uint32_t x = 0; x < widthInBits; x+=bpp) {
		uint32_t offset_into_byte = x % 8;
		uint32_t pixel = (currentbyte & (bitmask << offset_into_byte)) >> offset_into_byte;
		printf("%x\n", pixel);

		if (offset_into_byte + bpp == 8) {
			currentbyte = img[currentByteIndex++];
		}
	}
}

int main()
{
	sprite_low_colour();

	return EXIT_SUCCESS;
}