Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3216336
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:16:57+00:00 2026-05-17T15:16:57+00:00

I have a function named resize, which takes a source array, and resizes to

  • 0

I have a function named resize, which takes a source array, and resizes to new widths and height. The method I’m using, I think, is inefficient. I heard there’s a better way to do it. Anyway, the code below works when scale is an int. However, there’s a second function called half, where it uses resize to shrink an image in half. So I made scale a double, and used a typecast to convert it back to an int. This method is not working, and I dont know what the error is (the teacher uses his own grading and tests on these functions, and its not passing it). Can you spot the error, or is there a more efficient way to make a resize function?

public static int[][] resize(int[][] source, int newWidth, int newHeight) {

        int[][] newImage=new int[newWidth][newHeight];
        double scale=newWidth/(source.length);
        for(int i=0;i<newWidth/scale;i++)
            for(int j=0;j<newHeight/scale;j++)
                for (int s1=0;s1<scale;s1++) 
                    for (int s2=0;s2<scale;s2++) 
                        newImage[(int)(i*scale+s1)][(int)(j*scale+s2)] =source[i][j];

        return newImage; 
    }

    /**
     * Half the size of the image. This method should be just one line! Just
     * delegate the work to resize()!
     */
    public static int[][] half(int[][] source) {
        int[][] newImage=new int[source.length/2][source[0].length/2];
        newImage=resize(source,source.length/2,source[0].length/2);
        return newImage;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T15:16:57+00:00Added an answer on May 17, 2026 at 3:16 pm

    So one scheme for changing the size of an image is to resample it (technically this is really the only way, every variation is really just a different kind of resampling function).

    Cutting an image in half is super easy, you want to read every other pixel in each direction, and then load that pixel into the new half sized array. The hard part is making sure your bookkeeping is strong.

    static int[][] halfImage(int[][] orig){
        int[][] hi = new int[orig.length/2][orig[0].length/2];
    
        for(int r = 0, newr = 0; r < orig.length; r += 2, newr++){
            for(int c = 0, newc = 0; c < orig[0].length; c += 2, newc++){
                hi[newr][newc] = orig[r][c];
            }
        }
    
        return hi;
    }
    

    In the code above I’m indexing into the original image reading every other pixel in every other row starting at the 0th row and 0th column (assuming images are row major, here). Thus, r tells us which row in the original image we’re looking at, and c tells us which column in the original image we’re looking at. orig[r][c] gives us the “current” pixel.

    Similarly, newr and newc index into the “half-image” matrix designated hi. For each increment in newr or newc we increment r and c by 2, respectively. By doing this, we skip every other pixel as we iterate through the image.

    Writing a generalized resize routine that doesn’t operate on nice fractional quantities (like 1/2, 1/4, 1/8, etc.) is really pretty hard. You’d need to define a way to determine the value of a sub-pixel — a point between pixels — for more complicated factors, like 0.13243, for example. This is, of course, easy to do, and you can develop a very naive linear interpolation principle, where when you need the value between two pixels you simply take the surrounding pixels, construct a line between their values, then read the sub-pixel point from the line. More complicated versions of interpolation might be a sinc based interpolation…or one of many others in widely published literature.

    Blowing up the size of the image involves something a little different than we’ve done here (and if you do in fact have to write a generalized resize function you might consider splitting your function to handle upscaling and downscaling differently). You need to somehow create more values than you have originally — those interpolation functions work for that too. A trivial method might simply be to repeat a value between points until you have enough, and slight variations on this as well, where you might take so many values from the left and so many from the right for a particular position.

    What I’d encourage you to think about — and since this is homework I’ll stay away from the implementation — is treating the scaling factor as something that causes you to make observations on one image, and writes on the new image. When the scaling factor is less than 1 you generally sample from the original image to populate the new image and ignore some of the original image’s pixels. When the scaling factor is greater than 1, you generally write more often to the new image and might need to read the same value several times from the old image. (I’m doing a poor job highlighting the difference here, hopefully you see the dualism I’m getting at.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.