2 /**************************************************************************
4 * This code is developed by Adam Li. This software is an *
5 * implementation of a part of one or more MPEG-4 Video tools as *
6 * specified in ISO/IEC 14496-2 standard. Those intending to use this *
7 * software module in hardware or software products are advised that its *
8 * use may infringe existing patents or copyrights, and any such use *
9 * would be at such party's own risk. The original developer of this *
10 * software module and his/her company, and subsequent editors and their *
11 * companies (including Project Mayo), will have no liability for use of *
12 * this software or modifications or derivatives thereof. *
14 * Project Mayo gives users of the Codec a license to this software *
15 * module or modifications thereof for use in hardware or software *
16 * products claiming conformance to the MPEG-4 Video Standard as *
17 * described in the Open DivX license. *
19 * The complete Open DivX license can be found at *
20 * http://www.projectmayo.com/opendivx/license.php . *
22 **************************************************************************/
24 /**************************************************************************
28 * Copyright (C) 2001 Project Mayo
32 * DivX Advance Research Center <darc@projectmayo.com>
34 **************************************************************************/
36 /* This file contains some utility functions to manipulate Image data */
38 /* Some codes of this project come from MoMuSys MPEG-4 implementation. */
39 /* Please see seperate acknowledgement file for a list of contributors. */
43 /* private prototypes */
45 Char
*ecalloc(Int n
, Int s
);
46 Char
*erealloc(Char
*p
, Int n
);
47 Void
CopyImageI(ImageI
*image_in
, ImageI
*image_out
);
48 Void
CopyImageF(ImageF
*image_in
, ImageF
*image_out
);
49 Void
SetConstantImageI(ImageI
*image
, SInt val
);
50 Void
SetConstantImageF(ImageF
*image
, Float val
);
51 Void
SubImageI(ImageI
*image_in1
, ImageI
*image_in2
, ImageI
*image_out
);
52 Void
SubImageF(ImageF
*image_in1
, ImageF
*image_in2
, ImageF
*image_out
);
55 /* The following is the Image maintance functions. */
57 /***********************************************************CommentBegin******
59 * -- AllocImage -- Allocates memory for an Image structure
62 * Allocates memory for an Image structure, including memory
63 * for the actual image pixels. The image pixels can be of type
64 * UChar, SInt or Float depending on the parameter type.
67 * A pointer to the allocated Image structure.
70 * Allocates memory for the Image if possible, otherwise a
71 * memory allocation error is signalled and the program will
74 ***********************************************************CommentEnd********/
77 AllocImage(UInt size_x
, UInt size_y
, ImageType type
)
81 image
= (Image
*) emalloc(sizeof(Image
));
83 image
->version
= VERSION
;
89 image
->data
= (ImageData
*) emalloc(sizeof(ImageData
));
94 image
->data
->s
= (SInt
*) ecalloc(size_x
*size_y
,sizeof(SInt
));
98 image
->data
->f
= (Float
*) ecalloc(size_x
*size_y
,sizeof(Float
));
102 image
->data
->u
= (UChar
*) ecalloc(size_x
*size_y
,sizeof(UChar
));
106 image
->f
= image
->data
->s
; /* For compatibility with KHOROS */
112 /***********************************************************CommentBegin******
114 * -- FreeImage -- Frees up all the memory associated with an Image structure
117 * Frees up all the memory associated with an Image structure
119 ***********************************************************CommentEnd********/
122 FreeImage(Image
*image
)
128 if (image
== NULL
) return;
133 ps
= (SInt
*)GetImageData(image
);
134 if(ps
!= NULL
) free((Char
*)ps
);
135 free((Char
*) image
->data
);
140 pf
= (Float
*)GetImageData(image
);
141 if(pf
!= NULL
) free((Char
*)pf
);
142 free((Char
*) image
->data
);
147 pu
= (UChar
*)GetImageData(image
);
148 if(pu
!= NULL
) free((Char
*)pu
);
149 free((Char
*) image
->data
);
156 /***********************************************************CommentBegin******
158 * -- CopyImage -- Copies the pixel values of one image to another image
161 * Copies the pixel values of one image to another image.
164 * Currently, the function exits with an error if
165 * the source and destination images are not of the same allocated
168 * Also, the images of type UChar are not yet supported.
170 ***********************************************************CommentEnd********/
173 CopyImage(Image
*image_in
, Image
*image_out
)
175 switch(image_out
->type
)
178 CopyImageI(image_in
,image_out
);
182 CopyImageF(image_in
,image_out
);
189 /***********************************************************CommentBegin******
193 ***********************************************************CommentEnd********/
196 CopyImageI(ImageI
*image_in
, ImageI
*image_out
)
198 SInt
*p_in
= image_in
->data
->s
,
199 *p_out
= image_out
->data
->s
,
202 UInt sx_in
= image_in
->x
,
203 sx_out
= image_out
->x
,
205 sy_out
= image_out
->y
,
206 sxy_in
= sx_in
* sy_in
;
208 p_end
= p_in
+ sxy_in
;
210 while (p_in
!= p_end
)
220 /***********************************************************CommentBegin******
222 * -- CopyImageF -- Copies image_in to image_out
224 ***********************************************************CommentEnd********/
227 CopyImageF(ImageF
*image_in
, ImageF
*image_out
)
229 Float
*p_in
= image_in
->data
->f
,
230 *p_out
= image_out
->data
->f
,
233 UInt sx_in
= image_in
->x
,
234 sx_out
= image_out
->x
,
236 sy_out
= image_out
->y
,
237 sxy_in
= sx_in
* sy_in
;
239 p_end
= p_in
+ sxy_in
;
241 while (p_in
!= p_end
)
251 /***********************************************************CommentBegin******
253 * -- SetConstantImage -- Sets each pixel in the Image to val
256 * Sets each pixel in the Image to val.
259 * Note val is a Float. If image is not a Float image then
260 * val is cast to the appropriate type i.e. SInt, unsigned
264 * Images of type UChar are not yet supported.
266 ***********************************************************CommentEnd********/
269 SetConstantImage(Image
*image
, Float val
)
274 SetConstantImageI(image
,(SInt
)val
);
278 SetConstantImageF(image
,val
);
285 /***********************************************************CommentBegin******
287 * -- SetConstantImageI --
289 ***********************************************************CommentEnd********/
292 SetConstantImageI(ImageI
*image
, SInt val
)
294 SInt
*p
= image
->data
->s
,
297 UInt sxy
= image
->x
* image
->y
;
300 memset (p
, 0, sxy
* 2);
314 /***********************************************************CommentBegin******
316 * -- SetConstantImageF --
318 ***********************************************************CommentEnd********/
321 SetConstantImageF(ImageF
*image
, Float val
)
323 Float
*p
= image
->data
->f
,
326 UInt sxy
= image
->x
* image
->y
;
339 /***********************************************************CommentBegin******
341 * -- SubImage -- Subtracts two images
344 * Subtracts two images and stores the result in the third
345 * i.e. image_out = image_in1 - image_in2
348 * Currently, the function exits with an error if
349 * the source and destination images are not of the same allocated
352 * Also, the images of type UChar are not yet supported.
354 ***********************************************************CommentEnd********/
357 SubImage(Image
*image_in1
, Image
*image_in2
, Image
*image_out
)
359 switch(image_in1
->type
)
362 SubImageI(image_in1
,image_in2
,image_out
);
366 SubImageF(image_in1
,image_in2
,image_out
);
371 /***********************************************************CommentBegin******
375 ***********************************************************CommentEnd********/
378 SubImageI(ImageI
*image_in1
, ImageI
*image_in2
, ImageI
*image_out
)
380 SInt
*p
= image_out
->data
->s
,
381 *p1
= image_in1
->data
->s
,
382 *p2
= image_in2
->data
->s
,
385 UInt sx_in1
= image_in1
->x
,
386 sx_in2
= image_in2
->x
,
387 sx_out
= image_out
->x
,
388 sy_in1
= image_in1
->y
,
389 sy_in2
= image_in2
->y
,
390 sy_out
= image_out
->y
,
391 sxy
= sx_out
* sy_out
;
405 /***********************************************************CommentBegin******
409 ***********************************************************CommentEnd********/
412 SubImageF(ImageF
*image_in1
, ImageF
*image_in2
, ImageF
*image_out
)
414 Float
*p
= image_out
->data
->f
,
415 *p1
= image_in1
->data
->f
,
416 *p2
= image_in2
->data
->f
,
419 UInt sx_in1
= image_in1
->x
,
420 sx_in2
= image_in2
->x
,
421 sx_out
= image_out
->x
,
422 sy_in1
= image_in1
->y
,
423 sy_in2
= image_in2
->y
,
424 sy_out
= image_out
->y
,
425 sxy
= sx_out
* sy_out
;
439 /* The following is Vop maintance functions. */
446 vop
= (Vop
*)ecalloc(1,sizeof(Vop
));
453 AllocVop(UInt x
, UInt y
)
461 /* first allocate memory for the data structure */
467 /* Allocate image fields */
468 y_chan
= AllocImage(x
,y
,SHORT_TYPE
);
469 u_chan
= AllocImage(x
/2,y
/2,SHORT_TYPE
);
470 v_chan
= AllocImage(x
/2,y
/2,SHORT_TYPE
);
472 /* Include image fields in structure */
473 FreeImage(vop
->y_chan
);
474 vop
->y_chan
= y_chan
;
476 FreeImage(vop
->u_chan
);
477 vop
->u_chan
= u_chan
;
479 FreeImage(vop
->v_chan
);
480 vop
->v_chan
= v_chan
;
498 Image
*data
=NULL
; /*SpSc: added the initialization */
502 /* Deallocate memory for image fields */
521 CopyVopNonImageField(Vop
*in
,Vop
*out
)
523 /* out->quant_precision = in->quant_precision;
524 out->bits_per_pixel = in->bits_per_pixel;
526 // out->mod_time_base = in->mod_time_base;
527 // out->time_inc = in->time_inc;
528 out
->prediction_type
= in
->prediction_type
;
529 /* out->rounding_type = in->rounding_type;
530 out->width = in->width;
531 out->height = in->height;
532 out->hor_spat_ref = in->hor_spat_ref;
533 out->ver_spat_ref = in->ver_spat_ref;
534 out->quantizer = in->quantizer;
535 out->intra_quantizer = in->intra_quantizer;
536 out->time_increment_resolution = in->time_increment_resolution;
538 out->intra_acdc_pred_disable = in->intra_acdc_pred_disable;
539 out->fcode_for = in->fcode_for;
540 out->sr_for = in->sr_for;
542 out->intra_dc_vlc_thr = in->intra_dc_vlc_thr;
547 /* The following is Memory allocation functions. */
549 /***********************************************************CommentBegin******
551 * -- emalloc -- Memory allocation with error handling
554 * Memory allocation with error handling.
557 * WARNING: There is a problem when you want to allocate more memory
558 * than size can handle. Malloc gets as argument an unsigned. It poses
559 * a problem when sizeof(unsigned) < sizeof(Char *)...
561 ***********************************************************CommentEnd********/
568 p
= (Char
*) malloc((UInt
)n
);
572 /***********************************************************CommentBegin******
574 * -- ecalloc -- Memory allocation with error handling
577 * Memory allocation with error handling.
580 * WARNING: There is a problem when you want to allocate more memory
581 * than size can handle. Malloc gets as argument an unsigned. It poses
582 * a problem when sizeof(unsigned) < sizeof(Char *)...
584 ***********************************************************CommentEnd********/
587 ecalloc(Int n
, Int s
)
591 p
= (Char
*) calloc((UInt
)n
,(UInt
)s
);
596 /***********************************************************CommentBegin******
598 * -- erealloc -- Memory re-allocation with error handling.
601 * Memory re-allocation with error handling
604 * WARNING: There is a problem when you want to allocate more memory
605 * than size can handle. Malloc gets as argument an unsigned. It poses
606 * a problem when sizeof(unsigned) < sizeof(Char *)...
608 ***********************************************************CommentEnd********/
611 erealloc(Char
*p
, Int n
)
613 p
= (Char
*) realloc(p
,(UInt
)n
);