Skip to content
This repository has been archived by the owner on Apr 25, 2022. It is now read-only.

Commit

Permalink
Moved color manipulation to utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcin Kurczewski committed Mar 5, 2015
1 parent 30f421a commit a53bac2
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 62 deletions.
72 changes: 21 additions & 51 deletions src/formats/touhou/anm_archive.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "formats/image.h"
#include "formats/touhou/anm_archive.h"
#include "util/colors.h"
#include "util/itos.h"
using namespace Formats::Touhou;

Expand Down Expand Up @@ -138,64 +139,33 @@ namespace
size_t pixel_data_size = width * height * 4;
std::unique_ptr<char[]> pixel_data(new char[pixel_data_size]);
uint32_t *pixel_ptr = reinterpret_cast<uint32_t*>(pixel_data.get());
switch (format)
for (size_t y = 0; y < height; y ++)
{
case 1:
//8 8 8 8 BGRA
for (size_t y = 0; y < height; y ++)
for (size_t x = 0; x < width; x ++)
{
switch (format)
{
for (size_t x = 0; x < width; x ++)
case 1:
*pixel_ptr ++ = file_io.read_u32_le();
}
break;
break;

case 3:
//5 6 5 RGB
for (size_t y = 0; y < height; y ++)
{
for (size_t x = 0; x < width; x ++)
{
uint16_t word = file_io.read_u16_le();
*pixel_ptr ++
= ((word & 0x1f) << 3)
| ((word & 0x7e0) << 5)
| ((word & 0xf800) << 8)
| 0xff000000;
}
}
break;
case 3:
*pixel_ptr ++ = rgb565(file_io.read_u16_le());
break;

case 5:
//4 4 4 4 BGRA
for (size_t y = 0; y < height; y ++)
{
for (size_t x = 0; x < width; x ++)
{
uint16_t word = file_io.read_u16_le();
*pixel_ptr ++
= ((word & 0xf) << 4)
| ((word & 0xf0) << 8)
| ((word & 0xf00) << 12)
| ((word & 0xf000) << 16);
}
}
break;
case 5:
*pixel_ptr ++ = rgba4444(file_io.read_u16_le());
break;

case 7:
//8 gray
for (size_t y = 0; y < height; y ++)
{
for (size_t x = 0; x < width; x ++)
{
uint8_t byte = file_io.read_u8();
*pixel_ptr ++
= byte | (byte << 8) | (byte << 16) | 0xff000000;
}
}
break;
case 7:
*pixel_ptr ++ = rgba_gray(file_io.read_u8());
break;

default:
throw std::runtime_error("Unknown format: " + itos(format));
default:
throw std::runtime_error(
"Unknown color format: " + itos(format));
}
}
}

std::unique_ptr<File> subfile(new File);
Expand Down
12 changes: 1 addition & 11 deletions src/formats/touhou/pak_archive.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "formats/image.h"
#include "formats/sound.h"
#include "formats/touhou/pak_archive.h"
#include "util/colors.h"
#include "util/itos.h"
using namespace Formats::Touhou;

Expand Down Expand Up @@ -62,17 +63,6 @@ namespace

namespace
{
inline uint32_t rgba5551(uint16_t w)
{
uint8_t c2 = (w >> 8) & 0xff;
uint8_t c1 = w & 0xff;
uint8_t r = (c1 & 0x1f) << 3;
uint8_t g = ((c1 & 0xe0) >> 2) | ((c2 & 3) << 6);
uint8_t b = (c2 & 0x7c) << 1;
uint8_t a = c2 & 0x80 ? 0xff : 0;
return r | (g << 8) | (b << 16) | (a << 24);
}

class ImageArchive : public Archive
{
public:
Expand Down
30 changes: 30 additions & 0 deletions src/util/colors.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "util/colors.h"

uint32_t rgb565(uint16_t word)
{
return ((word & 0x1f) << 3)
| ((word & 0x7e0) << 5)
| ((word & 0xf800) << 8)
| 0xff000000;
}

uint32_t rgba4444(uint16_t word)
{
return ((word & 0xf) << 4)
| ((word & 0xf0) << 8)
| ((word & 0xf00) << 12)
| ((word & 0xf000) << 16);
}

uint32_t rgba5551(uint16_t word)
{
return ((word & 0x1f) << 3)
| ((word & 0x3e0) << 6)
| ((word & 0x7c00) << 9)
| (word & 0x8000 ? 0xff000000 : 0);
}

uint32_t rgba_gray(uint8_t byte)
{
return byte | (byte << 8) | (byte << 16) | 0xff000000;
}
10 changes: 10 additions & 0 deletions src/util/colors.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef UTIL_COLORS_H
#define UTIL_COLORS_H
#include <cstdint>

uint32_t rgb565(uint16_t word);
uint32_t rgba5551(uint16_t word);
uint32_t rgba4444(uint16_t word);
uint32_t rgba_gray(uint8_t byte);

#endif

0 comments on commit a53bac2

Please sign in to comment.