int main() { gdImagePtr im; int x, y, c; FILE *out; char path[1024]; int r=0; im = gdImageCreateTrueColor(256, 256); gdImageAlphaBlending( im, gdEffectReplace ); for (x=0; x<256; x++) { for (y=0; y<256; y++) { c = (y/2 << 24) + (x << 16) + (x << 8) + x; gdImageSetPixel(im, x, y, c ); } } gdImageAlphaBlending( im, gdEffectOverlay ); gdImageFilledRectangle(im, 0, 0, 255, 255, 0xff7f00); if (gdTrueColorGetGreen(gdImageGetPixel(im, 0, 128)) != 0x00) { r = 1; } if (gdTrueColorGetGreen(gdImageGetPixel(im, 128, 128)) != 0x80) { r = 1; } if (gdTrueColorGetGreen(gdImageGetPixel(im, 255, 128)) != 0xff) { r = 1; } gdImageDestroy(im); return r; }
int ImgGetAbsColorDiff(gdImagePtr img1, gdImagePtr img2, gdImagePtr imgresult) { int p1, p2; int x, y; int dr, dg, db; int tr, tg, tb; tr = tg = tb = 0; for (y = 0; y != THUMB_CY; y++) { for (x = 0; x != THUMB_CX; x++) { p1 = img1->tpixels[y][x]; p2 = img2->tpixels[y][x]; dr = abs(gdTrueColorGetRed(p1) - gdTrueColorGetRed(p2)); dg = abs(gdTrueColorGetGreen(p1) - gdTrueColorGetGreen(p2)); db = abs(gdTrueColorGetBlue(p1) - gdTrueColorGetBlue(p2)); tr += dr; tg += dg; tb += db; imgresult->tpixels[y][x] = (dr << 16) | (dg << 8) | (db << 0); } } tr /= THUMB_NPIXELS; tg /= THUMB_NPIXELS; tb /= THUMB_NPIXELS; return (tr << 16) | (tg << 8) | (tb << 0); }
/* This routine reads a png-file from disk and puts it into buff in a format the LCD understands. (basically 16-bit rgb) */ int readpic(char* filename, char* buff) { FILE *in; gdImagePtr im; int x,y; int c,r,g,b; in=fopen(filename,"rb"); if (in==NULL) return 0; //Should try other formats too im=gdImageCreateFromPng(in); fclose(in); if (im==NULL) return 0; for (y=0; y<128; y++) { for (x=0; x<128; x++) { c = gdImageGetPixel(im, x, y); if (gdImageTrueColor(im) ) { r=gdTrueColorGetRed(c); g=gdTrueColorGetGreen(c); b=gdTrueColorGetBlue(c); } else { r=gdImageRed(im,c); g=gdImageGreen(im,c); b=gdImageBlue(im,c); } r>>=3; g>>=2; b>>=3; c=(r<<11)+(g<<5)+b; buff[x*2+y*256]=(c>>8); buff[x*2+y*256+1]=(c&255); } } gdImageDestroy(im); return 1; }
void buildaudio () { uint16_t x , y , k ; uint32_t pixel ; uint8_t r[320], g[320], b[320] ; printf( "Adding image to audio data.\n" ) ; for ( y=0 ; y<256 ; y++ ) { // printf( "Row [%d] Sample [%d].\n" , y , g_samples ) ; // read image data for ( x=0 ; x<320 ; x++ ) { pixel = gdImageGetTrueColorPixel( g_imgp, x, y ) ; //printf( "Got pixel.\n" ) ; // get color data r[x] = gdTrueColorGetRed( pixel ) ; g[x] = gdTrueColorGetGreen( pixel ) ; b[x] = gdTrueColorGetBlue( pixel ) ; } // add row markers to audio // sync playtone( 1200 , 4862 ) ; // porch playtone( 1500 , 572 ) ; // each pixel is 457.6us long in Martin 1 // add audio for green channel for this row for ( k=0 ; k<320 ; k++ ) { playtone( toneval( g[k] ) , 457.6 ) ; } // separator tone playtone( 1500 , 572 ) ; // bloo channel for ( k=0 ; k<320 ; k++ ) { playtone( toneval( b[k] ) , 457.6 ) ; } playtone( 1500 , 572 ) ; // red channel for ( k=0 ; k<320 ; k++ ) { playtone( toneval( r[k] ) , 457.6 ) ; } playtone( 1500 , 572 ) ; } // end for y printf( "Done adding image to audio data.\n" ) ; } // end buildaudio
void* diffImagesMT(void *arg) { diffImageMTArgs *args = (diffImageMTArgs*)arg; double difference = 0.0; int x,y, mx = ei::Tools::maxWidth, my = ei::Tools::maxHeight; // Loop Y then X. 6% faster for the GD library. for (y = args->rowStart; y < my; y += 2) for (x = 0; x < mx; x++) { int c1 = gdImageGetPixel(args->oldImage, x, y); int c2 = gdImageGetPixel(args->newImage, x, y); int r = gdTrueColorGetRed(c1) - gdTrueColorGetRed(c2); int g = gdTrueColorGetGreen(c1) - gdTrueColorGetGreen(c2); int b = gdTrueColorGetBlue(c1) - gdTrueColorGetBlue(c2); difference += (r*r + g*g + b*b); } args->result = difference; return 0; }
static void gd_loadimage_cairo(GVJ_t * job, usershape_t *us, boxf b, boolean filled) { cairo_t *cr = (cairo_t *) job->context; /* target context */ unsigned int x, y, stride, width, height, px; unsigned char *data; cairo_surface_t *surface; /* source surface */ gdImagePtr im; if ((im = gd_loadimage(job, us))) { width = im->sx; height = im->sy; // cairo_format_stride_for_width() not available prior to cairo-1.6.4 or so (fc9) //stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32, width); stride = width*4; data = malloc (stride * height); surface = cairo_image_surface_create_for_data (data, CAIRO_FORMAT_ARGB32, width, height, stride); if (im->trueColor) { for (y = 0; y < height; y++) { for (x = 0; x < width; x++) { px = gdImageTrueColorPixel(im, x, y); *data++ = gdTrueColorGetBlue(px); *data++ = gdTrueColorGetGreen(px); *data++ = gdTrueColorGetRed(px); *data++ = (0x7F-gdTrueColorGetAlpha(px)) << 1; } } } else { for (y = 0; y < height; y++) { for (x = 0; x < width; x++) { px = gdImagePalettePixel(im, x, y); *data++ = im->blue[px]; *data++ = im->green[px]; *data++ = im->red[px]; *data++ = (px==im->transparent)?0x00:0xff; } } } cairo_save(cr); cairo_translate(cr, (b.LL.x + (b.UR.x - b.LL.x) * (1. - (job->dpi.x) / 96.) / 2.), (-b.UR.y + (b.UR.y - b.LL.y) * (1. - (job->dpi.y) / 96.) / 2.)); cairo_scale(cr, ((b.UR.x - b.LL.x) * (job->dpi.x) / (96. * us->w)), ((b.UR.y - b.LL.y) * (job->dpi.y) / (96. * us->h))); cairo_set_source_surface (cr, surface, 0, 0); cairo_paint (cr); cairo_restore(cr); cairo_surface_destroy(surface); } }
static void ax203_encode_block_yuv(int **src, int src_x, int src_y, char *dest) { uint8_t Y[4]; int8_t U, V; int x, y; /* Step 1 convert to YUV */ for (y = 0; y < 2; y ++) { for (x = 0; x < 2; x ++) { int p = src[src_y + y][src_x + x]; Y[y * 2 + x] = gdTrueColorGetRed(p) * 0.257 + gdTrueColorGetGreen(p) * 0.504 + gdTrueColorGetBlue(p) * 0.098 + 16; } } { int p1 = src[src_y ][src_x ]; int p2 = src[src_y ][src_x + 1]; int p3 = src[src_y + 1][src_x ]; int p4 = src[src_y + 1][src_x + 1]; int r = (gdTrueColorGetRed(p1) + gdTrueColorGetRed(p2) + gdTrueColorGetRed(p3) + gdTrueColorGetRed(p4)) / 4; int g = (gdTrueColorGetGreen(p1) + gdTrueColorGetGreen(p2) + gdTrueColorGetGreen(p3) + gdTrueColorGetGreen(p4)) / 4; int b = (gdTrueColorGetBlue(p1) + gdTrueColorGetBlue(p2) + gdTrueColorGetBlue(p3) + gdTrueColorGetBlue(p4)) / 4; U = 0.439 * b - 0.291 * g - 0.148 * r; V = 0.439 * r - 0.368 * g - 0.071 * b; } for (x = 0; x < 4; x++) dest[x] = Y[x] & 0xf8; dest[0] |= (U & 0xe0) >> 5; dest[1] |= (U & 0x1c) >> 2; dest[2] |= (V & 0xe0) >> 5; dest[3] |= (V & 0x1c) >> 2; }
/* bring the palette colors in im2 to be closer to im1 * */ int gdImageColorMatch (gdImagePtr im1, gdImagePtr im2) { unsigned long *buf; /* stores our calculations */ unsigned long *bp; /* buf ptr */ int color, rgb; int x,y; int count; if( !im1->trueColor ) { return -1; /* im1 must be True Color */ } if( im2->trueColor ) { return -2; /* im2 must be indexed */ } if( (im1->sx != im2->sx) || (im1->sy != im2->sy) ) { return -3; /* the images are meant to be the same dimensions */ } if (im2->colorsTotal<1) { return -4; /* At least 1 color must be allocated */ } buf = (unsigned long *)safe_emalloc(sizeof(unsigned long), 5 * im2->colorsTotal, 0); memset( buf, 0, sizeof(unsigned long) * 5 * im2->colorsTotal ); for (x=0; x<im1->sx; x++) { for( y=0; y<im1->sy; y++ ) { color = im2->pixels[y][x]; rgb = im1->tpixels[y][x]; bp = buf + (color * 5); (*(bp++))++; *(bp++) += gdTrueColorGetRed(rgb); *(bp++) += gdTrueColorGetGreen(rgb); *(bp++) += gdTrueColorGetBlue(rgb); *(bp++) += gdTrueColorGetAlpha(rgb); } } bp = buf; for (color=0; color<im2->colorsTotal; color++) { count = *(bp++); if( count > 0 ) { im2->red[color] = *(bp++) / count; im2->green[color] = *(bp++) / count; im2->blue[color] = *(bp++) / count; im2->alpha[color] = *(bp++) / count; } else { bp += 4; } } gdFree(buf); return 0; }
//Builds audio scan data for the Scottie series of specifications. //Applicable to Scottie 1, Scottie 2, Scottie SSTV modes //Each pixel is scanned for pixeltime microseconds void buildaudio_s (double pixeltime) { uint16_t x , y , k ; uint32_t pixel ; uint8_t r[320], g[320], b[320] ; printf( "Adding image to audio data.\n" ) ; //add starting sync pulse playtone( 1200 , 9000); for ( y=0 ; y<256 ; y++ ) { // read image data for ( x=0 ; x<320 ; x++ ) { pixel = gdImageGetTrueColorPixel( g_imgp, x, y ) ; // get color data r[x] = gdTrueColorGetRed( pixel ) ; g[x] = gdTrueColorGetGreen( pixel ) ; b[x] = gdTrueColorGetBlue( pixel ) ; } //seperator pulse playtone(1500, 1500); // add audio for green channel for this row for ( k=0 ; k<320 ; k++ ) playtone( toneval_rgb( g[k] ) , pixeltime ) ; // separator tone playtone(1500, 1500) ; // blue channel for ( k=0 ; k<320 ; k++ ) playtone( toneval_rgb( b[k] ) , pixeltime ) ; //sync pulse playtone(1200, 9000); //sync porch playtone(1500 , 1500) ; // red channel for ( k=0 ; k<320 ; k++ ) playtone( toneval_rgb( r[k] ) , pixeltime ) ; } // end for y printf( "Done adding image to audio data.\n" ) ; } // end buildaudio_s
PLINT plD_read_pixel_gd (PLStream *pls, short x, short y) { png_Dev *dev=(png_Dev *)pls->dev; PLINT colour; unsigned char R,G,B; colour=gdImageGetTrueColorPixel(dev->im_out, x, y); R=gdTrueColorGetRed(colour); G=gdTrueColorGetGreen(colour); B=gdTrueColorGetBlue(colour); colour=RGB(R,G,B); return(colour); }
/* This routine reads a png-file from disk and puts it into buff in a format lib understands. (basically 24-bit rgb) */ int sendpic(st2205_handle *h, char* filename) { FILE *in; gdImagePtr im; unsigned char* pixels; int x,y; int p=0; unsigned int c,r,g,b; pixels=malloc(h->width*h->height*3); in=fopen(filename,"rb"); if (in==NULL) return 0; //Should try other formats too im=gdImageCreateFromPng(in); fclose(in); if (im==NULL) { printf("%s: not a png-file.\n",filename); return 0; } for (y=0; y<h->width; y++) { for (x=0; x<h->height; x++) { c = gdImageGetPixel(im, x, y); if (gdImageTrueColor(im) ) { r=gdTrueColorGetRed(c); g=gdTrueColorGetGreen(c); b=gdTrueColorGetBlue(c); } else { r=gdImageRed(im,c); g=gdImageGreen(im,c); b=gdImageBlue(im,c); } // pixels[p++]=0xff; // pixels[p++]=0; // pixels[p++]=0; pixels[p++]=r; pixels[p++]=g; pixels[p++]=b; } } st2205_send_data(h,pixels); gdImageDestroy(im); return 1; }
void Texture::init() { glGenTextures(1, &tid); glBindTexture(GL_TEXTURE_2D, tid); int datSize = w*h*4; // unsigned char data[datSize]; unsigned char* data = new unsigned char[datSize]; for(int j=0; j<h; j++) for(int i=0; i<w; i++) { // gdTrueColor* are in gd.h (ARGB - alpha is 7-bit) int key = (j*w+i)*4; data[key] = gdTrueColorGetRed(im->tpixels[h-1-j][i]); data[key+1] = gdTrueColorGetGreen(im->tpixels[h-1-j][i]); data[key+2] = gdTrueColorGetBlue(im->tpixels[h-1-j][i]); data[key+3] = (127 - gdTrueColorGetAlpha(im->tpixels[h-1-j][i]))<<1; } // well, knowing that textures are at most medium size, // just use auto-mipmap generation. we actually don't have to // be bothered by power-of-two issues. gluBuild2DMipmaps(GL_TEXTURE_2D, 4, w, h, GL_RGBA, GL_UNSIGNED_BYTE, data); // glTexImage2D(GL_TEXTURE_2D, 0, 4, wpo2, hpo2, 0, GL_RGBA, // GL_UNSIGNED_BYTE, data); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); if(im) gdImageDestroy(im); delete data; }
/** * Based on libgd/gd_topal.c */ static void ngx_pngquant_convert_gd_pixel_to_rgba(liq_color output_row[], int y, int width, void *userinfo) { gdImagePtr oim = userinfo; int x; for(x = 0; x < width; x++) { output_row[x].r = gdTrueColorGetRed(oim->tpixels[y][x]) * 255/gdRedMax; output_row[x].g = gdTrueColorGetGreen(oim->tpixels[y][x]) * 255/gdGreenMax; output_row[x].b = gdTrueColorGetBlue(oim->tpixels[y][x]) * 255/gdBlueMax; int alpha = gdTrueColorGetAlpha(oim->tpixels[y][x]); if (gdAlphaOpaque < gdAlphaTransparent) { alpha = gdAlphaTransparent - alpha; } output_row[x].a = alpha * 255/gdAlphaMax; } }
void _ThumbCacheUpdateDirScan(FILE *tc, const char *dir) { unsigned int status; LPTCRECORD ptcrec; char *fn, relfn[MAX_PATH]; int dirlen, len; time_t mtime; #ifdef _WIN32 HANDLE hFindFile; WIN32_FIND_DATA ffd; #else DIR *dirp; struct stat st; struct dirent *entry; #endif dirlen = strlen(dir); if (dirlen + 3 >= MAX_PATH) { fprintf(stderr, "ERROR: filename too long\n"); return; } strcpy(relfn, dir); #ifdef _WIN32 strcpy(relfn + dirlen, "*"); hFindFile = FindFirstFile(relfn, &ffd); if (hFindFile == INVALID_HANDLE_VALUE) { status = GetLastError(); if (status != ERROR_FILE_NOT_FOUND) printerr("FindFirstFile"); return; } do { fn = ffd.cFileName; if ((ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && scan_recursive) { #else dirp = opendir(relfn); if (!dirp) { perror("opendir"); return; } while ((entry = readdir(dirp))) { if (lstat(entry->d_name, &st) == -1) { fprintf(stderr, "ERROR: couldn't stat %s, skipping\n", fn); continue; } fn = entry->d_name; if (S_ISDIR(st.st_mode) && scan_recursive) { #endif if (!(fn[0] == '.' && (!fn[1] || (fn[1] == '.' && !fn[2])))) { len = dirlen + strlen(fn); if (len + 1 >= MAX_PATH) { fprintf(stderr, "ERROR: total rel path len of " "%s too long, skipping\n", fn); continue; } strcpy(relfn + dirlen, fn); relfn[len] = PATH_SEPARATOR; relfn[len + 1] = '\0'; _ThumbCacheUpdateDirScan(tc, relfn); } } else if (ImgIsImageFile(fn)) { len = dirlen + strlen(fn); if (len >= MAX_PATH) { fprintf(stderr, "ERROR: total rel path len of " "%s too long, skipping\n", fn); continue; } #ifdef _WIN32 mtime = FileTimeToUnixTime(ffd.ftLastWriteTime); #else mtime = st.st_mtime; #endif strcpy(relfn + dirlen, fn); fn = HtGetItem(cacheht, relfn); if (fn) { ptcrec = (LPTCRECORD)(fn - sizeof(TCRECORD)); if (mtime != ptcrec->ent.mtime) { if (verbose) printf("Updating %s...\n", relfn); if (!ThumbCacheReplace(tc, relfn, ptcrec, mtime)) printerr("ThumbCacheReplace"); } } else { if (verbose) printf("Adding %s to thumb cache...\n", relfn); if (!ThumbCacheAdd(tc, relfn, mtime)) printerr("ThumbCacheAdd"); else nadded++; } } #ifdef _WIN32 } while (FindNextFile(hFindFile, &ffd)); FindClose(hFindFile); #else } if (closedir(dirp) == -1) perror("closedir"); #endif } float _ThumbCalcKey(int **tpixels) { unsigned int tr, tg, tb; float avg_r, avg_g, avg_b; int pixel, x, y; tr = 0; tg = 0; tb = 0; for (y = 0; y != THUMB_CY; y++) { for (x = 0; x != THUMB_CX; x++) { pixel = tpixels[y][x]; tr += gdTrueColorGetRed(pixel); tg += gdTrueColorGetGreen(pixel); tb += gdTrueColorGetBlue(pixel); } } avg_r = (float)tr / (float)THUMB_NPIXELS; avg_g = (float)tg / (float)THUMB_NPIXELS; avg_b = (float)tb / (float)THUMB_NPIXELS; return (avg_r * avg_r) + (avg_g * avg_g) + (avg_b * avg_b); }
/* Compare two buffers, returning the number of pixels that are * different and the maximum difference of any single color channel in * result_ret. * * This function should be rewritten to compare all formats supported by * cairo_format_t instead of taking a mask as a parameter. */ void gdTestImageDiff(gdImagePtr buf_a, gdImagePtr buf_b, gdImagePtr buf_diff, CuTestImageResult *result_ret) { int x, y; int c1, c2; for (y = 0; y < gdImageSY(buf_a); y++) { for (x = 0; x < gdImageSX(buf_a); x++) { c1 = gdImageGetTrueColorPixel(buf_a, x, y); c2 = gdImageGetTrueColorPixel(buf_b, x, y); /* check if the pixels are the same */ if (c1 != c2) { int r1,b1,g1,a1,r2,b2,g2,a2; unsigned int diff_a,diff_r,diff_g,diff_b; a1 = gdTrueColorGetAlpha(c1); a2 = gdTrueColorGetAlpha(c2); diff_a = abs (a1 - a2); diff_a *= 4; /* emphasize */ if (diff_a) { diff_a += 128; /* make sure it's visible */ } if (diff_a > gdAlphaMax) { diff_a = gdAlphaMax/2; } r1 = gdTrueColorGetRed(c1); r2 = gdTrueColorGetRed(c2); diff_r = abs (r1 - r2); /* diff_r *= 4; /* emphasize */ if (diff_r) { diff_r += gdRedMax/2; /* make sure it's visible */ } if (diff_r > 255) { diff_r = 255; } g1 = gdTrueColorGetGreen(c1); g2 = gdTrueColorGetGreen(c2); diff_g = abs (g1 - g2); diff_g *= 4; /* emphasize */ if (diff_g) { diff_g += gdGreenMax/2; /* make sure it's visible */ } if (diff_g > 255) { diff_g = 255; } b1 = gdTrueColorGetBlue(c1); b2 = gdTrueColorGetBlue(c2); diff_b = abs (b1 - b2); diff_b *= 4; /* emphasize */ if (diff_b) { diff_b += gdBlueMax/2; /* make sure it's visible */ } if (diff_b > 255) { diff_b = 255; } result_ret->pixels_changed++; if (buf_diff) gdImageSetPixel(buf_diff, x,y, gdTrueColorAlpha(diff_r, diff_g, diff_b, diff_a)); } else { if (buf_diff) gdImageSetPixel(buf_diff, x,y, gdTrueColorAlpha(255,255,255,0)); } } } }
static void _gdImageGd2 (gdImagePtr im, gdIOCtx * out, int cs, int fmt) { int ncx, ncy, cx, cy; int x, y, ylo, yhi, xlo, xhi; int chunkLen; int chunkNum = 0; char *chunkData = NULL; /* So we can gdFree it with impunity. */ char *compData = NULL; /* So we can gdFree it with impunity. */ uLongf compLen; int idxPos = 0; int idxSize; t_chunk_info *chunkIdx = NULL; int posSave; int bytesPerPixel = im->trueColor ? 4 : 1; int compMax = 0; /*printf("Trying to write GD2 file\n"); */ /* */ /* Force fmt to a valid value since we don't return anything. */ /* */ if ((fmt != GD2_FMT_RAW) && (fmt != GD2_FMT_COMPRESSED)) { fmt = im->trueColor ? GD2_FMT_TRUECOLOR_COMPRESSED : GD2_FMT_COMPRESSED; }; if (im->trueColor) { fmt += 2; } /* */ /* Make sure chunk size is valid. These are arbitrary values; 64 because it seems */ /* a little silly to expect performance improvements on a 64x64 bit scale, and */ /* 4096 because we buffer one chunk, and a 16MB buffer seems a little large - it may be */ /* OK for one user, but for another to read it, they require the buffer. */ /* */ if (cs == 0) { cs = GD2_CHUNKSIZE; } else if (cs < GD2_CHUNKSIZE_MIN) { cs = GD2_CHUNKSIZE_MIN; } else if (cs > GD2_CHUNKSIZE_MAX) { cs = GD2_CHUNKSIZE_MAX; }; /* Work out number of chunks. */ ncx = im->sx / cs + 1; ncy = im->sy / cs + 1; /* Write the standard header. */ _gd2PutHeader (im, out, cs, fmt, ncx, ncy); if (gd2_compressed (fmt)) { /* */ /* Work out size of buffer for compressed data, If CHUNKSIZE is large, */ /* then these will be large! */ /* */ /* The zlib notes say output buffer size should be (input size) * 1.01 * 12 */ /* - we'll use 1.02 to be paranoid. */ /* */ compMax = cs * bytesPerPixel * cs * 1.02 + 12; /* */ /* Allocate the buffers. */ /* */ chunkData = gdCalloc (cs * bytesPerPixel * cs, 1); if (!chunkData) { goto fail; } compData = gdCalloc (compMax, 1); if (!compData) { goto fail; } /* */ /* Save the file position of chunk index, and allocate enough space for */ /* each chunk_info block . */ /* */ idxPos = gdTell (out); idxSize = ncx * ncy * sizeof (t_chunk_info); GD2_DBG (printf ("Index size is %d\n", idxSize)); gdSeek (out, idxPos + idxSize); chunkIdx = gdCalloc (idxSize * sizeof (t_chunk_info), 1); if (!chunkIdx) { goto fail; } }; _gdPutColors (im, out); GD2_DBG (printf ("Size: %dx%d\n", im->sx, im->sy)); GD2_DBG (printf ("Chunks: %dx%d\n", ncx, ncy)); for (cy = 0; (cy < ncy); cy++) { for (cx = 0; (cx < ncx); cx++) { ylo = cy * cs; yhi = ylo + cs; if (yhi > im->sy) { yhi = im->sy; }; GD2_DBG (printf ("Processing Chunk (%dx%d), y from %d to %d\n", cx, cy, ylo, yhi)); chunkLen = 0; for (y = ylo; (y < yhi); y++) { /*GD2_DBG(printf("y=%d: ",y)); */ xlo = cx * cs; xhi = xlo + cs; if (xhi > im->sx) { xhi = im->sx; }; if (gd2_compressed (fmt)) { for (x = xlo; x < xhi; x++) { /* 2.0.11: use truecolor pixel array. TBB */ /*GD2_DBG(printf("%d...",x)); */ if (im->trueColor) { int p = im->tpixels[y][x]; chunkData[chunkLen++] = gdTrueColorGetAlpha (p); chunkData[chunkLen++] = gdTrueColorGetRed (p); chunkData[chunkLen++] = gdTrueColorGetGreen (p); chunkData[chunkLen++] = gdTrueColorGetBlue (p); } else { int p = im->pixels[y][x]; chunkData[chunkLen++] = p; } }; } else { for (x = xlo; x < xhi; x++) { /*GD2_DBG(printf("%d, ",x)); */ if (im->trueColor) { gdPutInt (im->tpixels[y][x], out); } else { gdPutC ((unsigned char) im->pixels[y][x], out); } }; }; /*GD2_DBG(printf("y=%d done.\n",y)); */ }; if (gd2_compressed (fmt)) { compLen = compMax; if (compress ((unsigned char *) &compData[0], &compLen, (unsigned char *) &chunkData[0], chunkLen) != Z_OK) { printf ("Error from compressing\n"); } else { chunkIdx[chunkNum].offset = gdTell (out); chunkIdx[chunkNum++].size = compLen; GD2_DBG (printf ("Chunk %d size %d offset %d\n", chunkNum, chunkIdx[chunkNum - 1].size, chunkIdx[chunkNum - 1].offset)); if (gdPutBuf (compData, compLen, out) <= 0) { fprintf(stderr, "gd write error\n"); }; }; }; }; }; if (gd2_compressed (fmt)) { /* Save the position, write the index, restore position (paranoia). */ GD2_DBG (printf ("Seeking %d to write index\n", idxPos)); posSave = gdTell (out); gdSeek (out, idxPos); GD2_DBG (printf ("Writing index\n")); for (x = 0; x < chunkNum; x++) { GD2_DBG (printf ("Chunk %d size %d offset %d\n", x, chunkIdx[x].size, chunkIdx[x].offset)); gdPutInt (chunkIdx[x].offset, out); gdPutInt (chunkIdx[x].size, out); }; /* We don't use fwrite for *endian reasons. */ /*fwrite(chunkIdx, sizeof(int)*2, chunkNum, out); */ gdSeek (out, posSave); }; /*printf("Memory block size is %d\n",gdTell(out)); */ fail: GD2_DBG (printf ("Freeing memory\n")); if (chunkData) { gdFree (chunkData); } if (compData) { gdFree (compData); } if (chunkIdx) { gdFree (chunkIdx); } GD2_DBG (printf ("Done\n")); }
BGD_DECLARE(void) gdImageJpegCtx(gdImagePtr im, gdIOCtx *outfile, int quality) { struct jpeg_compress_struct cinfo; struct jpeg_error_mgr jerr; int i, j, jidx; /* volatile so we can gdFree it on return from longjmp */ volatile JSAMPROW row = 0; JSAMPROW rowptr[1]; jmpbuf_wrapper jmpbufw; JDIMENSION nlines; char comment[255]; #ifdef JPEG_DEBUG gd_error_ex(GD_DEBUG, "gd-jpeg: gd JPEG version %s\n", GD_JPEG_VERSION); gd_error_ex(GD_DEBUG, "gd-jpeg: JPEG library version %d, %d-bit sample values\n", JPEG_LIB_VERSION, BITS_IN_JSAMPLE); if (!im->trueColor) { for(i = 0; i < im->colorsTotal; i++) { if(!im->open[i]) { gd_error_ex(GD_DEBUG, "gd-jpeg: gd colormap index %d: (%d, %d, %d)\n", i, im->red[i], im->green[i], im->blue[i]); } } } #endif /* JPEG_DEBUG */ memset(&cinfo, 0, sizeof(cinfo)); memset(&jerr, 0, sizeof(jerr)); cinfo.err = jpeg_std_error(&jerr); cinfo.client_data = &jmpbufw; if(setjmp(jmpbufw.jmpbuf) != 0) { /* we're here courtesy of longjmp */ if(row) { gdFree(row); } return; } cinfo.err->emit_message = jpeg_emit_message; cinfo.err->error_exit = fatal_jpeg_error; jpeg_create_compress(&cinfo); cinfo.image_width = im->sx; cinfo.image_height = im->sy; cinfo.input_components = 3; /* # of color components per pixel */ cinfo.in_color_space = JCS_RGB; /* colorspace of input image */ jpeg_set_defaults(&cinfo); cinfo.density_unit = 1; cinfo.X_density = im->res_x; cinfo.Y_density = im->res_y; if(quality >= 0) { jpeg_set_quality(&cinfo, quality, TRUE); } /* If user requests interlace, translate that to progressive JPEG */ if(gdImageGetInterlaced(im)) { #ifdef JPEG_DEBUG gd_error_ex(GD_DEBUG, "gd-jpeg: interlace set, outputting progressive JPEG image\n"); #endif jpeg_simple_progression(&cinfo); } jpeg_gdIOCtx_dest(&cinfo, outfile); row = (JSAMPROW)gdCalloc(1, cinfo.image_width * cinfo.input_components * sizeof(JSAMPLE)); if(row == 0) { gd_error("gd-jpeg: error: unable to allocate JPEG row structure: gdCalloc returns NULL\n"); jpeg_destroy_compress(&cinfo); return; } rowptr[0] = row; jpeg_start_compress(&cinfo, TRUE); sprintf(comment, "CREATOR: gd-jpeg v%s (using IJG JPEG v%d),", GD_JPEG_VERSION, JPEG_LIB_VERSION); if(quality >= 0) { sprintf (comment + strlen(comment), " quality = %d\n", quality); } else { strcat(comment + strlen(comment), " default quality\n"); } jpeg_write_marker(&cinfo, JPEG_COM, (unsigned char *) comment, (unsigned int)strlen(comment)); if(im->trueColor) { #if BITS_IN_JSAMPLE == 12 gd_error( "gd-jpeg: error: jpeg library was compiled for 12-bit\n" "precision. This is mostly useless, because JPEGs on the web are\n" "8-bit and such versions of the jpeg library won't read or write\n" "them. GD doesn't support these unusual images. Edit your\n" "jmorecfg.h file to specify the correct precision and completely\n" "'make clean' and 'make install' libjpeg again. Sorry.\n" ); goto error; #endif /* BITS_IN_JSAMPLE == 12 */ for(i = 0; i < im->sy; i++) { for(jidx = 0, j = 0; j < im->sx; j++) { int val = im->tpixels[i][j]; row[jidx++] = gdTrueColorGetRed(val); row[jidx++] = gdTrueColorGetGreen(val); row[jidx++] = gdTrueColorGetBlue(val); } nlines = jpeg_write_scanlines(&cinfo, rowptr, 1); if(nlines != 1) { gd_error("gd_jpeg: warning: jpeg_write_scanlines returns %u -- expected 1\n", nlines); } } } else { for(i = 0; i < im->sy; i++) { for(jidx = 0, j = 0; j < im->sx; j++) { int idx = im->pixels[i][j]; /* * NB: Although gd RGB values are ints, their max value is * 255 (see the documentation for gdImageColorAllocate()) * -- perfect for 8-bit JPEG encoding (which is the norm) */ #if BITS_IN_JSAMPLE == 8 row[jidx++] = im->red[idx]; row[jidx++] = im->green[idx]; row[jidx++] = im->blue[idx]; #elif BITS_IN_JSAMPLE == 12 row[jidx++] = im->red[idx] << 4; row[jidx++] = im->green[idx] << 4; row[jidx++] = im->blue[idx] << 4; #else #error IJG JPEG library BITS_IN_JSAMPLE value must be 8 or 12 #endif } nlines = jpeg_write_scanlines(&cinfo, rowptr, 1); if(nlines != 1) { gd_error("gd_jpeg: warning: jpeg_write_scanlines" " returns %u -- expected 1\n", nlines); } } } jpeg_finish_compress(&cinfo); jpeg_destroy_compress(&cinfo); gdFree(row); }
static inline int GetGreen(const uint32* rgba) { return gdTrueColorGetGreen(*rgba); }
static void gd_loadimage_ps(GVJ_t * job, usershape_t *us, boxf b, boolean filled) { gdImagePtr im = NULL; int X, Y, x, y, px; if ((im = gd_loadimage(job, us))) { X = im->sx; Y = im->sy; gvputs(job, "save\n"); /* define image data as string array (one per raster line) */ gvputs(job, "/myctr 0 def\n"); gvputs(job, "/myarray [\n"); if (im->trueColor) { for (y = 0; y < Y; y++) { gvputs(job, "<"); for (x = 0; x < X; x++) { px = gdImageTrueColorPixel(im, x, y); gvprintf(job, "%02x%02x%02x", gdTrueColorGetRed(px), gdTrueColorGetGreen(px), gdTrueColorGetBlue(px)); } gvputs(job, ">\n"); } } else { for (y = 0; y < Y; y++) { gvputs(job, "<"); for (x = 0; x < X; x++) { px = gdImagePalettePixel(im, x, y); gvprintf(job, "%02x%02x%02x", im->red[px], im->green[px], im->blue[px]); } gvputs(job, ">\n"); } } gvputs(job, "] def\n"); gvputs(job,"/myproc { myarray myctr get /myctr myctr 1 add def } def\n"); /* this sets the position of the image */ gvprintf(job, "%g %g translate\n", (b.LL.x + (b.UR.x - b.LL.x) * (1. - (job->dpi.x) / 96.) / 2.), (b.LL.y + (b.UR.y - b.LL.y) * (1. - (job->dpi.y) / 96.) / 2.)); /* this sets the rendered size to fit the box */ gvprintf(job,"%g %g scale\n", ((b.UR.x - b.LL.x) * (job->dpi.x) / 96.), ((b.UR.y - b.LL.y) * (job->dpi.y) / 96.)); /* xsize ysize bits-per-sample [matrix] */ gvprintf(job, "%d %d 8 [%d 0 0 %d 0 %d]\n", X, Y, X, -Y, Y); gvputs(job, "{myproc} false 3 colorimage\n"); gvputs(job, "restore\n"); } }
void gdImageCopyResampled(gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int srcX, int srcY, int dstW, int dstH, int srcW, int srcH) { int x, y; double sy1, sy2, sx1, sx2; for (y = dstY; y < dstY + dstH; y++) { sy1 = ((double)y - (double)dstY) * (double)srcH / (double)dstH; sy2 = ((double)(y + 1) - (double)dstY) * (double)srcH / (double)dstH; for (x = dstX; x < dstX + dstW; x++) { double sx, sy; double spixels = 0; double red = 0.0, green = 0.0, blue = 0.0, alpha = 0.0; sx1 = ((double)x - (double)dstX) * (double)srcW / (double)dstW; sx2 = ((double)(x + 1) - (double)dstX) * (double)srcW / (double)dstW; sy = sy1; do { double yportion; if (floor2(sy) == floor2(sy1)) { yportion = 1.0 - (sy - floor2(sy)); if (yportion > sy2 - sy1) yportion = sy2 - sy1; sy = floor2(sy); } else if (sy == floor2(sy2)) { yportion = sy2 - floor2(sy2); } else { yportion = 1.0; } sx = sx1; do { double xportion; double pcontribution; int p; if (floor2(sx) == floor2(sx1)) { xportion = 1.0 - (sx - floor2(sx)); if (xportion > sx2 - sx1) xportion = sx2 - sx1; sx = floor2 (sx); } else if (sx == floor2(sx2)) { xportion = sx2 - floor2(sx2); } else { xportion = 1.0; } pcontribution = xportion * yportion; p = gdImageGetTrueColorPixel(src, (int)sx + srcX, (int)sy + srcY); red += gdTrueColorGetRed (p) * pcontribution; green += gdTrueColorGetGreen (p) * pcontribution; blue += gdTrueColorGetBlue (p) * pcontribution; spixels += xportion * yportion; sx += 1.0; } while (sx < sx2); sy += 1.0; } while (sy < sy2); if (spixels != 0.0) { red /= spixels; green /= spixels; blue /= spixels; } if (red > 255.0) red = 255.0; if (green > 255.0) green = 255.0; if (blue > 255.0) blue = 255.0; gdImageSetPixel(dst, x, y, gdTrueColorAlpha((int)red, (int)green, (int)blue, 0)); } } }
static int puzzle_getview_from_gdimage(PuzzleContext * const context, PuzzleView * const view, gdImagePtr gdimage) { unsigned int x, y; const unsigned int x0 = 0U, y0 = 0U; unsigned int x1, y1; unsigned char *maptr; int pixel; view->map = NULL; view->width = (unsigned int) gdImageSX(gdimage); view->height = (unsigned int) gdImageSY(gdimage); view->sizeof_map = (size_t) (view->width * view->height); if (view->width > context->puzzle_max_width || view->height > context->puzzle_max_height) { return -1; } if (view->sizeof_map <= (size_t) 0U || INT_MAX / view->width < view->height || SIZE_MAX / view->width < view->height || (unsigned int) view->sizeof_map != view->sizeof_map) { puzzle_err_bug(__FILE__, __LINE__); } x1 = view->width - 1U; y1 = view->height - 1U; if (view->width <= 0U || view->height <= 0U) { puzzle_err_bug(__FILE__, __LINE__); } if ((view->map = calloc(view->sizeof_map, sizeof *view->map)) == NULL) { return -1; } if (x1 > INT_MAX || y1 > INT_MAX) { /* GD uses "int" for coordinates */ puzzle_err_bug(__FILE__, __LINE__); } maptr = view->map; x = x1; if (gdImageTrueColor(gdimage) != 0) { do { y = y1; do { pixel = gdImageGetTrueColorPixel(gdimage, (int) x, (int) y); *maptr++ = (unsigned char) ((gdTrueColorGetRed(pixel) * 77 + gdTrueColorGetGreen(pixel) * 151 + gdTrueColorGetBlue(pixel) * 28 + 128) / 256); } while (y-- != y0); } while (x-- != x0); } else { do { y = y1; do { pixel = gdImagePalettePixel(gdimage, x, y); *maptr++ = (unsigned char) ((gdimage->red[pixel] * 77 + gdimage->green[pixel] * 151 + gdimage->blue[pixel] * 28 + 128) / 256); } while (y-- != y0); } while (x-- != x0); } return 0; }
/* For 32 bit source only. */ void BitmapScale32 (uint8_t *dst, int dstW, int dstH, const uint8_t *src, int iDeltaLine, int srcW, int srcH) { int x, y; for (y = 0; y < dstH; y++) { FIXEDPOINT sy1 = INT_TO_FIXEDPOINT(y * srcH) / dstH; FIXEDPOINT sy2 = INT_TO_FIXEDPOINT((y + 1) * srcH) / dstH; for (x = 0; x < dstW; x++) { FIXEDPOINT red = 0, green = 0, blue = 0; FIXEDPOINT sx1 = INT_TO_FIXEDPOINT(x * srcW) / dstW; FIXEDPOINT sx2 = INT_TO_FIXEDPOINT((x + 1) * srcW) / dstW; FIXEDPOINT spixels = (sx2 - sx1) * (sy2 - sy1); FIXEDPOINT sy = sy1; do { FIXEDPOINT yportion; if (FIXEDPOINT_FLOOR (sy) == FIXEDPOINT_FLOOR (sy1)) { yportion = INT_TO_FIXEDPOINT(1) - FIXEDPOINT_FRACTION(sy); if (yportion > sy2 - sy1) { yportion = sy2 - sy1; } sy = FIXEDPOINT_FLOOR (sy); } else if (sy == FIXEDPOINT_FLOOR (sy2)) { yportion = FIXEDPOINT_FRACTION(sy2); } else { yportion = INT_TO_FIXEDPOINT(1); } const uint8_t *pu8SrcLine = src + iDeltaLine * FIXEDPOINT_TO_INT(sy); FIXEDPOINT sx = sx1; do { FIXEDPOINT xportion; FIXEDPOINT pcontribution; int p; if (FIXEDPOINT_FLOOR (sx) == FIXEDPOINT_FLOOR (sx1)) { xportion = INT_TO_FIXEDPOINT(1) - FIXEDPOINT_FRACTION(sx); if (xportion > sx2 - sx1) { xportion = sx2 - sx1; } pcontribution = xportion * yportion; sx = FIXEDPOINT_FLOOR (sx); } else if (sx == FIXEDPOINT_FLOOR (sx2)) { xportion = FIXEDPOINT_FRACTION(sx2); pcontribution = xportion * yportion; } else { xportion = INT_TO_FIXEDPOINT(1); pcontribution = xportion * yportion; } /* Color depth specific code begin */ p = *(uint32_t *)(pu8SrcLine + FIXEDPOINT_TO_INT(sx) * 4); /* Color depth specific code end */ red += gdTrueColorGetRed (p) * pcontribution; green += gdTrueColorGetGreen (p) * pcontribution; blue += gdTrueColorGetBlue (p) * pcontribution; sx += INT_TO_FIXEDPOINT(1); } while (sx < sx2); sy += INT_TO_FIXEDPOINT(1); } while (sy < sy2); if (spixels != 0) { red /= spixels; green /= spixels; blue /= spixels; } /* Clamping to allow for rounding errors above */ if (red > 255) { red = 255; } if (green > 255) { green = 255; } if (blue > 255) { blue = 255; } gdImageSetPixel (dst, x, y, ( ((int) red) << 16) + (((int) green) << 8) + ((int) blue), dstW); } } }
//Builds audio scan data for the Robot 36. //Applicable to only Robot 36. void buildaudio_r36 () { uint16_t x , y , k ; uint32_t pixel1, pixel2; uint8_t r1, g1, b1, r2, g2, b2, avgr, avgg, avgb; uint8_t y1[320], y2[320], ry[320], by[320]; printf( "Adding image to audio data.\n" ) ; for ( y=0 ; y<240 ; y+=2 ) { // read image data for ( x=0 ; x<320 ; x++ ) { //even line pixel pixel1 = gdImageGetTrueColorPixel( g_imgp, x, y ) ; //odd line pixel pixel2 = gdImageGetTrueColorPixel( g_imgp, x, y+1) ; // get color data r1 = gdTrueColorGetRed( pixel1 ); //first line (even) of red intensities r2 = gdTrueColorGetRed( pixel2 ); //second line (odd) of red intensities g1 = gdTrueColorGetGreen( pixel1 ); g2 = gdTrueColorGetGreen( pixel2 ); b1 = gdTrueColorGetBlue( pixel1 ); b2 = gdTrueColorGetBlue( pixel2 ); avgr = (uint8_t)( ((uint16_t)r1 + (uint16_t)r2) / 2 ); avgg = (uint8_t)( ((uint16_t)g1 + (uint16_t)g2) / 2 ); avgb = (uint8_t)( ((uint16_t)b1 + (uint16_t)b2) / 2 ); //Y value of even lines y1[x] = 16.0 + (0.003906 * ((65.738 * (float)r1) + (129.057 * (float)g1) + (25.064 * (float)b1))); //Y value of odd lines y2[x] = 16.0 + (0.003906 * ((65.738 * (float)r2) + (129.057 * (float)g2) + (25.064 * (float)b2))); //R-Y value of the average of the two lines, to be transmitted even scans ry[x] = 128.0 + (0.003906 * ((112.439 * (float)avgr) + (-94.154 * (float)avgg) + (-18.285 * (float)avgb))); //B-Y value of the average of the two lines, to be transmitted odd scans by[x] = 128.0 + (0.003906 * ((-37.945 * (float)avgr) + (-74.494 * (float)avgg) + (112.439 * (float)avgb))); } //begin robot 36 code //even lines //sync playtone( 1200 , 9000 ) ; //porch playtone( 1500 , 3000 ) ; //y scan, even, 88ms total, 320 points, 275us per pixel for ( k = 0; k < 320; k++ ) { playtone( toneval_yuv( y1[k] ) , 275 ) ; } //even line seperator playtone( 1500 , 4500 ); //porch playtone( 1900 , 1500 ); //R-Y scan, 44ms total, 320 points, 137.5us per pixel for ( k = 0; k < 320; k++ ) { playtone( toneval_yuv( ry[k] ) , 137.5 ); } //odd lines // sync playtone( 1200 , 9000 ) ; // porch playtone( 1500 , 3000 ) ; //y scan, odd, 88ms total, 320 points, 275us per pixel for ( k = 0; k < 320; k++ ) { playtone( toneval_yuv( y2[k] ) , 275 ) ; } //odd line seperator playtone( 2300 , 4500 ); //porch playtone( 1900 , 1500 ); //B-Y scan, 44ms total, 320 points, 137.5us per pixel for ( k = 0; k < 320; k++) { playtone( toneval_yuv( by[k] ) , 137.5); } } // end for y printf( "Done adding image to audio data.\n" ) ; } // end buildaudio_r36
void gdImageCopyResampled (uint8_t *dst, uint8_t *src, int dstX, int dstY, int srcX, int srcY, int dstW, int dstH, int srcW, int srcH) { int x, y; double sy1, sy2, sx1, sx2; for (y = dstY; (y < dstY + dstH); y++) { sy1 = ((double) y - (double) dstY) * (double) srcH / (double) dstH; sy2 = ((double) (y + 1) - (double) dstY) * (double) srcH / (double) dstH; for (x = dstX; (x < dstX + dstW); x++) { double sx, sy; double spixels = 0; double red = 0.0, green = 0.0, blue = 0.0, alpha = 0.0; sx1 = ((double) x - (double) dstX) * (double) srcW / dstW; sx2 = ((double) (x + 1) - (double) dstX) * (double) srcW / dstW; sy = sy1; do { double yportion; if (floor2 (sy) == floor2 (sy1)) { yportion = 1.0 - (sy - (double)floor2 (sy)); if (yportion > sy2 - sy1) { yportion = sy2 - sy1; } sy = (double)floor2 (sy); } else if (sy == floor2 (sy2)) { yportion = sy2 - (double)floor2 (sy2); } else { yportion = 1.0; } sx = sx1; do { double xportion; double pcontribution; int p; if (floor2 (sx) == floor2 (sx1)) { xportion = 1.0 - (sx - (double)floor2 (sx)); if (xportion > sx2 - sx1) { xportion = sx2 - sx1; } sx = (double)floor2 (sx); } else if (sx == floor2 (sx2)) { xportion = sx2 - (double)floor2 (sx2); } else { xportion = 1.0; } pcontribution = xportion * yportion; /* 2.08: previously srcX and srcY were ignored. Andrew Pattison */ p = gdImageGetTrueColorPixel (src, (int) sx + srcX, (int) sy + srcY, srcW); red += gdTrueColorGetRed (p) * pcontribution; green += gdTrueColorGetGreen (p) * pcontribution; blue += gdTrueColorGetBlue (p) * pcontribution; alpha += gdTrueColorGetAlpha (p) * pcontribution; spixels += xportion * yportion; sx += 1.0; } while (sx < sx2); sy += 1.0; } while (sy < sy2); if (spixels != 0.0) { red /= spixels; green /= spixels; blue /= spixels; alpha /= spixels; } /* Clamping to allow for rounding errors above */ if (red > 255.0) { red = 255.0; } if (green > 255.0) { green = 255.0; } if (blue > 255.0) { blue = 255.0; } if (alpha > gdAlphaMax) { alpha = gdAlphaMax; } gdImageSetPixel (dst, x, y, gdTrueColorAlpha ((int) red, (int) green, (int) blue, (int) alpha), dstW); } } }