int pngtofile(FILE *fin, FILE *fout) { gdImagePtr im = gdImageCreateFromPng(fin); int c = gdImageGetPixel(im, gdImageSX(im) - 1, gdImageSY(im) - 1); int data_size = (gdImageRed(im, c) << 8*2) + (gdImageGreen(im, c) << 8*1) + (gdImageBlue(im, c)); unsigned char buf[3]; long written_bytes = 0; int x, y; int nb = 0; for(y = 0; y < gdImageSY(im); y++) { for(x = 0; x < gdImageSX(im); x++) { c = gdImageGetPixel(im, x, y); buf[0] = gdImageRed(im, c); buf[1] = gdImageGreen(im, c); buf[2] = gdImageBlue(im, c); if(written_bytes >= data_size) { break; /* FIXME */ } else { nb = written_bytes + 3 > data_size ? data_size - written_bytes : 3; written_bytes += fwrite(buf, 1, nb, fout); } } } gdImageDestroy(im); return 1; }
/* * This is a very simple and experimental deinterlacer, * there is a lot of room for improvement here. * * (For example: Making it work) * */ gdImage *fx_deinterlace(gdImage *src, char *options) { int x, y; MSG("Deinterlacing image."); for(y = 1; y < gdImageSY(src) - 1; y += 2) { for(x = 0; x < gdImageSX(src); x++) { int c, cu, cd, d; c = gdImageGetPixel(src, x, y); cu = gdImageGetPixel(src, x, y - 1); cd = gdImageGetPixel(src, x, y + 1); /* Calculate the difference of the pixel (x,y) from * the average of it's neighbours above and below. */ d = 0xFF - abs(GREY(cu) - GREY(cd)); d = (abs(GREY(cu) - (0xFF - GREY(c)) - GREY(cd)) * d) / 0xFF; c = RGBMIX(c, RGBMIX(cu, cd, 128), d); gdImageSetPixel(src, x, y, c); } } return(src); }
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; }
gdImagePtr imagefilter_difference(gdImagePtr img1, gdImagePtr img2) { int x, y, c1, c2, r, g, b, np; int sx = img1->sx < img2->sx ? img1->sx : img2->sx; int sy = img1->sy < img2->sy ? img1->sy : img2->sy; gdImagePtr img = gdImageCreateTrueColor(sx, sy); for (y = 0; y < sy; y++) for (x = 0; x < sx; x++) { c1 = gdImageGetPixel(img1, x, y); c2 = gdImageGetPixel(img2, x, y); r = gdImageRed(img1, c1) - gdImageRed(img2, c2); g = gdImageGreen(img1, c1) - gdImageGreen(img2, c2); b = gdImageBlue(img1, c1) - gdImageBlue(img2, c2); np = gdImageColorAllocate(img, r < 0 ? -r : r, g < 0 ? -g : g, b < 0 ? -b : b ); gdImageSetPixel(img, x, y, np); } return img; }
int main(int argc, char *argv[]){ FILE *fp; gdImagePtr im1, im2, imOut; int i,x,y,xsize,ysize,c; int c1,c2; if(argc != 4){ fprintf(stderr, "[%s] compiled [%s/%s %s]\n", argv[0], __DATE__, __TIME__, DIRE); fprintf(stderr, "Usage : %s in-gif1 in-gif2 out-gif\n", argv[0]); exit(1); } im1 = fromGif(argv[1]); im2 = fromGif(argv[2]); outfile = argv[3]; xsize = gdImageSX(im1); ysize = gdImageSY(im2); imOut = gdImageCreate(xsize, ysize); for(i=0; i<gdImageColorsTotal(im1); i++){ int r,g,b; r = gdImageRed( im1, i); g = gdImageGreen(im1, i); b = gdImageBlue( im1, i); } for(y=0; y<ysize; y++){ int r,g,b; for(x=0; x<xsize; x++){ c1 = gdImageGetPixel(im1, x, y); c2 = gdImageGetPixel(im2, x, y); r = gdImageRed( im1, c1) - gdImageRed( im2, c2); g = gdImageGreen(im1, c1) - gdImageGreen(im2, c2); b = gdImageBlue( im1, c1) - gdImageBlue( im2, c2); r = (r + 256) % 256; g = (g + 256) % 256; b = (b + 256) % 256; c = allocOrExact(imOut, r, g, b); gdImageSetPixel(imOut, x, y, c); } } gdImageDestroy(im1); gdImageDestroy(im2); fp = fopen(outfile, "wb"); if(!fp){ fprintf(stderr, "Can't open [%s]\n", outfile); exit(1); } gdImageGif(imOut, fp); fclose(fp); gdImageDestroy(imOut); return 0; }
BGD_DECLARE(int) gdImagePixelate(gdImagePtr im, int block_size, const unsigned int mode) { int x, y; if (block_size <= 0) { return 0; } else if (block_size == 1) { return 1; } switch (mode) { case GD_PIXELATE_UPPERLEFT: for (y = 0; y < im->sy; y += block_size) { for (x = 0; x < im->sx; x += block_size) { if (gdImageBoundsSafe(im, x, y)) { int c = gdImageGetPixel(im, x, y); gdImageFilledRectangle(im, x, y, x + block_size - 1, y + block_size - 1, c); } } } break; case GD_PIXELATE_AVERAGE: for (y = 0; y < im->sy; y += block_size) { for (x = 0; x < im->sx; x += block_size) { int a, r, g, b, c; int total; int cx, cy; a = r = g = b = c = total = 0; /* sampling */ for (cy = 0; cy < block_size; cy++) { for (cx = 0; cx < block_size; cx++) { if (!gdImageBoundsSafe(im, x + cx, y + cy)) { continue; } c = gdImageGetPixel(im, x + cx, y + cy); a += gdImageAlpha(im, c); r += gdImageRed(im, c); g += gdImageGreen(im, c); b += gdImageBlue(im, c); total++; } } /* drawing */ if (total > 0) { c = gdImageColorResolveAlpha(im, r / total, g / total, b / total, a / total); gdImageFilledRectangle(im, x, y, x + block_size - 1, y + block_size - 1, c); } } } break; default: return 0; } return 1; }
/* 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 draw_image(OLED64x48 *oled, gdImagePtr image) { uint8_t pages[HEIGHT / 8][WIDTH]; int row, column, bit, index; // Convert pixels into pages for (row = 0; row < HEIGHT / 8; row++) { for (column = 0; column < WIDTH; column++) { pages[row][column] = 0; for (bit = 0; bit < 8; bit++) { index = gdImageGetPixel(image, column, (row * 8) + bit); if (gdImageRed(image, index) > 0) { pages[row][column] |= 1 << bit; } } } } // Write all pages oled_64x48_new_window(oled, 0, WIDTH - 1, 0, HEIGHT / 8 - 1); for (row = 0; row < HEIGHT / 8; row++) { oled_64x48_write(oled, pages[row]); } }
static void gdImageBrushApply(gdImagePtr im, int x, int y) { int lx, ly; int hy; int hx; int x1, y1, x2, y2; int srcx, srcy; if (!im->brush) { return; } hy = gdImageSY(im->brush)/2; y1 = y - hy; y2 = y1 + gdImageSY(im->brush); hx = gdImageSX(im->brush)/2; x1 = x - hx; x2 = x1 + gdImageSX(im->brush); srcy = 0; for (ly = y1; (ly < y2); ly++) { srcx = 0; for (lx = x1; (lx < x2); lx++) { int p; p = gdImageGetPixel(im->brush, srcx, srcy); /* Allow for non-square brushes! */ if (p != gdImageGetTransparent(im->brush)) { gdImageSetPixel(im, lx, ly, im->brushColorMap[p]); } srcx++; } srcy++; } }
int main() { gdImagePtr im; int bordercolor, color; im = gdImageCreateTrueColor(100, 100); gdImageAlphaBlending(im, 1); gdImageSaveAlpha(im, 1); bordercolor = gdImageColorAllocateAlpha(im, 0, 0, 0, 2); color = gdImageColorAllocateAlpha(im, 0, 0, 0, 1); gdImageFillToBorder(im, 5, 5, bordercolor, color); color = gdImageGetPixel(im, 5, 5); gdImageDestroy(im); if (gdTestAssert(color==0x1000000)) { return 0; } else { printf("c: %X, expected %X\n", color, 0x1000000); return -1; } }
int fswc_add_image_jpeg(src_t *src, avgbmp_t *abitmap) { uint32_t x, y, hlength; uint8_t *himg = NULL; gdImage *im; int i; /* MJPEG data may lack the DHT segment required for decoding... */ i = verify_jpeg_dht(src->img, src->length, &himg, &hlength); im = gdImageCreateFromJpegPtr(hlength, himg); if(i == 1) free(himg); if(!im) return(-1); for(y = 0; y < src->height; y++) for(x = 0; x < src->width; x++) { int c = gdImageGetPixel(im, x, y); *(abitmap++) += (c & 0xFF0000) >> 16; *(abitmap++) += (c & 0x00FF00) >> 8; *(abitmap++) += (c & 0x0000FF); } gdImageDestroy(im); return(0); }
unsigned char *readpng(const char *filename,int *width, int *height){ FILE *file; gdImagePtr image; unsigned char *dataptr,*dptr; int i,j; unsigned int intrgb; file = fopen(filename, "rb"); if(file == NULL)return NULL; image = gdImageCreateFromPng(file); fclose(file); *width=gdImageSX(image); *height=gdImageSY(image); if( NewMemory((void **)&dataptr,(unsigned int)(4*(*width)*(*height)) )==0){ gdImageDestroy(image); return NULL; } dptr=dataptr; for (i = 0; i<*height; i++){ for(j=0;j<*width;j++){ intrgb=(unsigned int)gdImageGetPixel(image,j,(unsigned int)(*height-(1+i))); *dptr++ = (intrgb>>16)&255; *dptr++ = (intrgb>>8)&255; *dptr++ = intrgb&255; *dptr++ = 0xff; } } gdImageDestroy(image); return dataptr; }
/* gdImageWBMPCtx * -------------- * Write the image as a wbmp file * Parameters are: * image: gd image structure; * fg: the index of the foreground color. any other value will be * considered as background and will not be written * out: the stream where to write */ BGD_DECLARE(void) gdImageWBMPCtx(gdImagePtr image, int fg, gdIOCtx *out) { int x, y, pos; Wbmp *wbmp; /* create the WBMP */ if((wbmp = createwbmp(gdImageSX(image), gdImageSY(image), WBMP_WHITE)) == NULL) { fprintf(stderr, "Could not create WBMP\n"); return; } /* fill up the WBMP structure */ pos = 0; for(y = 0; y < gdImageSY(image); y++) { for(x = 0; x < gdImageSX(image); x++) { if(gdImageGetPixel(image, x, y) == fg) { wbmp->bitmap[pos] = WBMP_BLACK; } pos++; } } /* write the WBMP to a gd file descriptor */ if(writewbmp(wbmp, &gd_putout, out)) { fprintf(stderr, "Could not save WBMP\n"); } /* des submitted this bugfix: gdFree the memory. */ freewbmp(wbmp); }
result_t Image::getPixel(int32_t x, int32_t y, int32_t &retVal) { if (!m_image) return CHECK_ERROR(CALL_E_INVALID_CALL); retVal = gdImageGetPixel(m_image, x, y); return 0; }
/* {{{ gdImageXbmCtx */ void gdImageXbmCtx(gdImagePtr image, char* file_name, int fg, gdIOCtx * out) { int x, y, c, b, sx, sy, p; char *name, *f; size_t i, l; name = file_name; if ((f = strrchr(name, '/')) != NULL) name = f+1; if ((f = strrchr(name, '\\')) != NULL) name = f+1; name = estrdup(name); if ((f = strrchr(name, '.')) != NULL && !strcasecmp(f, ".XBM")) *f = '\0'; if ((l = strlen(name)) == 0) { efree(name); name = estrdup("image"); } else { for (i=0; i<l; i++) { /* only in C-locale isalnum() would work */ if (!isupper(name[i]) && !islower(name[i]) && !isdigit(name[i])) { name[i] = '_'; } } } gdCtxPrintf(out, "#define %s_width %d\n", name, gdImageSX(image)); gdCtxPrintf(out, "#define %s_height %d\n", name, gdImageSY(image)); gdCtxPrintf(out, "static unsigned char %s_bits[] = {\n ", name); efree(name); b = 1; p = 0; c = 0; sx = gdImageSX(image); sy = gdImageSY(image); for (y = 0; y < sy; y++) { for (x = 0; x < sx; x++) { if (gdImageGetPixel(image, x, y) == fg) { c |= b; } if ((b == 128) || (x == sx - 1)) { b = 1; if (p) { gdCtxPrintf(out, ", "); if (!(p%12)) { gdCtxPrintf(out, "\n "); p = 12; } } p++; gdCtxPrintf(out, "0x%02X", c); c = 0; } else { b <<= 1; } } } gdCtxPrintf(out, "};\n"); }
gdImage *fx_rotate(gdImage *src, char *options) { int x, y; gdImage *im; int angle = atoi(options); /* Restrict angle to 0-360 range. */ if((angle %= 360) < 0) angle += 360; /* Round to nearest right angle. */ x = (angle + 45) % 360; angle = x - (x % 90); /* Not rotating 0 degrees. */ if(angle == 0) { MSG("Not rotating 0 degrees."); return(src); } /* 180 can be done with two flips. */ if(angle == 180) { fx_flip(src, "h,v"); return(src); } MSG("Rotating image %i degrees. %ix%i -> %ix%i.", angle, gdImageSX(src), gdImageSY(src), gdImageSY(src), gdImageSX(src)); /* Create rotated image. */ im = gdImageCreateTrueColor(gdImageSY(src), gdImageSX(src)); if(!im) { WARN("Out of memory."); return(src); } for(y = 0; y < gdImageSY(src); y++) for(x = 0; x < gdImageSX(src); x++) { int c = gdImageGetPixel(src, x, y); if(angle == 90) { gdImageSetPixel(im, gdImageSX(im) - y - 1, x, c); } else { gdImageSetPixel(im, y, gdImageSY(im) - x - 1, c); } } gdImageDestroy(src); return(im); }
uint8_t LcdWriteImagePng(LcdSpi* lcd, const char *filename) { uint16_t x = 100; uint16_t y = 100; uint16_t xm = 0; uint16_t ym = 0; uint16_t i = 0; int c; gdImagePtr img; FILE *filePtr; uint8_t b; if (!lcd) { return 1; } if (!filename) { return 2; } filePtr = fopen(filename, "rb"); if (filePtr == NULL) { return 3; } img = gdImageCreateFromPng(filePtr); if (img == NULL) { return 4; } xm = gdImageSX(img); ym = gdImageSY(img); printf("%i x %i\n", xm, ym); if (xm != 132 || ym != 32) { return 5; } for (y = 0; y < ym; y += 8) { LcdSetPos(lcd, y / 8, 0); GpioSetValue(PIN_LCD_A0, 1); for (x = 0; x < xm; x++) { b = 0; for (i = 0; i < 8; i++) { c = gdImageGetPixel(img, x, y + i); if (img->red[c] < 0x7f || img->green[c] < 0x7f || img->blue[c] < 0x7f) { //if (c) { b |= (1 << i); } } LcdWriteByte(lcd->mS0, b); } } fclose(filePtr); gdImageDestroy(img); return 0; }
void hqx_scale(gdImagePtr srcIm, int factor, gdImagePtr* result) { gdImagePtr dstIm; uint32_t *srcBuffer, *dstBuffer; size_t srcSize, dstSize; uint32_t w, h; uint32_t x, y; w = gdImageSX(srcIm); h = gdImageSY(srcIm); srcSize = sizeof(uint32_t) * w * h; dstSize = sizeof(uint32_t) * factor * w * factor * h; // Unfortunately it doesn't work to simply pass the gd buffer from the // gdImage struct to hqx, and the gd documentation explicitly says // not to access member fields directly. Thus we allocate two buffers // and copy the data back and forth as a workaround. srcBuffer = (uint32_t*)emalloc(srcSize); for (y = 0; y < h; y++) { for (x = 0; x < w; x++) { srcBuffer[w*y + x] = gdImageGetPixel(srcIm, x, y); } } dstBuffer = (uint32_t*)emalloc(dstSize); if (factor == 4) { hq4x_32_rb(srcBuffer, sizeof(uint32_t)*w, dstBuffer, sizeof(uint32_t)*factor*w, w, h); } else if (factor == 3) { hq3x_32_rb(srcBuffer, sizeof(uint32_t)*w, dstBuffer, sizeof(uint32_t)*factor*w, w, h); } else if (factor == 2) { hq2x_32_rb(srcBuffer, sizeof(uint32_t)*w, dstBuffer, sizeof(uint32_t)*factor*w, w, h); } dstIm = gdImageCreateTrueColor(factor*w, factor*h); gdImageAlphaBlending(dstIm, 0); gdImageSaveAlpha(dstIm, 1); for (y = 0; y < factor*h; y++) { for (x = 0; x < factor*w; x++) { gdImageSetPixel(dstIm, x, y, dstBuffer[factor*w*y + x]); } } efree(srcBuffer); efree(dstBuffer); *result = dstIm; }
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; }
result_t Image::rotate(int32_t dir) { if (dir != gd_base::_LEFT && dir != gd_base::_RIGHT) return CHECK_ERROR(CALL_E_INVALIDARG); int32_t sx = gdImageSX(m_image); int32_t sy = gdImageSY(m_image); int32_t i, j; gdImagePtr newImage; if (gdImageTrueColor(m_image)) newImage = gdImageCreateTrueColor(sy, sx); else { newImage = gdImageCreate(sy, sx); gdImagePaletteCopy(newImage, m_image); } gdImageColorTransparent(newImage, gdImageGetTransparent(m_image)); if (dir == gd_base::_LEFT) { for (i = 0; i < sx; i++) for (j = 0; j < sy; j++) gdImageSetPixel(newImage, j, sx - i - 1, gdImageGetPixel(m_image, i, j)); } else { for (i = 0; i < sx; i++) for (j = 0; j < sy; j++) gdImageSetPixel(newImage, sy - j - 1, i, gdImageGetPixel(m_image, i, j)); } setExtMemory(-1); gdImageDestroy(m_image); m_image = newImage; setExtMemory(); return 0; }
JBoolean PadColormap ( gdImagePtr image ) { JSize colorCount = gdImageColorsTotal(image); if (colorCount >= kMinColorCount) { return kJFalse; } const JSize extraColorCount = kMinColorCount - colorCount; int x = gdImageSX(image) - extraColorCount; if (x < 0) { cerr << "image is too small to fit extra colors on single raster line" << endl; exit(1); } int y = gdImageSY(image) - 1; int c = gdImageGetPixel(image, x,y); int r = gdImageRed (image, c); int g = gdImageGreen(image, c); int b = gdImageBlue (image, c); int delta = -1; if (r < 127 || g < 127 || b < 127) { delta = +1; } for (JIndex i=1; i<=extraColorCount; i++) { assert( x < gdImageSX(image) ); while ((c = gdImageColorExact(image, r,g,b)) != -1) { r = JMax(0, JMin(r + delta, 255)); g = JMax(0, JMin(g + delta, 255)); b = JMax(0, JMin(b + delta, 255)); } c = gdImageColorAllocate(image, r,g,b); assert( c != -1 ); gdImageSetPixel(image, x,y, c); x++; } return kJTrue; }
int main() { gdImagePtr im; int black, white; int error = 0; int xs = 300, xe = 350, i; im = gdImageCreateTrueColor(400, 200); if (im == NULL) { gdTestErrorMsg("gdImageCreate failed.\n"); return 1; } white = gdImageColorAllocate(im, 255,255,255); black = gdImageColorAllocate(im, 0,0,0); gdImageFilledRectangle(im, 0, 0, 400,200, white); gdImageRectangle(im, xs, 95, xe, 95, black); for (i = xs; i <= xe; i++) { int c = gdImageGetPixel(im, i, 94); if (c != white) { error |=1; } } for (i = xs; i <= xe; i++) { int c = gdImageGetPixel(im, i, 95); if (c != black) { error |=1; } } for (i = xs; i <= xe; i++) { int c = gdImageGetPixel(im, i, 96); if (c != white) { error |=1; } } gdImageDestroy(im); return error; }
/* This algorithm comes from pnmcrop (http://netpbm.sourceforge.net/) * Three steps: * - if 3 corners are equal. * - if two are equal. * - Last solution: average the colors */ static int gdGuessBackgroundColorFromCorners(gdImagePtr im, int *color) { const int tl = gdImageGetPixel(im, 0, 0); const int tr = gdImageGetPixel(im, gdImageSX(im) - 1, 0); const int bl = gdImageGetPixel(im, 0, gdImageSY(im) -1); const int br = gdImageGetPixel(im, gdImageSX(im) - 1, gdImageSY(im) -1); if (tr == bl && tr == br) { *color = tr; return 3; } else if (tl == bl && tl == br) { *color = tl; return 3; } else if (tl == tr && tl == br) { *color = tl; return 3; } else if (tl == tr && tl == bl) { *color = tl; return 3; } else if (tl == tr || tl == bl || tl == br) { *color = tl; return 2; } else if (tr == bl || tr == br) { *color = tr; return 2; } else if (br == bl) { *color = bl; return 2; } else { register int r,b,g,a; r = (int)(0.5f + (gdImageRed(im, tl) + gdImageRed(im, tr) + gdImageRed(im, bl) + gdImageRed(im, br)) / 4); g = (int)(0.5f + (gdImageGreen(im, tl) + gdImageGreen(im, tr) + gdImageGreen(im, bl) + gdImageGreen(im, br)) / 4); b = (int)(0.5f + (gdImageBlue(im, tl) + gdImageBlue(im, tr) + gdImageBlue(im, bl) + gdImageBlue(im, br)) / 4); a = (int)(0.5f + (gdImageAlpha(im, tl) + gdImageAlpha(im, tr) + gdImageAlpha(im, bl) + gdImageAlpha(im, br)) / 4); *color = gdImageColorClosestAlpha(im, r, g, b, a); return 0; } }
int v4lDecodeImage(v4lT* s, v4lBufT* decoded, v4lBufT* encoded, int w, int h, bool scale) { if( s->fmt.fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV ) { if( scale ) fprintf(stderr, "Warning: scaling YUYV input is not implemented yet, will pad instead\n"); if( decoded->length < encoded->length*2 ) fprintf(stderr, "Decode buffer is too small to hold the decoded image\n"); //v4lDecodeYUYV uses w/h of encoded/decoded to calculate padding (if necessary) encoded->w = s->fmt.fmt.pix.width; encoded->h = s->fmt.fmt.pix.height; decoded->w = w ? w : encoded->w; decoded->h = h ? h : encoded->h; return v4lDecodeYUYV(decoded, encoded); } if( s->fmt.fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG ) { gdImagePtr img = v4lDecodeMJPEG(encoded->data, encoded->length); if( !img || !gdImageTrueColor(img) ) { fprintf(stderr, "Could not decode jpeg image, conversion error?\n"); return 1; } int imgWidth = gdImageSX(img); int imgHeight = gdImageSY(img); if( w && h && (imgWidth != w || imgHeight != h) && scale ) { //scale image if necessary, padding will be done below gdImagePtr src = img; img = gdImageCreateTrueColor(w, h); gdImageCopyResized(img, src, 0, 0, 0, 0, w, h, imgWidth, imgHeight); gdImageDestroy(src); imgWidth = w; imgHeight = h; } decoded->w = imgWidth; decoded->h = imgHeight; if( decoded->length < decoded->w*decoded->h*4 ) { fprintf(stderr, "Decode buffer is too small to hold the decoded image\n"); return 1; } //copy pixel data (and implicitly pad with black borders) for( int y = 0; y < imgHeight; y++ ) for( int x = 0; x < imgWidth; x++ ) *(((uint32_t*)decoded->data)+y*decoded->w+x) = gdImageGetPixel(img, x, y); gdImageDestroy(img); return 0; } fprintf(stderr, "Could not decode image. No supported format.\n"); return 1; }
gdImage *fx_greyscale(gdImage *src, char *options) { int x, y; MSG("Greyscaling image."); for(y = 0; y < gdImageSY(src); y++) for(x = 0; x < gdImageSX(src); x++) { uint8_t c = GREY(gdImageGetPixel(src, x, y)); gdImageSetPixel(src, x, y, RGB(c, c, c)); } return(src); }
const char *generate_verify_num() { /* Declare the image */ gdImagePtr im; /* Declare output files */ //FILE *gifout; /* Declare color indexes */ int black; int white; int x, y, z; int rd; static char s[10]; /* Allocate the image: 64 pixels across by 64 pixels tall */ im = gdImageCreate(40, 16); /* Allocate the color black (red, green and blue all minimum). Since this is the first color in a new image, it will be the background color. */ black = gdImageColorAllocate(im, 0, 0, 0); white = gdImageColorAllocate(im, 255, 255, 255); srandom(time(0)%getpid()); rd=random()%(100000); sprintf(s, "%05d", rd); gdImageString(im, gdFontGetLarge(), 0, 0, s, white); for (z=0; z<20; z++) { x=random()%(im->sx); y=random()%(im->sy); gdImageSetPixel(im, x, y, white); } for (y=0; y<im->sy; y++) { for (x=0; x<im->sx; x++) { if (gdImageGetPixel(im, x, y)) outc('o'); else outc(' '); } outc('\n'); } gdImageDestroy(im); oflush(); return s; }
/* * Return the next pixel from the image */ static int GIFNextPixel(gdImagePtr im, GifCtx *ctx) { int r; if( ctx->CountDown == 0 ) return EOF; --(ctx->CountDown); r = gdImageGetPixel(im, ctx->curx, ctx->cury); BumpPixel(ctx); return r; }
gdImage *fx_invert(gdImage *src, char *options) { int x, y; MSG("Inverting image."); for(y = 0; y < gdImageSY(src); y++) for(x = 0; x < gdImageSX(src); x++) { /* Overwrite the pixel with a negative of its value. */ gdImageSetPixel(src, x, y, 0xFFFFFF - gdImageGetPixel(src, x, y)); } return(src); }
void readBack(){ gdImagePtr im; FILE *in; int result; int i, j, x, y, maxX, maxY, c; in = fopen("back55.png", "rb"); im = gdImageCreateFromPng(in); if (!im) { printf("Cannot open file\n"); exit(1);} maxX = gdImageSX(im); maxY = gdImageSY(im); for (y=0; y< maxY; y++){ for (x=0; x < maxX; x++){ c = gdImageGetPixel(im, x, y); backSkyBox[maxY-y-1][x][0] = gdImageRed(im, c); backSkyBox[maxY-y-1][x][1] = gdImageGreen(im, c); backSkyBox[maxY-y-1][x][2] = gdImageBlue(im, c); backSkyBox[maxY-y-1][x][3] = 255; } } }
static void gdImageTileApply(gdImagePtr im, int x, int y) { int srcx, srcy; int p; if (!im->tile) { return; } srcx = x % gdImageSX(im->tile); srcy = y % gdImageSY(im->tile); p = gdImageGetPixel(im->tile, srcx, srcy); /* Allow for transparency */ if (p != gdImageGetTransparent(im->tile)) { gdImageSetPixel(im, x, y, im->tileColorMap[p]); } }