6 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version. The Blender
12 * Foundation also sells licenses for use in proprietary software under
13 * the Blender License. See http://www.blender.org/BL/ for information
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
26 * All rights reserved.
28 * The Original Code is: all of this file.
30 * Contributor(s): none yet.
32 * ***** END GPL/BL DUAL LICENSE BLOCK *****
36 * \brief This file handles loading and saving of amiga files.
38 * \warning This file contains endian code. Some way should be found to move
39 * this code out of here.
40 * \help Endian #defines are in multiple files!
44 #include "imbuf_patch.h"
46 #include "IMB_imbuf_types.h"
47 #include "IMB_imbuf.h"
50 #include "IMB_allocimbuf.h"
51 #include "IMB_bitplanes.h"
52 #include "IMB_amiga.h"
58 /* actually hard coded endianness */
60 * \brief Makes a 4 bit id for a 32 bit value: Big-endian
61 * \todo How is this used?
63 #define GET_BIG_LONG(x) (((uchar *) (x))[0] << 24 | ((uchar *) (x))[1] << 16 | ((uchar *) (x))[2] << 8 | ((uchar *) (x))[3])
65 * \brief Makes a 4 bit id for a 16? bit value: Little-endian
66 * \todo How is this used?
68 #define GET_LITTLE_LONG(x) (((uchar *) (x))[3] << 24 | ((uchar *) (x))[2] << 16 | ((uchar *) (x))[1] << 8 | ((uchar *) (x))[0])
70 * \brief Converts between little and big endian: 32 bit values
72 #define SWAP_L(x) (((x << 24) & 0xff000000) | ((x << 8) & 0xff0000) | ((x >> 8) & 0xff00) | ((x >> 24) & 0xff))
74 * \brief Converts between little and big endian: 16 bit values
76 #define SWAP_S(x) (((x << 8) & 0xff00) | ((x >> 8) & 0xff))
78 /* more endianness... should move to a separate file... */
79 #if defined(__sgi) || defined (__sparc) || defined (__sparc__) || defined (__PPC__) || defined (__ppc__) || defined (__BIG_ENDIAN__)
80 #define GET_ID GET_BIG_LONG
81 #define LITTLE_LONG SWAP_LONG
83 #define GET_ID GET_LITTLE_LONG
84 #define LITTLE_LONG ENDIAN_NOP
87 static uchar *decodebodyscanl(uchar *body, short bytes, uchar **list, short d)
99 if (i & 128){ /* fill */
100 if (i==128) continue; /* nop */
114 *(point++) = *(body++);
119 if (todo) return (0);
125 static uchar *decodebodyh(struct ImBuf *ibuf, uchar *body)
128 body=decodebodyscanl(body, WIDTHB(ibuf->x), (uchar **)ibuf->planes, ibuf->depth);
132 short skipx,i,bytes,y;
134 list = imb_copyplanelist(ibuf);
135 if (list == 0) return (0);
138 bytes = WIDTHB(ibuf->x);
142 body=decodebodyscanl(body, bytes, (uchar **)list, ibuf->depth);
143 if (body == 0) return (0);
145 for (i=ibuf->depth-1;i>=0;i--){
155 static uchar *decodebodykolum(uchar *body, short bytes, uchar **list, short d, int next)
167 if (i & 128){ /* fill */
168 if (i==128) continue; /* nop */
190 if (todo) return (0);
196 static uchar *decodebodyv(struct ImBuf *ibuf, uchar *body)
199 short skipx,i,bytes,times;
201 list = (uchar **)imb_copyplanelist(ibuf);
202 if (list == 0) return (0);
205 times = WIDTHB(ibuf->x);
206 skipx = ibuf->skipx << 2;
208 for (;times>0;times--){
209 body=decodebodykolum(body,bytes,list,ibuf->depth,skipx);
210 if (body == 0) return (0);
212 for (i=ibuf->depth-1;i>=0;i--){
220 static uchar *makebody(uchar **planes, short bytes, short depth, uchar *buf)
222 uchar *bitplstart,*temp;
224 register uchar last,this,*bitpl;
229 for (;depth>0;depth--){
243 if (this == bitpl[-3]){ /* three identical ones? */
244 todo -= 1; /* set todo */
248 }while (--todo != 0);
250 copy=bitpl-bitplstart;
259 if (copy>MAXDAT) last = MAXDAT;
273 while (*bitpl++ == this){ /* search for first different bye */
274 if (--todo == 0) break; /* or end of line */
277 copy = bitpl-bitplstart;
284 *buf++ = -(MAXRUN-1);
302 short imb_encodebodyh(struct ImBuf *ibuf, int file)
304 uchar *buf, *endbuf, *max;
305 int size, line, ok = TRUE;
309 line = WIDTHB(ibuf->x) * ibuf->depth;
310 line += (line >> 6) + 10;
312 if (size < 16384) size = 16384;
314 buf = (uchar *) malloc(size);
315 if (buf == 0) return (0);
317 max = buf + size - line;
319 list = imb_copyplanelist(ibuf);
329 for (y=ibuf->y;y>0;y--){
330 endbuf = makebody((uchar **)list, WIDTHB(ibuf->x), ibuf->depth, endbuf);
335 if (endbuf >= max || y == 1){
337 if (write(file,buf,size)!=size) ok = -19;
340 for (i=ibuf->depth-1;i>=0;i--){
343 if (ok != TRUE) break;
352 short imb_encodebodyv(struct ImBuf *ibuf, int file)
358 buf = (uchar *) malloc((ibuf->y + (ibuf->y >> 6) + 10) * ibuf->depth);
359 if (buf == 0) return (0);
361 ibufv=IMB_allocImBuf((ibuf->y)<<3,1, ibuf->depth, 0, 1);
369 for(x = WIDTHB(ibuf->x);x>0;x--){
372 for(i = ibuf->depth-1 ;i>=0;i--){
373 register uchar *p1,*p2;
377 skipx = (ibuf->skipx)*sizeof(int *);
378 p1=(uchar *)ibuf->planes[i];
379 p2=(uchar *)ibufv->planes[i];
382 for (y=ibuf->y;y>0;y--){
389 endbuf=makebody((uchar **)ibufv->planes, ibuf->y, ibuf->depth, buf);
390 if (endbuf==0) return (-20);
391 if (write(file,buf,endbuf-buf)!=endbuf-buf) return (-19);
394 IMB_freeImBuf(ibufv);
398 static uchar *readbody(struct ImBuf *ibuf, uchar *body)
400 int skipbuf,skipbdy,depth,y,offset = 0;
402 skipbuf = ibuf->skipx;
403 skipbdy = WIDTHB(ibuf->x);
405 for (y = ibuf->y; y> 0; y--){
406 for( depth = 0; depth < ibuf->depth; depth ++){
407 memcpy(ibuf->planes[depth] + offset, body, skipbdy);
416 * \brief Loads an amiga (.ami) image.
418 * \param iffmem A pointer to a memory location.
419 * \param flags A set of bit flags determining what parts of the image to load.
420 * \return Returns 0 if loading the image fails, otherwise returns a pointer to an ImBuf.
422 * I am fairly certain of what is going on in this function, so if I am
423 * wrong, please let me know, so I can update the docs!
425 struct ImBuf *imb_loadamiga(int *iffmem,int flags)
427 int chunk,totlen,len,*cmap=0,cmaplen,*mem,ftype=0;
429 struct BitMapHeader bmhd;
430 struct ImBuf *ibuf=0;
433 * \internal The memory address to the data is copiend into mem.
437 * \internal The w member of the BitMapHeader is initialized to 0 because
438 * it will be tested to see if it has been set later.
443 * \internal The first three chunks must have the form: FORMxxxxILBM
444 * else the function returns with 0;
445 * FORM and ILBM are defined in imbuf_patch.h
447 if (GET_ID(mem) != FORM) return (0);
448 if (GET_ID(mem+2) != ILBM) return (0);
450 * \internal The second chunk is the total size of the image.
452 totlen= (GET_BIG_LONG(mem+1) + 1) & ~1;
454 * \internal mem is incremented to skip the first three chunks.
458 * \internal Anyone know why the total length is decreased by four here?
464 * \internal The while loop retrieves at most four blocks of memory:
465 * - bmhd: the bit map header
466 * - body: which is the image data
467 * - cmap: the color map
468 * - ftype: the file type (what does CAMG stand for?)
469 * The body and the bitmap header are required.
473 len= (GET_BIG_LONG(mem+1) + 1) & ~1;
480 memcpy(&bmhd, mem, sizeof(struct BitMapHeader));
482 bmhd.w = BIG_SHORT(bmhd.w);
483 bmhd.h = BIG_SHORT(bmhd.h);
484 bmhd.x = BIG_SHORT(bmhd.x);
485 bmhd.y = BIG_SHORT(bmhd.y);
486 bmhd.transparentColor = BIG_SHORT(bmhd.transparentColor);
487 bmhd.pageWidth = BIG_SHORT(bmhd.pageWidth);
488 bmhd.pageHeight = BIG_SHORT(bmhd.pageHeight);
499 ftype = GET_BIG_LONG(mem);
502 mem = (int *)((uchar *)mem +len);
504 * \intern Anything after the first BODY ID is discarded.
509 * \internal After the while loop, the existance of body and bmhd are detected.
511 if (bmhd.w == 0) return (0);
512 if (body == 0) return (0);
515 * \internal if the IB_test bit is set in flags, don't do masking.
516 * (I'm not too sure about this) In any case, allocate the memory
517 * for the imbuf, and return 0 if this fails.
519 if (flags & IB_test) ibuf = IMB_allocImBuf(bmhd.w, bmhd.h, bmhd.nPlanes, 0, 0);
520 else ibuf = IMB_allocImBuf(bmhd.w, bmhd.h, bmhd.nPlanes + (bmhd.masking & 1),0,1);
522 if (ibuf == 0) return (0);
525 * \internal Set the AMI bit in ftype.
527 ibuf->ftype = (ftype | AMI);
530 * \internal If there was a cmap chunk in the data, add the cmap
531 * to the ImBuf and copy the data there.
535 ibuf->maxcol = cmaplen;
536 imb_addcmapImBuf(ibuf);
537 /* this functions needs a 3rd arg: the number of
538 * columns.... why did this work before? */
539 /* imb_makecolarray(ibuf, cmap); */
540 imb_makecolarray(ibuf, cmap, 0);
544 * \internal If the IB_test bit of flags was set, we're done:
545 * If the IB_freem bit is set, free the data pointed to by iffmem.
548 if (flags & IB_test){
549 if (flags & IB_freem) free(iffmem);
554 * \internal Check the bitmap header to see if there is any
555 * compression. 0 is no, 1 is horizontal, 2 is vertical.
556 * Load the data according to the type of compression.
558 switch (bmhd.compression){
560 body= readbody(ibuf, body);
563 body= decodebodyh(ibuf,body);
566 body= decodebodyv(ibuf,body);
567 ibuf->type |= IB_subdlta;
572 * \internal If the IB_freem bit is set, free the data pointed to by iffmem.
574 if (flags & IB_freem) free(iffmem);
577 * \internal If there was some problem loading the body
578 * data, free the memory already allocated in ibuf and
587 * \internal Set the bit depth to the number of planes in bmhd.
588 * This discards the "stencil" data (What is the stencil? Alpha channel?)
591 ibuf->depth = bmhd.nPlanes;
594 * \internal If the IB_rect bit is set in flags, add the rect and
595 * get rid of the planes.
597 if (flags & IB_rect){
598 imb_addrectImBuf(ibuf);
600 imb_freeplanesImBuf(ibuf);
602 * \internal If the image has a color map, apply it.
605 if ((flags & IB_cmap) == 0) IMB_applycmap(ibuf);
606 } else if (ibuf->depth == 18){ /** \internal No color map, and the bit depths is 18, convert to 24-bit */
611 for(i=ibuf->x * ibuf->y ; i>0 ; i--){
613 col = ((col & 0x3f000) << 6) + ((col & 0xfc0) << 4) + ((col & 0x3f) << 2);
614 col += (col & 0xc0c0c0) >> 6;
618 } else if (ibuf->depth <= 8) { /** \internal No colormap and no 24 bits, so it's b&w */
622 if (ibuf->depth < 8){
623 rect = (uchar *) ibuf->rect;
625 shift = 8 - ibuf->depth;
626 for (size = ibuf->x * ibuf->y; size > 0; size --){
631 rect = (uchar *) ibuf->rect;
632 for (size = ibuf->x * ibuf->y; size > 0; size --){
633 rect[1] = rect[2] = rect[3];
641 * \internal Anyone know what IB_ttob is? What does IMB_flipy do?
643 if ((flags & IB_ttob) == 0) IMB_flipy(ibuf);
646 * \internal Last thing to do before returning is to flip the bits from rgba to abgr.
650 IMB_convert_rgba_to_abgr(ibuf->x*ibuf->y, ibuf->rect);