Java Array Manipulation -


i have function named resize, takes source array, , resizes new widths , height. method i'm using, think, inefficient. heard there's better way it. anyway, code below works when scale int. however, there's second function called half, uses resize shrink image in half. made scale double, , used typecast convert int. method not working, , dont know error (the teacher uses own grading , tests on these functions, , not passing it). can spot error, or there more efficient way make 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++)                 (int s1=0;s1<scale;s1++)                      (int s2=0;s2<scale;s2++)                          newimage[(int)(i*scale+s1)][(int)(j*scale+s2)] =source[i][j];          return newimage;      }      /**      * half size of image. method should 1 line!      * delegate work 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;     } 

so 1 scheme changing size of image resample (technically way, every variation different kind of resampling function).

cutting image in half super easy, want read every other pixel in each direction, , load pixel new half sized array. hard part making sure bookkeeping 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 code above i'm indexing original image reading every other pixel in every other row starting @ 0th row , 0th column (assuming images row major, here). thus, r tells row in original image we're looking at, , c tells column in original image we're looking at. orig[r][c] gives "current" pixel.

similarly, newr , newc index "half-image" matrix designated hi. each increment in newr or newc increment r , c 2, respectively. doing this, skip every other pixel iterate through image.

writing generalized resize routine doesn't operate on nice fractional quantities (like 1/2, 1/4, 1/8, etc.) pretty hard. you'd need define way determine value of sub-pixel -- point between pixels -- more complicated factors, 0.13243, example. is, of course, easy do, , can develop naive linear interpolation principle, when need value between 2 pixels take surrounding pixels, construct line between values, read sub-pixel point line. more complicated versions of interpolation might sinc based interpolation...or 1 of many others in published literature.

blowing size of image involves little different we've done here (and if in fact have write generalized resize function might consider splitting function handle upscaling , downscaling differently). need somehow create more values have -- interpolation functions work too. trivial method might repeat value between points until have enough, , slight variations on well, might take many values left , many right particular position.

what i'd encourage think -- , since homework i'll stay away implementation -- treating scaling factor causes make observations on 1 image, , writes on new image. when scaling factor less 1 sample original image populate new image , ignore of original image's pixels. when scaling factor greater 1, write more new image , might need read same value several times old image. (i'm doing poor job highlighting difference here, see dualism i'm getting at.)


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -