Página principal | Jerarquía de la clase | Lista alfabética | Lista de componentes | Lista de archivos | Miembros de las clases | Archivos de los miembros | Páginas relacionadas

_jedimg2.c

Ir a la documentación de este archivo.
00001 #include "jed_plt.h"
00002 
00003 #include <stdio.h> 
00004 
00005 #define BYTE unsigned char   
00006 
00007 #if (PLATAFORMA != SPARC64_LINUX_GCC) && (PLATAFORMA != i386_LINUX_GCC) && (PLATAFORMA != SUN) && (PLATAFORMA != CYGNUS_WIN32) && (PLATAFORMA != POWERPC_AIX_VACPP)
00008 
00009 #define IMPERR()                                                            \
00010     fprintf(stderr,                                                         \
00011 "AQUYNZA: Error fatal - JPEGlib No disponible\n"                            \
00012 "AQUYNZA ha sido compilado con la opcion JPEG_ENABLED desactivada.  "       \
00013 "Edite el\n"                                                                \
00014 "archivo jed_defs.h, defina la constante JPEG_ENABLED y recompile el \n"    \
00015 "proyecto.  Es posible que esto se deba a que la libreria \"jpeglib\" no\n" \
00016 "esta disponible en su sistema.  Consulte al administrador del sistema y\n" \
00017 "solicite la instalacion de dicha libreria en caso de ser necesario."       \
00018     );
00019 
00020 void
00021 IMAGEN_exportar_jpeg(char *filename, int calidad_jpeg,
00022                      int x_tam, int y_tam)
00023 {
00024     IMPERR();
00025     fprintf(stderr,"Intente exportar a \"%s\" con la calidad %d,\n"
00026     "una imagen de <%d x %d> pixels.", filename, calidad_jpeg, x_tam, y_tam);
00027     fflush(stderr);
00028 }
00029 
00030 BYTE *
00031 IMAGEN_importar_jpeg(FILE *fd, int *x_tam, int *y_tam,
00032                      int *components, char *error_cad)
00033 {
00034     IMPERR();
00035 
00036     /*if ( fd ) fclose(fd);*/
00037 
00038     (*x_tam) = 0;
00039     (*y_tam) = 0;
00040     (*components) = 0;
00041     error_cad[0] = '\0';
00042     return NULL;
00043 }
00044 #endif
00045 
00046 #if (PLATAFORMA == i386_LINUX_GCC) || (PLATAFORMA == SUN) || (PLATAFORMA == CYGNUS_WIN32) || (PLATAFORMA == SPARC64_LINUX_GCC) || (PLATAFORMA == POWERPC_AIX_VACPP)
00047 
00048 /*******************************************************************/
00049 /* JPEG DECOMPRESSION CODE EXTRACTED FROM JPEG LIBRARY SAMPLE CODE */
00050 /*******************************************************************/
00051 
00052 #include <stdio.h>
00053 #include <stdlib.h>
00054 #include <string.h>
00055 #include <math.h>
00056 #include <malloc.h>
00057 #include <time.h>
00058 #include <setjmp.h>
00059 
00060 #include "jpeglib.h"
00061 
00062 /*= DEFINICIONES LOCALES ====================================================*/
00063 
00070 struct _MANEJADOR_DE_ERRORES_JPEG_ {
00071   struct jpeg_error_mgr pub;    /* "public" fields */
00072 
00073   jmp_buf setjmp_buffer;        /* for return to caller */
00074 };
00075 
00076 typedef struct _MANEJADOR_DE_ERRORES_JPEG_ * _MANEJADOR_DE_ERRORES_JPEG_PTR;
00077 
00078 /*= FUNCIONES UTILITARIAS (PRIVADAS) ========================================*/
00079 
00080 void
00081 my_error_exit(j_common_ptr cinfo)
00082 /*
00083  * Here's the routine that will replace the standard error_exit method:
00084  */
00085 {
00086     /* cinfo->err really points to a _MANEJADOR_DE_ERRORES_JPEG_ struct, so coerce pointer */
00087     _MANEJADOR_DE_ERRORES_JPEG_PTR myerr = 
00088         (_MANEJADOR_DE_ERRORES_JPEG_PTR) cinfo->err;
00089 
00090     /* Always display the message. */
00091     /* We could postpone this until after returning, if we chose. */
00092     (*cinfo->err->output_message)(cinfo);
00093 
00094     /* Return control to the setjmp point */
00095     longjmp(myerr->setjmp_buffer, 1);
00096 }
00097 
00098 /*= SERVICIOS PUBLICOS ======================================================*/
00099 
00100 BYTE *
00101 IMAGEN_importar_jpeg(FILE *fd, int *x_tam, int *y_tam, int *components,
00102                      char *error_cad)
00106 {
00107     struct jpeg_decompress_struct encabezado;
00108     /* We use our private extension JPEG error handler.
00109      * Note that this struct must live as long as the main JPEG parameter
00110      * struct, to avoid dangling-pointer problems.                       */
00111     struct _MANEJADOR_DE_ERRORES_JPEG_ jerr;
00112     /* More stuff */
00113     JSAMPARRAY buffer;          /* Output row buffer */
00114     int row_stride;             /* physical row width in output buffer */
00115     long i;
00116     JSAMPLE *image_buffer;    
00117 
00118     error_cad[0] = '\0';
00119 
00120     /*- Step 1: allocate and initialize JPEG decompression object -----------*/
00121     /* Inicializa la libreria JPEG */
00122     encabezado.err = jpeg_std_error(&jerr.pub);
00123     jerr.pub.error_exit = my_error_exit;
00124     if ( setjmp(jerr.setjmp_buffer) ) {
00125         jpeg_destroy_decompress(&encabezado);
00126         fprintf(stderr, "<JED_IMG> - ERROR: abriendo un JPEG.\n");
00127         fflush(stderr);
00128         return NULL;
00129     }
00130     jpeg_create_decompress(&encabezado);   
00131     jpeg_stdio_src(&encabezado, fd);     /* Indica de donde leer los datos  */
00132     jpeg_read_header(&encabezado, TRUE); /* Carga el encabezado             */
00133     jpeg_start_decompress(&encabezado);  /* Inicia la lectura de pixels     */
00134 
00135     /*Make a one-row-high sample array that will go away when done with image*/
00136     row_stride = encabezado.output_width * encabezado.output_components;
00137     buffer = (*encabezado.mem->alloc_sarray)
00138                 ((j_common_ptr)&encabezado, JPOOL_IMAGE, row_stride, 1);
00139 
00140     /*- Step 6: while (scan lines remain to be read) ------------------------*/
00141     /*- jpeg_read_scanlines(...);                                           -*/
00142 
00143     image_buffer = (JSAMPLE *)malloc(encabezado.image_width * 
00144                                      encabezado.image_height * 
00145                                      encabezado.output_components);
00146     if ( !image_buffer ) {
00147         fprintf(stderr, "<JED_IMG> - ERROR: "
00148             "No hay memoria para una imagen de %d X %d pixels.\n",
00149             encabezado.image_width, encabezado.image_height);
00150         fflush(stderr);
00151         exit(-1);
00152     }
00153     
00154     
00155     for ( i = encabezado.output_height - 1;
00156           encabezado.output_scanline < encabezado.output_height; i-- ) {
00157         jpeg_read_scanlines(&encabezado, buffer, 1);
00158         memcpy(image_buffer +
00159           encabezado.image_width*encabezado.output_components*i,
00160           buffer[0], row_stride);
00161     }
00162 
00163     /*- Step 7: Finish decompression ---------------------------------------*/
00164     jpeg_finish_decompress(&encabezado);
00165     jpeg_destroy_decompress(&encabezado);
00166 
00167     /* At this point you may want to check to see whether any corrupt-data
00168      * warnings occurred (test whether jerr.pub.num_warnings is nonzero). */
00169 
00170     if ( jerr.pub.num_warnings != 0 ) {
00171         sprintf(error_cad, "[Warnings JPEG = %d].", jerr.pub.num_warnings);
00172     }
00173 
00174     /*- Finalmente, deje los resultados listos ------------------------------*/
00175     (*x_tam) = encabezado.image_width;
00176     (*y_tam) = encabezado.image_height;
00177     (*components) = encabezado.output_components;
00178     return image_buffer;
00179 }
00180 
00181 void
00182 IMAGEN_exportar_jpeg(FILE *fd, int calidad_jpeg, int x_tam, int y_tam,
00183                      BYTE *Data)
00191 {
00192     static int i;
00193     static struct jpeg_compress_struct cinfo;
00194     static struct jpeg_error_mgr       jerr;
00195     static JSAMPROW row_pointer[1];     /* pointer to JSAMPLE row[s]  */
00196                         
00197     /*- Abra el archivito -------------------------------------------------*/
00198     if ( !fd ) {
00199         return;
00200     }
00201 
00202     /*- Exporte la imagen -------------------------------------------------*/
00203     cinfo.err = jpeg_std_error(&jerr);
00204     jpeg_create_compress(&cinfo);
00205     jpeg_stdio_dest(&cinfo, fd);
00206 
00207     cinfo.image_width = x_tam;        /* image width and height, in pixels */
00208     cinfo.image_height = y_tam;
00209     cinfo.input_components = 3;       /* # of color components per pixel   */
00210     cinfo.in_color_space = JCS_RGB;   /* colorspace of input image         */
00211     jpeg_set_defaults(&cinfo);
00212     jpeg_set_quality(&cinfo, calidad_jpeg, TRUE);
00213     jpeg_start_compress(&cinfo, TRUE);
00214     
00215     for ( i = cinfo.image_height; i > 0; i-- ) {
00216         /* jpeg_write_scanlines expects an array of pointers to scanlines.
00217          * Here the array is only one element long, but you could pass
00218          * more than one scanline at a time if that's more convenient. */
00219         row_pointer[0] = & Data[i * x_tam * 3];
00220         jpeg_write_scanlines(&cinfo, row_pointer, 1);
00221     }
00222     jpeg_finish_compress(&cinfo);
00223     jpeg_destroy_compress(&cinfo);
00224 }
00225 
00226 /*= EOF =====================================================================*/
00227 #endif
00228 

Este archivo HTML ha sido generado automáticamente a partir del código fuente AQUYNZA. NO LO EDITE. Para mayor información contacte al autor.