Attempt to convert a C++ program to Java. Memcpy help needed -
i'm converting c++ program java, , have hit hitch in there no memcpy in java.
i've taken best crack @ it, after doing research, i'm hesitant , unsure if doing c++ program should, found information found far little confusing.
(java code still has memcpy in commented out refernce)
the notes both
image = input image
output = output image
n = width of image
m = height of image.
in c++ code, element unsigned short int.
c++ code
void medianfilter(element* image, element* result, int n, int m) { // check arguments if (!image || n < 1 || m < 1) return; // allocate memory signal extension element* extension = new element[(n + 2) * (m + 2)]; // check memory allocation if (!extension) return; // create image extension (int = 0; < m; ++i) { memcpy(extension + (n + 2) * (i + 1) + 1, image + n * i, n * sizeof(element)); extension[(n + 2) * (i + 1)] = image[n * i]; extension[(n + 2) * (i + 2) - 1] = image[n * (i + 1) - 1]; } // fill first line of image extension memcpy(extension, extension + n + 2, (n + 2) * sizeof(element)); // fill last line of image extension memcpy(extension + (n + 2) * (m + 1), extension + (n + 2) * m, (n + 2) * sizeof(element)); // call median filter implementation _medianfilter(extension, result ? result : image, n + 2, m + 2); // free memory delete[] extension; }
java code far.
public static void medianfilter(long[] image, long[] result, int n, int m) { // check arguments if (n < 1 || m < 1) return; // allocate memory signal extension long[] extension = new long[(n + 2) * (m + 2)]; // create image extension (int = 0; < m; ++i) { //memcpy(extension + (n + 2) * (i + 1) + 1, image + n * i, n * sizeof(element)); for( int k = 0 ; k < n ; k++) { extension[(n+2 * (i+1)+1)+k] = image[(n*i)+k]; } extension[(n + 2) * (i + 1)] = image[n * i]; extension[(n + 2) * (i + 2) - 1] = image[n * (i + 1) - 1]; } // fill first line of image extension //memcpy(extension, extension + n + 2, (n + 2) * sizeof(element)); // fill last line of image extension //memcpy(extension + (n + 2) * (m + 1), extension + (n + 2) * m, (n + 2) * sizeof(element)); // call median filter implementation //_medianfilter(extension, result ? result : image, n + 2, m + 2); }
i highly suspect i've done wrong, wanted assistance on this. (the rest of program easy, doesn't use memcpy).
java have memcpy(). unsafe class has copymemory() method identical memcpy(). of course, memcpy(), provides no protection memory overlays, data destruction, etc. not clear if memcpy() or memmove(). can used copy actual addresses actual addresses or references references. note if references used, must provide offset (or jvm die asap).
unsafe.copymemory() works (up 2 gb per second on old tired pc). use @ own risk. note unsafe class not exist jvm implementations.
you should take @ "tricks direct memory access in java" (http://highlyscalable.wordpress.com/2012/02/02/direct-memory-access-in-java/) , "java magic. part 4: sun.misc.unsafe" (memcpy function in c++ java equivalent) additional ideas. these guys versed in how low level (and risky) operations in java.
Comments
Post a Comment