fsdfds
Dependencies: BLE_API mbed-dev-bin nRF51822
Fork of microbit-dal by
MicroBitImage.h
00001 /* 00002 The MIT License (MIT) 00003 00004 Copyright (c) 2016 British Broadcasting Corporation. 00005 This software is provided by Lancaster University by arrangement with the BBC. 00006 00007 Permission is hereby granted, free of charge, to any person obtaining a 00008 copy of this software and associated documentation files (the "Software"), 00009 to deal in the Software without restriction, including without limitation 00010 the rights to use, copy, modify, merge, publish, distribute, sublicense, 00011 and/or sell copies of the Software, and to permit persons to whom the 00012 Software is furnished to do so, subject to the following conditions: 00013 00014 The above copyright notice and this permission notice shall be included in 00015 all copies or substantial portions of the Software. 00016 00017 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00018 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00019 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 00020 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00021 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 00022 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 00023 DEALINGS IN THE SOFTWARE. 00024 */ 00025 00026 #ifndef MICROBIT_IMAGE_H 00027 #define MICROBIT_IMAGE_H 00028 00029 #include "mbed.h" 00030 #include "MicroBitConfig.h" 00031 #include "ManagedString.h" 00032 #include "RefCounted.h" 00033 00034 struct ImageData : RefCounted 00035 { 00036 uint16_t width; // Width in pixels 00037 uint16_t height; // Height in pixels 00038 uint8_t data[0]; // 2D array representing the bitmap image 00039 }; 00040 00041 /** 00042 * Class definition for a MicroBitImage. 00043 * 00044 * An MicroBitImage is a simple bitmap representation of an image. 00045 * n.b. This is a mutable, managed type. 00046 */ 00047 class MicroBitImage 00048 { 00049 ImageData *ptr; // Pointer to payload data 00050 00051 00052 /** 00053 * Internal constructor which provides sanity checking and initialises class properties. 00054 * 00055 * @param x the width of the image 00056 * 00057 * @param y the height of the image 00058 * 00059 * @param bitmap an array of integers that make up an image. 00060 */ 00061 void init(const int16_t x, const int16_t y, const uint8_t *bitmap); 00062 00063 /** 00064 * Internal constructor which defaults to the Empty Image instance variable 00065 */ 00066 void init_empty(); 00067 00068 public: 00069 static MicroBitImage EmptyImage; // Shared representation of a null image. 00070 00071 /** 00072 * Get current ptr, do not decr() it, and set the current instance to empty image. 00073 * 00074 * This is to be used by specialized runtimes which pass ImageData around. 00075 */ 00076 ImageData *leakData(); 00077 00078 /** 00079 * Return a 2D array representing the bitmap image. 00080 */ 00081 uint8_t *getBitmap() 00082 { 00083 return ptr->data; 00084 } 00085 00086 /** 00087 * Constructor. 00088 * Create an image from a specially prepared constant array, with no copying. Will call ptr->incr(). 00089 * 00090 * @param ptr The literal - first two bytes should be 0xff, then width, 0, height, 0, and the bitmap. Width and height are 16 bit. The literal has to be 4-byte aligned. 00091 * 00092 * @code 00093 * static const uint8_t heart[] __attribute__ ((aligned (4))) = { 0xff, 0xff, 10, 0, 5, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart 00094 * MicroBitImage i((ImageData*)(void*)heart); 00095 * @endcode 00096 */ 00097 MicroBitImage(ImageData *ptr); 00098 00099 /** 00100 * Default Constructor. 00101 * Creates a new reference to the empty MicroBitImage bitmap 00102 * 00103 * @code 00104 * MicroBitImage i(); //an empty image instance 00105 * @endcode 00106 */ 00107 MicroBitImage(); 00108 00109 00110 /** 00111 * Copy Constructor. 00112 * Add ourselves as a reference to an existing MicroBitImage. 00113 * 00114 * @param image The MicroBitImage to reference. 00115 * 00116 * @code 00117 * MicroBitImage i("0,1,0,1,0\n"); 00118 * MicroBitImage i2(i); //points to i 00119 * @endcode 00120 */ 00121 MicroBitImage(const MicroBitImage &image); 00122 00123 /** 00124 * Constructor. 00125 * Create a blank bitmap representation of a given size. 00126 * 00127 * @param s A text based representation of the image given whitespace delimited numeric values. 00128 * 00129 * @code 00130 * MicroBitImage i("0,1,0,1,0\n1,0,1,0,1\n0,1,0,1,0\n1,0,1,0,1\n0,1,0,1,0\n"); // 5x5 image 00131 * @endcode 00132 */ 00133 explicit MicroBitImage(const char *s); 00134 00135 /** 00136 * Constructor. 00137 * Create a blank bitmap representation of a given size. 00138 * 00139 * @param x the width of the image. 00140 * 00141 * @param y the height of the image. 00142 * 00143 * Bitmap buffer is linear, with 8 bits per pixel, row by row, 00144 * top to bottom with no word alignment. Stride is therefore the image width in pixels. 00145 * in where w and h are width and height respectively, the layout is therefore: 00146 * 00147 * |[0,0]...[w,o][1,0]...[w,1] ... [[w,h] 00148 * 00149 * A copy of the image is made in RAM, as images are mutable. 00150 */ 00151 MicroBitImage(const int16_t x, const int16_t y); 00152 00153 /** 00154 * Constructor. 00155 * Create a bitmap representation of a given size, based on a given buffer. 00156 * 00157 * @param x the width of the image. 00158 * 00159 * @param y the height of the image. 00160 * 00161 * @param bitmap a 2D array representing the image. 00162 * 00163 * @code 00164 * const uint8_t heart[] = { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart 00165 * MicroBitImage i(10,5,heart); 00166 * @endcode 00167 */ 00168 MicroBitImage(const int16_t x, const int16_t y, const uint8_t *bitmap); 00169 00170 /** 00171 * Destructor. 00172 * 00173 * Removes buffer resources held by the instance. 00174 */ 00175 ~MicroBitImage(); 00176 00177 /** 00178 * Copy assign operation. 00179 * 00180 * Called when one MicroBitImage is assigned the value of another using the '=' operator. 00181 * 00182 * Decrement our reference count and free up the buffer as necessary. 00183 * 00184 * Then, update our buffer to refer to that of the supplied MicroBitImage, 00185 * and increase its reference count. 00186 * 00187 * @param s The MicroBitImage to reference. 00188 * 00189 * @code 00190 * const uint8_t heart[] = { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart 00191 * MicroBitImage i(10,5,heart); 00192 * MicroBitImage i1(); 00193 * i1 = i; // i1 now references i 00194 * @endcode 00195 */ 00196 MicroBitImage& operator = (const MicroBitImage& i); 00197 00198 00199 /** 00200 * Equality operation. 00201 * 00202 * Called when one MicroBitImage is tested to be equal to another using the '==' operator. 00203 * 00204 * @param i The MicroBitImage to test ourselves against. 00205 * 00206 * @return true if this MicroBitImage is identical to the one supplied, false otherwise. 00207 * 00208 * @code 00209 * MicroBitDisplay display; 00210 * MicroBitImage i(); 00211 * MicroBitImage i1(); 00212 * 00213 * if(i == i1) //will be true 00214 * display.scroll("true"); 00215 * @endcode 00216 */ 00217 bool operator== (const MicroBitImage& i); 00218 00219 /** 00220 * Resets all pixels in this image to 0. 00221 * 00222 * @code 00223 * MicroBitImage i("0,1,0,1,0\n1,0,1,0,1\n0,1,0,1,0\n1,0,1,0,1\n0,1,0,1,0\n"); // 5x5 image 00224 * i.clear(); 00225 * @endcode 00226 */ 00227 void clear(); 00228 00229 /** 00230 * Sets the pixel at the given co-ordinates to a given value. 00231 * 00232 * @param x The co-ordinate of the pixel to change. 00233 * 00234 * @param y The co-ordinate of the pixel to change. 00235 * 00236 * @param value The new value of the pixel (the brightness level 0-255) 00237 * 00238 * @return MICROBIT_OK, or MICROBIT_INVALID_PARAMETER. 00239 * 00240 * @code 00241 * MicroBitImage i("0,1,0,1,0\n1,0,1,0,1\n0,1,0,1,0\n1,0,1,0,1\n0,1,0,1,0\n"); // 5x5 image 00242 * i.setPixelValue(0,0,255); 00243 * @endcode 00244 * 00245 * @note all coordinates originate from the top left of an image. 00246 */ 00247 int setPixelValue(int16_t x , int16_t y, uint8_t value); 00248 00249 /** 00250 * Retrieves the value of a given pixel. 00251 * 00252 * @param x The x co-ordinate of the pixel to read. Must be within the dimensions of the image. 00253 * 00254 * @param y The y co-ordinate of the pixel to read. Must be within the dimensions of the image. 00255 * 00256 * @return The value assigned to the given pixel location (the brightness level 0-255), or MICROBIT_INVALID_PARAMETER. 00257 * 00258 * @code 00259 * MicroBitImage i("0,1,0,1,0\n1,0,1,0,1\n0,1,0,1,0\n1,0,1,0,1\n0,1,0,1,0\n"); // 5x5 image 00260 * i.getPixelValue(0,0); //should be 0; 00261 * @endcode 00262 */ 00263 int getPixelValue(int16_t x , int16_t y); 00264 00265 /** 00266 * Replaces the content of this image with that of a given 2D array representing 00267 * the image. 00268 * 00269 * @param x the width of the image. Must be within the dimensions of the image. 00270 * 00271 * @param y the width of the image. Must be within the dimensions of the image. 00272 * 00273 * @param bitmap a 2D array representing the image. 00274 * 00275 * @return MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER. 00276 * 00277 * @code 00278 * const uint8_t heart[] = { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart 00279 * MicroBitImage i(); 00280 * i.printImage(0,0,heart); 00281 * @endcode 00282 * 00283 * @note all coordinates originate from the top left of an image. 00284 */ 00285 int printImage(int16_t x, int16_t y, const uint8_t *bitmap); 00286 00287 /** 00288 * Pastes a given bitmap at the given co-ordinates. 00289 * 00290 * Any pixels in the relevant area of this image are replaced. 00291 * 00292 * @param image The MicroBitImage to paste. 00293 * 00294 * @param x The leftmost X co-ordinate in this image where the given image should be pasted. Defaults to 0. 00295 * 00296 * @param y The uppermost Y co-ordinate in this image where the given image should be pasted. Defaults to 0. 00297 * 00298 * @param alpha set to 1 if transparency clear pixels in given image should be treated as transparent. Set to 0 otherwise. Defaults to 0. 00299 * 00300 * @return The number of pixels written. 00301 * 00302 * @code 00303 * const uint8_t heart[] = { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart 00304 * MicroBitImage i(10,5,heart); // a big heart 00305 * i.paste(i, -5, 0); // a small heart 00306 * @endcode 00307 */ 00308 int paste(const MicroBitImage &image, int16_t x = 0, int16_t y = 0, uint8_t alpha = 0); 00309 00310 /** 00311 * Prints a character to the display at the given location 00312 * 00313 * @param c The character to display. 00314 * 00315 * @param x The x co-ordinate of on the image to place the top left of the character. Defaults to 0. 00316 * 00317 * @param y The y co-ordinate of on the image to place the top left of the character. Defaults to 0. 00318 * 00319 * @return MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER. 00320 * 00321 * @code 00322 * MicroBitImage i(5,5); 00323 * i.print('a'); 00324 * @endcode 00325 */ 00326 int print(char c, int16_t x = 0, int16_t y = 0); 00327 00328 /** 00329 * Shifts the pixels in this Image a given number of pixels to the left. 00330 * 00331 * @param n The number of pixels to shift. 00332 * 00333 * @return MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER. 00334 * 00335 * @code 00336 * const uint8_t heart[] = { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart 00337 * MicroBitImage i(10,5,heart); // a big heart 00338 * i.shiftLeft(5); // a small heart 00339 * @endcode 00340 */ 00341 int shiftLeft(int16_t n); 00342 00343 /** 00344 * Shifts the pixels in this Image a given number of pixels to the right. 00345 * 00346 * @param n The number of pixels to shift. 00347 * 00348 * @return MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER. 00349 * 00350 * @code 00351 * const uint8_t heart[] = { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart 00352 * MicroBitImage i(10,5,heart); // a big heart 00353 * i.shiftLeft(5); // a small heart 00354 * i.shiftRight(5); // a big heart 00355 * @endcode 00356 */ 00357 int shiftRight(int16_t n); 00358 00359 /** 00360 * Shifts the pixels in this Image a given number of pixels to upward. 00361 * 00362 * @param n The number of pixels to shift. 00363 * 00364 * @return MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER. 00365 * 00366 * @code 00367 * const uint8_t heart[] = { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart 00368 * MicroBitImage i(10,5,heart); 00369 * i.shiftUp(1); 00370 * @endcode 00371 */ 00372 int shiftUp(int16_t n); 00373 00374 /** 00375 * Shifts the pixels in this Image a given number of pixels to downward. 00376 * 00377 * @param n The number of pixels to shift. 00378 * 00379 * @return MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER. 00380 * 00381 * @code 00382 * const uint8_t heart[] = { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart 00383 * MicroBitImage i(10,5,heart); 00384 * i.shiftDown(1); 00385 * @endcode 00386 */ 00387 int shiftDown(int16_t n); 00388 00389 /** 00390 * Gets the width of this image. 00391 * 00392 * @return The width of this image. 00393 * 00394 * @code 00395 * const uint8_t heart[] = { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart 00396 * MicroBitImage i(10,5,heart); 00397 * i.getWidth(); // equals 10... 00398 * @endcode 00399 */ 00400 int getWidth() const 00401 { 00402 return ptr->width; 00403 } 00404 00405 /** 00406 * Gets the height of this image. 00407 * 00408 * @return The height of this image. 00409 * 00410 * @code 00411 * const uint8_t heart[] = { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart 00412 * MicroBitImage i(10,5,heart); 00413 * i.getHeight(); // equals 5... 00414 * @endcode 00415 */ 00416 int getHeight() const 00417 { 00418 return ptr->height; 00419 } 00420 00421 /** 00422 * Gets number of bytes in the bitmap, ie., width * height. 00423 * 00424 * @return The size of the bitmap. 00425 * 00426 * @code 00427 * const uint8_t heart[] = { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart 00428 * MicroBitImage i(10,5,heart); 00429 * i.getSize(); // equals 50... 00430 * @endcode 00431 */ 00432 int getSize() const 00433 { 00434 return ptr->width * ptr->height; 00435 } 00436 00437 /** 00438 * Converts the bitmap to a csv ManagedString. 00439 * 00440 * @code 00441 * const uint8_t heart[] = { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart 00442 * MicroBitImage i(10,5,heart); 00443 * uBit.serial.printString(i.toString()); // "0,1,0,1,0,0,0,0,0,0\n..." 00444 * @endcode 00445 */ 00446 ManagedString toString(); 00447 00448 /** 00449 * Crops the image to the given dimensions. 00450 * 00451 * @param startx the location to start the crop in the x-axis 00452 * 00453 * @param starty the location to start the crop in the y-axis 00454 * 00455 * @param width the width of the desired cropped region 00456 * 00457 * @param height the height of the desired cropped region 00458 * 00459 * @code 00460 * const uint8_t heart[] = { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart 00461 * MicroBitImage i(10,5,heart); 00462 * i.crop(0,0,2,2).toString() // "0,1\n1,1\n" 00463 * @endcode 00464 */ 00465 MicroBitImage crop(int startx, int starty, int finx, int finy); 00466 00467 /** 00468 * Check if image is read-only (i.e., residing in flash). 00469 */ 00470 bool isReadOnly(); 00471 00472 /** 00473 * Create a copy of the image bitmap. Used particularly, when isReadOnly() is true. 00474 * 00475 * @return an instance of MicroBitImage which can be modified independently of the current instance 00476 */ 00477 MicroBitImage clone(); 00478 }; 00479 00480 #endif
Generated on Wed Jul 13 2022 00:58:03 by 1.7.2