/**
  * Shifts the pixels in this Image a given number of pixels to upward.
  *
  * @param n The number of pixels to shift.
  *
  * @return MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER.
  *
  * @code
  * 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
  * MicroBitImage i(10,5,heart);
  * i.shiftUp(1);
  * @endcode
  */
int MicroBitImage::shiftUp(int16_t n)
{
    uint8_t *pOut, *pIn;

    if (n <= 0 )
        return MICROBIT_INVALID_PARAMETER;

    if(n >= getHeight())
    {
        clear();
        return MICROBIT_OK;
    }

    pOut = getBitmap();
    pIn = getBitmap()+getWidth()*n;

    for (int y = 0; y < getHeight(); y++)
    {
        // Copy, and blank fill the leftmost column.
        if (y < getHeight()-n)
            memcpy(pOut, pIn, getWidth());
        else
            memclr(pOut, getWidth());

        pIn += getWidth();
        pOut += getWidth();
    }

    return MICROBIT_OK;
}
/**
  * Crops the image to the given dimensions.
  *
  * @param startx the location to start the crop in the x-axis
  *
  * @param starty the location to start the crop in the y-axis
  *
  * @param width the width of the desired cropped region
  *
  * @param height the height of the desired cropped region
  *
  * @code
  * 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
  * MicroBitImage i(10,5,heart);
  * i.crop(0,0,2,2).toString() // "0,1\n1,1\n"
  * @endcode
  */
MicroBitImage MicroBitImage::crop(int startx, int starty, int cropWidth, int cropHeight)
{
    int newWidth = startx + cropWidth;
    int newHeight = starty + cropHeight;

    if (newWidth >= getWidth() || newWidth <=0)
        newWidth = getWidth();

    if (newHeight >= getHeight() || newHeight <= 0)
        newHeight = getHeight();

    //allocate our storage.
    uint8_t cropped[newWidth * newHeight];

    //calculate the pointer to where we want to begin cropping
    uint8_t *copyPointer = getBitmap() + (getWidth() * starty) + startx;

    //get a reference to our storage
    uint8_t *pastePointer = cropped;

    //go through row by row and select our image.
    for (int i = starty; i < newHeight; i++)
    {
        memcpy(pastePointer, copyPointer, newWidth);

        copyPointer += getWidth();
        pastePointer += newHeight;
    }

    return MicroBitImage(newWidth, newHeight, cropped);
}
/**
  * Pastes a given bitmap at the given co-ordinates.
  *
  * Any pixels in the relevant area of this image are replaced.
  *
  * @param image The MicroBitImage to paste.
  *
  * @param x The leftmost X co-ordinate in this image where the given image should be pasted. Defaults to 0.
  *
  * @param y The uppermost Y co-ordinate in this image where the given image should be pasted. Defaults to 0.
  *
  * @param alpha set to 1 if transparency clear pixels in given image should be treated as transparent. Set to 0 otherwise.  Defaults to 0.
  *
  * @return The number of pixels written.
  *
  * @code
  * 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
  * MicroBitImage i(10,5,heart); // a big heart
  * i.paste(i, -5, 0); // a small heart
  * @endcode
  */
int MicroBitImage::paste(const MicroBitImage &image, int16_t x, int16_t y, uint8_t alpha)
{
    uint8_t *pIn, *pOut;
    int cx, cy;
    int pxWritten = 0;

    // Sanity check.
    // We permit writes that overlap us, but ones that are clearly out of scope we can filter early.
    if (x >= getWidth() || y >= getHeight() || x+image.getWidth() <= 0 || y+image.getHeight() <= 0)
        return 0;

    //Calculate the number of byte we need to copy in each dimension.
    cx = x < 0 ? min(image.getWidth() + x, getWidth()) : min(image.getWidth(), getWidth() - x);
    cy = y < 0 ? min(image.getHeight() + y, getHeight()) : min(image.getHeight(), getHeight() - y);

    // Calculate sane start pointer.
    pIn = image.ptr->data;
    pIn += (x < 0) ? -x : 0;
    pIn += (y < 0) ? -image.getWidth()*y : 0;

    pOut = getBitmap();
    pOut += (x > 0) ? x : 0;
    pOut += (y > 0) ? getWidth()*y : 0;

    // Copy the image, stride by stride
    // If we want primitive transparecy, we do this byte by byte.
    // If we don't, use a more efficient block memory copy instead. Every little helps!

    if (alpha)
    {
        for (int i=0; i<cy; i++)
        {
            for (int j=0; j<cx; j++)
            {
                // Copy this byte if appropriate.
                if (*(pIn+j) != 0){
                    *(pOut+j) = *(pIn+j);
                    pxWritten++;
                }
            }

            pIn += image.getWidth();
            pOut += getWidth();
        }
    }
    else
    {
        for (int i=0; i<cy; i++)
        {
            memcpy(pOut, pIn, cx);

            pxWritten += cx;
            pIn += image.getWidth();
            pOut += getWidth();
        }
    }

    return pxWritten;
}
Example #4
0
Font* Resource::getFont( const std::string& filename, unsigned int textColor )
{
	std::string key = filename + toString(textColor);
	if (mFonts.find(key) == mFonts.end())
	{
		Font* font = new Font(getBitmap(filename, textColor), ' ', 'z');

		mFonts[key] = font;
	}

	return mFonts[key];
}
__unused JNIEXPORT jlong JNICALL
Java_pl_droidsonroids_gif_GifInfoHandle_renderFrame(JNIEnv *env, jclass __unused handleClass,
                                                    jlong gifInfo, jobject jbitmap) {
	GifInfo *info = (GifInfo *) (intptr_t) gifInfo;
	if (info == NULL)
		return -1;

	long renderStartTime = getRealTime();
	void *pixels;
	if (lockPixels(env, jbitmap, info, &pixels) != 0) {
		return 0;
	}
	DDGifSlurp(info, true, false);
	if (info->currentIndex == 0)
		prepareCanvas(pixels, info);
	const uint_fast32_t frameDuration = getBitmap((argb *) pixels, info);
	unlockPixels(env, jbitmap);
	return calculateInvalidationDelay(info, renderStartTime, frameDuration);
}
/**
  * Converts the bitmap to a csv ManagedString.
  *
  * @code
  * 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
  * MicroBitImage i(10,5,heart);
  * uBit.serial.printString(i.toString()); // "0,1,0,1,0,0,0,0,0,0\n..."
  * @endcode
  */
ManagedString MicroBitImage::toString()
{
    //width including commans and \n * height
    int stringSize = getSize() * 2;

    //plus one for string terminator
    char parseBuffer[stringSize + 1];

    parseBuffer[stringSize] = '\0';

    uint8_t *bitmapPtr = getBitmap();

    int parseIndex = 0;
    int widthCount = 0;

    while (parseIndex < stringSize)
    {
        if(*bitmapPtr)
            parseBuffer[parseIndex] = '1';
        else
            parseBuffer[parseIndex] = '0';

        parseIndex++;

        if(widthCount == getWidth()-1)
        {
            parseBuffer[parseIndex] = '\n';
            widthCount = 0;
        }
        else
        {
            parseBuffer[parseIndex] = ',';
            widthCount++;
        }

        parseIndex++;
        bitmapPtr++;
    }

    return ManagedString(parseBuffer);
}
Example #7
0
File: SkJS.cpp Project: 3rdexp/soui
SkJS::SkJS(void* hwnd) : SkOSWindow(hwnd) {
    if ((fRuntime = JS_NewRuntime(0x100000)) == NULL) {
        SkASSERT(0);
        return;
    }
    if ((fContext = JS_NewContext(fRuntime, 0x1000)) == NULL) {
        SkASSERT(0);
        return;
    }
    ;
    if ((fGlobal = JS_NewObject(fContext, &global_class, NULL, NULL)) == NULL) {
        SkASSERT(0);
        return;
    }
    if (JS_InitStandardClasses(fContext, fGlobal) == NULL) {
        SkASSERT(0);
        return;
    }
    setConfig(SkBitmap::kARGB32_Config);
    updateSize();
    setVisibleP(true);
    InitializeDisplayables(getBitmap(), fContext, fGlobal, NULL);
}
/**
  * Shifts the pixels in this Image a given number of pixels to the right.
  *
  * @param n The number of pixels to shift.
  *
  * @return MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER.
  *
  * @code
  * 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
  * MicroBitImage i(10,5,heart); // a big heart
  * i.shiftLeft(5); // a small heart
  * i.shiftRight(5); // a big heart
  * @endcode
  */
int MicroBitImage::shiftRight(int16_t n)
{
    uint8_t *p = getBitmap();
    int pixels = getWidth()-n;

    if (n <= 0)
        return MICROBIT_INVALID_PARAMETER;

    if(n >= getWidth())
    {
        clear();
        return MICROBIT_OK;
    }

    for (int y = 0; y < getHeight(); y++)
    {
        // Copy, and blank fill the leftmost column.
        memmove(p+n, p, pixels);
        memclr(p, n);
        p += getWidth();
    }

    return MICROBIT_OK;
}
/**
  * Create a copy of the image bitmap. Used particularly, when isReadOnly() is true.
  *
  * @return an instance of MicroBitImage which can be modified independently of the current instance
  */
MicroBitImage MicroBitImage::clone()
{
    return MicroBitImage(getWidth(), getHeight(), getBitmap());
}
/**
  * Resets all pixels in this image to 0.
  *
  * @code
  * 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
  * i.clear();
  * @endcode
  */
void MicroBitImage::clear()
{
    memclr(getBitmap(), getSize());
}
/**
  * Equality operation.
  *
  * Called when one MicroBitImage is tested to be equal to another using the '==' operator.
  *
  * @param i The MicroBitImage to test ourselves against.
  *
  * @return true if this MicroBitImage is identical to the one supplied, false otherwise.
  *
  * @code
  * MicroBitDisplay display;
  * MicroBitImage i();
  * MicroBitImage i1();
  *
  * if(i == i1) //will be true
  *     display.scroll("true");
  * @endcode
  */
bool MicroBitImage::operator== (const MicroBitImage& i)
{
    if (ptr == i.ptr)
        return true;
    else
        return (ptr->width == i.ptr->width && ptr->height == i.ptr->height && (memcmp(getBitmap(), i.ptr->data, getSize())==0));
}
Example #12
0
BITMAP* Resource::getBitmap(const std::string &filename) {
	return getBitmap(filename, makecol(255, 255, 255));
}
Example #13
0
int main() {
	// Setup IMU to get DMP data
    setup();
    usleep(100000);

    if (!bcm2835_init()) {
        return 1;
    }
    bcm2835_spi_begin();
    bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, 0);
    bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS1, 0);
    bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_256); //4096); //2048);
    bcm2835_spi_setDataMode(BCM2835_SPI_MODE1);
    bcm2835_spi_chipSelect(BCM2835_SPI_CS0);

    int var = 0;
    uint8_t imu_buf[BUF_SIZE];
    for (var = 0; var < BUF_SIZE; var++) {
        imu_buf[var] = 0x00;
    }
    char * lcd_buf = getBitmap();
    char RX = '0';
    char TX = '0';
    int i = 0;
    int ct = 0;
    int err = 0;
    int op_err = 0;
    int packets = 0;
    int seconds = 0;
    unsigned char bat1 = 0;
    unsigned char bat2 = 0;
    int data_p = 0;
    printf("Begin\n");
    while (1) {
		    clock_t total = clock();

        // Establishing communication for getting battery data
        /*
        TX = BAT;
        RX = bcm2835_spi_transfer(TX);
        RX = bcm2835_spi_transfer(TX);
        while (RX != BAT) {
            op_err++;
            usleep(10000);
            RX = bcm2835_spi_transfer(TX);
            printf("| tx:%x rx:%x(%c) |", TX, RX, RX);
            if (op_err % 10 == 0)
                printf("\n");
        }
        RX = bcm2835_spi_transfer(TX);
        bat1 = RX;
        RX = bcm2835_spi_transfer(TX);
        bat2 = RX;
        */
        // battery status is stored in bat1 and bat2
        data_p = 0;

        //trasnferring 1024 bytes of data: 12 at a time
        while (data_p < BUF_SIZE) {
            TX = ACCEL;
            RX = bcm2835_spi_transfer(TX);
            RX = bcm2835_spi_transfer(TX);
            while (RX != ACCEL) {
                op_err++;
                usleep(10000);
                RX = bcm2835_spi_transfer(TX);
                printf("| tx:%x rx:%x(%c) |", TX, RX, RX);
                if (op_err % 10 == 0)
                    printf("\n");
            }
            //printf("%d:Bat: %d%% %f Data: ", data_p, bat1, (bat1 + (float) bat2 / 256));
            for (i = 0; i < CHUNK_SIZE; i++) {
                TX = lcd_buf[data_p + i];
                RX = bcm2835_spi_transfer(TX);
                imu_buf[data_p + i] = RX;
                //printf("%x ", RX);
            }
            /*
            int x = (imu_buf[data_p + 0] << 8) | imu_buf[data_p + 1];
            int y = (imu_buf[data_p + 2] << 8) | imu_buf[data_p + 3];
            int z = (imu_buf[data_p + 4] << 8) | imu_buf[data_p + 5];
            int gx = (imu_buf[data_p + 6] << 8) | imu_buf[data_p + 7];
            int gy = (imu_buf[data_p + 8] << 8) | imu_buf[data_p + 9];
            int gz = (imu_buf[data_p + 10] << 8) | imu_buf[data_p + 11];
            printf("\nax:%d y:%d z:%d. gx:%d y:%d z:%d\n", x, y, z, gx, gy, gz);
            */
            TX = VALID;
            RX = bcm2835_spi_transfer(TX);
            if (RX == VALID) {
                data_p += CHUNK_SIZE;
            }
            else{
              printf("Invalid\n");
            }


            mpu.dmpGetQuaternion(&q, &imu_buf[data_p-CHUNK_SIZE]);
            mpu.dmpGetGravity(&gravity, &q);
            mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
            if(packets%10==5){
              printf("ypr  %7.2f %7.2f %7.2f   ", ypr[0] * 180 / M_PI, ypr[1] * 180 / M_PI, ypr[2] * 180 / M_PI);
              float diffsec = (float) (clock() - total) / CLOCKS_PER_SEC;
              printf("ERR %d|| total time = %f\n", op_err, diffsec);
            }
            //  gettimeofday(&tval_after, NULL);
            //   timersub(&tval_after, &tval_before, &tval_result);
            //   printf(" ---- transfer time %ld.%06ld sec", (long int)tval_result.tv_sec, (long int)tval_result.tv_usec);
            //   ct++;
            //   printf("\n");
            //if(packets%100==0)
            usleep(TIME_BETWEEN_CHUNK);
        }
 //       printf("PACKET %d successfully transferred\n", packets);
        packets++;
        usleep(TIME_BETWEEN_BUF);
        lcd_buf = getBitmap();
    }

    bcm2835_spi_end();
    bcm2835_close();

}