Choco OS  V.0.16.9.0
Join to the chocolate world
oc_colormap.c
Go to the documentation of this file.
1 
27 #include <oc_colormap.h>
28 #include <stddef.h>
29 #include <oc_stdlib.h>
30 #include <oc_errors.h>
31 
37 #define _________________________________________INTERFACE_FUNCTIONS_SECTION________________________________________________________________
38 
39 //==========================================================================================================================================
40 //==========================================================================================================================================
41 oC_ColorMap_t * oC_ColorMap_New( Allocator_t Allocator , AllocationFlags_t AllocationFlags , oC_Pixel_ResolutionUInt_t Width , oC_Pixel_ResolutionUInt_t Height , oC_ColorFormat_t ColorFormat )
42 {
43  oC_ColorMap_t * colorMap = NULL;
44 
45  if(Width > 0 && Height > 0 && ColorFormat != oC_ColorFormat_Unknown)
46  {
47 
48  colorMap = kmalloc(sizeof(oC_ColorMap_t) , Allocator , AllocationFlags);
49 
50  if(colorMap != NULL)
51  {
52  colorMap->Map = kmalloc(oC_Color_GetFormatSize(ColorFormat) * Width * Height , Allocator , AllocationFlags);
53 
54  if(colorMap->Map != NULL)
55  {
56  colorMap->ColorFormat = ColorFormat;
57  colorMap->Height = Height;
58  colorMap->Width = Width;
59  colorMap->MagicNumber = oC_ColorMap_MagicNumber;
60  colorMap->FormatSize = oC_Color_GetFormatSize(ColorFormat);
61 
62  }
63  else
64  {
65  oC_SaveIfFalse("ColorMap::New: cannot release memory" , kfree(colorMap,AllocationFlags), oC_ErrorCode_ReleaseError);
66 
67  colorMap = NULL;
68  }
69  }
70  }
71 
72 
73 
74  return colorMap;
75 }
76 
77 //==========================================================================================================================================
78 //==========================================================================================================================================
79 bool oC_ColorMap_Delete( oC_ColorMap_t * ColorMap , AllocationFlags_t AllocationFlags )
80 {
81  bool deleted = false;
82 
83  if(oC_ColorMap_IsCorrect(ColorMap))
84  {
85  if(isram(ColorMap))
86  {
87  ColorMap->MagicNumber = 0;
88 
89  if(kfree(ColorMap->WriteMap,AllocationFlags) && kfree(ColorMap,AllocationFlags))
90  {
91  deleted = true;
92  }
93  }
94  }
95 
96  return deleted;
97 }
98 
99 //==========================================================================================================================================
100 //==========================================================================================================================================
101 bool oC_ColorMap_IsCorrect( const oC_ColorMap_t * ColorMap )
102 {
103  return isaddresscorrect(ColorMap) && ColorMap->MagicNumber == oC_ColorMap_MagicNumber && isaddresscorrect(ColorMap->Map);
104 }
105 
106 //==========================================================================================================================================
107 //==========================================================================================================================================
108 oC_Color_t oC_ColorMap_GetColor( const oC_ColorMap_t * ColorMap , oC_Pixel_Position_t Position , oC_ColorFormat_t ColorFormat )
109 {
110  oC_Color_t color = 0;
111 
112  if(oC_ColorMap_IsCorrect(ColorMap) && oC_ColorMap_IsPositionCorrect(ColorMap,Position))
113  {
114  oC_UInt_t formatSize = oC_Color_GetFormatSize(ColorMap->ColorFormat);
115  const uint8_t * colorRef = &ColorMap->ReadMap[(Position.X + ColorMap->Width * Position.Y)* formatSize ];
116 
117  for(uint8_t byteIndex = 0; byteIndex < formatSize ; byteIndex++)
118  {
119  color |= (*colorRef) << byteIndex * 8;
120  colorRef++;
121  }
122 
123  color = oC_Color_ConvertToFormat(color,ColorMap->ColorFormat,ColorFormat);
124  }
125 
126  return color;
127 }
128 
129 //==========================================================================================================================================
130 //==========================================================================================================================================
131 static inline void SetColor(oC_Pixel_Position_t Position , oC_Pixel_ResolutionUInt_t Width , oC_Pixel_ResolutionUInt_t Height , uint32_t FormatSize , uint8_t ColorMap[Height][Width][FormatSize] , oC_Color_t Color )
132 {
133  static const uint8_t bitOffset[4] = {
134  0 ,
135  8 ,
136  16 ,
137  32
138  };
139 
140  for(uint8_t byteIndex = 0 ; byteIndex < FormatSize ; byteIndex++)
141  {
142  ColorMap[Position.Y][Position.X][byteIndex] = (uint8_t)((Color >> bitOffset[byteIndex]) & 0xFF);
143  }
144 }
145 
146 //==========================================================================================================================================
147 //==========================================================================================================================================
148 bool oC_ColorMap_SetColor ( oC_ColorMap_t * ColorMap , oC_Pixel_Position_t Position , oC_Color_t Color , oC_ColorFormat_t ColorFormat )
149 {
150  bool colorSet = false;
151 
152  /* To speed up this operation it is not checked if the color map is correct, but only if it is not NULL */
153  if(ColorMap != NULL && Position.X < ColorMap->Width && Position.Y < ColorMap->Height)
154  {
155  oC_Color_t color = oC_Color_ConvertToFormat(Color,ColorFormat,ColorMap->ColorFormat);
156  SetColor(Position,ColorMap->Width,ColorMap->Height,ColorMap->FormatSize,ColorMap->GenericWriteMap,color);
157  colorSet = true;
158  }
159 
160  return colorSet;
161 }
162 
163 //==========================================================================================================================================
164 //==========================================================================================================================================
165 bool oC_ColorMap_IsPositionCorrect( const oC_ColorMap_t * ColorMap , oC_Pixel_Position_t Position )
166 {
167  return oC_ColorMap_IsCorrect(ColorMap) && Position.X < ColorMap->Width && Position.Y < ColorMap->Height;
168 }
169 
170 //==========================================================================================================================================
171 //==========================================================================================================================================
172 bool oC_ColorMap_DrawChar( oC_ColorMap_t * ColorMap , oC_Pixel_Position_t Position , oC_Color_t Color , oC_ColorFormat_t ColorFormat , uint16_t C , oC_Font_t Font )
173 {
174  bool drawed = false;
175 
176  if(oC_ColorMap_IsCorrect(ColorMap) && isram(ColorMap->Map) && isaddresscorrect(Font) && ColorFormat != oC_ColorFormat_Unknown && oC_ColorMap_IsPositionCorrect(ColorMap,Position))
177  {
178  oC_Font_CharacterMap_t characterMap = {0};
179 
180  if(oC_Font_ReadCharacterMap(Font,C,&characterMap))
181  {
182  oC_Pixel_ResolutionUInt_t x = 0;
183  oC_Pixel_ResolutionUInt_t y = 0;
184  uint8_t characterMapBitIndex = 0;
185  uint8_t characterMapByteIndex= 0;
186  uint8_t characterMapByte = characterMap.Data[characterMapByteIndex];
187  oC_Pixel_Position_t drawPosition = {0};
188  drawed = true;
189 
190  for( y = 0 ; y < characterMap.Height && drawed; y++ )
191  {
192  drawPosition.Y = Position.Y + y;
193 
194  for( x = 0 ; x < characterMap.Width && drawed; x++ )
195  {
196  drawPosition.X = Position.X + x;
197 
198  // Check if this pixel should be drawed
199  if(characterMapByte & (1<<characterMapBitIndex))
200  {
201  // If draw position is not correct (it is outside of the screen)
202  // then just do not draw it.
203  if(oC_ColorMap_IsPositionCorrect(ColorMap,drawPosition))
204  {
205  drawed = oC_ColorMap_SetColor(ColorMap,drawPosition,Color,ColorFormat);
206  }
207  }
208  // /////////////////////////////////////////////////////
209  // counting next character map byte
210  characterMapBitIndex++;
211 
212  if(characterMapBitIndex == 8)
213  {
214  characterMapBitIndex = 0;
215  characterMapByte = characterMap.Data[++characterMapByteIndex];
216  }
217  // counting next character map byte
218  // /////////////////////////////////////////////////////
219 
220  }
221 
222  characterMapBitIndex = 0;
223  characterMapByte = characterMap.Data[++characterMapByteIndex];
224  }
225  }
226  }
227 
228  return drawed;
229 }
230 
231 
232 
233 #undef _________________________________________INTERFACE_FUNCTIONS_SECTION________________________________________________________________
234 
235 
FILE_DESCRIPTION
identifier for allocations
Definition: oc_stdlib.h:159
static const oC_Allocator_t Allocator
Definition: oc_eth.c:152
#define NULL
pointer to a zero
Definition: oc_null.h:37