jmagick.h
上传用户:qingzhou
上传日期:2013-04-21
资源大小:733k
文件大小:18k
源码类别:

破解

开发平台:

Java

  1. #ifndef __JMAGICK__
  2. #define __JMAGICK__
  3. #include <magick/image.h>
  4. /*
  5.  * Convenience function to help throw an MagickException.
  6.  */
  7. void throwMagickException(JNIEnv *env, const char *mesg);
  8. /*
  9.  * Convenience function to help throw an MagickApiException.
  10.  *
  11.  * Input:
  12.  *   mesg       JMagick message
  13.  *   exception  points to a ImageMagick ExceptionInfo structure
  14.  */
  15. void throwMagickApiException(JNIEnv *env,
  16.      const char *mesg,
  17.      const ExceptionInfo *exception);
  18. /*
  19.  * Convenience function to retreive a handle from an object.
  20.  *
  21.  * Input:
  22.  *   env         Java VM environment
  23.  *   obj         Java object for which handle is to be retrieved
  24.  *   handleName  name of the handle in the object
  25.  *   fieldId     if non-null, contains the field ID. 0 to request retrieval.
  26.  *
  27.  * Output:
  28.  *   fieldId     if non-null, will contain field ID of the handle on output.
  29.  */
  30. void *getHandle(JNIEnv *env,
  31. jobject obj,
  32. const char *handleName,
  33. jfieldID *fieldId);
  34. /*
  35.  * Convenience function to set a handle in an object.
  36.  *
  37.  * Input:
  38.  *   env         Java VM environment
  39.  *   obj         Java object for which handle is to be retrieved
  40.  *   handleName  name of the handle in the object
  41.  *   fieldId     if non-null, contains the field ID. 0 to request retrieval.
  42.  *
  43.  * Output:
  44.  *   fieldId     if non-null, will contain field ID of the handle on output.
  45.  *
  46.  * Return:
  47.  *   non-zero    if successful
  48.  *   zero        if failure
  49.  */
  50. int setHandle(JNIEnv *env,
  51.       jobject obj,
  52.       const char *handleName,
  53.       void *handle,
  54.       jfieldID *fieldId);
  55. /*
  56.  * Retrieve the int value of the specified field.
  57.  *
  58.  * Input:
  59.  *   env        Java VM environment.
  60.  *   obj        Java object for which the value is to be retrieved.
  61.  *   fieldName  name of the field to be retrieved.
  62.  *   fieldID    if non-null, points to field ID. 0 to request retrieval.
  63.  *
  64.  * Output:
  65.  *   iRect      to be initilised by values in jRect.
  66.  *   fieldID    if non-null, will contain the field ID.
  67.  *   value      to contain the retrieved value. Must not be null.
  68.  *
  69.  * Return:
  70.  *   non-zero   if successful
  71.  *   zero       if failed
  72.  */
  73. int getIntFieldValue(JNIEnv *env,
  74.      jobject obj,
  75.      const char *fieldName,
  76.      jfieldID *fieldID,
  77.      jint *value);
  78. /*
  79.  * Store the int value of the specified field.
  80.  *
  81.  * Input:
  82.  *   env        Java VM environment.
  83.  *   obj        Java object for which the value is to be retrieved.
  84.  *   fieldName  name of the field to be retrieved.
  85.  *   fieldID    if non-null, points to field ID. 0 to request retrieval.
  86.  *   value      to contain the value to be stored.
  87.  *  
  88.  * Output:
  89.  *   fieldID    if non-null, will contain the field ID.
  90.  *  
  91.  * Return:
  92.  *   non-zero   if successful
  93.  *   zero       if failed
  94.  */ 
  95. int setIntFieldValue(JNIEnv *env,
  96.                      jobject obj,
  97.                      const char *fieldName,
  98.                      jfieldID *fieldID,
  99.                      jint value);
  100. /*
  101.  * From a java.awt.Rectangle object, construct a ImageMagick
  102.  * RectangleInfo, as passed in from the parameter.
  103.  *
  104.  * Input:
  105.  *   env        Java VM environment
  106.  *   jRect      an instance of java.awt.Rectangle
  107.  *
  108.  * Output:
  109.  *   iRect      to be initilised by values in jRect
  110.  *
  111.  * Return:
  112.  *   non-zero   if successful
  113.  *   zero       if failed
  114.  */
  115. int getRectangle(JNIEnv *env, jobject jRect, RectangleInfo *iRect);
  116. /*
  117.  * From a magick.PixelPacket object, construct a ImageMagick
  118.  * PixelPacket, as passed in from the parameter.
  119.  *
  120.  * Input:
  121.  *   env           Java VM environment
  122.  *   jPixelPacket  an instance of magick.PixelPacket
  123.  *
  124.  * Output:
  125.  *   iPixelPacket  to be initilised by values in jPixelPacket
  126.  *
  127.  * Return:
  128.  *   non-zero   if successful
  129.  *   zero       if failed
  130.  */
  131. int getPixelPacket(JNIEnv *env,
  132.    jobject jPixelPacket,
  133.    PixelPacket *iPixelPacket);
  134. /*
  135.  * Construct a new Java magick.MagickImage object and set the
  136.  * handle.
  137.  *
  138.  * Input:
  139.  *   env     Java VM environment
  140.  *   image   ImageMagick image handle
  141.  *
  142.  * Return:
  143.  *   A new instance of magick.MagickImage object.
  144.  *
  145.  */
  146. jobject newImageObject(JNIEnv *env, Image* image);
  147. /*
  148.  * Set a attribute in a generic handle to string.
  149.  *
  150.  * Input:
  151.  *   env        Java VM environment
  152.  *   attribVar  points to a C string so as to set the value
  153.  *   jstr       Java string for which to set the attrib
  154.  *
  155.  * Output:
  156.  *   attribVar  points to a new C string with content from jstr
  157.  */
  158. void setHandleAttribute(JNIEnv *env, char **attribVar, jstring jstr);
  159. /*
  160.  * Given the C ProfileInfo structure and the Java ProfileInfo object,
  161.  * acquire the contents of the Java ProfileInfo object and store it in
  162.  * the C ProfileInfo structure.
  163.  * 
  164.  * Input:
  165.  *   env            JNI environment
  166.  *   profileObj     Java ProfileInfo object for which field values are to be
  167.  *                  obtain to store into the C ProfileInfo structure
  168.  * Output:
  169.  *   profileInfo    C ProfileINfo structure to store field values
  170.  */
  171. void setProfileInfo(JNIEnv *env,
  172.                     ProfileInfo *profileInfo,
  173.                     jobject profileObj);
  174. /*
  175.  * Given the C ProfileInfo structure, construct a Java ProfileInfo
  176.  * object with values obtained from the C ProfileInfo structure.
  177.  * Input:
  178.  *   env           JNI environment
  179.  *   profileInfo   C ProfileInfo structure
  180.  * Return:
  181.  *   Java ProfileInfo object
  182.  */
  183. jobject getProfileInfo(JNIEnv *env, ProfileInfo *profileInfo);
  184. /*
  185.  * Convenience macro to set an attribute in the object handle.
  186.  */
  187. #define setMethod(funcName, fieldName, handleName, handleType, fieldType)     
  188. JNIEXPORT void JNICALL funcName                                               
  189.     (JNIEnv *env, jobject self, fieldType value)                              
  190. {                                                                             
  191.     handleType *info = NULL;                                                  
  192.                                                                               
  193.     info = (handleType *) getHandle(env, self, handleName, NULL);             
  194.     if (info == NULL) {                                                       
  195. throwMagickException(env, "Unable to retrieve handle");               
  196. return;                                                               
  197.     }                                                                         
  198.                                                                               
  199.     info->fieldName = value;                                                  
  200. }
  201. /*
  202.  * Convenience macro to get an attribute of an object handle.
  203.  */
  204. #define getMethod(funcName, fieldName, handleName, handleType, fieldType)     
  205. JNIEXPORT fieldType JNICALL funcName                                          
  206.     (JNIEnv *env, jobject self)                                               
  207. {                                                                             
  208.     handleType *info = NULL;                                                  
  209.                                                                               
  210.     info = (handleType *) getHandle(env, self, handleName, NULL);             
  211.     if (info == NULL) {                                                       
  212. throwMagickException(env, "Unable to retrieve handle");               
  213. return (fieldType) 0;                                                 
  214.     }                                                                         
  215.                                                                               
  216.     return info->fieldName;                                                   
  217. }
  218. /*
  219.  * Convenience macro to set an integer attribute in the object handle.
  220.  */
  221. #define setIntMethod(funcName, fieldName, handleName, handleType)             
  222.         setMethod(funcName, fieldName, handleName, handleType, jint)
  223. /*
  224.  * Convenience macro to get an integer attribute of an object handle.
  225.  */
  226. #define getIntMethod(funcName, fieldName, handleName, handleType)             
  227.         getMethod(funcName, fieldName, handleName, handleType, jint)
  228. /*
  229.  * Convenience macro to set an boolean attribute in the object handle.
  230.  */
  231. #define setBoolMethod(funcName, fieldName, handleName, handleType)            
  232.         setMethod(funcName, fieldName, handleName, handleType, jboolean)
  233. /*
  234.  * Convenience macro to get an boolean attribute of an object handle.
  235.  */
  236. #define getBoolMethod(funcName, fieldName, handleName, handleType)            
  237.         getMethod(funcName, fieldName, handleName, handleType, jboolean)
  238. /*
  239.  * Convenience macro to set an short attribute in the object handle.
  240.  */
  241. #define setShortMethod(funcName, fieldName, handleName, handleType)           
  242.         setMethod(funcName, fieldName, handleName, handleType, jshort)
  243. /*
  244.  * Convenience macro to get an byte attribute of an object handle.
  245.  */
  246. #define getByteMethod(funcName, fieldName, handleName, handleType)            
  247.         getMethod(funcName, fieldName, handleName, handleType, jbyte)
  248. /*
  249.  * Convenience macro to set an byte attribute in the object handle.
  250.  */
  251. #define setByteMethod(funcName, fieldName, handleName, handleType)            
  252.         setMethod(funcName, fieldName, handleName, handleType, jbyte)
  253. /*
  254.  * Convenience macro to get a double attribute of an object handle.
  255.  */
  256. #define getDoubleMethod(funcName, fieldName, handleName, handleType)          
  257.         getMethod(funcName, fieldName, handleName, handleType, jdouble)
  258. /*
  259.  * Convenience macro to set an double attribute in the object handle.
  260.  */
  261. #define setDoubleMethod(funcName, fieldName, handleName, handleType)          
  262.         setMethod(funcName, fieldName, handleName, handleType, jdouble)
  263. /*
  264.  * Convenience macro to set a string attribute in the object handle.
  265.  */
  266. #define setStringMethod(funcName, fieldName, handleName, handleType)          
  267. JNIEXPORT void JNICALL funcName                                               
  268.     (JNIEnv *env, jobject self, jstring value)                                
  269. {                                                                             
  270.     handleType *info = NULL;                                                  
  271.     const char *cstr = NULL;                                                  
  272.                                                                               
  273.     info = (handleType *) getHandle(env, self, handleName, NULL);             
  274.     if (info == NULL) {                                                       
  275. throwMagickException(env, "Unable to retrieve handle");               
  276. return;                                                               
  277.     }                                                                         
  278.                                                                               
  279.     if (info->fieldName != NULL) {                                            
  280. LiberateMemory((void**) &info->fieldName);                            
  281. info->fieldName = NULL;                                               
  282.     }                                                                         
  283.                                                                               
  284.     cstr = (*env)->GetStringUTFChars(env, value, 0);                          
  285.     if (cstr == NULL) {                                                       
  286. throwMagickException(env, "Unable to retrieve Java string chars");    
  287. return;                                                               
  288.     }                                                                         
  289.     info->fieldName = (char *) AcquireString(cstr);                           
  290.     if (info->fieldName == NULL) {                                            
  291. throwMagickException(env, "Unable to allocate memory");               
  292.     }                                                                         
  293.     (*env)->ReleaseStringUTFChars(env, value, cstr);                          
  294. }
  295. /*
  296.  * Convenience macro to get a string attribute in the object handle.
  297.  */
  298. #define getStringMethod(funcName, fieldName, handleName, handleType)          
  299. JNIEXPORT jstring JNICALL funcName                                            
  300.     (JNIEnv *env, jobject self)                                               
  301. {                                                                             
  302.     handleType *info = NULL;                                                  
  303.     jstring jstr = NULL;                                                      
  304.                                                                               
  305.     info = (handleType *) getHandle(env, self, handleName, NULL);             
  306.     if (info == NULL) {                                                       
  307. throwMagickException(env, "Unable to retrieve handle");               
  308. return NULL;                                                          
  309.     }                                                                         
  310.                                                                               
  311.     if (info->fieldName == NULL) {                                            
  312. return NULL;                                                          
  313.     }                                                                         
  314.                                                                               
  315.     jstr = (*env)->NewStringUTF(env, info->fieldName);                        
  316.     if (jstr == NULL) {                                                       
  317. throwMagickException(env, "Unable to construct new string");          
  318. return NULL;                                                          
  319.     }                                                                         
  320.                                                                               
  321.     return jstr;                                                              
  322. }
  323. /*
  324.  * Convenience macro to set a PixelPacket attribute in the object handle.
  325.  */
  326. #define setPixelPacketMethod(funcName, fieldName, handleName, handleType)     
  327. JNIEXPORT void JNICALL funcName                                               
  328.     (JNIEnv *env, jobject self, jobject jPixelPacket)                         
  329. {                                                                             
  330.     handleType *info = NULL;                                                  
  331.                                                                               
  332.     info = (handleType *) getHandle(env, self, handleName, NULL);             
  333.     if (info == NULL) {                                                       
  334. throwMagickException(env, "Unable to retrieve handle");               
  335. return;                                                               
  336.     }                                                                         
  337.                                                                               
  338.     if (!getPixelPacket(env, jPixelPacket, &info->fieldName)) {               
  339. throwMagickException(env, "Unable to set PixelPacket");               
  340. return;                                                               
  341.     }                                                                         
  342. }
  343. /*
  344.  * Convenience macro to get a PixelPacket attribute in the object handle.
  345.  */
  346. #define getPixelPacketMethod(funcName, fieldName, handleName, handleType)     
  347. JNIEXPORT jobject JNICALL funcName                                            
  348.     (JNIEnv *env, jobject self)                                               
  349. {                                                                             
  350.     handleType *info = NULL;                                                  
  351.     jobject jPixelPacket = NULL;                                              
  352.     jclass pixelPacketClass;                                                  
  353.     jmethodID consMethodID;                                                   
  354.                                                                               
  355.     info = (handleType *) getHandle(env, self, handleName, NULL);             
  356.     if (info == NULL) {                                                       
  357. throwMagickException(env, "Unable to retrieve handle");               
  358. return NULL;                                                          
  359.     }                                                                         
  360.                                                                               
  361.     pixelPacketClass = (*env)->FindClass(env, "magick/PixelPacket");          
  362.     if (pixelPacketClass == 0) {                                              
  363. throwMagickException(env,                                             
  364.      "Unable to locate class magick.PixelPacket");    
  365. return NULL;                                                          
  366.     }                                                                         
  367.                                                                               
  368.     consMethodID = (*env)->GetMethodID(env, pixelPacketClass,                 
  369.        "<init>", "(IIII)V");                  
  370.     if (consMethodID == 0) {                                                  
  371. throwMagickException(env, "Unable to construct magick.PixelPacket");  
  372. return NULL;                                                          
  373.     }                                                                         
  374.                                                                               
  375.     jPixelPacket = (*env)->NewObject(env, pixelPacketClass, consMethodID,     
  376.                      (jint) info->fieldName.red,              
  377.                      (jint) info->fieldName.green,            
  378.                      (jint) info->fieldName.blue,             
  379.                      (jint) info->fieldName.opacity);         
  380.     if (jPixelPacket == NULL) {                                               
  381. throwMagickException(env, "Unable to construct magick.PixelPacket");  
  382. return NULL;                                                          
  383.     }                                                                         
  384.                                                                               
  385.     return jPixelPacket;                                                      
  386. }
  387. #endif /* __JMAGICK__ */