Code:
#include <SDL.h>
#include "engine.h"
#include "SDL_image.h"
#include "SDL_mixer.h"
#include <iostream>
#include <fstream>
#include <windows.h>
using namespace std;
int ICEC();
int DICEC();
SDL_Surface * screen, * inimage;
int main(int argc, char *argv[])
{
if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO|SDL_INIT_TIMER) == -1)
return -1;
atexit(SDL_Quit);
screen = SDL_SetVideoMode(512, 512, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);
ICEC();
DICEC();
return 0;
}
int ICEC()
{
SDL_Surface * inimage = IMG_Load("Image\\test.bmp");
// vars
int w = inimage->w;
int h = inimage->h;
Uint8 bpp = inimage->format->BytesPerPixel;
bool subsample = true;
ofstream out;
//write width, height, and bytes per pixel as a header
out.open("Image\\test.ice", ios::binary);
out.write(reinterpret_cast<char*>(&w), sizeof(int));
out.write(reinterpret_cast<char*>(&h), sizeof(int));
out.write(reinterpret_cast<char*>(&bpp), sizeof(Uint8));
switch (bpp)
{
case 3:
Uint8 R, G, B, Y1,Y2,Y3,Y4;
Uint32 U, V;
Uint8 U2, V2;
if (subsample == true)
{
for (int x = 0; x < w; x+=2)
{
for (int y = 0; y < h; y+=2)
{
U = 0; V = 0;
Slock(inimage);
SDL_GetRGB((Uint32)inimage->pixels + y*inimage->pitch + x * 3, inimage->format, &R, &G, &B);
Y1 = (0.299*R + 0.587*G + 0.114*B);
U+=(- 0.147*R - 0.289*G + 0.436*B);
V+=(0.615*R - 0.515*G - 0.100*B);
SDL_GetRGB((Uint32)inimage->pixels + y*inimage->pitch + (x+1) * 3, inimage->format, &R, &G, &B);
Y2 = (0.299*R + 0.587*G + 0.114*B);
U+=(- 0.147*R - 0.289*G + 0.436*B);
V+=(0.615*R - 0.515*G - 0.100*B);
SDL_GetRGB((Uint32)inimage->pixels + (y+1)*inimage->pitch + (x+1) * 3, inimage->format, &R, &G, &B);
Y3 = (0.299*R + 0.587*G + 0.114*B);
U+=(- 0.147*R - 0.289*G + 0.436*B);
V+=(0.615*R - 0.515*G - 0.100*B);
SDL_GetRGB((Uint32)inimage->pixels + (y+1)*inimage->pitch + x * 3, inimage->format, &R, &G, &B);
Y4 = (0.299*R + 0.587*G + 0.114*B);
U+=(- 0.147*R - 0.289*G + 0.436*B);
V+=(0.615*R - 0.515*G - 0.100*B);
Sulock(inimage);
out.write(reinterpret_cast<char*>(&Y1), sizeof(Uint8));
out.write(reinterpret_cast<char*>(&Y2), sizeof(Uint8));
out.write(reinterpret_cast<char*>(&Y3), sizeof(Uint8));
out.write(reinterpret_cast<char*>(&Y4), sizeof(Uint8));
U2 = (Uint8)(U/4);
V2 = (Uint8)(V/4);
out.write(reinterpret_cast<char*>(&U2), sizeof(Uint8));
out.write(reinterpret_cast<char*>(&V2), sizeof(Uint8));
}
}
}
break;
}
out.close();
return 0;
}
int DICEC()
{
//vars
ifstream read;
int w, h;
Uint8 R, G, B, Y1,Y2,Y3,Y4,U,V;
Uint8 bpp;
bool subsample = true;
//header
read.open("Images\\test.ice", ios::binary);
read.read(reinterpret_cast<char*>(&w), sizeof(int));
read.read(reinterpret_cast<char*>(&h), sizeof(int));
read.read(reinterpret_cast<char*>(&bpp), sizeof(Uint8));
printf("%d%d%d", w, h, bpp);
//main
switch (bpp)
{
case 3:
if (subsample == true)
{
for (int x = 0; x < w; x+=2)
{
for (int y = 0; y < h; y+=2)
{
read.read(reinterpret_cast<char*>(&Y1), sizeof(Uint8));
read.read(reinterpret_cast<char*>(&Y2), sizeof(Uint8));
read.read(reinterpret_cast<char*>(&Y3), sizeof(Uint8));
read.read(reinterpret_cast<char*>(&Y4), sizeof(Uint8));
read.read(reinterpret_cast<char*>(&U), sizeof(Uint8));
read.read(reinterpret_cast<char*>(&V), sizeof(Uint8));
Slock(screen);
R = Y1 + 1.140*V;G = Y1 - 0.395*U - 0.581*V;B = Y1 + 2.032*U;
DrawPixel(screen, x, y, R, G, B);
R = Y2 + 1.140*V;G = Y2 - 0.395*U - 0.581*V;B = Y2 + 2.032*U;
DrawPixel(screen, x+1, y, R, G, B);
R = Y3 + 1.140*V;G = Y3 - 0.395*U - 0.581*V;B = Y3 + 2.032*U;
DrawPixel(screen, x+1, y+1, R, G, B);
R = Y4 + 1.140*V;G = Y4 - 0.395*U - 0.581*V;B = Y4 + 2.032*U;
DrawPixel(screen, x, y+1, R, G, B);
Sulock(screen);
}
}
}
else
{
}
break;
}
read.close();
while (!GetAsyncKeyState(VK_ESCAPE))
{
SDL_Flip(screen);
SDL_Delay(1000);
}
return 0;
}