Exemplo n.º 1
0
int
lpTextures::addTexture(char *fname)
{
 int texnum;
 unsigned char *p;


 if(num_textures + 1 > max_textures) growTextures();
 texnum = num_textures;

 tex[texnum] = new texture_t;

#if 0
 if((tex[texnum]->pixels = read_jpeg(fname, &tex[texnum]->imgXsize,
			  &tex[texnum]->imgYsize,
			  &tex[texnum]->imgZsize)) == 0) return(0);
#endif

 p = read_jpeg(fname, &tex[texnum]->imgXsize,
			  &tex[texnum]->imgYsize,
			  &tex[texnum]->imgZsize);
 if(!p) return 0;
 tex[texnum]->pixels = p;
 num_textures++;

// printf("\n\nAddTextureFile: added %s %dx%dx%d as texnum %d \n",fname,tex[texnum]->imgXsize,
//       tex[texnum]->imgYsize, tex[texnum]->imgZsize,texnum);

 last_accessed = texnum;
 return(texnum);
}
Exemplo n.º 2
0
static GLuint makeTexture(const char *filename)
{
    int width, height;
    unsigned char *pixels = read_jpeg(filename, &width, &height);
    GLuint texture;

    if (!pixels)
        return 0;

    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);

    glTexImage2D(
        GL_TEXTURE_2D, 0,           /* target, level */
        GL_RGB8,                    /* internal format */
        width, height, 0,           /* width, height, border */
        GL_BGR, GL_UNSIGNED_BYTE,   /* external format, type */
        pixels                      /* pixels */
    );
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    free(pixels);
    return texture;
}
Exemplo n.º 3
0
Arquivo: main.c Projeto: qsyousef/dsp
int main (int argc, char *argv[])
{
	unsigned char *rawpic = NULL;
	unsigned char *warpedpic = NULL;
	size_t width, height;
	int num_components, color_space;

	/* Get raw data */
	rawpic = read_jpeg ("shrek_cat_eyes.jpg", &width, &height, &num_components, &color_space);
	if (!rawpic)
		goto out;
	warpedpic = calloc(1, width*height*num_components);
	if (!warpedpic) 
		goto out;
	
	/* do some processing here */
	warp(rawpic, warpedpic, width, height, num_components);
	//thresholding(rawpic, warpedpic, width, height, num_components);
	//shift(rawpic, warpedpic, width, height, num_components);

	/* write it back as jpeg */
	write_jpeg ("out.jpg", warpedpic, width, height, num_components, color_space);
	printf("Done :)\n");
out:
	if (rawpic)
		free(rawpic);
	if (warpedpic)
		free(warpedpic);
	return 0;
}
Exemplo n.º 4
0
int main(int argc,char* argv[]) {
	if(argc != 2) 
		{ fprintf(stderr,"\nusage: %s file.jpg\n\n",argv[0]); return 1; }

	FILE *f = fopen(argv[1],"rb");
	if(!f) { perror(argv[1]); return 1; }

	read_jpeg(f);

	return 0;
}
Exemplo n.º 5
0
	rgb*	read_jpeg(const char* filename)
	// Create and read a new image from the given filename, if possible.
	{
		SDL_RWops*	in = SDL_RWFromFile(filename, "rb");
		if (in)
		{
			rgb*	im = read_jpeg(in);
			SDL_RWclose(in);
			return im;
		}
		else {
			return NULL;
		}
	}
Exemplo n.º 6
0
/**
 * Load image
 * Parameters:
 *      path:   input path 
 *      mem:    memory for decoding
 * Return:
 *           0  OK
 *          -1  ERROR
 */
int loadImage (const char *path, Bitmap_t *mem) 
{
    VALIDATE_NOT_NULL2 (path, mem);
    const char const *postfix = getFilePostfix (path);
    if (NULL == postfix) {
        LogE ("Failed getFilePostfix in loadImage\n");
        return -1;
    }

    if (strcasecmp (postfix, "jpg") == 0) {
        return read_jpeg (path, mem);
    }
    else if (strcasecmp (postfix, "png") == 0) {
        return read_png (path, mem);
    }
    else {
        LogE ("Invalid postfix name (%s) in loadImage\n", postfix);
        return -1;
    }
}
Exemplo n.º 7
0
bool GPhotoCCD::grabImage()
{
    //char ext[16];
    uint8_t *memptr = PrimaryCCD.getFrameBuffer();
	size_t memsize;
    int fd, naxis=2, w, h, bpp=8;

    if (sim)
    {

      w= PrimaryCCD.getXRes();
      h= PrimaryCCD.getYRes();
      size_t fullbuf_size = w*h + 512;
      uint8_t * fullbuf = (uint8_t *) malloc(fullbuf_size);
      for (int i = 0; i < h; i++)
        for (int j = 0; j < w; j++)
          fullbuf[i * w + j] = rand() % 255;

      // Starting address if subframing
      memptr = fullbuf + (PrimaryCCD.getSubY() * PrimaryCCD.getXRes()) + PrimaryCCD.getSubX();
      memsize = PrimaryCCD.getSubW() * PrimaryCCD.getSubH() * PrimaryCCD.getBPP()/8;

      PrimaryCCD.setFrameBuffer(memptr);
      PrimaryCCD.setFrameBufferSize(memsize, false);
      //PrimaryCCD.setResolution(w, h);
      //PrimaryCCD.setFrame(0, 0, w, h);
      PrimaryCCD.setNAxis(naxis);
      PrimaryCCD.setBPP(bpp);

      ExposureComplete(&PrimaryCCD);
      return true;

    }

    // If only save to SD Card, let's not upload back to client
    /*if (UploadS[GP_UPLOAD_SDCARD].s == ISS_ON)
    {
        DEBUG(INDI::Logger::DBG_SESSION, "Exposure complete. Image saved to SD Card.");
        ExposureComplete(&PrimaryCCD);
        return true;
    }*/


    if (transferFormatS[0].s == ISS_ON)
    {
	    
        char tmpfile[] = "/tmp/indi_XXXXXX";

        //dcraw can't read from stdin, so we need to write to disk then read it back
        fd = mkstemp(tmpfile);

        int ret = gphoto_read_exposure_fd(gphotodrv, fd);

        if (ret != GP_OK || fd == -1)
        {
            if (fd == -1)
                DEBUGF(INDI::Logger::DBG_ERROR, "Exposure failed to save image. Cannot create temp file %s", tmpfile);
            else
                DEBUGF(INDI::Logger::DBG_ERROR, "Exposure failed to save image... %s", gp_result_as_string(ret));
            unlink(tmpfile);
            return false;
        }

        if (!strcmp(gphoto_get_file_extension(gphotodrv), "unknown"))
        {
                DEBUG(INDI::Logger::DBG_ERROR, "Exposure failed.");
                return false;
        }

        /* We're done exposing */
        DEBUG(INDI::Logger::DBG_SESSION, "Exposure done, downloading image...");
    
    
        if(strcasecmp(gphoto_get_file_extension(gphotodrv), "jpg") == 0 ||
           strcasecmp(gphoto_get_file_extension(gphotodrv), "jpeg") == 0)
        {
                if (read_jpeg(tmpfile, &memptr, &memsize, &naxis, &w, &h))
                {
                    DEBUG(INDI::Logger::DBG_ERROR, "Exposure failed to parse jpeg.");
                    unlink(tmpfile);
                    return false;
                }

                DEBUGF(INDI::Logger::DBG_DEBUG, "read_jpeg: memsize (%d) naxis (%d) w (%d) h (%d) bpp (%d)", memsize, naxis, w, h, bpp);
        }
        else
        {
                if (read_dcraw(tmpfile, &memptr, &memsize, &naxis, &w, &h, &bpp))
                {
                    DEBUG(INDI::Logger::DBG_ERROR, "Exposure failed to parse raw image.");
                    unlink(tmpfile);
                    return false;
                }
		
                DEBUGF(INDI::Logger::DBG_DEBUG, "read_dcraw: memsize (%d) naxis (%d) w (%d) h (%d) bpp (%d)", memsize, naxis, w, h, bpp);

                unlink(tmpfile);
        }

        PrimaryCCD.setImageExtension("fits");

        // If subframing is requested
        if (frameInitialized && (PrimaryCCD.getSubH() < PrimaryCCD.getYRes() || PrimaryCCD.getSubW() < PrimaryCCD.getXRes()))
        {
            int subFrameSize  = PrimaryCCD.getSubW() * PrimaryCCD.getSubH() * bpp/8 * ((naxis == 3) ? 3 : 1);
            int oneFrameSize  = PrimaryCCD.getSubW() * PrimaryCCD.getSubH() * bpp/8;
            uint8_t *subframeBuf = (uint8_t *) malloc(subFrameSize);

            int startY= PrimaryCCD.getSubY();
            int endY  = startY + PrimaryCCD.getSubH();
            int lineW = PrimaryCCD.getSubW() * bpp/8;
            int subX = PrimaryCCD.getSubX();

            if (naxis == 2)
            {
                for (int i=startY ; i < endY; i++)
                    memcpy(subframeBuf + (i - startY) * lineW, memptr + (i * w + subX) * bpp/8 , lineW);
            }
            else
            {
                uint8_t *subR = subframeBuf;
                uint8_t *subG = subframeBuf + oneFrameSize;
                uint8_t *subB = subframeBuf + oneFrameSize * 2;

                uint8_t *startR = memptr;
                uint8_t *startG = memptr + (w * h * bpp/8);
                uint8_t *startB = memptr + (w * h * bpp/8 * 2);

                for (int i=startY; i < endY; i++)
                {
                    memcpy(subR + (i-startY) * lineW, startR + (i * w + subX) * bpp/8 , lineW );
                    memcpy(subG + (i-startY) * lineW, startG + (i * w + subX) * bpp/8 , lineW );
                    memcpy(subB + (i-startY) * lineW, startB + (i * w + subX) * bpp/8 , lineW );
                }
            }

            PrimaryCCD.setFrameBuffer(subframeBuf);
            PrimaryCCD.setFrameBufferSize(subFrameSize, false);
            PrimaryCCD.setResolution(w, h);
            PrimaryCCD.setNAxis(naxis);
            PrimaryCCD.setBPP(bpp);

            ExposureComplete(&PrimaryCCD);

            // Restore old pointer and release memory
            PrimaryCCD.setFrameBuffer(memptr);
            PrimaryCCD.setFrameBufferSize(memsize, false);
            free(subframeBuf);
        }
        else
        {
            // We need to initially set the frame dimensions for the first time since it is unknown at the time of connection.
            frameInitialized = true;

            PrimaryCCD.setFrame(0, 0, w, h);
            PrimaryCCD.setFrameBuffer(memptr);
            PrimaryCCD.setFrameBufferSize(memsize, false);
            PrimaryCCD.setResolution(w, h);
            PrimaryCCD.setNAxis(naxis);
            PrimaryCCD.setBPP(bpp);

            ExposureComplete(&PrimaryCCD);
        }

    }
    else
    {        
        int rc = gphoto_read_exposure(gphotodrv);

        if (rc != 0)
        {
            DEBUG(INDI::Logger::DBG_ERROR, "Failed to expose.");
            return rc;
        }
	
        /* We're done exposing */
         DEBUG(INDI::Logger::DBG_DEBUG, "Exposure done, downloading image...");
         uint8_t *newMemptr = NULL;
         gphoto_get_buffer(gphotodrv, (const char **)&newMemptr, &memsize);
         memptr = (uint8_t *)realloc(memptr, memsize); // We copy the obtained memory pointer to avoid freeing some gphoto memory
         memcpy(memptr, newMemptr, memsize); //

         gphoto_get_dimensions(gphotodrv, &w, &h);

        PrimaryCCD.setImageExtension(gphoto_get_file_extension(gphotodrv));
        if (w > 0 && h > 0)
            PrimaryCCD.setFrame(0, 0, w, h);
        PrimaryCCD.setFrameBuffer(memptr);
        PrimaryCCD.setFrameBufferSize(memsize, false);
        if (w > 0 && h > 0)
            PrimaryCCD.setResolution(w, h);
        PrimaryCCD.setNAxis(naxis);
        PrimaryCCD.setBPP(bpp);

        ExposureComplete(&PrimaryCCD);
    }

    return true;
}
Exemplo n.º 8
0
int
read_image(const char *filename, int *width, int *height, 
	   unsigned char *rgb)
{
    char buf[4];
    unsigned char *ubuf = (unsigned char *) buf;
    int success = 0;

    FILE *file;
    file = fopen(filename, "rb");
    if (file == NULL) return(0);
  
    /* see what kind of file we have */

    fread(buf, 1, 4, file);
    fclose(file);

    if (!strncmp("BM", buf, 2))
    {
        success = read_bmp(filename, width, height, rgb);
    }
    else if (!strncmp("GIF8", buf, 4))
    {
#ifdef HAVE_LIBGIF
        success = read_gif(filename, width, height, rgb);
#else
        fprintf(stderr, 
                "Sorry, this program was not compiled with GIF support\n");
        success = 0;
#endif /* HAVE_LIBGIF */
    }
    else if ((ubuf[0] == 0xff) && (ubuf[1] == 0xd8))
    {
#ifdef HAVE_LIBJPEG
        success = read_jpeg(filename, width, height, rgb);
#else
        fprintf(stderr, 
                "Sorry, this program was not compiled with JPEG support\n");
        success = 0;
#endif /* HAVE_LIBJPEG */
    }
    else if ((ubuf[0] == 0x89) && !strncmp("PNG", buf+1, 3))
    {
#ifdef HAVE_LIBPNG
        success = read_png(filename, width, height, rgb);
#else
        fprintf(stderr, 
                "Sorry, this program was not compiled with PNG support\n");
        success = 0;
#endif /* HAVE_LIBPNG */
    }
    else if ((   !strncmp("P6\n", buf, 3))
             || (!strncmp("P5\n", buf, 3))
             || (!strncmp("P4\n", buf, 3))
             || (!strncmp("P3\n", buf, 3))
             || (!strncmp("P2\n", buf, 3))
             || (!strncmp("P1\n", buf, 3)))
    {
#ifdef HAVE_LIBPNM
        success = read_pnm(filename, width, height, rgb);
#else
        fprintf(stderr, 
                "Sorry, this program was not compiled with PNM support\n");
        success = 0;
#endif /* HAVE_LIBPNM */
    }
    else if (((!strncmp ("MM", buf, 2)) && (ubuf[2] == 0x00) 
              && (ubuf[3] == 0x2a))
             || ((!strncmp ("II", buf, 2)) && (ubuf[2] == 0x2a) 
                 && (ubuf[3] == 0x00)))
    {
#ifdef HAVE_LIBTIFF
        success = read_tiff(filename, width, height, rgb);
#else
        fprintf(stderr, 
                "Sorry, this program was not compiled with TIFF support\n");
        success = 0;
#endif
    }
    else
    {
        fprintf(stderr, "Unknown image format\n");
        success = 0;
    }

    return(success);
}
Exemplo n.º 9
0
int main(int argc, char **argv) {
	extern int optind;
	extern char *optarg;
	int i;

	char *outfile = NULL;
	int de = 0;

	while ((i = getopt(argc, argv, "o:d")) != -1) {
		switch (i) {
		case 'o':
			outfile = optarg;
			break;

		case 'd':
			de = 1;
			break;

		default:
			usage(argv);
			exit(EXIT_FAILURE);
		}
	}

	if (argc - optind != 1) {
		usage(argv);
		exit(EXIT_FAILURE);
	}

	if (outfile == NULL && isatty(1)) {
		fprintf(stderr, "Didn't specify -o and standard output is a terminal\n");
		exit(EXIT_FAILURE);
	}
	FILE *outfp = stdout;
	if (outfile != NULL) {
		outfp = fopen(outfile, "wb");
		if (outfp == NULL) {
			perror(outfile);
			exit(EXIT_FAILURE);
		}
	}

	int width, height;
	unsigned char *buf;

	{
		{
			{
				char *url = argv[optind];

				CURL *curl = curl_easy_init();
				if (curl == NULL) {
					fprintf(stderr, "Curl won't start\n");
					exit(EXIT_FAILURE);
				}

				struct data data;
				data.buf = NULL;
				data.len = 0;
				data.nalloc = 0;

				curl_easy_setopt(curl, CURLOPT_URL, url);
				curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
				curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data);
				curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_receive);

				CURLcode res = curl_easy_perform(curl);
				if (res != CURLE_OK) {
					fprintf(stderr, "Can't retrieve %s: %s\n", url,
						curl_easy_strerror(res));
					exit(EXIT_FAILURE);
				}

				struct image *i;

				if (data.len >= 4 && memcmp(data.buf, "\x89PNG", 4) == 0) {
					i = read_png(data.buf, data.len);
				} else if (data.len >= 2 && memcmp(data.buf, "\xFF\xD8", 2) == 0) {
					i = read_jpeg(data.buf, data.len);
				} else {
					fprintf(stderr, "Don't recognize file format\n");

					free(data.buf);
					curl_easy_cleanup(curl);
					exit(EXIT_FAILURE);
				}

				free(data.buf);
				curl_easy_cleanup(curl);

				width = i->width;
				height = i->height;
				buf = malloc(i->width * i->height * 4);

				int x, y;
				for (y = 0; y < i->height; y++) {
					for (x = 0; x < i->width; x++) {
						if (i->depth == 4) {
							double as = buf[((y) * width + x) * 4 + 3] / 255.0;
							double rs = buf[((y) * width + x) * 4 + 0] / 255.0 * as;
							double gs = buf[((y) * width + x) * 4 + 1] / 255.0 * as;
							double bs = buf[((y) * width + x) * 4 + 2] / 255.0 * as;

							double ad = i->buf[(y * i->width + x) * 4 + 3] / 255.0;
							double rd = i->buf[(y * i->width + x) * 4 + 0] / 255.0 * ad;
							double gd = i->buf[(y * i->width + x) * 4 + 1] / 255.0 * ad;
							double bd = i->buf[(y * i->width + x) * 4 + 2] / 255.0 * ad;

							// https://code.google.com/p/pulpcore/wiki/TutorialBlendModes
							double ar = as * (1 - ad) + ad;
							double rr = rs * (1 - ad) + rd;
							double gr = gs * (1 - ad) + gd;
							double br = bs * (1 - ad) + bd;

							buf[((y) * width + x) * 4 + 3] = ar * 255.0;
							buf[((y) * width + x) * 4 + 0] = rr / ar * 255.0;
							buf[((y) * width + x) * 4 + 1] = gr / ar * 255.0;
							buf[((y) * width + x) * 4 + 2] = br / ar * 255.0;
						} else if (i->depth == 3) {
							buf[((y) * width + x) * 4 + 0] = i->buf[(y * i->width + x) * 3 + 0];
							buf[((y) * width + x) * 4 + 1] = i->buf[(y * i->width + x) * 3 + 1];
							buf[((y) * width + x) * 4 + 2] = i->buf[(y * i->width + x) * 3 + 2];
							buf[((y) * width + x) * 4 + 3] = 255;
						} else {
							buf[((y) * width) * 4 + 0] = i->buf[(y * i->width + x) * i->depth + 0];
							buf[((y) * width) * 4 + 1] = i->buf[(y * i->width + x) * i->depth + 0];
							buf[((y) * width) * 4 + 2] = i->buf[(y * i->width + x) * i->depth + 0];
							buf[((y) * width) * 4 + 3] = 255;
						}
					}
				}

				free(i->buf);
				free(i);
			}
		}
	}

	unsigned char *rows[height];
	for (i = 0; i < height; i++) {
		rows[i] = buf + i * (4 * width);
	}

	convert(buf, width, height, de);

	png_structp png_ptr;
	png_infop info_ptr;

	png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, fail, fail, fail);
	if (png_ptr == NULL) {
		fprintf(stderr, "PNG failure (write struct)\n");
		exit(EXIT_FAILURE);
	}
	info_ptr = png_create_info_struct(png_ptr);
	if (info_ptr == NULL) {
		png_destroy_write_struct(&png_ptr, NULL);
		fprintf(stderr, "PNG failure (info struct)\n");
		exit(EXIT_FAILURE);
	}

	png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
	png_set_rows(png_ptr, info_ptr, rows);
	png_init_io(png_ptr, outfp);
	png_write_png(png_ptr, info_ptr, 0, NULL);
	png_destroy_write_struct(&png_ptr, &info_ptr);

	if (outfile != NULL) {
		fclose(outfp);
	}

	return 0;
}