Gridarta Editor
CollectedFaceProvider.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2023 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.model.face;
21 
22 import java.io.File;
23 import java.io.FileNotFoundException;
24 import java.io.IOException;
25 import java.io.RandomAccessFile;
26 import java.util.HashMap;
27 import java.util.Map;
28 import javax.swing.ImageIcon;
29 import org.jetbrains.annotations.NotNull;
30 import org.jetbrains.annotations.Nullable;
31 
42 
47  private final Map<String, Long> positions = new HashMap<>();
48 
52  @NotNull
53  private final RandomAccessFile file;
54 
60  public CollectedFaceProvider(final File file) throws FileNotFoundException {
61  this.file = new RandomAccessFile(file, "r");
62  }
63 
70  public CollectedFaceProvider(final String fileName) throws FileNotFoundException {
71  file = new RandomAccessFile(fileName, "r");
72  }
73 
80  public void addInfo(@NotNull final String faceName, final int pos, final int size) {
81  positions.put(faceName, (long) pos << 32 | size);
82  }
83 
84  @Nullable
85  @Override
86  protected ImageIcon createImage(@NotNull final String faceName, final long stretch) {
87  final Long position = positions.get(faceName);
88  if (position == null) {
89  return null;
90  }
91  final byte[] buf;
92  try {
93  final long posI = position;
94  file.seek(posI >> 32);
95  buf = new byte[(int) posI];
96  file.readFully(buf);
97  } catch (final IOException e) {
98  return null;
99  }
100  return new ImageIcon(buf);
101  }
102 
103 }
net.sf.gridarta.model.face.AbstractFaceProvider
Abstract Base class for FaceProviders which implements a memory sensitive cache.
Definition: AbstractFaceProvider.java:36
net.sf.gridarta.model.face.CollectedFaceProvider.file
final RandomAccessFile file
The file to read from.
Definition: CollectedFaceProvider.java:53
net.sf.gridarta.model.face.CollectedFaceProvider.positions
final Map< String, Long > positions
The icon position and size are stored here.
Definition: CollectedFaceProvider.java:47
net.sf.gridarta.model.face.CollectedFaceProvider.addInfo
void addInfo(@NotNull final String faceName, final int pos, final int size)
Report position and size of a face for loading it later.
Definition: CollectedFaceProvider.java:80
net.sf.gridarta.model.face.CollectedFaceProvider.CollectedFaceProvider
CollectedFaceProvider(final String fileName)
Creates a new instance.
Definition: CollectedFaceProvider.java:70
net.sf.gridarta.model.face.CollectedFaceProvider
Implementation of FaceProvider which reads images from the collected PNG archive.
Definition: CollectedFaceProvider.java:41
net.sf.gridarta.model.face.CollectedFaceProvider.CollectedFaceProvider
CollectedFaceProvider(final File file)
Creates a new instance.
Definition: CollectedFaceProvider.java:60
net.sf.gridarta.model.face.CollectedFaceProvider.createImage
ImageIcon createImage(@NotNull final String faceName, final long stretch)
Creates an image not found in the cache.
Definition: CollectedFaceProvider.java:86