wavelength 1.0.0
Loading...
Searching...
No Matches
rndbitmap.h
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
4
5#include "os/binstream.h"
6#include "os/prnstream.h"
7
16class RndBitmap {
17public:
19 enum Order {
23 };
24
27 mBuffer = nullptr;
28 mMip = nullptr;
29 Reset();
30 }
31
34 Reset();
35 }
36
44 int NumMips() const;
45
53 int PixelBytes() const;
54
62 int PaletteBytes() const;
63
74
86 unsigned char NearestColor(unsigned char nRed,
87 unsigned char nGreen,
88 unsigned char nBlue,
89 unsigned char nAlpha) const;
90
104 void ReadColor(const unsigned char *pData,
105 unsigned char &nRed,
106 unsigned char &nGreen,
107 unsigned char &nBlue,
108 unsigned char &nAlpha) const;
109
123 void WriteColor(unsigned char nRed,
124 unsigned char nGreen,
125 unsigned char nBlue,
126 unsigned char nAlpha,
127 unsigned char *pData) const;
128
138 void SetMip(std::unique_ptr<RndBitmap> pMip);
139
146 void Reset();
147
159 void Create(const RndBitmap &source, int nBpp, int nOrder);
160
171 static bool CheckDims(int nWidth, int nHeight, int nBpp);
172
185 void Create(int nWidth, int nHeight, int nRowBytes, int nBpp, int nOrder);
186
204 void Create(int nWidth,
205 int nHeight,
206 int nRowBytes,
207 int nBpp,
208 int nOrder,
209 unsigned char *pPalette,
210 unsigned char *pPixels,
211 unsigned char *pBuffer);
212
223 void Create(unsigned char *pBuffer);
224
237 int PixelOffset(int nX, int nY, int &nHighNibble) const;
238
248 int PixelIndex(int nX, int nY) const;
249
259 void SetPixelIndex(int nX, int nY, unsigned char nIndex);
260
268 int MaxPixelIndex() const;
269
279
288 bool LoadMip(const char *pszPath);
289
300 void LoadAlpha(const char *pszPath);
301
313 bool LoadBmp(const char *pszPath);
314
324 void SaveBmp(const char *pszPath) const;
325
334 bool SamePalette(const RndBitmap &other) const;
335
344 bool SameFormat(const RndBitmap &other) const;
345
359 void Blit(const RndBitmap &source,
360 int nDestX,
361 int nDestY,
362 int nSourceX,
363 int nSourceY,
364 int nWidth,
365 int nHeight);
366
379 void PixelColor(int nX,
380 int nY,
381 unsigned char &nRed,
382 unsigned char &nGreen,
383 unsigned char &nBlue,
384 unsigned char &nAlpha) const;
385
398 void SetPixelColor(int nX,
399 int nY,
400 unsigned char nRed,
401 unsigned char nGreen,
402 unsigned char nBlue,
403 unsigned char nAlpha);
404
416 int PaletteOffset(int nIndex) const;
417
429 void PaletteColor(int nIndex,
430 unsigned char &nRed,
431 unsigned char &nGreen,
432 unsigned char &nBlue,
433 unsigned char &nAlpha) const;
434
446 void SetPaletteColor(int nIndex,
447 unsigned char nRed,
448 unsigned char nGreen,
449 unsigned char nBlue,
450 unsigned char nAlpha);
451
459 void Print(PrnStream &stream) const;
460
468 void Save(BinStream &stream) const;
469
475 static unsigned char sRev;
476
478 unsigned short GetWidth() const {
479 return mWidth;
480 }
481
483 unsigned short GetHeight() const {
484 return mHeight;
485 }
486
488 unsigned short GetRowBytes() const {
489 return mRowBytes;
490 }
491
493 unsigned char GetBpp() const {
494 return mBpp;
495 }
496
498 unsigned char GetOrder() const {
499 return mOrder;
500 }
501
503 unsigned char *GetPixels() const {
504 return mPixels;
505 }
506
508 unsigned char *GetPalette() const {
509 return mPalette;
510 }
511
513 const RndBitmap *GetMip() const {
514 return mMip.get();
515 }
516
519 return mMip.get();
520 }
521
522private:
523 unsigned short mWidth;
524 unsigned short mHeight;
525 unsigned short mRowBytes;
526 unsigned char mBpp;
527 unsigned char mOrder;
528 unsigned char *mPixels;
529 unsigned char *mPalette;
530 unsigned char *mBuffer;
531 std::unique_ptr<RndBitmap> mMip;
532};
Stream of raw bytes, the base of the memory, file, and network streams.
Definition binstream.h:22
Text output stream.
Definition prnstream.h:16
int MaxPixelIndex() const
Report the highest palette index any pixel uses.
void PaletteColor(int nIndex, unsigned char &nRed, unsigned char &nGreen, unsigned char &nBlue, unsigned char &nAlpha) const
Report a palette entry.
~RndBitmap()
Release the pixels and the mipmaps.
Definition rndbitmap.h:33
unsigned char GetBpp() const
Definition rndbitmap.h:493
void Create(const RndBitmap &source, int nBpp, int nOrder)
Make a copy of another bitmap and its mipmaps in another format.
void Print(PrnStream &stream) const
Write the size, the format, and the mipmap count of the bitmap.
int PixelBytes() const
Report the size of the pixels in bytes.
void ConvertTo8Bpp()
Convert the pixels to 8 bits per pixel, keeping the palette entries.
bool SamePalette(const RndBitmap &other) const
Report whether another bitmap has the same palette entries.
static unsigned char sRev
The format version Save() writes.
Definition rndbitmap.h:475
void WriteColor(unsigned char nRed, unsigned char nGreen, unsigned char nBlue, unsigned char nAlpha, unsigned char *pData) const
Encode one colour in the bitmap's format.
const RndBitmap * GetMip() const
Definition rndbitmap.h:513
void SetPaletteColor(int nIndex, unsigned char nRed, unsigned char nGreen, unsigned char nBlue, unsigned char nAlpha)
Set a palette entry.
int PixelIndex(int nX, int nY) const
Report the palette index of a pixel.
void Reset()
Release the pixels and the mipmaps and empty the bitmap.
unsigned short GetHeight() const
Definition rndbitmap.h:483
int PaletteBytes() const
Report the size of the palette in bytes, or 0 for a bitmap without one.
int PaletteOffset(int nIndex) const
Map a palette index to its position in the palette, which the GS order of an 8-bit palette permutes.
int NumMips() const
Count the mipmaps that follow the bitmap.
void ReadColor(const unsigned char *pData, unsigned char &nRed, unsigned char &nGreen, unsigned char &nBlue, unsigned char &nAlpha) const
Decode one stored colour, a palette entry or a direct pixel, in the bitmap's format.
void Save(BinStream &stream) const
Write a 16-byte header, the palette, and the pixels of the bitmap and each mipmap.
void PixelColor(int nX, int nY, unsigned char &nRed, unsigned char &nGreen, unsigned char &nBlue, unsigned char &nAlpha) const
Report the colour of a pixel.
void Create(int nWidth, int nHeight, int nRowBytes, int nBpp, int nOrder)
Allocate a bitmap of a size and a format, with a palette when it has 8 bits per pixel or fewer.
void SetPixelIndex(int nX, int nY, unsigned char nIndex)
Set the palette index of a pixel.
unsigned char * GetPalette() const
Definition rndbitmap.h:508
void LoadAlpha(const char *pszPath)
Take the alpha of each pixel from the red of a BMP file of the same size and palette use.
unsigned short GetWidth() const
Definition rndbitmap.h:478
void SaveBmp(const char *pszPath) const
Write the bitmap as a BMP file.
Order
The bits of mOrder.
Definition rndbitmap.h:19
@ kOrderSwizzled
Definition rndbitmap.h:22
@ kOrderGs
Definition rndbitmap.h:21
@ kOrderRGBA
Definition rndbitmap.h:20
void Create(unsigned char *pBuffer)
Describe a bitmap that Save() wrote into a buffer, without copying it.
bool LoadMip(const char *pszPath)
Load a BMP file and add it as the last mipmap.
RndBitmap * GetMip()
Definition rndbitmap.h:518
unsigned char * GetPixels() const
Definition rndbitmap.h:503
unsigned short GetRowBytes() const
Definition rndbitmap.h:488
void NormalizeLayout()
Derive the row size when it is unset, and drop the swizzled order from a bitmap too small or too deep...
void SetPixelColor(int nX, int nY, unsigned char nRed, unsigned char nGreen, unsigned char nBlue, unsigned char nAlpha)
Set the colour of a pixel, or its nearest palette entry.
unsigned char GetOrder() const
Definition rndbitmap.h:498
bool SameFormat(const RndBitmap &other) const
Report whether another bitmap stores pixels the same way, so rows copy unchanged.
static bool CheckDims(int nWidth, int nHeight, int nBpp)
Report whether a size and a depth are valid, with a notice when they are not.
unsigned char NearestColor(unsigned char nRed, unsigned char nGreen, unsigned char nBlue, unsigned char nAlpha) const
Find the palette entry nearest to a colour, by the sum of the component differences.
int PixelOffset(int nX, int nY, int &nHighNibble) const
Find the byte of the pixels with a pixel, in the GS block order for a swizzled bitmap.
RndBitmap()
Construct an empty bitmap.
Definition rndbitmap.h:26
void SetMip(std::unique_ptr< RndBitmap > pMip)
Make a bitmap of half the size, with the same palette, the next mipmap.
bool LoadBmp(const char *pszPath)
Load an uncompressed BMP file of 4 or more bits per pixel.
void Create(int nWidth, int nHeight, int nRowBytes, int nBpp, int nOrder, unsigned char *pPalette, unsigned char *pPixels, unsigned char *pBuffer)
Describe pixels and a palette that already exist.
void Blit(const RndBitmap &source, int nDestX, int nDestY, int nSourceX, int nSourceY, int nWidth, int nHeight)
Copy a rectangle of another bitmap into this one, converting the colours.