IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 28949


Ignore:
Timestamp:
Aug 17, 2010, 6:39:38 PM (16 years ago)
Author:
eugene
Message:

add ability to create bDraw overlays

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ipp-20100621/psLib/src/jpeg/psImageJpeg.c

    r27145 r28949  
    77#include <string.h>
    88
     9#include <kapa.h>
    910#include "psMemory.h"
    1011#include "psImage.h"
     
    135136}
    136137
     138// XXX Add colormap bar with scale (min -> max)
     139// XXX Add option to plot the source overlay (pass in bDrawBuffer populated with points?)
     140// XXX need to update bDraw APIs to pass in/out structure and avoid the local static
    137141bool psImageJpeg(const psImageJpegColormap *map, const psImage *image, const char *filename,
     142                 float min, float max)
     143{
     144    PS_ASSERT_PTR_NON_NULL(map, false);
     145    PS_ASSERT_IMAGE_NON_NULL(image, false);
     146    PS_ASSERT_IMAGE_TYPE(image, PS_TYPE_F32, false);
     147    PS_ASSERT_VECTOR_NON_NULL(map->red, false);
     148    PS_ASSERT_VECTOR_NON_NULL(map->green, false);
     149    PS_ASSERT_VECTOR_NON_NULL(map->blue, false);
     150    PS_ASSERT_PTR_NON_NULL(filename, false);
     151    PS_ASSERT_INT_POSITIVE(strlen(filename), false);
     152    PS_ASSERT_FLOAT_REAL(min, false);
     153    PS_ASSERT_FLOAT_REAL(max, false);
     154
     155    float zero, scale;
     156    struct jpeg_compress_struct cinfo;
     157    struct jpeg_error_mgr jerr;
     158
     159    long pixel;
     160    JSAMPLE *jpegLine;   // Points to data for current line
     161    JSAMPROW jpegLineList[1];  // pointer to JSAMPLE row[s]
     162    JSAMPLE *jpegImage;
     163    JSAMPLE *outPix;
     164
     165    /* JPEG init calls */
     166    cinfo.err = jpeg_std_error (&jerr);
     167    jpeg_create_compress (&cinfo);
     168
     169    /* open file, prep for jpeg */
     170    FILE *f = fopen(filename, "w");
     171    if (!f) {
     172        psError(PS_ERR_IO, true, "failed to open %s for output\n", filename);
     173        return false;
     174    }
     175    jpeg_stdio_dest(&cinfo, f);
     176
     177    /* set up color jpeg buffers */
     178    int quality = 75;
     179    cinfo.image_width = image->numCols; // image width and height, in pixels
     180    cinfo.image_height = image->numRows;
     181    cinfo.input_components = 3;
     182    cinfo.in_color_space = JCS_RGB;
     183    jpeg_set_defaults (&cinfo);
     184    jpeg_set_quality (&cinfo, quality, true); // limit to baseline-JPEG values
     185    jpeg_start_compress (&cinfo, true);
     186
     187    psU8 *Rpix = map->red->data.U8;
     188    psU8 *Gpix = map->green->data.U8;
     189    psU8 *Bpix = map->blue->data.U8;
     190
     191    if (max == min) {
     192        zero = min - 0.1;
     193        scale = 256.0/0.2;
     194    } else {
     195        zero = min;
     196        scale = 256.0/(max - min);
     197    }
     198
     199    int dx = image->numCols;
     200    int dy = image->numRows;
     201
     202    // output image buffer and line buffer
     203    jpegLine = psAlloc (3*dx*sizeof(JSAMPLE));
     204    jpegImage = psAlloc (3*dx*dy*sizeof(JSAMPLE));
     205
     206    // first copy the image data into the output buffer
     207    for (int j = 0; j < dy; j++) {
     208        psF32 *row = image->data.F32[j];
     209
     210        outPix = jpegLine;
     211        for (int i = 0; i < dx; i++, outPix += 3) {
     212            if (isfinite(row[i])) {
     213                pixel = PS_JPEG_SCALEVALUE(row[i],zero,scale);
     214                outPix[0] = Rpix[pixel];
     215                outPix[1] = Gpix[pixel];
     216                outPix[2] = Bpix[pixel];
     217            } else {
     218                // XXX NAN value should be set per-color map
     219                outPix[0] = 0x00;
     220                outPix[1] = 0xff;
     221                outPix[2] = 0x00;
     222            }
     223        }
     224        memcpy (&jpegImage[j*3*dx], jpegLine, 3*dx);
     225    }
     226
     227    bDrawBuffer *bdbuf = bDrawBufferCreate(dx, dy);
     228    bDrawSetBuffer(bdbuf);
     229    bDrawColor red = KapaColorByName("red");
     230    bDrawSetStyle (red, 1, 0);
     231    bDrawCircle(40.0, 20.0, 3.0);
     232
     233    {
     234        int Npalette;
     235        png_color *palette = KapaPNGPalette (&Npalette);
     236        bDrawColor white = KapaColorByName ("white");
     237        for (int j = 0; j < dy; j++) {
     238            for (int i = 0; i < dx; i++) {
     239                bDrawColor color = bdbuf[0].pixels[j][i];
     240                if (color == white) continue;
     241                jpegImage[j*3*dx + 3*i + 0] = palette[color].red;
     242                jpegImage[j*3*dx + 3*i + 1] = palette[color].green;
     243                jpegImage[j*3*dx + 3*i + 2] = palette[color].blue;
     244            }
     245        }
     246    }
     247    bDrawBufferFree (bdbuf);
     248
     249    // write out the image buffer
     250    for (int j = 0; j < image->numRows; j++) {
     251        jpegLineList[0] = &jpegImage[j*3*dx];
     252        if (jpeg_write_scanlines(&cinfo, jpegLineList, 1) == 0) {
     253            psError(PS_ERR_IO, true, "Unable to write line %d to JPEG", j);
     254            psFree(jpegLine);
     255            psFree(jpegImage);
     256            fclose(f);
     257            return false;
     258        }
     259    }
     260
     261    jpeg_finish_compress(&cinfo);
     262    if (fclose(f) == EOF) {
     263        psError(PS_ERR_IO, true, "Failed to close %s", filename);
     264        psFree(jpegLine);
     265        psFree(jpegImage);
     266        return false;
     267    }
     268    jpeg_destroy_compress(&cinfo);
     269
     270    psFree(jpegLine);
     271    psFree(jpegImage);
     272    return true;
     273}
     274
     275bool psImageJpegOld(const psImageJpegColormap *map, const psImage *image, const char *filename,
    138276                 float min, float max)
    139277{
Note: See TracChangeset for help on using the changeset viewer.