Results 1 to 4 of 4

Thread: Nearest Neighboor on Arduino

  1. #1

    Thread Starter
    Hyperactive Member gaouser's Avatar
    Join Date
    Mar 2022
    Location
    World:\Turkey\User32.DLL
    Posts
    481

    Nearest Neighboor on Arduino

    Hi guys I can't really do Nearest Neighboor Resizing on 1D array bitmap with my arduino this is my code

    Code:
    uint16_t* resizePixels(uint16_t *pixels,int w1,int h1,int w2,int h2) {
        uint16_t *temp = new uint16_t[w2*h2] ;
        double x_ratio = w1/(double)w2 ;
        double y_ratio = h1/(double)h2 ;
        double px, py ; 
        for (int i=0;i<h2;i++) {
            for (int j=0;j<w2;j++) {
                px = floor(j*x_ratio) ;
                py = floor(i*y_ratio) ;
                temp[(i*w2)+j] = pixels[(uint16_t)((py*w1)+px)] ;
            }
        }
        return temp ;
    }
    it seems to do nothing. I use it like this (dbc is drawbitmap function that works)
    Code:
    dBC(x, y, w,h,resizePixels(Bit1,60,30,w,h));
    Nothing happened on the screen. but without resize code,it displays so i know dbc function works

  2. #2
    Fanatic Member Peter Porter's Avatar
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    563

    Re: Nearest Neighboor on Arduino

    I know this thread is two years old. Gaouser's code's main issue is due to the use of floor() and the improper handling of pixel indexing. Below is an updated version for anyone who might have a similar problem:

    Code:
    uint16_t* resizePixels(uint16_t *pixels, int w1, int h1, int w2, int h2) {
        uint16_t *temp = new uint16_t[w2 * h2];
        double x_ratio = (double)w1 / w2;
        double y_ratio = (double)h1 / h2;
        int px, py;  // Use integer for pixel positions
    
        for (int i = 0; i < h2; i++) {
            for (int j = 0; j < w2; j++) {
                px = (int)(j * x_ratio);  // No need for floor() since casting works here
                py = (int)(i * y_ratio);  // Same here
    
                // Ensure correct indexing into the original pixel array
                temp[(i * w2) + j] = pixels[(py * w1) + px];
            }
        }
        return temp;
    }

  3. #3

    Thread Starter
    Hyperactive Member gaouser's Avatar
    Join Date
    Mar 2022
    Location
    World:\Turkey\User32.DLL
    Posts
    481

    Re: Nearest Neighboor on Arduino

    Quote Originally Posted by Peter Porter View Post
    I know this thread is two years old. Gaouser's code's main issue is due to the use of floor() and the improper handling of pixel indexing. Below is an updated version for anyone who might have a similar problem:

    Code:
    uint16_t* resizePixels(uint16_t *pixels, int w1, int h1, int w2, int h2) {
        uint16_t *temp = new uint16_t[w2 * h2];
        double x_ratio = (double)w1 / w2;
        double y_ratio = (double)h1 / h2;
        int px, py;  // Use integer for pixel positions
    
        for (int i = 0; i < h2; i++) {
            for (int j = 0; j < w2; j++) {
                px = (int)(j * x_ratio);  // No need for floor() since casting works here
                py = (int)(i * y_ratio);  // Same here
    
                // Ensure correct indexing into the original pixel array
                temp[(i * w2) + j] = pixels[(py * w1) + px];
            }
        }
        return temp;
    }
    Literally thank you man!
    MicrosoftWindowsxp
    Professional

    was the peak Windows version not gonna lie

  4. #4

    Thread Starter
    Hyperactive Member gaouser's Avatar
    Join Date
    Mar 2022
    Location
    World:\Turkey\User32.DLL
    Posts
    481

    Re: Nearest Neighboor on Arduino

    This forum is really better than Arduino forum. they literally tell you to go resize it on PC there like if the bitmap didnt need to be scaled to 240^240 diffrent combinations
    MicrosoftWindowsxp
    Professional

    was the peak Windows version not gonna lie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width