Problem with compiling C function
"Maciej Grygorcewicz" <[email protected]> Wed, 9 Jan 2008 18:23:31 +0100
| Newsgroups | gmane.comp.db.postgresql.cygwin |
|---|---|
| Message-ID | <[email protected]> |
------=_Part_4320_6428972.1199899411486
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
Hello
I ma working on a server-side function written in "C" which should get a
bytea type image - simple jpg picture , resize and compress it with defined
paramters and return also a bytea type. I made working test code that worked
fine as a console program.
My boss demands it to be compiled in Windows (postgreSQL server is running
on Windows 2003) , so I created this project in Borland C++ Builder 6.0.
I am not using Cygwin.
PostgreSQL C external function has to be built into dll , so I created new
project DLL Wizard - C style dll.
Later I changed some code , and pasted it to the DLL "C" style , and wanted
to build dll.
My Code:
-----------------------------------------------------------------------------
#include "postgres.h "
#include <windows.h>
#include <io.h>
#include <fcntl.h>
#include <stat.h>
#include <setjmp>
#include "jerror.h"
#include "jpeglib.h"
#pragma argsused
extern __declspec(dllexport) bytea* kompresuj(bytea* obrazek,int zdjwyj_sz,
int zdjwyj_w,int stkompr);
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void*
lpReserved)
{
return 1;
}
struct my_error_mgr {
struct jpeg_error_mgr pub;
jmp_buf setjmp_buffer;
};
typedef struct my_error_mgr *my_error_ptr;
void my_error_exit(j_common_ptr cinfo)
{
my_error_ptr myerr = (my_error_ptr) cinfo->err;
(*cinfo->err->output_message) (cinfo);
longjmp(myerr->setjmp_buffer, 1);
}
int czytajwymiarywej(char *filename,int wymiary[])
{ struct jpeg_decompress_struct cinfo;
struct my_error_mgr jerr;
FILE *infile;
if ((infile = open(filename, O_RDONLY|O_BINARY,S_IREAD)) == NULL) {
return -1;
}
jerr.pub.error_exit = my_error_exit;
cinfo.err = jpeg_std_error(&jerr.pub);
jpeg_CreateDecompress(&cinfo,JPEG_LIB_VERSION,(size_t)sizeof(cinfo));
if (setjmp(jerr.setjmp_buffer)) {
jpeg_destroy_decompress(&cinfo);
fclose(infile);
return -2;
}
jpeg_stdio_src(&cinfo, infile);
(void) jpeg_read_header(&cinfo, TRUE);
wymiary[0] = cinfo.image_width;
wymiary[1] = cinfo.image_height;
jpeg_destroy_decompress(&cinfo);
return 0;
}
int read_jpeg(j_decompress_ptr cinfoptr, unsigned char **image_buffer,int
*image_width, int *image_height)
{ JSAMPARRAY jpeg_buffer;
long int i_buffer, i_image;
(void) jpeg_start_decompress(cinfoptr);
*image_buffer = (unsigned char *) malloc(*image_width *
*image_height * (*cinfoptr).out_color_components* sizeof(unsigned char));
if (*image_buffer == NULL) {
(void) jpeg_finish_decompress(cinfoptr);
jpeg_destroy_decompress(cinfoptr);
return -3;
}
jpeg_buffer = (*(*cinfoptr).mem->alloc_sarray)((j_common_ptr)
cinfoptr, JPOOL_IMAGE,(*cinfoptr).output_width *
(*cinfoptr).output_components, 1);
i_image = 0;
while ((*cinfoptr).output_scanline < (*cinfoptr).output_height) {
(void) jpeg_read_scanlines(cinfoptr, jpeg_buffer, 1);
if ((*cinfoptr).output_components == 3) {
for (i_buffer = 0; (unsigned)i_buffer <
(*cinfoptr).output_width * (*cinfoptr).output_components; i_buffer++,
i_image++)
(*image_buffer)[i_image] = jpeg_buffer[0][i_buffer];
}
}
(void) jpeg_finish_decompress(cinfoptr);
jpeg_destroy_decompress(cinfoptr);
return 0;
}
int write_jpeg(j_compress_ptr cinfoptr, unsigned char *image_buffer,int
quality, int image_width, int image_height)
{ JSAMPROW row_pointer[1];
if (quality < 0) quality = 0; else if(quality > 100) quality = 100;
jpeg_set_quality(cinfoptr, quality, FALSE);
jpeg_start_compress(cinfoptr, TRUE);
while ((*cinfoptr).next_scanline < (*cinfoptr).image_height) {
row_pointer[0] = &image_buffer[(*cinfoptr).next_scanline*
image_width * (*cinfoptr).input_components];
(void) jpeg_write_scanlines(cinfoptr, row_pointer, 1);
}
jpeg_finish_compress(cinfoptr);
jpeg_destroy_compress(cinfoptr);
return 0;
}
bytea* kompresuj(bytea* obrazek,int zdjwyj_sz, int zdjwyj_w,int stkompr)
{
int rc;unsigned char *zdjwej,*zdjwyj;
int zdjwej_w, zdjwej_sz;
int jakosc,i, j, k, ib, jb; double ratio;
int rozmiar;
bytea* zdjwyjwyn;
/* Dekompresja - ustawianie obiektu cinfo */
struct jpeg_decompress_struct cinfo;
struct my_error_mgr jerr;
/* Kompresja dane do kompresji */
struct jpeg_compress_struct cinfokompr;
struct my_error_mgr jerr1;
/* ------------------------*/
//typedef void* declspec(dllimport) funkcjaTYP (MemoryContext,Size);
//HINSTANCE uchwytDLL = LoadLibrary("libecpg.dll");
//funkcjaTYP *funkcja = (funkcjaTYP*) GetProcAddress(uchwytDLL,
"_MemoryContextAlloc");
jerr.pub.error_exit = my_error_exit;
cinfo.err = jpeg_std_error(&jerr.pub);
jpeg_CreateDecompress(&cinfo,JPEG_LIB_VERSION,(size_t)sizeof(cinfo));
if (setjmp(jerr.setjmp_buffer)) {
jpeg_destroy_decompress(&cinfo);
};
jpeg_stdio_src(&cinfo, (FILE*)obrazek->vl_dat);
(void) jpeg_read_header(&cinfo, TRUE);
cinfo.dct_method = JDCT_FLOAT;
cinfo.scale_denom = 1;
cinfo.do_block_smoothing = true;
cinfo.out_color_space = JCS_RGB;
cinfo.quantize_colors = 0;
//info.desired_number_of_colors = wymilosckolbmp;
cinfo.two_pass_quantize = true;
cinfo.dither_mode = JDITHER_ORDERED;
if (zdjwej_sz > zdjwej_w)
{
ratio = zdjwyj_sz / (double)zdjwej_sz;
zdjwyj_w = (int)((double)zdjwej_w * ratio);
}
else
{
ratio = zdjwyj_w / (double)zdjwej_w;
zdjwyj_sz = (int)((double)zdjwej_sz * ratio);
}
rc = read_jpeg(&cinfo, &zdjwej ,&zdjwej_sz, &zdjwej_w);
if (rc != 0) {jpeg_destroy_decompress(&cinfo);}
else
{
zdjwyj = (unsigned char*)malloc(zdjwyj_sz * zdjwyj_w *
cinfo.output_components * sizeof(unsigned char));
for (j = 0; j < zdjwyj_w; j++)
{
jb = (int)((double)j / ratio);
for (i = 0; i < zdjwyj_sz; i++)
{
ib = (int)((double)i / ratio);
for (k = 0; k < 3; k++) zdjwyj[(j *
zdjwyj_sz + i) * 3 + k]= zdjwej[(jb * zdjwej_sz + ib) * 3 + k];
}
}
free(zdjwej);
jerr1.pub.error_exit = my_error_exit;
cinfokompr.err = jpeg_std_error(&jerr1.pub);
jpeg_create_compress(&cinfokompr);
//zdjwyjwyn = (bytea*) palloc(sizeof(bytea));
jpeg_stdio_dest(&cinfokompr, (FILE*)zdjwyjwyn->vl_dat);
cinfokompr.image_width = zdjwyj_sz;
cinfokompr.image_height = zdjwyj_w;
cinfokompr.in_color_space = JCS_RGB;
jpeg_set_defaults(&cinfokompr);
cinfokompr.dct_method = JDCT_FLOAT;
jpeg_set_linear_quality(&cinfokompr, 80,0);
cinfokompr.smoothing_factor = 20;
rc = write_jpeg(&cinfokompr,zdjwyj,stkompr,zdjwyj_sz,
zdjwyj_w);
free(zdjwyj);
if (rc!= 0) return NULL ;
return zdjwyjwyn;
}
return NULL;
}
--------------------------------------
But linker threw such errors:
"
Unresolved External: _pgwin32_fopen referended from .... // and so on
Unresolved External: _pg_sprintfl referended from .... // and so on "
Why???? I have included postgres.h , and all other required files .
I tried to build almost the same code but in C++ style DLL.
DLL file was created succesfully , but on the other hand postgreSQL couldn't
find exported function...
DLL was in non-undernstadable format for postgresql.
Is it fault of windows??? Have I to use Cygwin , and cygwin include and
library files to successfully build this function??
Ahh , function has to be called by a Trigger ...
Can you advice me some solution???
Maciej Grygorcewicz
[email protected] ,
[email protected]
GG 1638280
--
Pozdrawiam
Maciej Grygorcewicz
[email protected] ,
[email protected]
GG 1638280
------=_Part_4320_6428972.1199899411486
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
<span class="gmail_quote"><br></span>Hello <br><br>I ma working on a server-side function written in "C" which should get a bytea type image - simple jpg picture , resize and compress it with defined paramters and return also a bytea type. I made working test code that worked fine as a console program.
<br>My boss demands it to be compiled in Windows (postgreSQL server is running on Windows 2003) , so I created this project in Borland C++ Builder 6.0. <br>I am not using Cygwin. <br>PostgreSQL C external function has to be built into dll , so I created new project DLL Wizard - C style dll.
<br>Later I changed some code , and pasted it to the DLL "C" style , and wanted to build dll.<br>My Code:<br>-----------------------------------------------------------------------------<br>#include "postgres.h
"<br>#include <windows.h><br>#include <io.h><br>#include <fcntl.h><br>#include <stat.h><br>#include <setjmp><br>#include "jerror.h"<br>#include "jpeglib.h"<br> <br>
#pragma argsused
<br>extern __declspec(dllexport) bytea* kompresuj(bytea* obrazek,int zdjwyj_sz, int zdjwyj_w,int stkompr);<br><br>int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)<br>{<br> return 1;
<br>}<br><br>struct my_error_mgr {<br>struct jpeg_error_mgr pub;<br>jmp_buf setjmp_buffer;<br>};<br>typedef struct my_error_mgr *my_error_ptr;<br>void my_error_exit(j_common_ptr cinfo)<br>{<br>my_error_ptr myerr = (my_error_ptr) cinfo->err;
<br>(*cinfo->err->output_message) (cinfo);<br>longjmp(myerr->setjmp_buffer, 1);<br>}<br><br>int czytajwymiarywej(char *filename,int wymiary[])<br>{ struct jpeg_decompress_struct cinfo;<br> struct my_error_mgr jerr;
<br> FILE *infile;<br><br> if ((infile = open(filename, O_RDONLY|O_BINARY,S_IREAD)) == NULL) {<br> return -1;<br> }<br><br> jerr.pub.error_exit = my_error_exit;<br> cinfo.err = jpeg_std_error(&jerr.pub);
<br> jpeg_CreateDecompress(&cinfo,JPEG_LIB_VERSION,(size_t)sizeof(cinfo));<br> if (setjmp(jerr.setjmp_buffer)) {<br> jpeg_destroy_decompress(&cinfo);<br> fclose(infile);<br> return -2;
<br> }<br><br> jpeg_stdio_src(&cinfo, infile);<br> (void) jpeg_read_header(&cinfo, TRUE);<br> wymiary[0] = cinfo.image_width;<br> wymiary[1] = cinfo.image_height;<br> jpeg_destroy_decompress(&cinfo);
<br> return 0;<br>}<br>int read_jpeg(j_decompress_ptr cinfoptr, unsigned char **image_buffer,int *image_width, int *image_height)<br>{ JSAMPARRAY jpeg_buffer;<br> long int i_buffer, i_image;<br> (void) jpeg_start_decompress(cinfoptr);
<br> *image_buffer = (unsigned char *) malloc(*image_width * *image_height * (*cinfoptr).out_color_components* sizeof(unsigned char));<br> if (*image_buffer == NULL) {<br> (void) jpeg_finish_decompress(cinfoptr);
<br> jpeg_destroy_decompress(cinfoptr);<br> return -3;<br> }<br> jpeg_buffer = (*(*cinfoptr).mem->alloc_sarray)((j_common_ptr) cinfoptr, JPOOL_IMAGE,(*cinfoptr).output_width * (*cinfoptr).output_components, 1);
<br> i_image = 0;<br> while ((*cinfoptr).output_scanline < (*cinfoptr).output_height) {<br> (void) jpeg_read_scanlines(cinfoptr, jpeg_buffer, 1);<br> if ((*cinfoptr).output_components == 3) {
<br> for (i_buffer = 0; (unsigned)i_buffer < (*cinfoptr).output_width * (*cinfoptr).output_components; i_buffer++, i_image++)<br> (*image_buffer)[i_image] = jpeg_buffer[0][i_buffer];
<br> }<br> }<br> (void) jpeg_finish_decompress(cinfoptr);<br> jpeg_destroy_decompress(cinfoptr);<br> return 0;<br>}<br>int write_jpeg(j_compress_ptr cinfoptr, unsigned char *image_buffer,int quality, int image_width, int image_height)
<br>{ JSAMPROW row_pointer[1];<br><br>if (quality < 0) quality = 0; else if(quality > 100) quality = 100;<br>jpeg_set_quality(cinfoptr, quality, FALSE);<br>jpeg_start_compress(cinfoptr, TRUE);<br><br>while ((*cinfoptr).next_scanline < (*cinfoptr).image_height) {
<br> row_pointer[0] = &image_buffer[(*cinfoptr).next_scanline* image_width * (*cinfoptr).input_components];<br> (void) jpeg_write_scanlines(cinfoptr, row_pointer, 1);<br> }<br>jpeg_finish_compress(cinfoptr);
<br>jpeg_destroy_compress(cinfoptr);<br>return 0;<br>}<br><br>bytea* kompresuj(bytea* obrazek,int zdjwyj_sz, int zdjwyj_w,int stkompr)<br>{ <br> int rc;unsigned char *zdjwej,*zdjwyj;<br> int zdjwej_w, zdjwej_sz;<br>
int jakosc,i, j, k, ib, jb; double ratio;<br> int rozmiar;<br> bytea* zdjwyjwyn;<br> /* Dekompresja - ustawianie obiektu cinfo */<br> struct jpeg_decompress_struct cinfo;<br> struct my_error_mgr jerr;
<br> /* Kompresja dane do kompresji */<br> struct jpeg_compress_struct cinfokompr;<br> struct my_error_mgr jerr1;<br> /* ------------------------*/<br> //typedef void* declspec(dllimport) funkcjaTYP (MemoryContext,Size);
<br> //HINSTANCE uchwytDLL = LoadLibrary("libecpg.dll");<br> //funkcjaTYP *funkcja = (funkcjaTYP*) GetProcAddress(uchwytDLL, "_MemoryContextAlloc");<br> jerr.pub.error_exit = my_error_exit;
<br> cinfo.err = jpeg_std_error(&jerr.pub);<br> jpeg_CreateDecompress(&cinfo,JPEG_LIB_VERSION,(size_t)sizeof(cinfo));<br> if (setjmp(jerr.setjmp_buffer)) {<br> jpeg_destroy_decompress(&cinfo);
<br> };<br> jpeg_stdio_src(&cinfo, (FILE*)obrazek->vl_dat);<br> (void) jpeg_read_header(&cinfo, TRUE);<br><br> cinfo.dct_method = JDCT_FLOAT;<br> cinfo.scale_denom
= 1;<br> cinfo.do_block_smoothing = true;<br> cinfo.out_color_space = JCS_RGB;<br> cinfo.quantize_colors = 0;<br> //info.desired_number_of_colors = wymilosckolbmp;
<br> cinfo.two_pass_quantize = true;<br> cinfo.dither_mode = JDITHER_ORDERED;<br> if (zdjwej_sz > zdjwej_w)<br> {<br> ratio = zdjwyj_sz / (double)zdjwej_sz;
<br> zdjwyj_w = (int)((double)zdjwej_w * ratio);<br> }<br> else<br> {<br> ratio = zdjwyj_w / (double)zdjwej_w;<br> zdjwyj_sz = (int)((double)zdjwej_sz * ratio);
<br> }<br> rc = read_jpeg(&cinfo, &zdjwej ,&zdjwej_sz, &zdjwej_w);<br> if (rc != 0) {jpeg_destroy_decompress(&cinfo);}<br> else<br> {
<br> zdjwyj = (unsigned char*)malloc(zdjwyj_sz * zdjwyj_w *cinfo.output_components * sizeof(unsigned char));<br> for (j = 0; j < zdjwyj_w; j++)<br> {<br> jb = (int)((double)j / ratio);
<br> for (i = 0; i < zdjwyj_sz; i++)<br> {<br> ib = (int)((double)i / ratio);<br> for (k = 0; k < 3; k++) zdjwyj[(j * zdjwyj_sz + i) * 3 + k]= zdjwej[(jb * zdjwej_sz + ib) * 3
+ k];
<br> }<br> }<br> free(zdjwej);<br> jerr1.pub.error_exit = my_error_exit;<br> cinfokompr.err = jpeg_std_error(&jerr1.pub);<br> jpeg_create_compress(&cinfokompr);
<br> //zdjwyjwyn = (bytea*) palloc(sizeof(bytea));<br> jpeg_stdio_dest(&cinfokompr, (FILE*)zdjwyjwyn->vl_dat);<br><br> cinfokompr.image_width = zdjwyj_sz;<br>
cinfokompr.image_height = zdjwyj_w;<br> cinfokompr.in_color_space = JCS_RGB;<br> jpeg_set_defaults(&cinfokompr);<br> cinfokompr.dct_method = JDCT_FLOAT;
<br> jpeg_set_linear_quality(&cinfokompr, 80,0);<br> cinfokompr.smoothing_factor = 20;<br> <br> rc = write_jpeg(&cinfokompr,zdjwyj,stkompr,zdjwyj_sz, zdjwyj_w);
<br> free(zdjwyj);<br> if (rc!= 0) return NULL ;<br> return zdjwyjwyn;<br> }<br> return NULL;<br>}<br>--------------------------------------<br><br>
<br>But linker threw such errors:<br>"<br>
Unresolved External: _pgwin32_fopen referended from .... // and so on<br>
Unresolved External: _pg_sprintfl referended from .... // and so on "<br><br>Why???? I have included postgres.h , and all other required files .<br>I tried to build almost the same code but in C++ style DLL. <br>DLL file was created succesfully , but on the other hand postgreSQL couldn't find exported function...
<br>DLL was in non-undernstadable format for postgresql.<br><br>Is it fault of windows??? Have I to use Cygwin , and cygwin include and library files to successfully build this function??<br>Ahh , function has to be called by a Trigger ...
<br><br>Can you advice me some solution???<br><span class="sg"><br clear="all"><br><br><br>Maciej Grygorcewicz<br><a href="mailto:[email protected]" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">
[email protected]</a> ,<br><a href="mailto:[email protected]" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">[email protected]
</a><br>GG 1638280
</span><br clear="all"><br>-- <br>Pozdrawiam<br><br>Maciej Grygorcewicz<br><a href="mailto:[email protected]">[email protected]</a> ,<br><a href="mailto:[email protected]">[email protected]</a><br>GG 1638280
------=_Part_4320_6428972.1199899411486--