summaryrefslogtreecommitdiff
path: root/src/net/sourceforge/plantuml/mjpeg/MJPEGGenerator.java
blob: e61ecf2c9777df36f31903051d3a10f16c830f1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
/* ========================================================================
 * PlantUML : a free UML diagram generator
 * ========================================================================
 *
 * (C) Copyright 2009-2014, Arnaud Roques
 *
 * Project Info:  http://plantuml.sourceforge.net
 * 
 * This file is part of PlantUML.
 *
 * Licensed under The MIT License (Massachusetts Institute of Technology License)
 * 
 * See http://opensource.org/licenses/MIT
 * 
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
 * IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 *
 * Original Author:  Arnaud Roques
 */
/*
 * MJPEGGenerator.java
 *
 * Created on April 17, 2006, 11:48 PM
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package net.sourceforge.plantuml.mjpeg;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Arrays;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

import net.sourceforge.plantuml.StringUtils;

/**
 *
 * @author monceaux
 */
public class MJPEGGenerator
{
    /*
     *  Info needed for MJPEG AVI
     *
     *  - size of file minus "RIFF & 4 byte file size"
     *
     */
    
    int width = 0;
    int height = 0;
    double framerate = 0;
    int numFrames = 0;
    File aviFile = null;
    FileOutputStream aviOutput = null;
    FileChannel aviChannel = null;
    
    long riffOffset = 0;
    long aviMovieOffset = 0;
    
    AVIIndexList indexlist = null;
    
    /** Creates a new instance of MJPEGGenerator */
    public MJPEGGenerator(File aviFile, int width, int height, double framerate, int numFrames) throws IOException
    {
        this.aviFile = aviFile;
        this.width = width;
        this.height = height;
        this.framerate = framerate;
        this.numFrames = numFrames;
        aviOutput = new FileOutputStream(aviFile);
        aviChannel = aviOutput.getChannel();
        
        RIFFHeader rh = new RIFFHeader();
        aviOutput.write(rh.toBytes());
        aviOutput.write(new AVIMainHeader().toBytes());
        aviOutput.write(new AVIStreamList().toBytes());
        aviOutput.write(new AVIStreamHeader().toBytes());
        aviOutput.write(new AVIStreamFormat().toBytes());
        aviOutput.write(new AVIJunk().toBytes());
        aviMovieOffset = aviChannel.position();
        aviOutput.write(new AVIMovieList().toBytes());
        indexlist = new AVIIndexList();
    }
    
    public void addImage(Image image) throws IOException
    {
        byte[] fcc = new byte[]{'0','0','d','b'};
        byte[] imagedata = writeImageToBytes(image);
        int useLength = imagedata.length;
        long position = aviChannel.position();
        int extra = (useLength+(int)position) % 4;
        if(extra > 0)
            useLength = useLength + extra;
        
        indexlist.addAVIIndex((int)position,useLength);
        
        aviOutput.write(fcc);
        aviOutput.write(intBytes(swapInt(useLength)));
        aviOutput.write(imagedata);
        if(extra > 0)
        {
            for(int i = 0; i < extra; i++)
                aviOutput.write(0);
        }
        imagedata = null;
    }
    
    public void finishAVI() throws IOException
    {
        byte[] indexlistBytes = indexlist.toBytes();
        aviOutput.write(indexlistBytes);
        aviOutput.close();
        long size = aviFile.length();
        RandomAccessFile raf = new RandomAccessFile(aviFile, "rw");
        raf.seek(4);
        raf.write(intBytes(swapInt((int)size - 8)));
        raf.seek(aviMovieOffset+4);
        raf.write(intBytes(swapInt((int)(size - 8 - aviMovieOffset - indexlistBytes.length))));
        raf.close();
    }
    
//    public void writeAVI(File file) throws Exception
//    {
//        OutputStream os = new FileOutputStream(file);
//        
//        // RIFFHeader
//        // AVIMainHeader
//        // AVIStreamList
//        // AVIStreamHeader
//        // AVIStreamFormat
//        // write 00db and image bytes...
//    }
    
    public static int swapInt(int v)
    {
        return  (v >>> 24) | (v << 24) |
                ((v << 8) & 0x00FF0000) | ((v >> 8) & 0x0000FF00);
    }
    
    public static short swapShort(short v)
    {
        return (short)((v >>> 8) | (v << 8));
    }
    
    public static byte[] intBytes(int i)
    {
        byte[] b = new byte[4];
        b[0] = (byte)(i >>> 24);
        b[1] = (byte)((i >>> 16) & 0x000000FF);
        b[2] = (byte)((i >>> 8) & 0x000000FF);
        b[3] = (byte)(i & 0x000000FF);
        
        return b;
    }
    
    public static byte[] shortBytes(short i)
    {
        byte[] b = new byte[2];
        b[0] = (byte)(i >>> 8);
        b[1] = (byte)(i & 0x000000FF);
        
        return b;
    }
    
    private class RIFFHeader
    {
        public byte[] fcc = new byte[]{'R','I','F','F'};
        public int fileSize = 0;
        public byte[] fcc2 = new byte[]{'A','V','I',' '};
        public byte[] fcc3 = new byte[]{'L','I','S','T'};
        public int listSize = 200;
        public byte[] fcc4 = new byte[]{'h','d','r','l'};
        
        public RIFFHeader()
        {
            
        }
        
        public byte[] toBytes() throws IOException
        {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            baos.write(fcc);
            baos.write(intBytes(swapInt(fileSize)));
            baos.write(fcc2);
            baos.write(fcc3);
            baos.write(intBytes(swapInt(listSize)));
            baos.write(fcc4);
            baos.close();
            
            return baos.toByteArray();
        }
    }
    
    private class AVIMainHeader
    {
        /*
         *
         FOURCC fcc;
    DWORD  cb;
    DWORD  dwMicroSecPerFrame;
    DWORD  dwMaxBytesPerSec;
    DWORD  dwPaddingGranularity;
    DWORD  dwFlags;
    DWORD  dwTotalFrames;
    DWORD  dwInitialFrames;
    DWORD  dwStreams;
    DWORD  dwSuggestedBufferSize;
    DWORD  dwWidth;
    DWORD  dwHeight;
    DWORD  dwReserved[4];
         */
        
        public byte[] fcc = new byte[]{'a','v','i','h'};
        public int cb = 56;
        public int dwMicroSecPerFrame = 0; //  (1 / frames per sec) * 1,000,000
        public int dwMaxBytesPerSec = 10000000;
        public int dwPaddingGranularity = 0;
        public int dwFlags =  65552;
        public int dwTotalFrames = 0;  // replace with correct value
        public int dwInitialFrames = 0;
        public int dwStreams = 1;
        public int dwSuggestedBufferSize = 0;
        public int dwWidth = 0;  // replace with correct value
        public int dwHeight = 0; // replace with correct value
        public int[] dwReserved = new int[4];
        
        public AVIMainHeader()
        {
            dwMicroSecPerFrame = (int)((1.0/framerate)*1000000.0);
            dwWidth = width;
            dwHeight = height;
            dwTotalFrames = numFrames;
        }
        
        public byte[] toBytes() throws IOException
        {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            baos.write(fcc);
            baos.write(intBytes(swapInt(cb)));
            baos.write(intBytes(swapInt(dwMicroSecPerFrame)));
            baos.write(intBytes(swapInt(dwMaxBytesPerSec)));
            baos.write(intBytes(swapInt(dwPaddingGranularity)));
            baos.write(intBytes(swapInt(dwFlags)));
            baos.write(intBytes(swapInt(dwTotalFrames)));
            baos.write(intBytes(swapInt(dwInitialFrames)));
            baos.write(intBytes(swapInt(dwStreams)));
            baos.write(intBytes(swapInt(dwSuggestedBufferSize)));
            baos.write(intBytes(swapInt(dwWidth)));
            baos.write(intBytes(swapInt(dwHeight)));
            baos.write(intBytes(swapInt(dwReserved[0])));
            baos.write(intBytes(swapInt(dwReserved[1])));
            baos.write(intBytes(swapInt(dwReserved[2])));
            baos.write(intBytes(swapInt(dwReserved[3])));
            baos.close();
            
            return baos.toByteArray();
        }
    }
    
    private class AVIStreamList
    {
        public byte[] fcc = new byte[]{'L','I','S','T'};
        public int size = 124;
        public byte[] fcc2 = new byte[]{'s','t','r','l'};
        
        public AVIStreamList()
        {
            
        }
        
        public byte[] toBytes() throws IOException
        {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            baos.write(fcc);
            baos.write(intBytes(swapInt(size)));
            baos.write(fcc2);
            baos.close();
            
            return baos.toByteArray();
        }
    }
    
    private class AVIStreamHeader
    {
        /*
         FOURCC fcc;
     DWORD  cb;
     FOURCC fccType;
     FOURCC fccHandler;
     DWORD  dwFlags;
     WORD   wPriority;
     WORD   wLanguage;
     DWORD  dwInitialFrames;
     DWORD  dwScale;
     DWORD  dwRate;
     DWORD  dwStart;
     DWORD  dwLength;
     DWORD  dwSuggestedBufferSize;
     DWORD  dwQuality;
     DWORD  dwSampleSize;
     struct {
         short int left;
         short int top;
         short int right;
         short int bottom;
     }  rcFrame;
         */
        
        public byte[] fcc = new byte[]{'s','t','r','h'};
        public int cb = 64;
        public byte[] fccType = new byte[]{'v','i','d','s'};
        public byte[] fccHandler = new byte[]{'M','J','P','G'};
        public int dwFlags = 0;
        public short wPriority = 0;
        public short wLanguage = 0;
        public int dwInitialFrames = 0;
        public int dwScale = 0; // microseconds per frame
        public int dwRate = 1000000; // dwRate / dwScale = frame rate
        public int dwStart = 0;
        public int dwLength = 0; // num frames
        public int dwSuggestedBufferSize = 0;
        public int dwQuality = -1;
        public int dwSampleSize = 0;
        public int left = 0;
        public int top = 0;
        public int right = 0;
        public int bottom = 0;
        
        public AVIStreamHeader()
        {
            dwScale = (int)((1.0/framerate)*1000000.0);
            dwLength = numFrames;
        }
        
        public byte[] toBytes() throws IOException
        {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            baos.write(fcc);
            baos.write(intBytes(swapInt(cb)));
            baos.write(fccType);
            baos.write(fccHandler);
            baos.write(intBytes(swapInt(dwFlags)));
            baos.write(shortBytes(swapShort(wPriority)));
            baos.write(shortBytes(swapShort(wLanguage)));
            baos.write(intBytes(swapInt(dwInitialFrames)));
            baos.write(intBytes(swapInt(dwScale)));
            baos.write(intBytes(swapInt(dwRate)));
            baos.write(intBytes(swapInt(dwStart)));
            baos.write(intBytes(swapInt(dwLength)));
            baos.write(intBytes(swapInt(dwSuggestedBufferSize)));
            baos.write(intBytes(swapInt(dwQuality)));
            baos.write(intBytes(swapInt(dwSampleSize)));
            baos.write(intBytes(swapInt(left)));
            baos.write(intBytes(swapInt(top)));
            baos.write(intBytes(swapInt(right)));
            baos.write(intBytes(swapInt(bottom)));
            baos.close();
            
            return baos.toByteArray();
        }
    }
    
    private class AVIStreamFormat
    {
        /*
         FOURCC fcc;
     DWORD  cb;
     DWORD  biSize;
    LONG   biWidth;
    LONG   biHeight;
    WORD   biPlanes;
    WORD   biBitCount;
    DWORD  biCompression;
    DWORD  biSizeImage;
    LONG   biXPelsPerMeter;
    LONG   biYPelsPerMeter;
    DWORD  biClrUsed;
    DWORD  biClrImportant;
         */
        
        public byte[] fcc = new byte[]{'s','t','r','f'};
        public int cb = 40;
        public int biSize = 40; // same as cb
        public int biWidth = 0;
        public int biHeight = 0;
        public short biPlanes = 1;
        public short biBitCount = 24;
        public byte[] biCompression = new byte[]{'M','J','P','G'};
        public int biSizeImage = 0; // width x height in pixels
        public int biXPelsPerMeter = 0;
        public int biYPelsPerMeter = 0;
        public int biClrUsed = 0;
        public int biClrImportant = 0;
        
        
        public AVIStreamFormat()
        {
            biWidth = width;
            biHeight = height;
            biSizeImage = width * height;
        }
        
        public byte[] toBytes() throws IOException
        {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            baos.write(fcc);
            baos.write(intBytes(swapInt(cb)));
            baos.write(intBytes(swapInt(biSize)));
            baos.write(intBytes(swapInt(biWidth)));
            baos.write(intBytes(swapInt(biHeight)));
            baos.write(shortBytes(swapShort(biPlanes)));
            baos.write(shortBytes(swapShort(biBitCount)));
            baos.write(biCompression);
            baos.write(intBytes(swapInt(biSizeImage)));
            baos.write(intBytes(swapInt(biXPelsPerMeter)));
            baos.write(intBytes(swapInt(biYPelsPerMeter)));
            baos.write(intBytes(swapInt(biClrUsed)));
            baos.write(intBytes(swapInt(biClrImportant)));
            baos.close();
            
            return baos.toByteArray();
        }
    }
    
    private class AVIMovieList
    {
        public byte[] fcc = new byte[]{'L','I','S','T'};
        public int listSize = 0;
        public byte[] fcc2 = new byte[]{'m','o','v','i'};
        // 00db size jpg image data ...
        
        public AVIMovieList()
        {
            
        }
        
        public byte[] toBytes() throws IOException
        {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            baos.write(fcc);
            baos.write(intBytes(swapInt(listSize)));
            baos.write(fcc2);
            baos.close();
            
            return baos.toByteArray();
        }
    }
    
    private class AVIIndexList
    {
        public byte[] fcc = new byte[]{'i','d','x','1'};
        public int cb = 0;
        public ArrayList ind = new ArrayList();
        
        public AVIIndexList()
        {
            
        }
        
        public void addAVIIndex(AVIIndex ai)
        {
            ind.add(ai);
        }
        
        public void addAVIIndex(int dwOffset, int dwSize)
        {
            ind.add(new AVIIndex(dwOffset, dwSize));
        }
        
        public byte[] toBytes() throws IOException
        {
            cb = 16 * ind.size();
            
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            baos.write(fcc);
            baos.write(intBytes(swapInt(cb)));
            for(int i = 0; i < ind.size(); i++)
            {
                AVIIndex in = (AVIIndex)ind.get(i);
                baos.write(in.toBytes());
            }
            
            baos.close();
            
            return baos.toByteArray();
        }
    }
    
    private class AVIIndex
    {
        public byte[] fcc = new byte[]{'0','0','d','b'};
        public int dwFlags = 16;
        public int dwOffset = 0;
        public int dwSize = 0;
        
        public AVIIndex(int dwOffset, int dwSize)
        {
            this.dwOffset = dwOffset;
            this.dwSize = dwSize;
        }
        
        public byte[] toBytes() throws IOException
        {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            baos.write(fcc);
            baos.write(intBytes(swapInt(dwFlags)));
            baos.write(intBytes(swapInt(dwOffset)));
            baos.write(intBytes(swapInt(dwSize)));
            baos.close();
            
            return baos.toByteArray();
        }
    }
    
    private class AVIJunk
    {
        public byte[] fcc = new byte[]{'J','U','N','K'};
        public int size = 1808;
        public byte[] data = new byte[size];
        
        public AVIJunk()
        {
            Arrays.fill(data,(byte)0);
        }
        
        public byte[] toBytes() throws IOException
        {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            baos.write(fcc);
            baos.write(intBytes(swapInt(size)));
            baos.write(data);
            baos.close();
            
            return baos.toByteArray();
        }
    }
    
    private byte[] writeImageToBytes(Image image) throws IOException
    {
        BufferedImage bi = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Graphics2D g = bi.createGraphics();
        g.drawImage(image,0,0,width,height,null);
        ImageIO.write(bi,"jpg",baos);
        baos.close();
        bi = null;
        g = null;
        
        return baos.toByteArray();
    }
    
    public static void main(String[] args) throws Exception
    {
        double framerate = 12.0;
        double transitionDuration = 1; // seconds
        double slideDuration = 3; // seconds
        
        File photoDir = new File(args[0]);
        File[] files = photoDir.listFiles(new FilenameFilter(){
            public boolean accept(File dir, String name)
            {
                if(StringUtils.goLowerCase(name).endsWith("jpg"))
                    return true;
                return false;
            }
        });
        
        
        int numFrames = (int)(files.length * framerate * (slideDuration + transitionDuration) + (transitionDuration * framerate));
        MJPEGGenerator m = new MJPEGGenerator(new File(args[1]), 640, 480, framerate, numFrames);
        for(int i = 0; i < files.length; i++)
        {
            System.out.println("processing file "+i);
            ImageIcon ii = new ImageIcon(files[i].getCanonicalPath());
            m.addImage(ii.getImage());
        }
        m.finishAVI();
    }
    
}