SayoriOS  0.3.3
pixfmt.c
1 //
2 // Created by ndraey on 10.09.23.
3 //
4 
5 #include "sys/pixfmt.h"
6 
7 void pixfmt_conv(char* pixels, size_t bpp, size_t width, size_t height, koraidon_screen_pixfmt_t input_format, koraidon_screen_pixfmt_t output_format) {
8  if(input_format == output_format) {
9  return;
10  }
11 
12  size_t bytes_pp = bpp >> 3;
13 
14  bool in_rgb = input_format == SCREEN_RGB;
15  bool in_bgr = input_format == SCREEN_BGR;
16 
17  bool out_rgb = output_format == SCREEN_RGB;
18  bool out_bgr = output_format == SCREEN_BGR;
19 
20  for(size_t sy = 0; sy < height; sy++) {
21  for(size_t sx = 0; sx < width; sx++) {
22  size_t coords = (sy * (width * bytes_pp)) + (sx * bytes_pp);
23 
24  if((in_rgb && out_bgr) || (out_rgb && in_bgr)) {
25  char pixel = pixels[coords + 2]; // r / b
26  pixels[coords + 2] = pixels[coords + 0]; // b / r
27  pixels[coords + 0] = pixel;
28  }
29  }
30  }
31 }