sarju 0 Newbie Poster

Dear friends,

I am working on JNI (Java Native Interface) in my project as i have application and libraries in "c" programming language and i wish to make it accessible with java application. But i am facing some problem while writing java code as I am not expert with java language and working first time with java. So could you please help me out in following regrads:

I have two source file : encoder.c and decoder.c

encoder.c
-----------
encoder.c

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <wsq.h>
#include <dataio.h>


int wsq_encode_mem(unsigned char **odata, int *olen, const float r_bitrate,
unsigned char *idata, const int w, const int h,
const int d, const int ppi, char *comment_text)
{
int ret, num_pix;
float *fdata; /* floating point pixel image */
float m_shift, r_scale; /* shift/scale parameters */
short *qdata; /* quantized image pointer */
int qsize, qsize1, qsize2, qsize3; /* quantized block sizes */
unsigned char *huffbits, *huffvalues; /* huffman code parameters */
HUFFCODE *hufftable; /* huffcode table */
unsigned char *huff_buf; /* huffman encoded buffer */
int hsize, hsize1, hsize2, hsize3; /* Huffman coded blocks sizes */
unsigned char *wsq_data; /* compressed data buffer */
int wsq_alloc, wsq_len; /* number of bytes in buffer */
int block_sizes[2];

/* Compute the total number of pixels in image. */
num_pix = w * h;

/* Allocate floating point pixmap. */
if((fdata = (float *) malloc(num_pix*sizeof(float))) == NULL) {
fprintf(stderr,"ERROR : wsq_encode_1 : malloc : fdata\n");
return(-10);
}

/* Convert image pixels to floating point. */
conv_img_2_flt(fdata, &m_shift, &r_scale, idata, num_pix);

if(debug > 0)
fprintf(stderr, "Input image pixels converted to floating point\n\n");

/* Build WSQ decomposition trees */
build_wsq_trees(w_tree, W_TREELEN, q_tree, Q_TREELEN, w, h);

if(debug > 0)
fprintf(stderr, "Tables for wavelet decomposition finished\n\n");

/* WSQ decompose the image */
if(ret = wsq_decompose(fdata, w, h, w_tree, W_TREELEN,
hifilt, MAX_HIFILT, lofilt, MAX_LOFILT)){
free(fdata);
return(ret);
}

if(debug > 0)
fprintf(stderr, "WSQ decomposition of image finished\n\n");

/* Set compression ratio and 'q' to zero. */
quant_vals.cr = 0;
quant_vals.q = 0.0;
/* Assign specified r-bitrate into quantization structure. */
quant_vals.r = r_bitrate;
/* Compute subband variances. */
variance(&quant_vals, q_tree, Q_TREELEN, fdata, w, h);

if(debug > 0)
fprintf(stderr, "Subband variances computed\n\n");

/* Quantize the floating point pixmap. */
if(ret = quantize(&qdata, &qsize, &quant_vals, q_tree, Q_TREELEN,
fdata, w, h)){
free(fdata);
return(ret);
}

/* Done with floating point wsq subband data. */
free(fdata);

if(debug > 0)
fprintf(stderr, "WSQ subband decomposition data quantized\n\n");

/* Compute quantized WSQ subband block sizes */
quant_block_sizes(&qsize1, &qsize2, &qsize3, &quant_vals,
w_tree, W_TREELEN, q_tree, Q_TREELEN);

if(qsize != qsize1+qsize2+qsize3){
fprintf(stderr,
"ERROR : wsq_encode_1 : problem w/quantization block sizes\n");
return(-11);
}

/* Allocate a WSQ-encoded output buffer. Allocate this buffer */
/* to be the size of the original pixmap. If the encoded data */
/* exceeds this buffer size, then throw an error because we do */
/* not want our compressed data to be larger than the original */
/* image data. */
wsq_data = (unsigned char *)malloc(num_pix);
if(wsq_data == (unsigned char *)NULL){
free(qdata);
fprintf(stderr, "ERROR : wsq_encode_1 : malloc : wsq_data\n");
return(-12);
}
wsq_alloc = num_pix;
wsq_len = 0;

/* Add a Start Of Image (SOI) marker to the WSQ buffer. */
if(ret = putc_ushort(SOI_WSQ, wsq_data, wsq_alloc, &wsq_len)){
free(qdata);
free(wsq_data);
return(ret);
}

if(ret = putc_nistcom_wsq(comment_text, w, h, d, ppi, 1 /* lossy */,
r_bitrate, wsq_data, wsq_alloc, &wsq_len)){
free(qdata);
free(wsq_data);
return(ret);
}

/* Store the Wavelet filter taps to the WSQ buffer. */
if(ret = putc_transform_table(lofilt, MAX_LOFILT,
hifilt, MAX_HIFILT,
wsq_data, wsq_alloc, &wsq_len)){
free(qdata);
free(wsq_data);
return(ret);
}

/* Store the quantization parameters to the WSQ buffer. */
if(ret = putc_quantization_table(&quant_vals,
wsq_data, wsq_alloc, &wsq_len)){
free(qdata);
free(wsq_data);
return(ret);
}

/* Store a frame header to the WSQ buffer. */
if(ret = putc_frame_header_wsq(w, h, m_shift, r_scale,
wsq_data, wsq_alloc, &wsq_len)){
free(qdata);
free(wsq_data);
return(ret);
}

if(debug > 0)
fprintf(stderr, "SOI, tables, and frame header written\n\n");

/* Allocate a temporary buffer for holding compressed block data. */
/* This buffer is allocated to the size of the original input image, */
/* and it is "assumed" that the compressed blocks will not exceed */
/* this buffer size. */
huff_buf = (unsigned char *)malloc(num_pix);
if(huff_buf == (unsigned char *)NULL) {
free(qdata);
free(wsq_data);
fprintf(stderr, "ERROR : wsq_encode_1 : malloc : huff_buf\n");
return(-13);
}

/******************/
/* ENCODE Block 1 */
/******************/
/* Compute Huffman table for Block 1. */
if(ret = gen_hufftable_wsq(&hufftable, &huffbits, &huffvalues,
qdata, &qsize1, 1)){
free(qdata);
free(wsq_data);
free(huff_buf);
return(ret);
}

/* Store Huffman table for Block 1 to WSQ buffer. */
if(ret = putc_huffman_table(DHT_WSQ, 0, huffbits, huffvalues,
wsq_data, wsq_alloc, &wsq_len)){
free(qdata);
free(wsq_data);
free(huff_buf);
free(huffbits);
free(huffvalues);
free(hufftable);
return(ret);
}
free(huffbits);
free(huffvalues);

if(debug > 0)
fprintf(stderr, "Huffman code Table 1 generated and written\n\n");

/* Compress Block 1 data. */
if(ret = compress_block(huff_buf, &hsize1, qdata, qsize1,
MAX_HUFFCOEFF, MAX_HUFFZRUN, hufftable)){
free(qdata);
free(wsq_data);
free(huff_buf);
free(hufftable);
return(ret);
}
/* Done with current Huffman table. */
free(hufftable);

/* Accumulate number of bytes compressed. */
hsize = hsize1;

/* Store Block 1's header to WSQ buffer. */
if(ret = putc_block_header(0, wsq_data, wsq_alloc, &wsq_len)){
free(qdata);
free(wsq_data);
free(huff_buf);
return(ret);
}

/* Store Block 1's compressed data to WSQ buffer. */
if(ret = putc_bytes(huff_buf, hsize1, wsq_data, wsq_alloc, &wsq_len)){
free(qdata);
free(wsq_data);
free(huff_buf);
return(ret);
}

if(debug > 0)
fprintf(stderr, "Block 1 compressed and written\n\n");

/******************/
/* ENCODE Block 2 */
/******************/
/* Compute Huffman table for Blocks 2 & 3. */
block_sizes[0] = qsize2;
block_sizes[1] = qsize3;
if(ret = gen_hufftable_wsq(&hufftable, &huffbits, &huffvalues,
qdata+qsize1, block_sizes, 2)){
free(qdata);
free(wsq_data);
free(huff_buf);
return(ret);
}

/* Store Huffman table for Blocks 2 & 3 to WSQ buffer. */
if(ret = putc_huffman_table(DHT_WSQ, 1, huffbits, huffvalues,
wsq_data, wsq_alloc, &wsq_len)){
free(qdata);
free(wsq_data);
free(huff_buf);
free(huffbits);
free(huffvalues);
free(hufftable);
return(ret);
}
free(huffbits);
free(huffvalues);

if(debug > 0)
fprintf(stderr, "Huffman code Table 2 generated and written\n\n");

/* Compress Block 2 data. */
if(ret = compress_block(huff_buf, &hsize2, qdata+qsize1, qsize2,
MAX_HUFFCOEFF, MAX_HUFFZRUN, hufftable)){
free(qdata);
free(wsq_data);
free(huff_buf);
free(hufftable);
return(ret);
}

/* Accumulate number of bytes compressed. */
hsize += hsize2;

/* Store Block 2's header to WSQ buffer. */
if(ret = putc_block_header(1, wsq_data, wsq_alloc, &wsq_len)){
free(qdata);
free(wsq_data);
free(huff_buf);
free(hufftable);
return(ret);
}

/* Store Block 2's compressed data to WSQ buffer. */
if(ret = putc_bytes(huff_buf, hsize2, wsq_data, wsq_alloc, &wsq_len)){
free(qdata);
free(wsq_data);
free(huff_buf);
free(hufftable);
return(ret);
}

if(debug > 0)
fprintf(stderr, "Block 2 compressed and written\n\n");

/******************/
/* ENCODE Block 3 */
/******************/
/* Compress Block 3 data. */
if(ret = compress_block(huff_buf, &hsize3, qdata+qsize1+qsize2, qsize3,
MAX_HUFFCOEFF, MAX_HUFFZRUN, hufftable)){
free(qdata);
free(wsq_data);
free(huff_buf);
free(hufftable);
return(ret);
}
/* Done with current Huffman table. */
free(hufftable);

/* Done with quantized image buffer. */
free(qdata);

/* Accumulate number of bytes compressed. */
hsize += hsize3;

/* Store Block 3's header to WSQ buffer. */
if(ret = putc_block_header(1, wsq_data, wsq_alloc, &wsq_len)){
free(wsq_data);
free(huff_buf);
return(ret);
}

/* Store Block 3's compressed data to WSQ buffer. */
if(ret = putc_bytes(huff_buf, hsize3, wsq_data, wsq_alloc, &wsq_len)){
free(wsq_data);
free(huff_buf);
return(ret);
}

if(debug > 0)
fprintf(stderr, "Block 3 compressed and written\n\n");

/* Done with huffman compressing blocks, so done with buffer. */
free(huff_buf);

/* Add a End Of Image (EOI) marker to the WSQ buffer. */
if(ret = putc_ushort(EOI_WSQ, wsq_data, wsq_alloc, &wsq_len)){
free(wsq_data);
return(ret);
}

if(debug >= 1) {
fprintf(stderr,
"hsize1 = %d :: hsize2 = %d :: hsize3 = %d\n", hsize1, hsize2, hsize3);
fprintf(stderr,"@ r = %.3f :: complen = %d :: ratio = %.1f\n",
r_bitrate, hsize, (float)(num_pix)/(float)hsize);
}

*odata = wsq_data;
*olen = wsq_len;

/* Return normally. */
return(0);
}

Decoder.c
----------
Decoder.c

int wsq_decode_mem(unsigned char **odata, int *ow, int *oh, int *od, int *oppi,
int *lossyflag, unsigned char *idata, const int ilen)
{
int ret, i;
unsigned short marker; /* WSQ marker */
int num_pix; /* image size and counter */
int width, height, ppi; /* image parameters */
unsigned char *cdata; /* image pointer */
float *fdata; /* image pointers */
short *qdata; /* image pointers */
unsigned char *cbufptr; /* points to current byte in buffer */
unsigned char *ebufptr; /* points to end of buffer */


/* Set memory buffer pointers. */
cbufptr = idata;
ebufptr = idata + ilen;

/* Init DHT Tables to 0. */
for(i = 0; i < MAX_DHT_TABLES; i++)
(dht_table + i)->tabdef = 0;

/* Read the SOI marker. */
if(ret = getc_marker_wsq(&marker, SOI_WSQ, &cbufptr, ebufptr)){
return(ret);
}

/* Read in supporting tables up to the SOF marker. */
if(ret = getc_marker_wsq(&marker, TBLS_N_SOF, &cbufptr, ebufptr)){
return(ret);
}
while(marker != SOF_WSQ) {
if(ret = getc_table_wsq(marker, &dtt_table, &dqt_table, dht_table,
&cbufptr, ebufptr)){
return(ret);
}
if(ret = getc_marker_wsq(&marker, TBLS_N_SOF, &cbufptr, ebufptr)){
return(ret);
}
}

/* Read in the Frame Header. */
if(ret = getc_frame_header_wsq(&frm_header_wsq, &cbufptr, ebufptr)){
return(ret);
}
width = frm_header_wsq.width;
height = frm_header_wsq.height;
num_pix = width * height;

if(ret = getc_ppi_wsq(&ppi, idata, ilen))
return(ret);

if(debug > 0)
fprintf(stderr, "SOI, tables, and frame header read\n\n");

/* Build WSQ decomposition trees. */
build_wsq_trees(w_tree, W_TREELEN, q_tree, Q_TREELEN, width, height);

if(debug > 0)
fprintf(stderr, "Tables for wavelet decomposition finished\n\n");

/* Allocate working memory. */
qdata = (short *) malloc(num_pix * sizeof(short));
if(qdata == (short *)NULL) {
fprintf(stderr,"ERROR: wsq_decode_mem : malloc : qdata1\n");
return(-20);
}
/* Decode the Huffman encoded data blocks. */
if(ret = huffman_decode_data_mem(qdata, &dtt_table, &dqt_table, dht_table,
&cbufptr, ebufptr)){
free(qdata);
return(ret);
}

if(debug > 0)
fprintf(stderr,
"Quantized WSQ subband data blocks read and Huffman decoded\n\n");

/* Decode the quantize wavelet subband data. */
if(ret = unquantize(&fdata, &dqt_table, q_tree, Q_TREELEN,
qdata, width, height)){
free(qdata);
return(ret);
}

if(debug > 0)
fprintf(stderr, "WSQ subband data blocks unquantized\n\n");

/* Done with quantized wavelet subband data. */
free(qdata);

if(ret = wsq_reconstruct(fdata, width, height, w_tree, W_TREELEN,
&dtt_table)){
free(fdata);
return(ret);
}

if(debug > 0)
fprintf(stderr, "WSQ reconstruction of image finished\n\n");

cdata = (unsigned char *)malloc(num_pix * sizeof(unsigned char));
if(cdata == (unsigned char *)NULL) {
free(fdata);
fprintf(stderr,"ERROR: wsq_decode_mem : malloc : cdata\n");
return(-21);
}

/* Convert floating point pixels to unsigned char pixels. */
conv_img_2_uchar(cdata, fdata, width, height,
frm_header_wsq.m_shift, frm_header_wsq.r_scale);

/* Done with floating point pixels. */
free(fdata);

if(debug > 0)
fprintf(stderr, "Doubleing point pixels converted to unsigned char\n\n");


/* Assign reconstructed pixmap and attributes to output pointers. */
*odata = cdata;
*ow = width;
*oh = height;
*od = 8;
*oppi = ppi;
*lossyflag = 1;

/* Return normally. */
return(0);
}

---------------------------------------------------------------------


And for this i have made one java file wsq.java with native method declaration as below :

wsq.java

package com.axalto.wsq;

publi class Wsq
{

public native int wsq_decode_mem(Imageparam Param ); // decoder.c

public native int wsq_encode_mem(ImageParam param ); //encoder.c

static
{
System.loadLibrary("wsq");
}

}


ImageParam.java
---------------

public class ImageParam
{
private int width =336;
private int height =512;
private int depth; =8;
private int ppi; =500;
private byte outputdata[];
private byte inputdata[];
private int bufferlen;

// may be i have declared wrong visibility .... it may be public...

But now i dont understand that what should i declared in this object class. I need help to make this class. Someone guided me to declare get/set method for this class and. But i m not getting well that how many get and set method i should declare . So could you help me to make this class . Means what are the remaining things i should be declared ??... what are the packages i should import to make this class ???......SO anyone good with java plz help me out to make this class.....

I am new to programming, so accept my apologies if this question is simple.

Plz help me out...
Thanx

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.