Blame view

build2/epsilon-master/ion/src/blackbox/display.cpp 2.29 KB
6663b6c9   adorian   projet complet av...
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
  #include <ion.h>
  #include <kandinsky.h>
  #include <stdlib.h>
  #include <png.h>
  #include "display.h"
  
  namespace Ion {
  namespace Display {
  
  static bool sFrameBufferActive = false;
  static KDColor sPixels[Ion::Display::Width*Ion::Display::Height];
  static KDFrameBuffer sFrameBuffer = KDFrameBuffer(sPixels, KDSize(Ion::Display::Width, Ion::Display::Height));
  
  void pushRect(KDRect r, const KDColor * pixels) {
    if (sFrameBufferActive) {
      sFrameBuffer.pushRect(r, pixels);
    }
  }
  
  void pushRectUniform(KDRect r, KDColor c) {
    if (sFrameBufferActive) {
      sFrameBuffer.pushRectUniform(r, c);
    }
  }
  
  void pullRect(KDRect r, KDColor * pixels) {
    if (sFrameBufferActive) {
      sFrameBuffer.pullRect(r, pixels);
    }
  }
  
  void waitForVBlank() {
  }
  
  }
  }
  
  namespace Ion {
  namespace Display {
  namespace Blackbox {
  
  const KDColor * frameBufferAddress() {
    return sPixels;
  }
  
  void setFrameBufferActive(bool enabled) {
    sFrameBufferActive = enabled;
  }
  
  typedef struct {
    uint8_t red;
    uint8_t green;
    uint8_t blue;
  } pixel_t;
  
  void writeFrameBufferToFile(const char * filename) {
    FILE * file = fopen(filename, "wb"); // Write in binary mode
    //ENSURE(file != NULL, "Opening file %s", filename);
  
    png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
    //ENSURE(png != NULL, "Allocating PNG write structure");
  
    png_infop info = png_create_info_struct(png);
    //ENSURE(info != NULL, "Allocating info structure");
  
    png_init_io(png, file);
  
    png_set_IHDR(png, info,
        Ion::Display::Width, Ion::Display::Height,
        8, // Number of bits per channel
        PNG_COLOR_TYPE_RGB,
        PNG_INTERLACE_NONE,
        PNG_COMPRESSION_TYPE_DEFAULT,
        PNG_FILTER_TYPE_DEFAULT);
  
    png_write_info(png, info);
  
    static_assert(sizeof(pixel_t) == 3, "pixel_t shall be 3 bytes long (RGB888 format)");
    pixel_t * row = (pixel_t *)malloc(3*Ion::Display::Width);
    for (int j=0;j<Ion::Display::Height;j++) {
      for (int i=0; i<Ion::Display::Width; i++) {
        KDColor c = sPixels[i+Ion::Display::Width*j];
        row[i].red = c.red();
        row[i].green = c.green();
        row[i].blue = c.blue();
      }
      png_write_row(png, (png_bytep)row);
    }
    free(row);
  
    png_write_end(png, NULL);
  
    png_free_data(png, info, PNG_FREE_ALL, -1); // -1 = all items
    png_destroy_write_struct(&png, (png_infopp)NULL);
    fclose(file);
  }
  
  }
  }
  }