Giant commit!
[blender.git] / source / blender / imbuf / intern / writeimage.c
1 /**
2  *
3  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version. The Blender
9  * Foundation also sells licenses for use in proprietary software under
10  * the Blender License.  See http://www.blender.org/BL/ for information
11  * about this.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL/BL DUAL LICENSE BLOCK *****
30  * writeimage.c
31  *
32  * $Id$
33  */
34
35 #ifdef WIN32
36 #include <io.h>
37 #endif
38
39 #include "BKE_global.h"
40 #include "BLI_blenlib.h"
41
42 #include "imbuf.h"
43 #include "imbuf_patch.h"
44
45 #include "IMB_imbuf_types.h"
46 #include "IMB_imbuf.h"
47
48 #include "IMB_allocimbuf.h"
49
50 #include "IMB_targa.h"
51 #include "IMB_jpeg.h"
52 #include "IMB_iris.h"
53 #include "IMB_ham.h"
54 #include "IMB_hamx.h"
55 #include "IMB_amiga.h"
56 #include "IMB_png.h"
57 #include "IMB_bmp.h"
58 #include "IMB_tiff.h"
59 #include "IMB_radiance_hdr.h"
60 #ifdef WITH_OPENEXR
61 #include "openexr/openexr_api.h"
62 #endif
63
64 #include "IMB_iff.h"
65 #include "IMB_bitplanes.h"
66 #include "IMB_divers.h"
67
68 /* added facility to copy with saving non-float rects */
69
70 short IMB_saveiff(struct ImBuf *ibuf, char *name, int flags)
71 {
72         short ok=TRUE,delpl=FALSE;
73         int file = -1;
74
75         if (ibuf==0) return (FALSE);
76         ibuf->flags = flags;
77
78         /* Put formats that take a filename here */
79         if (IS_jpg(ibuf)) {
80                 if(ibuf->rect==NULL && ibuf->rect_float)
81                         IMB_rect_from_float(ibuf);
82                 return imb_savejpeg(ibuf, name, flags);
83         }
84         if (IS_radhdr(ibuf)) {
85                 return imb_savehdr(ibuf, name, flags);
86         }
87         if (IS_png(ibuf)) {
88                 if(ibuf->rect==NULL && ibuf->rect_float)
89                         IMB_rect_from_float(ibuf);
90                 return imb_savepng(ibuf, name, flags);
91         }
92         if (IS_bmp(ibuf)) {
93                 if(ibuf->rect==NULL && ibuf->rect_float)
94                         IMB_rect_from_float(ibuf);
95                 return imb_savebmp(ibuf, name, flags);
96         }
97         if (IS_tga(ibuf)) {
98                 if(ibuf->rect==NULL && ibuf->rect_float)
99                         IMB_rect_from_float(ibuf);
100                 return imb_savetarga(ibuf, name, flags);
101         }
102         if (IS_iris(ibuf)) {
103                 if(ibuf->rect==NULL && ibuf->rect_float)
104                         IMB_rect_from_float(ibuf);
105                 return imb_saveiris(ibuf, name, flags);
106         }
107         if (G.have_libtiff && IS_tiff(ibuf)) {
108                 if(ibuf->rect==NULL && ibuf->rect_float)
109                         IMB_rect_from_float(ibuf);
110                 return imb_savetiff(ibuf, name, flags);
111         }
112 #ifdef WITH_OPENEXR
113         if (IS_openexr(ibuf)) {
114                 return imb_save_openexr(ibuf, name, flags);
115         }
116 #endif
117
118         file = open(name, O_BINARY | O_RDWR | O_CREAT | O_TRUNC, 0666);
119         if (file < 0) return (FALSE);
120
121         if (flags & IB_rect){
122                 if (ibuf->cmap){
123                         imb_checkncols(ibuf);
124                 }
125         }
126
127         /* Put formats that take a filehandle here */
128         ok = imb_start_iff(ibuf,file);
129         if (IS_amiga(ibuf)){
130                 IMB_flipy(ibuf);
131                 if (flags & IB_rect){
132                         if ((flags & IB_cmap) == 0) {
133                                 if (IS_ham(ibuf)){
134                                         if (ok) ok = imb_converttoham(ibuf);
135                                 }else if (ibuf->cmap){
136                                         if (ok) ok = IMB_converttocmap(ibuf);
137                                 }
138                         }
139                         if (ok){
140                                 if (ibuf->planes==0){
141                                         delpl=TRUE;
142                                         ok=imb_addplanesImBuf(ibuf);
143                                 }
144                                 imb_longtobp(ibuf);
145                         }
146                 }
147
148                 if (flags & IB_vert){
149                         if (ok) ok = imb_encodebodyv(ibuf,file);
150                 }
151                 else{
152                         if (ok) ok = imb_encodebodyh(ibuf,file);
153                 }
154                 if (ok) ok = imb_update_iff(file,BODY);
155         }else if (IS_anim(ibuf)) {
156                 if (ok) ok = imb_enc_anim(ibuf, file);
157                 if (ok) ok = imb_update_iff(file, BODY);
158         }
159         close(file);
160
161         if (ok==FALSE) {
162                 fprintf(stderr,"Couldn't save picture.\n");
163         }       
164         if (delpl) imb_freeplanesImBuf(ibuf);
165
166         return (ok);
167 }
168