Received: from localhost (unknown [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id D4BB32E3A7B for ; Wed, 9 Jan 2008 13:24:01 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-maia, port 10024) with ESMTP id 89076-03-5 for ; Wed, 9 Jan 2008 13:23:51 -0400 (AST) Received: from an-out-0708.google.com (an-out-0708.google.com [209.85.132.240]) by postgresql.org (Postfix) with ESMTP id 0CAD42E3A1E for ; Wed, 9 Jan 2008 13:23:32 -0400 (AST) Received: by an-out-0708.google.com with SMTP id c36so72162ana.33 for ; Wed, 09 Jan 2008 09:23:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to:subject:in-reply-to:mime-version:content-type:references; bh=OEqB+nt0Wras6O8J8OVmGcLn85o3cUm0akZTxdlgHjw=; b=jJip0yu9iITnYoe6sXSvOEJnRIcF4hPbau5bEiXOH7YGH5xY7xB0hKE4yfXjDbd7W3YM5pjO+ALVArMgjGGRkXlJ5IEfC1MxHs54bZCH/wA72CChSyVtPMQmOAHbaRb3v1kKUhlJNZ8f4zLftUjhlgX4hDS3e6Asp27pKvcI470= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:in-reply-to:mime-version:content-type:references; b=K90FVbbhIvQkgNk6oSbawzvcgRhUADlETNtgJRCfAK9MWr4MuiKxcH2JeTRrAJH8GDHBP3C1Bf323vl8NX84yf5WQF3Q87VT9B4lMNCp9BXKAO0SWd/r5DzYiDYKjM0Db+wlNMKtTdmz6NMzzpnCASdRkgAMNabn5b5loMmzWRw= Received: by 10.100.241.17 with SMTP id o17mr2000111anh.32.1199899411557; Wed, 09 Jan 2008 09:23:31 -0800 (PST) Received: by 10.100.132.6 with HTTP; Wed, 9 Jan 2008 09:23:31 -0800 (PST) Message-ID: Date: Wed, 9 Jan 2008 18:23:31 +0100 From: "Maciej Grygorcewicz" To: pgsql-cygwin@postgresql.org Subject: Problem with compiling C function In-Reply-To: MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_4320_6428972.1199899411486" References: X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200801/1 X-Sequence-Number: 4383 ------=_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 #include #include #include #include #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 maciekgrygorcewicz@gmail.com , prometeusz1@o2.pl GG 1638280 -- Pozdrawiam Maciej Grygorcewicz maciekgrygorcewicz@gmail.com , prometeusz1@o2.pl GG 1638280 ------=_Part_4320_6428972.1199899411486 Content-Type: text/html; 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
maciekgrygorcewicz@gmail.com ,
prometeusz1@o2.pl
GG 1638280


--
Pozdrawiam

Maciej Grygorcewicz
maciekgrygorcewicz@gmail.com ,
prometeusz1@o2.pl
GG 1638280 ------=_Part_4320_6428972.1199899411486--