On Thu, Jan 13, 2005 at 09:41:30AM -0500, Raymond Toy wrote:
> 
> First, indent better:

 Okay :-)  I am still undecided whether to use 2 or 8 spaces for the TAB though.


> Since I don't know what extract-image, array2d, and save-image do,
> it's kind of hard to figure out, but I suspect what you want and what
> you do is wrong.

 I have attached the lisp code.  Quite small.  Besides the lisp file, there's
another C file which has the body of extract_image and save_image.  That C file
in turn depends on libjpeg.

 
> Can you send a backtrace?  I suspect the (setf filename ...) is not
> what you want.

 Here's the bt:

; Loading #p"/home/ju/lisp/jpeglib/del.x86f".

Type-error in KERNEL::OBJECT-NOT-TYPE-ERROR-HANDLER:
   #.(SYSTEM:INT-SAP #x48001020) is not of type ALIEN::ALIEN-VALUE

Restarts:
  0: [CONTINUE] Return NIL from load of "del.x86f".
  1: [ABORT   ] Skip remaining initializations.

Debug  (type H for help)

(DEREF 1 #.(SYSTEM:INT-SAP #x48001020))[:EXTERNAL]
Source: Error finding source:
Error in function DEBUG::GET-FILE-TOP-LEVEL-FORM:  Source file no longer exists:

  target:code/alieneval.lisp.
0] backtrace

0: (DEREF 1 #.(SYSTEM:INT-SAP #x48001020))[:EXTERNAL]
1: (C::DO-CALL #<Code Object "Top-Level Form" {4801D447}> 79 80 4 ...)
2: (COMMON-LISP::FOP-FUNCALL-FOR-EFFECT)
3: (COMMON-LISP::LOAD-GROUP #<Stream for file "/home/ju/lisp/jpeglib/del.x86f">)
4: (COMMON-LISP::FASLOAD #<Stream for file "/home/ju/lisp/jpeglib/del.x86f">)
5: (COMMON-LISP::INTERNAL-LOAD #p"del.x86f"
                               #p"/home/ju/lisp/jpeglib/del.x86f"
                               :ERROR
                               :BINARY)
6: (COMMON-LISP::INTERNAL-LOAD #p"del.x86f"
                               #p"/home/ju/lisp/jpeglib/del.x86f"
                               :ERROR
                               NIL)
7: (LOAD "del.x86f" :VERBOSE NIL :PRINT ...)
8: (EXTENSIONS::INVOKE-SWITCH-DEMONS
    (#<Command Line Switch "eval" -- ("( sys::load-object-file \"libjpeg.so\" )"
)>
     #<Command Line Switch "eval" -- ("( sys::load-object-file \"./lib.so\" )")>
     #<Command Line Switch "load" -- ("del.x86f")>)
    (("quiet" . #) ("load" . #) ("eval" . #)))
9: (COMMON-LISP::%RESTART-LISP)
10: (COMMON-LISP::RESTART-LISP)

0]






-- Attached file included as plaintext by Listar --


( use-package "C-CALL" )
( use-package "ALIEN" )


;;; ( sys::load-object-file "libjpeg.so" )
;;; ( sys::load-object-file "./lib.so" )



;;; references a 2 dimensional C array.
( defmacro array2d ( name fd sd ) `( deref ( deref ,name ,fd ) ,sd ))


( def-alien-routine "extract_image" int
                                ( name c-string :in )
                                ( width ( * int ) :in )
                                ( height ( * int ) :in )
                                ( data ( * ( * ( * unsigned-char ))) :in ))


( def-alien-routine "save_image" int
                                ( name c-string :in )
                                ( width int :in )
                                ( height int :in )
                                ( data ( * ( * unsigned-char )) :in ))


( with-alien (( filename c-string )
              ( width int )
              ( height int )
              ( image_array ( * ( * unsigned-char ))))

        ( setf filename ( car *command-line-words* ))

        ( extract-image filename
                        ( addr width )
                        ( addr height )
                        ( addr image_array ))



;; If the pixel value is < 155, make it 0 i.e. black.
;;      ( dotimes ( h height )
;;              ( dotimes ( w width )
;;                      ( if    ( < 
;;                                ( array2d image_array h w )
;;                                155 )
;;                              ( setf ( array2d image_array h w ) 0 )
;;                              T )))


        ( setf filename "del.jpg" )

        ( save-image filename width height image_array ))


( quit )





-- Attached file included as plaintext by Listar --


#include <stdio.h>
#include <stdlib.h>
#include <jpeglib.h>
#include <jerror.h>



int
extract_image( char* name, int* width, int* height, unsigned char*** data ){

        struct jpeg_decompress_struct jsp ;
        struct jpeg_error_mgr bhulu ;
        JSAMPROW ar[1] ; 
        int i, j, line_size ;

        FILE* ss = fopen( name, "rb" ) ;
        if( !ss )
                return 1 ;

        jsp.err = jpeg_std_error( &bhulu ) ;
        jpeg_create_decompress( &jsp ) ;
        jpeg_stdio_src( &jsp, ss ) ;

        jpeg_read_header( &jsp, TRUE ) ;

        jsp.out_color_space = JCS_GRAYSCALE ;

        jpeg_start_decompress( &jsp ) ;

        *width = jsp.output_width ;
        *height = jsp.output_height ;
        line_size = jsp.output_width * jsp.output_components * sizeof(JSAMPLE) ;


        ar[0] = malloc( sizeof( JSAMPLE ) * line_size ) ;
        *data = malloc( sizeof( unsigned char* ) * jsp.output_height ) ;

        i = 0 ;
        while( jsp.output_scanline < jsp.output_height ){

                jpeg_read_scanlines( &jsp, ar, 1 ) ;

                ( *data )[i] = malloc( line_size ) ;
                memcpy( ( *data )[i], ar[0], line_size ) ;

                i++ ;
        }

        jpeg_finish_decompress( &jsp ) ;
        jpeg_destroy_decompress( &jsp ) ;
        fclose( ss ) ;

        return 0 ;
}



int
save_image( char* name, int width, int height, unsigned char** data ){

        int i ;
        struct jpeg_compress_struct jsp ;
        struct jpeg_error_mgr err ;
        JSAMPROW ar[1] ;

        FILE* ss = fopen( name, "wb" ) ;
        if( !ss )
                return 1 ;

        jsp.err = jpeg_std_error( &err ) ;
        jpeg_create_compress( &jsp ) ;

        jpeg_stdio_dest( &jsp, ss ) ;


        jsp.image_width = width ;
        jsp.image_height = height ;
        jsp.input_components = 1 ;
        jsp.in_color_space = JCS_GRAYSCALE ;

        jpeg_set_defaults( &jsp ) ;

        jpeg_start_compress( &jsp, TRUE ) ;


        i = 0 ;
        while( jsp.next_scanline < jsp.image_height ){
                ar[0] = data[i] ;

                jpeg_write_scanlines( &jsp, ar, 1 ) ;

                i++ ;
        }

        jpeg_finish_compress( &jsp ) ;
        jpeg_destroy_compress( &jsp ) ;
        fclose( ss ) ;

        return 0 ;
}



int
main( int argc, char* argv[] ){

        int i, j, width, height ;
        unsigned char** data ;

        if( argc != 3 ){
                puts( "2 args required." ) ;
                return 1 ;
        }


        if( extract_image( argv[1], &width, &height, &data ) ){
                puts( "Error reading image." ) ;
                return 1 ;
        }

        for( i=0; i<height; i++ )
                for( j=0; j<width; j++ )
                        if( data[i][j] > 150 )
                                data[i][j] = 255 ;

        if( save_image( argv[2], width, height, data ) ){
                puts( "Error writing image." ) ;
                return 1 ;
        }

/*
        for( i=0; i<height; i++ ){
                for( j=0; j<width; j++ )
                        if( data[i][j] != 255 )
                                printf( "x" ) ;
                        else
                                printf( "." ) ;

                puts( "" ) ;
        }


        printf( "W: %d \n", width ) ;
        printf( "H: %d \n", height ) ;
*/
        return 0 ;
}






Reply via email to