Esempio n. 1
0
/******************************************************************************
 * This routine should be called before any attempt to read an image.
 *****************************************************************************/
int
DGifGetRecordType(GifFileType * GifFile,
                  GifRecordType * Type) {

    GifByteType Buf;
    GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private;

    if (!IS_READABLE(Private)) {
        /* This file was NOT open for reading: */
        _GifError = D_GIF_ERR_NOT_READABLE;
        return GIF_ERROR;
    }

    if (READ(GifFile, &Buf, 1) != 1) {
        _GifError = D_GIF_ERR_READ_FAILED;
        return GIF_ERROR;
    }

    switch (Buf) {
      case ',':
          *Type = IMAGE_DESC_RECORD_TYPE;
          break;
      case '!':
          *Type = EXTENSION_RECORD_TYPE;
          break;
      case ';':
          *Type = TERMINATE_RECORD_TYPE;
          break;
      default:
          *Type = UNDEFINED_RECORD_TYPE;
          _GifError = D_GIF_ERR_WRONG_RECORD;
          return GIF_ERROR;
    }

    return GIF_OK;
}
Esempio n. 2
0
static int DGifGetImageDesc(GifFileType * GifFile)
{
	int i, BitsPerPixel;
	GifByteType Buf[3];
	GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
	SavedImage *sp;

	if (!IS_READABLE(Private))
	{
		_GifError = D_GIF_ERR_NOT_READABLE;
		return GIF_ERROR;
	}

	if (DGifGetWord(GifFile, &GifFile->Image.Left) == GIF_ERROR || DGifGetWord(GifFile, &GifFile->Image.Top) == GIF_ERROR || DGifGetWord(GifFile, &GifFile->Image.Width) == GIF_ERROR
			|| DGifGetWord(GifFile, &GifFile->Image.Height) == GIF_ERROR)
		return GIF_ERROR;
	if (READ(GifFile, Buf, 1) != 1)
	{
		_GifError = D_GIF_ERR_READ_FAILED;
		return GIF_ERROR;
	}
	BitsPerPixel = (Buf[0] & 0x07) + 1;
	GifFile->Image.Interlace = (Buf[0] & 0x40);
	if (Buf[0] & 0x80)
	{
		if (GifFile->Image.ColorMap && GifFile->SavedImages == NULL)
			FreeMapObject(GifFile->Image.ColorMap);

		GifFile->Image.ColorMap = MakeMapObject(1 << BitsPerPixel, NULL);
		if (GifFile->Image.ColorMap == NULL)
		{
			_GifError = D_GIF_ERR_NOT_ENOUGH_MEM;
			return GIF_ERROR;
		}

		for (i = 0; i < GifFile->Image.ColorMap->ColorCount; i++)
		{
			if (READ(GifFile, Buf, 3) != 3)
			{
				FreeMapObject(GifFile->Image.ColorMap);
				_GifError = D_GIF_ERR_READ_FAILED;
				GifFile->Image.ColorMap = NULL;
				return GIF_ERROR;
			}
			GifFile->Image.ColorMap->Colors[i].Red = Buf[0];
			GifFile->Image.ColorMap->Colors[i].Green = Buf[1];
			GifFile->Image.ColorMap->Colors[i].Blue = Buf[2];
		}
	}
	else if (GifFile->Image.ColorMap)
	{
		FreeMapObject(GifFile->Image.ColorMap);
		GifFile->Image.ColorMap = NULL;
	}

	if (GifFile->SavedImages)
	{
		if ((GifFile->SavedImages = (SavedImage *) realloc(GifFile->SavedImages, sizeof(SavedImage) * (GifFile->ImageCount + 1))) == NULL)
		{
			_GifError = D_GIF_ERR_NOT_ENOUGH_MEM;
			return GIF_ERROR;
		}
	}
	else
	{
		if ((GifFile->SavedImages = (SavedImage *) gif_malloc(sizeof(SavedImage))) == NULL)
		{
			_GifError = D_GIF_ERR_NOT_ENOUGH_MEM;
			return GIF_ERROR;
		}
	}

	sp = &GifFile->SavedImages[GifFile->ImageCount];
	memcpy(&sp->ImageDesc, &GifFile->Image, sizeof(GifImageDesc));
	if (GifFile->Image.ColorMap != NULL)
	{
		sp->ImageDesc.ColorMap = MakeMapObject(GifFile->Image.ColorMap->ColorCount, GifFile->Image.ColorMap->Colors);
		if (sp->ImageDesc.ColorMap == NULL)
		{
			_GifError = D_GIF_ERR_NOT_ENOUGH_MEM;
			return GIF_ERROR;
		}
	}
	sp->RasterBits = (unsigned char *) NULL;
	sp->ExtensionBlockCount = 0;
	sp->ExtensionBlocks = (ExtensionBlock *) NULL;

	GifFile->ImageCount++;

	Private->PixelCount = (long) GifFile->Image.Width * (long) GifFile->Image.Height;

	DGifSetupDecompress(GifFile);

	return GIF_OK;
}
Esempio n. 3
0
/******************************************************************************
 This routine should be called before any attempt to read an image.
 Note it is assumed the Image desc. header has been read.
******************************************************************************/
int
DGifGetImageDesc(GifFileType *GifFile)
{
    unsigned int BitsPerPixel;
    GifByteType Buf[3];
    GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private;
    SavedImage *sp;

    if (!IS_READABLE(Private)) {
        /* This file was NOT open for reading: */
        GifFile->Error = D_GIF_ERR_NOT_READABLE;
        return GIF_ERROR;
    }

    if (DGifGetWord(GifFile, &GifFile->Image.Left) == GIF_ERROR ||
        DGifGetWord(GifFile, &GifFile->Image.Top) == GIF_ERROR ||
        DGifGetWord(GifFile, &GifFile->Image.Width) == GIF_ERROR ||
        DGifGetWord(GifFile, &GifFile->Image.Height) == GIF_ERROR)
        return GIF_ERROR;
    if (READ(GifFile, Buf, 1) != 1) {
        GifFile->Error = D_GIF_ERR_READ_FAILED;
	GifFreeMapObject(GifFile->Image.ColorMap);
	GifFile->Image.ColorMap = NULL;
        return GIF_ERROR;
    }
    BitsPerPixel = (Buf[0] & 0x07) + 1;
    GifFile->Image.Interlace = (Buf[0] & 0x40) ? giftrue : giffalse;

    /* Setup the colormap */
    if (GifFile->Image.ColorMap) {
        GifFreeMapObject(GifFile->Image.ColorMap);
        GifFile->Image.ColorMap = NULL;
    }
    /* Does this image have local color map? */
    if (Buf[0] & 0x80) {
	unsigned int i;

        GifFile->Image.ColorMap = GifMakeMapObject(1 << BitsPerPixel, NULL);
        if (GifFile->Image.ColorMap == NULL) {
            GifFile->Error = D_GIF_ERR_NOT_ENOUGH_MEM;
            return GIF_ERROR;
        }

        /* Get the image local color map: */
        for (i = 0; i < GifFile->Image.ColorMap->ColorCount; i++) {
            if (READ(GifFile, Buf, 3) != 3) {
                GifFreeMapObject(GifFile->Image.ColorMap);
                GifFile->Error = D_GIF_ERR_READ_FAILED;
                GifFile->Image.ColorMap = NULL;
                return GIF_ERROR;
            }
            GifFile->Image.ColorMap->Colors[i].Red = Buf[0];
            GifFile->Image.ColorMap->Colors[i].Green = Buf[1];
            GifFile->Image.ColorMap->Colors[i].Blue = Buf[2];
        }
    }

    if (GifFile->SavedImages) {
        if ((GifFile->SavedImages = (SavedImage *)realloc(GifFile->SavedImages,
                                      sizeof(SavedImage) *
                                      (GifFile->ImageCount + 1))) == NULL) {
            GifFile->Error = D_GIF_ERR_NOT_ENOUGH_MEM;
            return GIF_ERROR;
        }
    } else {
        if ((GifFile->SavedImages =
             (SavedImage *) malloc(sizeof(SavedImage))) == NULL) {
            GifFile->Error = D_GIF_ERR_NOT_ENOUGH_MEM;
            return GIF_ERROR;
        }
    }

    sp = &GifFile->SavedImages[GifFile->ImageCount];
    memcpy(&sp->ImageDesc, &GifFile->Image, sizeof(GifImageDesc));
    if (GifFile->Image.ColorMap != NULL) {
        sp->ImageDesc.ColorMap = GifMakeMapObject(
                                 GifFile->Image.ColorMap->ColorCount,
                                 GifFile->Image.ColorMap->Colors);
        if (sp->ImageDesc.ColorMap == NULL) {
            GifFile->Error = D_GIF_ERR_NOT_ENOUGH_MEM;
            return GIF_ERROR;
        }
    }
    sp->RasterBits = (unsigned char *)NULL;
    sp->ExtensionBlockCount = 0;
    sp->ExtensionBlocks = (ExtensionBlock *) NULL;

    GifFile->ImageCount++;

    Private->PixelCount = (long)GifFile->Image.Width *
       (long)GifFile->Image.Height;

    /* Reset decompress algorithm parameters. */
    (void)DGifSetupDecompress(GifFile);

    return GIF_OK;
}
static int
load_body(struct request *rq,struct mem_obj *obj)
{
//	char	*answer = NULL;
//	struct	server_answ	answ_state;
    struct pollarg pollarg[2];
    //struct	av		*header = NULL;
    int r=0;
    unsigned body_size=obj->container->used;
    char answer[ANSW_SIZE+1];
// 	unsigned c_time=rq->client->set_time(0);
//	unsigned s_time=rq->server->set_time(0);

    if(obj->container && obj->container->used>0) {
        if(rq->server->send(obj->container->data,obj->container->used)<=0) {
            //printf("cann't send to client\n");
            goto error;
        }
    }
    if(obj->content_length<=0) { //if content-length is zero ,we don't cache it
        //printf("the content-length is zero\n");
        //pump_data(rq->server->get_socket(),rq->client->get_socket());
        if(create_select_pipe(rq->server,rq->client,conf.time_out[HTTP],0,-1)==-2)
            r=1;
        goto error;
    }
    pollarg[0].fd = rq->client->get_socket();
    pollarg[0].request = FD_POLL_RD;
    pollarg[1].fd = rq->server->get_socket();
    pollarg[1].request = FD_POLL_RD;
    forever() {
        if((r = poll_descriptors(2, &pollarg[0], conf.time_out[HTTP]*1000))<=0) {
            klog(DEBUG_LOG,"poll descriptors is error in file %s line %d\n",__FILE__,__LINE__);
            //	printf("1\n");
            goto done;
        }
        if(IS_READABLE(&pollarg[1]) || IS_HUPED(&pollarg[1])) {
            klog(DEBUG_LOG,"client close the connection in body\n");
            //		printf("2\n");
            goto done;
        }
        if(IS_READABLE(&pollarg[0]))
            r = rq->client->recv_t(answer, ANSW_SIZE,0);
        else {
            klog(DEBUG_LOG,"unknow error is happen\n");
            //	printf("3\n");
            goto done;
        }
        if ( r <=0  )
            goto done;
        body_size += r;
        if(body_size>obj->content_length) {
            //	destroy_obj(obj,0);
            if(rq->server->send(answer,r)<0)
                goto error;
            if(create_select_pipe(rq->server,rq->client,conf.time_out[HTTP],0,-1)==-2)
                r=1;
            goto error;
        }
        /* store data in hot_buff */
        /*		if(writet(so,answer,r,READ_ANSW_TIMEOUT)<=0)
        			goto error;
        */		if(rq->server->send(answer,r)<=0) {
            //		printf("cann't send to client\n");
            goto error;
        }
        if ( store_in_chain(answer, r, obj) ) {
            my_xlog(OOPS_LOG_SEVERE, "fill_mem_obj(): Can't store.\n");
            obj->flags |= FLAG_DEAD;
            //		printf("cann't store in chain\n");
            goto error;
        }
    }
done:
//	rq->server->set_time(s_time);
//	rq->client->set_time(c_time);
    if(body_size<=0 || body_size!=obj->content_length) { //数据不完整,不保存
        //printf("body_size != content_length\n");
        goto error;
    }
    return 1;

error:
//	rq->server->set_time(s_time);
//	rq->client->set_time(c_time);
    SET(obj->flags,FLAG_DEAD);
    return r;
}
static int load_head(struct request *rq,struct mem_obj *obj,head_info &m_head)
{
    //char	*answer = NULL;
    struct	server_answ	answ_state;
    struct pollarg pollarg[2];
    struct	av		*header = NULL;
    int r=0,downgrade_flags=0;
    char *answer= (char *)malloc(ANSW_SIZE+1);
    char *buf=answer;
    int len=ANSW_SIZE;
    int head_status=CONNECT_ERR;
    struct	buff	*hdrs_to_send=NULL;
//	rq->client->set_time(0);
//	rq->server->set_time(0);
    if ( !answer )
        goto error;
    memset(&answ_state,0, sizeof(answ_state));
    pollarg[0].fd = rq->client->get_socket();
    pollarg[0].request = FD_POLL_RD;
    pollarg[1].fd = rq->server->get_socket();
    pollarg[1].request = FD_POLL_RD;
    forever() {
        if((r = poll_descriptors(2, &pollarg[0], conf.time_out[HTTP]*1000))<=0) {
            klog(DEBUG_LOG,"poll descriptors is error in file %s line %d\n",__FILE__,__LINE__);
            goto error;
        }
        if(IS_READABLE(&pollarg[1]) || IS_HUPED(&pollarg[1])) {
            klog(DEBUG_LOG,"prev head value=%d,client close the connection\n",head_status);
            //head_status=CLIENT_HAVE_DATA;
            goto error;
        }
        if(IS_READABLE(&pollarg[0]))
            r = rq->client->recv_t(buf,len,0);
        else {
            klog(DEBUG_LOG,"unknow error is happen\n");
            goto error;
        }
        if ( r <= 0  ) {
            klog(DEBUG_LOG,"recv server data error\n");
            goto error;
        }
//		buf[r]=0;
//		printf("%s",buf);
        buf+=r;
        len-=r;
        if ( !(answ_state.state & GOT_HDR) ) {
            obj->size += r;
            if ( check_server_headers(&answ_state, obj, answer,r, rq) ) {
                head_status=HEAD_UNKNOW;
                rq->flags|=RESULT_CLOSE;
                //	printf("%s:%d\n",__FILE__,__LINE__);
                goto send_data;
            }

        }
        if ( answ_state.state & GOT_HDR ) {

            obj->flags|= answ_state.flags;
            obj->times = answ_state.times;
//			printf("obj->times.last_modified=%d\n",obj->times.last_modified);
            if ( !obj->times.date )
                obj->times.date = global_sec_timer;
            //	check_new_object_expiration(rq, obj);
            obj->status_code = answ_state.status_code;
            if ( obj->status_code != STATUS_OK && obj->status_code!=STATUS_NOT_MODIFIED) {
                //	printf("status is not ok\n");
                //	printf("obj->status_code=%d.\n",obj->status_code);
                rq->flags|=RESULT_CLOSE;
                head_status=HEAD_NOT_OK;
                goto send_data;
            }
            //	obj->flags |= FLAG_DEAD;
            if (TEST(obj->flags , ANSW_NO_STORE) ) {
                //printf("answer is no cache\n");
                head_status=HEAD_OK_NO_STORE;
                rq->flags|=RESULT_CLOSE;
                goto send_data;
            }
            //		obj->flags |= ANSW_NO_CACHE;
            //	downgrade_flags = downgrade(rq, obj);
            //	my_xlog(OOPS_LOG_HTTP|OOPS_LOG_DBG, "fill_mem_obj(): Downgrade flags: %x\n", downgrade_flags);
            hdrs_to_send = alloc_buff(CHUNK_SIZE); /* most headers fit this (?) */
            if ( !hdrs_to_send ) goto error;
            header = obj->headers;

            if ( !header ) {
                rq->flags|=RESULT_CLOSE;
                goto send_data ;
            }

            if ( TEST(obj->flags, ANSW_SHORT_CONTAINER) ) {
                //printf("answer is short contaner\n");
                head_status=HEAD_OK_NO_STORE;
                rq->flags|=RESULT_CLOSE;
                goto send_data;
            }
            /* first must be "HTTP/1.x 200 ..." */
            /*	if ( TEST(downgrade_flags, DOWNGRADE_ANSWER) ) {
            		attach_av_pair_to_buff("HTTP/1.0", header->val, hdrs_to_send);
            		header = header->next;
            	}
            */	while(header) {
                if(strncasecmp(header->attr,"Set-Cookie:",7)!=0) { //don't cache cookie
                    if(strncasecmp(header->attr,"Connection:",11)==0) {
                        if(!TEST(rq->flags,RQ_HAS_KEEP_CONNECTION)) {
                            attach_av_pair_to_buff("Connection:","close",hdrs_to_send);
                        } else {
                            attach_av_pair_to_buff("Connection:","keep-alive",hdrs_to_send);
                        }
                        goto next_header;
                    }
                } else {
                    //	printf("server has set-cookie\n");
                    head_status=HEAD_OK_NO_STORE;
                    rq->flags|=RESULT_CLOSE;
                    goto send_data;
                }
                attach_av_pair_to_buff(header->attr, header->val, hdrs_to_send);
next_header:
                header = header->next;
            }
            attach_av_pair_to_buff("", "", hdrs_to_send);
            //	obj->container=hdrs_to_send;
            //	hdrs_to_send->next=obj->hot_buff;
            if(obj->status_code==STATUS_NOT_MODIFIED) {
                head_status=HEAD_NOT_MODIFIED;
                m_head.head=hdrs_to_send->data;
                m_head.len=hdrs_to_send->used;
                hdrs_to_send->data=NULL;
                //	free_container(hdrs_to_send);
                goto done;
            }
            //	obj->container=obj->hot_buff;

            if(rq->server->send(hdrs_to_send->data,hdrs_to_send->used)<0) {
                //	free_container(hdrs_to_send);
                goto error;
            }
            //	free_container(hdrs_to_send);
            //		printf("%s\n",hdrs_to_send->data);
            head_status=HEAD_OK;
            goto done;
        }
        if(len<1) {
            //	printf("head size is too large\n");
            head_status=HEAD_UNKNOW;
            rq->flags|=RESULT_CLOSE;
            goto send_data;//head size is too large
        }

    }

error://get head failed
    obj->flags |= FLAG_DEAD;
    if(hdrs_to_send)
        free_container(hdrs_to_send);
    IF_FREE(answer);
    return head_status;
done://get head ok;
    if(hdrs_to_send)
        free_container(hdrs_to_send);
    free(answer);
    return head_status;
send_data://得到头失败,但要转发数据
    if(hdrs_to_send)
        free_container(hdrs_to_send);
    m_head.head=answer;
    m_head.len=ANSW_SIZE-len;
    answer[m_head.len]=0;
    return head_status;
}
Esempio n. 6
0
/******************************************************************************
*  Get one full scanned line (Line) of length LineLen from GIF file.	      *
******************************************************************************/
int DGifGetLine(GifFileType *GifFile, GifPixelType *Line, int LineLen)
{
    GifByteType *Dummy;
    GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;

    if (!IS_READABLE(Private)) {
	/* This file was NOT open for reading: */
	_GifError = D_GIF_ERR_NOT_READABLE;
	return GIF_ERROR;
    }

    if (!LineLen) LineLen = GifFile->Image.Width;

#if defined(__MSDOS__) || defined(__GNUC__)
    if ((Private->PixelCount -= LineLen) > 0xffff0000UL) {
#else
    if ((Private->PixelCount -= LineLen) > 0xffff0000) {
#endif /* __MSDOS__ */
	_GifError = D_GIF_ERR_DATA_TOO_BIG;
	return GIF_ERROR;
    }

    if (DGifDecompressLine(GifFile, Line, LineLen) == GIF_OK) {
	if (Private->PixelCount == 0) {
	    /* We probably would not be called any more, so lets clean 	     */
	    /* everything before we return: need to flush out all rest of    */
	    /* image until empty block (size 0) detected. We use GetCodeNext.*/
	    do if (DGifGetCodeNext(GifFile, &Dummy) == GIF_ERROR)
		return GIF_ERROR;
	    while (Dummy != NULL);
	}
	return GIF_OK;
    }
    else
	return GIF_ERROR;
}

/******************************************************************************
* Put one pixel (Pixel) into GIF file.					      *
******************************************************************************/
int DGifGetPixel(GifFileType *GifFile, GifPixelType Pixel)
{
    GifByteType *Dummy;
    GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;

    if (!IS_READABLE(Private)) {
	/* This file was NOT open for reading: */
	_GifError = D_GIF_ERR_NOT_READABLE;
	return GIF_ERROR;
    }

#if defined(__MSDOS__) || defined(__GNUC__)
    if (--Private->PixelCount > 0xffff0000UL)
#else
    if (--Private->PixelCount > 0xffff0000)
#endif /* __MSDOS__ */
    {
	_GifError = D_GIF_ERR_DATA_TOO_BIG;
	return GIF_ERROR;
    }

    if (DGifDecompressLine(GifFile, &Pixel, 1) == GIF_OK) {
	if (Private->PixelCount == 0) {
	    /* We probably would not be called any more, so lets clean 	     */
	    /* everything before we return: need to flush out all rest of    */
	    /* image until empty block (size 0) detected. We use GetCodeNext.*/
	    do if (DGifGetCodeNext(GifFile, &Dummy) == GIF_ERROR)
		return GIF_ERROR;
	    while (Dummy != NULL);
	}
	return GIF_OK;
    }
    else
	return GIF_ERROR;
}
Esempio n. 7
0
File: mpeg.c Progetto: E-LLP/QuIP
Image_File * mpeg_open(const char *name,int rw)
{
	Image_File *ifp;
	ImageDesc *idp;

#ifdef QUIP_DEBUG
printf("mpeg_open: IN\n");
#endif /* QUIP_DEBUG */
	
	ifp = IMG_FILE_CREAT(name,rw,IFT_MPEG);
	if( ifp==NO_IMAGE_FILE ) return(ifp);

	ifp->hdr = (Mpeg_Hdr *)getbuf( sizeof(Mpeg_Hdr) );


	if( IS_READABLE(ifp) ) {
	
		ifp->hdr->idp = (ImageDesc *)malloc( sizeof(ImageDesc) );
		idp = ifp->hdr->idp;

		SetMPEGOption (MPEG_DITHER, FULL_COLOR_DITHER);

		if (!OpenMPEG(ifp->if_fp, idp)) {
			advise("ERROR: OpenMPEG failed");
			exit(1);
		}

		/* fill in some basic header info */
		ifp->hdr->width = idp->Width;
		ifp->hdr->height = idp->Height;
#ifdef BPP_3
		ifp->hdr->depth = idp->Depth/8;
#else
		ifp->hdr->depth = idp->PixelSize/8;
#endif
		/* BUG: we need a more efficient way of counting frames! */
		ifp->hdr->frames = get_n_frs(ifp);

//		ifp->hdr->frames = get_n_of_frms(ifp->if_pathname);

		mpeg_to_dp(ifp->if_dp,ifp->hdr);

		
#ifdef QUIP_DEBUG_USING_SDL
{
		char buf[32];
		/* Initialize SDL */
		
		if ((SDL_Init(SDL_INIT_VIDEO) < 0) || !SDL_VideoDriverName(buf, 1)) {
			sprintf(error_string,"Couldn't init SDL video: %s\n",
				SDL_GetError());
			warn(error_string);
		}

		//screen = SDL_SetVideoMode(idp->Width, idp->Height, 0, 0);
		screen = SDL_SetVideoMode(idp->Width, idp->Height, 
				ifp->hdr->depth*8, 
				SDL_HWSURFACE/*video surface in hardware mem*/);

		if ( screen == NULL ) {
			fprintf(stderr, "Unable to set %dx%d video mode: %s\n",
				idp->Width, idp->Height, SDL_GetError());
			return NULL;
		}
}
#endif /* QUIP_DEBUG_USING_SDL */


	} else {

		ifp->hdr->enc_opts = (MPEGe_options *)malloc( sizeof(MPEGe_options) );
		
		/* Set default options. These can be changed later, if desired. */
		MPEGe_default_options(ifp->hdr->enc_opts);

		/* Start the library with default options */
		if( !MPEGe_open(ifp->if_fp, ifp->hdr->enc_opts) ) {
			warn("MPEGe lib initialization failure");
			return NULL;
		}

		/* It doesn't make sense to initialize the header for writing. */
	}
	
#ifdef QUIP_DEBUG
printf("mpeg_open: OUT\n");
#endif /* QUIP_DEBUG */

	return(ifp);
}
Esempio n. 8
0
 //读取单个帧的图片信息(图片的上下左右坐标,帧的局部颜色空间等)
int
DGifGetImageDesc(GifFileType *GifFile)
{
    unsigned int BitsPerPixel;
    GifByteType Buf[3];
    GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private;
    SavedImage *sp;

    if (!IS_READABLE(Private)) {
        /* This file was NOT open for reading: */
        GifFile->Error = D_GIF_ERR_NOT_READABLE;
        return GIF_ERROR;
    }

    //读取当前帧的上下左右坐标
    if (DGifGetWord(GifFile, &GifFile->Image.Left) == GIF_ERROR ||
        DGifGetWord(GifFile, &GifFile->Image.Top) == GIF_ERROR ||
        DGifGetWord(GifFile, &GifFile->Image.Width) == GIF_ERROR ||
        DGifGetWord(GifFile, &GifFile->Image.Height) == GIF_ERROR)
        return GIF_ERROR;

    //读取长度1字节的信息(即当前帧的Packed Field)
    if (READ(GifFile, Buf, 1) != 1) {
        GifFile->Error = D_GIF_ERR_READ_FAILED;
	GifFreeMapObject(GifFile->Image.ColorMap);
	GifFile->Image.ColorMap = NULL;
        return GIF_ERROR;
    }

    //局部颜色空间的大小
    BitsPerPixel = (Buf[0] & 0x07) + 1;

    //判断当前帧是否交错模式
    GifFile->Image.Interlace = (Buf[0] & 0x40) ? true : false;

    /* Setup the colormap */
    if (GifFile->Image.ColorMap) {
        GifFreeMapObject(GifFile->Image.ColorMap);
        GifFile->Image.ColorMap = NULL;
    }

    //判断当前帧是否使用局部的颜色空间
    if (Buf[0] & 0x80) {
	unsigned int i;

        GifFile->Image.ColorMap = GifMakeMapObject(1 << BitsPerPixel, NULL);
        if (GifFile->Image.ColorMap == NULL) {
            GifFile->Error = D_GIF_ERR_NOT_ENOUGH_MEM;
            return GIF_ERROR;
        }

        //获取局部颜色空间 并储存到临时的帧描述内
        for (i = 0; i < GifFile->Image.ColorMap->ColorCount; i++) {
	    /* coverity[check_return] */
            if (READ(GifFile, Buf, 3) != 3) {
                GifFreeMapObject(GifFile->Image.ColorMap);
                GifFile->Error = D_GIF_ERR_READ_FAILED;
                GifFile->Image.ColorMap = NULL;
                return GIF_ERROR;
            }
            GifFile->Image.ColorMap->Colors[i].Red = Buf[0];
            GifFile->Image.ColorMap->Colors[i].Green = Buf[1];
            GifFile->Image.ColorMap->Colors[i].Blue = Buf[2];
        }
    }

    //判断之前是否已经有储存帧图像
    if (GifFile->SavedImages) {
        //如果有已经储存的帧图像 需要重新分配空间
        SavedImage* new_saved_images =
            (SavedImage *)realloc(GifFile->SavedImages,
                            sizeof(SavedImage) * (GifFile->ImageCount + 1));
        if (new_saved_images == NULL) {
            GifFile->Error = D_GIF_ERR_NOT_ENOUGH_MEM;
            return GIF_ERROR;
        }
        GifFile->SavedImages = new_saved_images;
    } else {
        if ((GifFile->SavedImages =
             (SavedImage *) malloc(sizeof(SavedImage))) == NULL) {
            GifFile->Error = D_GIF_ERR_NOT_ENOUGH_MEM;
            return GIF_ERROR;
        }
    }

    //指向新的SavedImage
    sp = &GifFile->SavedImages[GifFile->ImageCount];
    //写入之前获取的帧图像描述
    memcpy(&sp->ImageDesc, &GifFile->Image, sizeof(GifImageDesc));
    //写入局部颜色空间
    if (GifFile->Image.ColorMap != NULL) {
        sp->ImageDesc.ColorMap = GifMakeMapObject(
                                 GifFile->Image.ColorMap->ColorCount,
                                 GifFile->Image.ColorMap->Colors);
        if (sp->ImageDesc.ColorMap == NULL) {
            GifFile->Error = D_GIF_ERR_NOT_ENOUGH_MEM;
            return GIF_ERROR;
        }
    }
    sp->RasterBits = (unsigned char *)NULL;
    sp->ExtensionBlockCount = 0;
    sp->ExtensionBlocks = (ExtensionBlock *) NULL;

    GifFile->ImageCount++;

    Private->PixelCount = (long)GifFile->Image.Width *
       (long)GifFile->Image.Height;

    /* Reset decompress algorithm parameters. */
    //DGifSetupDecompress
    return DGifSetupDecompress(GifFile);
}
/*
 * Helper method to get the response from application for GET requests.
 * The 'resp_payload' parameter will be used to get the response.
 * Common properties of the resource such as rt, if and links will not be set by this method.
 */
bool handle_get_req_helper(const char *res_uri, const char *query, OCRepPayload *resp_payload)
{
	RET_FALSE_IF_PARAM_IS_NULL(TAG, resp_payload);
	RET_FALSE_IF_NULL(TAG, g_handle_get_req_cb, "Request callback for GET is NULL.");

	// Setup the request message.
	st_things_get_request_message_s *req_msg = create_req_msg_inst_for_get(res_uri, query);
	RET_FALSE_IF_NULL(TAG, req_msg, "Failed to create request message for GET.");

	/* Based on the resource type of the request (specified in the query parameters like rt=oic.r.switch.binary) and
	   interface type in the query parameter, add proper set of property keys in the request message.
	   Application's response representation should include
	   only those properties which are present in the request message. */
	char *res_type = NULL;
	int count = 0;
	bool destroy_props = false;
	struct things_attribute_info_s **properties = NULL;

	// Get resource type from query parameter
	if (NULL != query && strlen(query) > 0) {
		bool found = false;
		bool result = get_query_value_internal(query, OC_RSRVD_RESOURCE_TYPE, &res_type, &found);
		if (found && !result) {	// If query is present but API returns false.
			THINGS_LOG_E(TAG, "Failed to get the resource type from query parameter(%s).", query);
			destroy_req_msg_inst_for_get(req_msg);
			return false;
		}
	}
	// Get supported set of properties based on resource type query parameter.
	// If resource type is not available, then get the properties based on resource uri.
	bool res = get_supported_properties(res_type, res_uri, &count, &properties, &destroy_props);
	if (!res) {
		THINGS_LOG_E(TAG, "Failed to get the resource properties based on its type or uri.");
		destroy_req_msg_inst_for_get(req_msg);
		things_free(res_type);
		return false;
	}
	// Calculate the length of all property keys
	int total_length_of_prop_keys = 0;
	for (int index = 0; index < count; index++) {
		if (NULL != properties[index]) {
			total_length_of_prop_keys += strlen(properties[index]->key);
		}
	}
	total_length_of_prop_keys += count;	// For delimiter
	total_length_of_prop_keys += 1;	// For null character

	// Allocate memory for holding property keys with delimiters
	req_msg->property_key = (char *)things_calloc(total_length_of_prop_keys, sizeof(char));
	if (!req_msg->property_key) {
		THINGS_LOG_E(TAG, "Failed to allocate memory for property key in GET request message.");
		if (destroy_props) {
			things_free(properties);
		}
		destroy_req_msg_inst_for_get(req_msg);
		things_free(res_type);
		return false;
	}
	// Get interface type from query parameter.
	char *if_type = NULL;
	if (NULL != query && strlen(query) > 0) {
		bool found = false;
		bool result = get_query_value_internal(query, OC_RSRVD_INTERFACE, &if_type, &found);
		if (found && !result) {	// If query is present but API returns false.
			THINGS_LOG_E(TAG, "Failed to get the interface type from query parameter(%s).", query);
			if (destroy_props) {
				things_free(properties);
			}
			destroy_req_msg_inst_for_get(req_msg);
			things_free(res_type);
			return false;
		}
	}

	bool handled = false;
	if (NULL != if_type && strlen(if_type) > 0) {
		THINGS_LOG_D(TAG, "Interface type is (%s).", if_type);
		if (0 == strncmp(if_type, OC_RSRVD_INTERFACE_READ, strlen(OC_RSRVD_INTERFACE_READ))) {
			// Add all the attributes which are readable.
			for (int index = 0; index < count; index++) {
				if (NULL != properties[index] && IS_READABLE(properties[index]->rw)) {
					add_property_key_in_get_req_msg(req_msg, properties[index]->key);
				}
			}
			handled = true;
		} else if (0 == strncmp(if_type, ST_THINGS_RSRVD_INTERFACE_READWRITE, strlen(ST_THINGS_RSRVD_INTERFACE_READWRITE))) {
			// Add all the attributes which are readable and writable.
			for (int index = 0; index < count; index++) {
				if (NULL != properties[index] && IS_READABLE(properties[index]->rw) && IS_WRITABLE(properties[index]->rw)) {
					add_property_key_in_get_req_msg(req_msg, properties[index]->key);
				}
			}
			handled = true;
		}
	}

	if (!handled) {				// For all other cases, add all the property keys.
		for (int index = 0; index < count; index++) {
			if (NULL != properties[index]) {
				add_property_key_in_get_req_msg(req_msg, properties[index]->key);
			}
		}
	}
	// Remove 'rt' and 'if' from query parameters
	if (NULL != query && strlen(query) > 0) {
		if (strstr(req_msg->query, OC_RSRVD_RESOURCE_TYPE) != NULL) {
			remove_query_parameter(req_msg->query, OC_RSRVD_RESOURCE_TYPE);
		}
		if (strstr(req_msg->query, OC_RSRVD_INTERFACE) != NULL) {
			remove_query_parameter(req_msg->query, OC_RSRVD_INTERFACE);
		}
	}
	// Setup the response representation for application. This representation will be handed over to the application.
	st_things_representation_s *things_resp_rep = create_representation_inst_internal(resp_payload);
	if (NULL != things_resp_rep) {
		res = g_handle_get_req_cb(req_msg, things_resp_rep);
		THINGS_LOG_D(TAG, "The result of application's callback : %s.", res ? "true" : "false");
		destroy_representation_inst_internal(things_resp_rep, false);
	} else {
		THINGS_LOG_E(TAG, "Failed to create response representation.");
		res = false;
	}

	destroy_req_msg_inst_for_get(req_msg);
	if (destroy_props) {
		things_free(properties);
	}
	things_free(res_type);
	things_free(if_type);

	return res;
}
Esempio n. 10
0
/******************************************************************************
*  Get one full scanned line (Line) of length LineLen from GIF file.	      *
******************************************************************************/
int DGifGetLine(GifFileType *GifFile, GifPixelType *Line, int LineLen)
{
    GifByteType *Dummy;
    GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;

    if (!IS_READABLE(Private)) {
	/* This file was NOT open for reading: */
	_GifError = D_GIF_ERR_NOT_READABLE;
	return GIF_ERROR;
    }

    if (!LineLen) LineLen = GifFile->Image.Width;

#if defined(__MSDOS__) || defined(__GNUC__)
    if ((Private->PixelCount -= LineLen) > 0xffff0000UL) {
#else
    if ((Private->PixelCount -= LineLen) > 0xffff0000) {
#endif /* __MSDOS__ */
	_GifError = D_GIF_ERR_DATA_TOO_BIG;
	return GIF_ERROR;
    }

    if (DGifDecompressLine(GifFile, Line, LineLen) == GIF_OK) {
	if (Private->PixelCount == 0) {
	    /* We probably would not be called any more, so lets clean 	     */
	    /* everything before we return: need to flush out all rest of    */
	    /* image until empty block (size 0) detected. We use GetCodeNext.*/
	    do if (DGifGetCodeNext(GifFile, &Dummy) == GIF_ERROR)
		return GIF_ERROR;
	    while (Dummy != NULL);
	}
	return GIF_OK;
    }
    else
	return GIF_ERROR;
}

/******************************************************************************
*   Get an extension block (see GIF manual) from gif file. This routine only  *
* returns the first data block, and DGifGetExtensionNext shouldbe called      *
* after this one until NULL extension is returned.			      *
*   The Extension should NOT be freed by the user (not dynamically allocated).*
*   Note it is assumed the Extension desc. header ('!') has been read.	      *
******************************************************************************/
int DGifGetExtension(GifFileType *GifFile, int *ExtCode,
						    GifByteType **Extension)
{
    GifByteType Buf;
    GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;

    if (!IS_READABLE(Private)) {
	/* This file was NOT open for reading: */
	_GifError = D_GIF_ERR_NOT_READABLE;
	return GIF_ERROR;
    }

    if (READ(GifFile,&Buf, 1) != 1) {
	_GifError = D_GIF_ERR_READ_FAILED;
	return GIF_ERROR;
    }
    *ExtCode = Buf;

    return DGifGetExtensionNext(GifFile, Extension);
}