Skip to content

Commit

Permalink
feat(ResourceManager): init
Browse files Browse the repository at this point in the history
  • Loading branch information
Dituon committed Jul 14, 2024
1 parent 04b2cea commit f6296a2
Show file tree
Hide file tree
Showing 25 changed files with 576 additions and 31 deletions.
7 changes: 5 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ repositories {
}

dependencies {
compileOnly 'org.projectlombok:lombok:1.18.4'
annotationProcessor 'org.projectlombok:lombok:1.18.4'
compileOnly 'org.projectlombok:lombok:1.18.34'
annotationProcessor 'org.projectlombok:lombok:1.18.34'

implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.3'
implementation "com.madgag:animated-gif-lib:1.4"
Expand All @@ -30,6 +30,9 @@ dependencies {

testImplementation "junit:junit:4.11"
testImplementation 'org.codehaus.groovy:groovy-json:3.0.18'

testCompileOnly 'org.projectlombok:lombok:1.18.34'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.34'
}

java {
Expand Down
1 change: 0 additions & 1 deletion src/main/java/moe/dituon/petpet/server/FormDataParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public FormDataParser(ServerPetService service, HttpExchange httpExchange) throw
int startPart = offsets.get(idx);
int endPart = payload.length;
if (idx < offsets.size() - 1) {
System.out.println(1);
endPart = offsets.get(idx + 1);
}
int partLength = endPart - startPart;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/moe/dituon/petpet/server/PetDataDTO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import kotlinx.serialization.json.Json
import moe.dituon.petpet.share.AvatarType
import moe.dituon.petpet.share.BasePetService.VERSION
import moe.dituon.petpet.share.TemplateDTO
import kotlin.streams.toList
import java.util.stream.Collectors

@Serializable
data class PetDataDTO(
Expand All @@ -21,7 +21,7 @@ data class PetDataDTO(
if (data.hidden == false) dataList.add(
PetDataObject(
key,
data.avatar.stream().map { a -> a.type }.toList(),
data.avatar.stream().map { a -> a.type }.collect(Collectors.toList()),
data.alias ?: emptyList()
)
)
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/moe/dituon/petpet/share/AvatarModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ private void buildImage() {
} else {
imageList = IntStream.range(0, maxLength)
.mapToObj(i -> imageList.get(i % imageList.size()))
.collect(Collectors.toList());;
.collect(Collectors.toList());
;
}
}
if (imageList.size() == 1) {
Expand Down Expand Up @@ -324,7 +325,7 @@ image, x, y, radius, getNElement(bulgeFilter.getStrength(), i)
sFilter.setTurbulence(getNElement(swimFilter.getTurbulence(), i));
sFilter.setTime(getNElement(swimFilter.getTime(), i));
image = sFilter.filter(image, null);
} else if (filter instanceof AvatarOilFilter){
} else if (filter instanceof AvatarOilFilter) {
AvatarOilFilter oilFilter = (AvatarOilFilter) filter;
PetpetOilFilter oFilter = new PetpetOilFilter();
oFilter.setLevels((int) getNElement(oilFilter.getLevels(), i));
Expand All @@ -336,7 +337,7 @@ image, x, y, radius, getNElement(bulgeFilter.getStrength(), i)
return image;
}

private static float getNElement(float[] array, int i){
private static float getNElement(float[] array, int i) {
return array[i % array.length];
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/moe/dituon/petpet/share/BackgroundModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public BackgroundModel(
: data.getLength();
}

public BufferedImage[] getImages(){
public BufferedImage[] getImages() {
var arr = new BufferedImage[this.length];
Arrays.fill(arr, getImage());
return arr;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/moe/dituon/petpet/share/BaseConfigFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static GifAvatarExtraDataProvider getGifAvatarExtraDataFromUrls(
}
}

public static GifAvatarExtraDataProvider toGifAvatarExtraDataProvider(AvatarExtraDataProvider extraData){
public static GifAvatarExtraDataProvider toGifAvatarExtraDataProvider(AvatarExtraDataProvider extraData) {
return new GifAvatarExtraDataProvider(
extraData.getFromAvatar() != null ? () -> List.of(extraData.getFromAvatar().invoke()) : null,
extraData.getToAvatar() != null ? () -> List.of(extraData.getToAvatar().invoke()) : null,
Expand All @@ -64,7 +64,7 @@ public static GifAvatarExtraDataProvider toGifAvatarExtraDataProvider(AvatarExtr
);
}

public static GifAvatarExtraDataProvider cacheAvatarExtraDataProvider(GifAvatarExtraDataProvider provider){
public static GifAvatarExtraDataProvider cacheAvatarExtraDataProvider(GifAvatarExtraDataProvider provider) {
var from = provider.getFromAvatar() == null ? null : provider.getFromAvatar().invoke();
var to = provider.getToAvatar() == null ? null : provider.getToAvatar().invoke();
var group = provider.getGroupAvatar() == null ? null : provider.getGroupAvatar().invoke();
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/moe/dituon/petpet/share/BaseImageMaker.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

public class BaseImageMaker {
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/moe/dituon/petpet/share/BaseLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.logging.*;
import java.util.logging.Formatter;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.StreamHandler;

import static java.util.logging.Level.INFO;

Expand Down
12 changes: 6 additions & 6 deletions src/main/java/moe/dituon/petpet/share/BufferedGifEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,28 +69,28 @@ private void configureRootMetadata(int delay, boolean loop) throws IIOInvalidTre
child.setAttribute("authenticationCode", "2.0");

int loopContinuously = loop ? 0 : 1;
child.setUserObject(new byte[]{ 0x1, (byte) (loopContinuously & 0xFF), (byte) (0)});
child.setUserObject(new byte[]{0x1, (byte) (loopContinuously & 0xFF), (byte) (0)});
appExtensionsNode.appendChild(child);
metadata.setFromTree(metaFormatName, root);
}

private static IIOMetadataNode getNode(IIOMetadataNode rootNode, String nodeName){
private static IIOMetadataNode getNode(IIOMetadataNode rootNode, String nodeName) {
int nNodes = rootNode.getLength();
for (int i = 0; i < nNodes; i++){
if (rootNode.item(i).getNodeName().equalsIgnoreCase(nodeName)){
for (int i = 0; i < nNodes; i++) {
if (rootNode.item(i).getNodeName().equalsIgnoreCase(nodeName)) {
return (IIOMetadataNode) rootNode.item(i);
}
}
IIOMetadataNode node = new IIOMetadataNode(nodeName);
rootNode.appendChild(node);
return(node);
return (node);
}

public void addFrame(RenderedImage img) throws IOException {
writer.writeToSequence(new IIOImage(img, null, metadata), params);
}

public InputStream getOutput(){
public InputStream getOutput() {
return output;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/moe/dituon/petpet/share/ImageSynthesis.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public static List<BufferedImage> execImageList(

latch.await();
return Arrays.asList(result);
} catch (InterruptedException ex){
} catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ public static List<BufferedImage> getWebImageAsList(String imageUrl) {
public static List<BufferedImage> getImageAsList(InputStream inputStream) throws IOException {
ReusableGifDecoder decoder = new ReusableGifDecoder();

try (inputStream) {
try (inputStream; inputStream) {
inputStream.mark(0); //循环利用inputStream, 避免重复获取
decoder.read(inputStream);

Expand Down
14 changes: 7 additions & 7 deletions src/main/java/moe/dituon/petpet/share/PetpetOilFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,23 @@ public int getSkip() {
return skip;
}

public void setRange(int range ) {
public void setRange(int range) {
this.range = range;
}

public int getRange() {
return range;
}

public void setLevels( int levels ) {
public void setLevels(int levels) {
this.levels = levels;
}

public int getLevels() {
return levels;
}

protected int[] filterPixels( int width, int height, int[] inPixels, Rectangle transformedSpace ) {
protected int[] filterPixels(int width, int height, int[] inPixels, Rectangle transformedSpace) {
int index = 0;
int[] rHistogram = new int[levels];
int[] gHistogram = new int[levels];
Expand All @@ -68,14 +68,14 @@ protected int[] filterPixels( int width, int height, int[] inPixels, Rectangle t
rHistogram[i] = gHistogram[i] = bHistogram[i] = rTotal[i] = gTotal[i] = bTotal[i] = 0;

for (int row = -range; row <= range; row += skip) {
int iy = y+row;
int iy = y + row;
int ioffset;
if (0 <= iy && iy < height) {
ioffset = iy*width;
ioffset = iy * width;
for (int col = -range; col <= range; col += skip) {
int ix = x + col;
if (0 <= ix && ix < width) {
int rgb = inPixels[ioffset+ix];
int rgb = inPixels[ioffset + ix];
int r = (rgb >> 16) & 0xff;
int g = (rgb >> 8) & 0xff;
int b = rgb & 0xff;
Expand Down Expand Up @@ -105,7 +105,7 @@ protected int[] filterPixels( int width, int height, int[] inPixels, Rectangle t
r = rTotal[r] / rHistogram[r];
g = gTotal[g] / gHistogram[g];
b = bTotal[b] / bHistogram[b];
outPixels[index] = (inPixels[index] & 0xff000000) | ( r << 16 ) | ( g << 8 ) | b;
outPixels[index] = (inPixels[index] & 0xff000000) | (r << 16) | (g << 8) | b;
index++;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/moe/dituon/petpet/share/element/FrameInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ public class FrameInfo {
public int canvasHeight;
public float multiple = 1.0F;

public FrameInfo(int index, int canvasWidth, int canvasHeight){
public FrameInfo(int index, int canvasWidth, int canvasHeight) {
this.index = index;
this.canvasWidth = canvasWidth;
this.canvasHeight = canvasHeight;
}

@NotNull
@Contract(value = "_, _ -> new", pure = true)
public static FrameInfo fromImage(BufferedImage image, int index){
public static FrameInfo fromImage(BufferedImage image, int index) {
return new FrameInfo(image.getWidth(), image.getWidth(), index);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

public interface TemplateElement extends Drawable {
int getWidth();

int getHeight();

Type getElementType();

enum Type {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ protected String buildText(String text, TextExtraData extraData, boolean greedy)
/**
* 在Graphics2D对象上绘制TextModel
*
* @param g2d 画布
* @param g2d 画布
*/
@Override
public void draw(Graphics2D g2d, FrameInfo info) {
Expand Down
121 changes: 121 additions & 0 deletions src/main/java/moe/dituon/petpet/share/filter/PetpetOilFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
Copyright 2006 Jerry Huxtable
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package moe.dituon.petpet.share.filter;

import com.jhlabs.image.WholeImageFilter;

import java.awt.*;

public class PetpetOilFilter extends WholeImageFilter {
private int skip = 4;
private int range = 12;
private int levels = 8;

public PetpetOilFilter() {
}

public void setSkip(int skip) {
this.skip = skip;
}

public int getSkip() {
return skip;
}

public void setRange(int range) {
this.range = range;
}

public int getRange() {
return range;
}

public void setLevels(int levels) {
this.levels = levels;
}

public int getLevels() {
return levels;
}

protected int[] filterPixels(int width, int height, int[] inPixels, Rectangle transformedSpace) {
int index = 0;
int[] rHistogram = new int[levels];
int[] gHistogram = new int[levels];
int[] bHistogram = new int[levels];
int[] rTotal = new int[levels];
int[] gTotal = new int[levels];
int[] bTotal = new int[levels];
int[] outPixels = new int[width * height];

for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
for (int i = 0; i < levels; i++)
rHistogram[i] = gHistogram[i] = bHistogram[i] = rTotal[i] = gTotal[i] = bTotal[i] = 0;

for (int row = -range; row <= range; row += skip) {
int iy = y + row;
int ioffset;
if (0 <= iy && iy < height) {
ioffset = iy * width;
for (int col = -range; col <= range; col += skip) {
int ix = x + col;
if (0 <= ix && ix < width) {
int rgb = inPixels[ioffset + ix];
int r = (rgb >> 16) & 0xff;
int g = (rgb >> 8) & 0xff;
int b = rgb & 0xff;
int ri = r * levels >> 8;
int gi = g * levels >> 8;
int bi = b * levels >> 8;
rTotal[ri] += r;
gTotal[gi] += g;
bTotal[bi] += b;
rHistogram[ri]++;
gHistogram[gi]++;
bHistogram[bi]++;
}
}
}
}

int r = 0, g = 0, b = 0;
for (int i = 1; i < levels; i++) {
if (rHistogram[i] > rHistogram[r])
r = i;
if (gHistogram[i] > gHistogram[g])
g = i;
if (bHistogram[i] > bHistogram[b])
b = i;
}
r = rTotal[r] / rHistogram[r];
g = gTotal[g] / gHistogram[g];
b = bTotal[b] / bHistogram[b];
outPixels[index] = (inPixels[index] & 0xff000000) | (r << 16) | (g << 8) | b;
index++;
}
}

return outPixels;
}

public String toString() {
return "Stylize/Oil...";
}

}

Loading

0 comments on commit f6296a2

Please sign in to comment.