int GIFsetColRep(Metafile *mf, int num, Gint idx, Gcobundl *rep) { Gint status = 1; int imf; mf_cgmo **cgmo = &mf->cgmo; for (imf = 0; imf < num; ++imf) { float r = rep->red, g = rep->green, b = rep->blue; GIFmetafile *meta = find_meta(cgmo[imf]); int gindex; assert(meta); /* Do the color allocation -- background color */ /* can't be reset */ if (idx > 0 && (gindex = allocate_color(meta, idx, r, g, b)) == -1){ msgWarn("GIFsetColRep : Can't allocate color (%f, %f, %f)\n", r, g, b); } else { msgInfo("GIFsetColRep: assigning color (%f, %f, %f) to GKS %d GIF %d\n", r, g, b, idx, gindex); status = OK; } } return status; }
/* * Open an output GIF file. */ int GIFmoOpen(WS_STATE_PTR ws) { /* Output file not opened or written to until flushed */ Gint status = 1; initLogFlag(); assert(ws != NULL); assert(ws->wscolour <= gdMaxColors); if ((ws->mf.cgmo = (mf_cgmo*)umalloc(sizeof(mf_cgmo))) != NULL) { mf_cgmo *cgmo = ws->mf.cgmo; cgmo->ws = ws; if (find_meta(cgmo) != 0){ msgWarn("GIFmoOpen:metafile already open\n"); } else { create_meta(cgmo, DefaultSize, DefaultSize); cgmo->ws = ws; cgmo->type = MF_GIF; unlink(ws->conn); status = OK; } } return status; }
void GIFresize(WS_STATE_PTR ws, Gpoint size) { mf_cgmo *cgmo = ws->mf.cgmo; GIFmetafile *meta = find_meta(cgmo); assert(meta); #if 0 if (meta->resize == 0){ msgErr("GIFresize: metafile contains primitives -- can't resize\n"); } else { copy_meta(cgmo, find_meta(cgmo), size.x, size.y); msgInfo("GIFresize: Resizing to (%f,%f)\n", size.x, size.y); } #endif if (meta->resize == 0){ msgWarn("GIFresize: metafile contains primitives that will be lost when resizing\n"); } copy_meta(cgmo, find_meta(cgmo), size.x, size.y); msgInfo("GIFresize: Resizing to (%f,%f)\n", size.x, size.y); }
int GIFsetGraphAttr(Metafile *mf, int num, Gint code, Gint attr) { int imf; mf_cgmo **cgmo = &mf->cgmo; for (imf = 0; imf < num; ++imf) { GIFmetafile *meta = find_meta(cgmo[imf]); assert(meta); switch(code){ case GKSM_POLYLINE_INDEX: set_lineColor(meta, attr, GBUNDLED); set_lineWidth(meta, 0.0, attr, GBUNDLED); set_lineStyle(meta, attr, GBUNDLED); break; case GKSM_POLYLINE_COLOUR_INDEX: set_lineColor(meta, attr, GINDIVIDUAL); break; case GKSM_LINETYPE: set_lineStyle(meta, attr, GINDIVIDUAL); break; case GKSM_TEXT_COLOUR_INDEX: unsupported("GIFsetGraphAttr : GKSM_TEXT_COLOUR_INDEX", attr); break; case GKSM_FILL_AREA_COLOUR_INDEX: set_fillColor(meta, attr); break; case GKSM_FILL_AREA_STYLE_INDEX: set_fillStyle(meta, attr); break; case GKSM_POLYMARKER_COLOUR_INDEX: unsupported("GIFsetGraphAttr : GKSM_POLYMARKER_COLOUR_INDEX", attr); break; case GKSM_FILL_AREA_INDEX: unsupported("GIFsetGraphAttr : GKSM_FILL_AREA_INDEX", attr); break; case GKSM_MARKER_TYPE: unsupported("GIFsetGraphAttr : GKSM_MARKER_TYPE", attr); break; case GKSM_POLYMARKER_INDEX: unsupported("GIFsetGraphAttr : GKSM_POLYMARKER_INDEX", attr); break; case GKSM_PICK_IDENTIFIER: unsupported("GIFsetGraphAttr : GKSM_PICK_IDENTIFIER", attr); break; case GKSM_TEXT_INDEX: unsupported("GIFsetGraphAttr : GKSM_TEXT_INDEX", attr); break; default: msgWarn("GIFsetGraphAttr: Unknown code %d\n", code); } } return OK; }
/* * Set the clipping rectangle in an output GIF file. */ int GIFsetClip(Metafile *mf, int num, Glimit *rect) { mf_cgmo *cgmo = mf->cgmo; GIFmetafile *meta = find_meta(cgmo); int x1,y1,x2,y2; xform(meta, rect->xmin, rect->ymin, &x1, &y1); xform(meta, rect->xmax, rect->ymax, &x2, &y2); gdSetClip(meta->image, x1,y1,x2,y2); return OK; }
/* * Set the interior fill-style in an output GIF file. * Unsupported */ int GIFsetFillStyle(Metafile *mf, int num, Gflinter style) { int imf; mf_cgmo **cgmo = &mf->cgmo; for (imf = 0; imf < num; ++imf) { GIFmetafile *meta = find_meta(cgmo[imf]); assert(meta); set_fillStyle(meta, style); } return OK; }
int GIFFlush(Metafile *mf, char *filename) { mf_cgmo *cgmo = mf->cgmo; GIFmetafile *meta = find_meta(cgmo); int status = 1; FILE *fp = fopen(filename, "w"); if (fp != NULL && meta != 0){ status = OK; gdImageGif(meta->image, fp); fclose(fp); } return status; }
/* * Set the clear flag in an output GIF file. */ int GIFclear(Metafile *mf, int num, Gclrflag flag) { int imf; mf_cgmo **cgmo = &mf->cgmo; for (imf = 0; imf < num; ++imf) { Gint i; GIFmetafile *meta = find_meta(cgmo[imf]); GIFcolor *color; assert(meta); meta->resize = 1; /* OK to resize */ color = &meta->colors[0]; gdImageBlockFill(meta->image, color->index); } return OK; }
/* int GIFFlusht0(Metafile *mf, char *filename) transparent, color 0 */ int GIFFlusht0(Metafile *mf, char *filename) { mf_cgmo *cgmo = mf->cgmo; GIFmetafile *meta = find_meta(cgmo); int status = 1; int tcolor; int i; FILE *fp = fopen(filename, "w"); if (fp != NULL && meta != 0){ status = OK; gdImageGif(meta->image, fp);/*save the image as gif */ fclose(fp); /* open the gif, change transparency and then save */ FILE *inGif; FILE *ouGif; gdImagePtr gifImg; inGif = fopen(filename,"rb"); gifImg = gdImageCreateFromGif(inGif); fclose(inGif); ouGif = fopen(filename,"w"); //(0,0,0) is for white background tcolor = gdImageColorClosest(gifImg, 255, 255, 255); for (i=0; i < 256; i+=1) { tcolor = gdImageColorClosest(gifImg, i, i, i); } gdImageColorTransparent(gifImg, tcolor); gdImageGif(gifImg, ouGif); fclose(ouGif); gdImageDestroy(gifImg); } return status; }
ledger_status ledger_partition_read(ledger_partition *partition, uint64_t start_id, size_t nmessages, ledger_message_set *messages) { ledger_status rc; ledger_journal_meta_entry *meta; ledger_journal journal; ledger_journal_options journal_options; uint64_t message_id; size_t messages_left; ledger_check_rc(partition->meta.nentries > 0, LEDGER_ERR_BAD_PARTITION, "No journal entry to read from"); journal_options.drop_corrupt = partition->options.drop_corrupt; journal_options.max_size_bytes = partition->options.journal_max_size_bytes; memset(messages, 0, sizeof(ledger_message_set)); messages->partition_num = partition->number; message_id = start_id; messages_left = nmessages; do { meta = find_meta(partition, message_id); rc = ledger_journal_open(&journal, partition->path, meta, &journal_options); ledger_check_rc(rc == LEDGER_OK, rc, "Failed to open journal"); rc = ledger_journal_read(&journal, message_id, messages_left, messages); ledger_check_rc(rc == LEDGER_OK || rc == LEDGER_NEXT, rc, "Failed to read from the journal"); ledger_journal_close(&journal); messages_left = messages_left - messages->nmessages; message_id = messages->next_id; if(meta->id == partition->meta.nentries-1) { // Reached the last journal, and there's no more messages to read break; } } while (rc == LEDGER_NEXT); return LEDGER_OK; error: ledger_journal_close(&journal); return rc; }
int GIFFlush(Metafile *mf, char *filename) { mf_cgmo *cgmo = mf->cgmo; GIFmetafile *meta = find_meta(cgmo); int status = 1; /* File w/ ".gif" only are not created (Ferret hack) */ if (strcasecmp(filename, ".gif") != 0){ FILE *fp = fopen(filename, "w"); if (fp != NULL && meta != 0){ status = OK; gdImageGif(meta->image, fp); fclose(fp); } } else { status = OK; } return status; }
/* * Close an output GIF file. */ int GIFmoClose(Metafile *mf, int *batmode) { int status = 1; /* return status error */ if (mf != NULL && mf->cgmo != NULL) { mf_cgmo *cgmo = mf->cgmo; GIFmetafile *meta = find_meta(cgmo); if (batmode != 0){ *status = GIFFlush(mf, meta->ws->conn); } if (meta != 0){ destroy_meta(cgmo, meta); } ufree((voidp)mf->cgmo); mf->cgmo = NULL; } return status; }
/* * Set the size of graphics in an output GIF file. */ int GIFsetGraphSize(Metafile *mf, int num, Gint code, double size) { int imf; mf_cgmo **cgmo = &mf->cgmo; for (imf = 0; imf < num; ++imf){ GIFmetafile *meta = find_meta(cgmo[imf]); assert(meta); switch(code){ case GKSM_LINEWIDTH_SCALE_FACTOR: set_lineWidth(meta, size, 0, GINDIVIDUAL); break; case GKSM_CHARACTER_EXPANSION_FACTOR: case GKSM_MARKER_SIZE_SCALE_FACTOR: case GKSM_CHARACTER_SPACING: return OK; /* Ignore */ default: msgWarn("GIFsetGraphSize: Unknown code %d\n", code); return OK; } } return OK; }
/* * Close an output GIF file. */ int GIFmoClose(Metafile *mf, int batmode) { int status = 1; /* return status error */ if (mf != NULL && mf->cgmo != NULL) { mf_cgmo *cgmo = mf->cgmo; GIFmetafile *meta = find_meta(cgmo); /* the call to GIFFlush had been commented out with the fix to ticket 1150. But that means that files created with "ferret -batch giffilename.gif" are not written. */ if (batmode != 0){ status = GIFFlush(mf, meta->ws->conn); } if (meta != 0){ destroy_meta(cgmo, meta); } ufree((voidp)mf->cgmo); mf->cgmo = NULL; } return status; }
int GIFoutputGraphic(Metafile *mf, int num, Gint code, Gint num_pt, Gpoint *pos) { int imf; mf_cgmo **cgmo = &mf->cgmo; for (imf = 0; imf < num; ++imf) { Gint i; GIFmetafile *meta = find_meta(cgmo[imf]); assert(meta); assert(num_pt > 0); meta->resize = 0; /* Not OK to resize */ /* Trivial accept and reject clipping of points. */ { int xlo = 1, xhi = 1, yhi = 1, ylo = 1; Gfloat lolimit = -1, hilimit = 2; int i; for (i=1; i < num_pt; ++i){ float x = pos[i].x; float y = pos[i].y; xlo &= (x < lolimit); xhi &= (x > hilimit); ylo &= (y < lolimit); yhi &= (y > hilimit); } if (xlo || xhi || ylo || yhi) return OK; /* If any points are extreme, toss the polygon. Someday, there will be a real clipping algorithm */ for (i=1; i < num_pt; ++i){ float x = pos[i].x; float y = pos[i].y; if (x < -10. || x > 10. || y < -10 || y > 10){ msgWarn("GIFoutputGraphic: Bad NDC point %f %f\n", x, y); return OK; } } } switch(code){ case GKSM_FILL_AREA: { gdPointPtr tpts = (gdPointPtr)umalloc(sizeof(gdPoint) * num_pt); assert(tpts); xform(meta, pos->x, pos->y, &tpts[0].x, &tpts[0].y); #ifdef DEBUG printf("--------------------------------------\n"); printf("Transform: (%f, %f) -> (%d,%d)\n", pos->x, pos->y, tpts[0].x, tpts[0].y); #endif ++pos; for (i=1; i < num_pt; ++i, ++pos){ xform(meta, pos->x, pos->y, &tpts[i].x, &tpts[i].y); #ifdef DEBUG printf("Transform: (%f, %f) -> (%d,%d)\n", pos->x, pos->y, tpts[i].x, tpts[i].y); #endif } if (meta->fillStyleIndex == 0){ gdImagePolygon(meta->image, tpts, num_pt, meta->fillIndex); } else { /* Eliminate colinear points (bug in gd.c) */ stripColinear(tpts, &num_pt); gdImageFilledPolygon(meta->image, tpts, num_pt, meta->fillIndex); } free(tpts); } break; case GKSM_POLYLINE: { int lineIndex = meta->styleIndex == 1 ? meta->lineIndex : gdStyled; gdPointPtr tpts = (gdPointPtr)umalloc(sizeof(gdPoint) * num_pt); assert(tpts); for (i=0; i < num_pt; ++i, ++pos){ xform(meta, pos->x, pos->y, &tpts[i].x, &tpts[i].y); } gdImageWideLines(meta->image, tpts, num_pt, lineIndex, meta->lineWidth); free(tpts); } break; case GKSM_POLYMARKER: msgWarn("GIFoutputGraphics: polymarker unsupported\n"); break; default: msgWarn("GIFoutputGraphics: Unknown code %d\n", code); } } return OK; }
/* Get value of an environment variable or special format variable. */ static char *get_file_var(const clamz_config *cfg, const clamz_track *tr, const char *var, int use_fallback) { const char *s; const char *fallback = "Unknown"; char nbuf[3]; char *value; int subst_raw = 0; if (!strcasecmp(var, "title")) s = tr->title; else if (!strcasecmp(var, "creator")) s = tr->creator; else if (!strcasecmp(var, "album")) s = tr->album; else if (!strcasecmp(var, "tracknum")) { s = tr->trackNum; if (s && s[0] && !s[1]) { nbuf[0] = '0'; nbuf[1] = s[0]; nbuf[2] = 0; s = nbuf; } fallback = "00"; } else if (!strcasecmp(var, "album_artist")) s = find_meta(tr->meta, TMETA_ALBUM_ARTIST); else if (!strcasecmp(var, "genre")) s = find_meta(tr->meta, TMETA_GENRE); else if (!strcasecmp(var, "discnum")) { s = find_meta(tr->meta, TMETA_DISC_NUM); fallback = "1"; } else if (!strcasecmp(var, "suffix")) { s = find_meta(tr->meta, TMETA_TRACK_TYPE); fallback = "mp3"; } else if (!strcasecmp(var, "asin")) s = find_meta(tr->meta, TMETA_ASIN); else if (!strcasecmp(var, "album_asin")) s = find_meta(tr->meta, TMETA_ALBUM_ASIN); else if (!strcasecmp(var, "amz_title")) s = tr->playlist->title; else if (!strcasecmp(var, "amz_creator")) s = tr->playlist->creator; else if (!strcasecmp(var, "amz_asin")) s = find_meta(tr->playlist->meta, PMETA_ASIN); else if (!strcasecmp(var, "amz_genre")) s = find_meta(tr->playlist->meta, PMETA_GENRE); else { s = getenv(var); fallback = ""; subst_raw = 1; } if (!s || !s[0]) { if (use_fallback) s = fallback; else s = ""; } if (subst_raw) { value = strdup(s); if (!value) print_error("Out of memory"); return value; } else { return convert_string(cfg, s); } }
int GIFcellArray(Metafile *mf, int num, Gpoint *ll, Gpoint *ur, Gpoint *lr, Gint row, Gint *colour, Gipoint *dim) { int dstX, dstY, sumX, sumY, xx2, yy2, c; int lx, ly, index, imf, srcW, srcH, dstW, dstH, npts; int x, y; /* Stretch vectors; code from gdImageCopyResized */ int *stx; int *sty; double accum; int tox, toy; int i, j, p; int num_pt; mf_cgmo **cgmo = &mf->cgmo; for (imf = 0; imf < num; ++imf) { Gint i; GIFmetafile *meta = find_meta(cgmo[imf]); assert(meta); assert(num_pt > 0); meta->resize = 0; /* Not OK to resize */ xform(meta, ll->x, ll->y, &dstX, &yy2); xform(meta, ur->x, ur->y, &xx2, &dstY); dstW = xx2 - dstX; dstH = yy2 - dstY; srcW = dim->x; srcH = dim->y; npts = srcW * srcH; /* More code from gdImageCopyResized. We only need to use floating point to determine the correct stretch vector for one line's worth. */ stx = (int *) malloc(sizeof(int) * srcW); sty = (int *) malloc(sizeof(int) * srcH); accum = 0; for (i=0; (i < srcW); i++) { int got; accum += (double)dstW/(double)srcW; got = floor(accum); stx[i] = got; accum -= got; } accum = 0; for (i=0; (i < srcH); i++) { int got; accum += (double)dstH/(double)srcH; got = floor(accum); sty[i] = got; accum -= got; } index = 0; c = colour[index]; toy = dstY; sumY = dstY; for (y=0; (y < srcH); y++) /* cells in y */ { tox = dstX; sumX = dstX; for (x=0; (x < srcW); x++) /* cells in y */ { toy = sumY; for (j=0; (j < sty[y]); j++) /* image pixels in y */ { p = j; tox = sumX; for (i=0; (i < stx[x]); i++) /* image pixels in x */ { p = i; gdImageSetPixel(meta->image, tox, toy, c); tox++; } toy++; } index++; c = 1; if (index < npts) { c = colour[index]; } sumX = sumX + stx[x]; } sumY = sumY + sty[y]; } free(stx); free(sty); } return OK; }
/* int GIFFlusht1(Metafile *mf, char *filename) transparent, color 1 (black background) */ int GIFFlusht1(Metafile *mf, char *filename) { mf_cgmo *cgmo = mf->cgmo; GIFmetafile *meta = find_meta(cgmo); int status = 1; int tcolor; int white; /* File w/ ".gif" only are not created (Ferret hack) */ if (strcasecmp(filename, ".gif") != 0){ FILE *fp = fopen(filename, "w"); if (fp != NULL && meta != 0){ status = OK; gdImageGif(meta->image, fp);/*save the image as gif */ fclose(fp); /* open the gif, change transparency and then save */ FILE *inGif; FILE *ouGif; gdImagePtr gifImg; inGif = fopen(filename,"rb"); gifImg = gdImageCreateFromGif(inGif); fclose(inGif); ouGif = fopen(filename,"w"); //(1,1,1) is for black background tcolor = gdImageColorClosest(gifImg, 0, 0, 0);; gdImageColorTransparent(gifImg, tcolor); gdImageGif(gifImg, ouGif); fclose(ouGif); gdImageDestroy(gifImg); /* copy logo image to an output image */ /* Set up an image the size of the Ferret plot image */ inGif = fopen(filename,"rb"); gifImg = gdImageCreateFromGif(inGif); fclose(inGif); /* Open the image to be the background. Copy to the output image */ FILE *inLog; gdImagePtr logImg; /* inLog = fopen("/home/users/ansley/ans_ferret/users/ansley/centered_noaa_logo.gif","rb"); */ inLog = fopen("/home/users/ansley/Wave-3.gif","rb"); logImg = gdImageCreateFromGif(inLog); fclose(inLog); gdImageCopy(gifImg, logImg, 0, 0, 0, 0, logImg->sx, logImg->sy); /* Now copy the transparent image to the output image */ inGif = fopen(filename,"rb"); transImg = gdImageCreateFromGif(inGif); fclose(inGif); gdImageCopy(gifImg, transImg, 0, 0, 0, 0, gifImg->sx, gifImg->sy); ouGif = fopen(filename,"w"); gdImageGif(gifImg, ouGif); fclose(ouGif); } } else { status = OK; } return status; }
/* int GIFFlusht0(Metafile *mf, char *filename) transparent, color 0 (white background) */ int GIFFlusht0(Metafile *mf, char *filename) { mf_cgmo *cgmo = mf->cgmo; GIFmetafile *meta = find_meta(cgmo); int status = 1; int tcolor; int i; /* Load the logo image */ /* from Jing's email: /* /* Through the function gdImageCopy, you can control the position of you logo and which /* part of your logo to copy. /* /* gdImageCopy(gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int srcX, int srcY, int w, int h) /* The dstX and dstY arguments specify the point in the destination image to which the region will be copied. /* The srcX and srcY arguments specify the upper left corner of the region in the source image. /* The w and h arguments specify the width and height of the region. /* more notes (ACM) /* the values of the arguments to gdImageCopy are in pixels. /* the upper left corner is the origin for the location on the destination image /* Specifying values smaller than the size of the logo image has the effect of chopping /* the image, not shrinking it. /* To place the image 100 pixels to the right of the orig in X on the destination image, /* and starting at 50 pixels from the top of the source image */ /* gdImageCopy(meta->image, logImg, 100, 0, 0, 50, logImg->sx, logImg->sy); */ /* FILE *inLog; /* gdImagePtr logImg; /* inLog = fopen("/home/users/ansley/ans_ferret/users/ansley/centered_noaa_logo.gif","rb"); /* inLog = fopen("/home/users/ansley/Wave-3.gif","rb");*/ /* logImg = gdImageCreateFromGif(inLog); /* /* Copy the logo to your figure meta->image */ /* /* gdImageCopy(meta->image, logImg, 0, 0, 0, 0, logImg->sx, logImg->sy); /* fclose(inLog); /* File w/ ".gif" only are not created (Ferret hack) */ if (strcasecmp(filename, ".gif") != 0){ FILE *fp = fopen(filename, "w"); if (fp != NULL && meta != 0){ status = OK; gdImageGif(meta->image, fp);/*save the image as gif */ fclose(fp); /* open the gif, change transparency and then save */ FILE *inGif; FILE *ouGif; gdImagePtr gifImg; gdImagePtr transImg; inGif = fopen(filename,"rb"); gifImg = gdImageCreateFromGif(inGif); fclose(inGif); ouGif = fopen(filename,"w"); //(0,0,0) is for white background tcolor = gdImageColorClosest(gifImg, 255, 255, 255); for (i=0; i < 256; i+=1) { tcolor = gdImageColorClosest(gifImg, i, i, i); } gdImageColorTransparent(gifImg, tcolor); gdImageGif(gifImg, ouGif); fclose(ouGif); gdImageDestroy(gifImg); /* copy logo image to an output image */ /* Set up an image the size of the Ferret plot image */ inGif = fopen(filename,"rb"); gifImg = gdImageCreateFromGif(inGif); fclose(inGif); /* Open the image to be the background. Copy to the output image */ FILE *inLog; gdImagePtr logImg; /* inLog = fopen("/home/users/ansley/ans_ferret/users/ansley/centered_noaa_logo.gif","rb"); */ inLog = fopen("/home/users/ansley/Wave-3.gif","rb"); logImg = gdImageCreateFromGif(inLog); fclose(inLog); gdImageCopy(gifImg, logImg, 0, 0, 0, 0, logImg->sx, logImg->sy); /* Now copy the transparent image to the output image */ inGif = fopen(filename,"rb"); transImg = gdImageCreateFromGif(inGif); fclose(inGif); gdImageCopy(gifImg, transImg, 0, 0, 0, 0, gifImg->sx, gifImg->sy); ouGif = fopen(filename,"w"); gdImageGif(gifImg, ouGif); fclose(ouGif); } } else { status = OK; } return status; }